Source for file DatasetMem.php

Documentation is available at DatasetMem.php

  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: DatasetMem
  4. // ----------------------------------------------------------------------------------
  5.  
  6. /**
  7. * In-memory implementation of a RDF dataset.
  8. * A RDF dataset set is a collection of named RDF graphs.
  9. *
  10. * <BR><BR>History:<UL>
  11. * <LI>05-11-2005 : First version of this class.</LI>
  12. * <LI>06-27-2005 : Support for a defaultGraph added</LI>
  13. *
  14. * @version V0.9.3
  15. * @author Daniel Westphal (http://www.d-westphal.de)
  16. * @author Chris Bizer <chris@bizer.de>
  17. *
  18. * @package dataset
  19. * @access public
  20. ***/
  21.  
  22. class DatasetMem extends Dataset
  23. {
  24. /**
  25. * Reference to a memmodel that holds the default graph
  26. *
  27. * @var resource memmodel
  28. * @access private
  29. */
  30. var $defaultGraph;
  31. /**
  32. * Name of the DatasetMem
  33. *
  34. * @var string
  35. * @access private
  36. */
  37. var $setName;
  38. /**
  39. * List of all NamedGraphs
  40. *
  41. * @var array
  42. * @access private
  43. */
  44. var $graphs=array();
  45. /**
  46. * Constructor
  47. * You can supply a Dataset name.
  48. *
  49. * @param string
  50. * @access public
  51. */
  52. function DatasetMem($datasetName = null)
  53. {
  54. $this->setDatasetName($datasetName);
  55. $this->defaultGraph=new MemModel();
  56. }
  57. // === Graph level methods ========================
  58. /**
  59. * Sets the Dataset name.
  60. *
  61. * @param string
  62. * @access public
  63. */
  64. function setDatasetName($datasetName)
  65. {
  66. $this->setName=$datasetName;
  67. }
  68. /**
  69. * Return the Dataset name.
  70. *
  71. * @return string
  72. * @access public
  73. */
  74. function getDatasetName()
  75. {
  76. return $this->setName;
  77. }
  78. /**
  79. * Adds a NamedGraph to the set. Will replace a NamedGraph with the same name that is already in the set.
  80. * @param NamedGraphMem
  81. */
  82. function addNamedGraph(&$graph)
  83. {
  84. $graphNameURI=$graph->getGraphName();
  85. $this->graphs[$graphNameURI]=&$graph;
  86. }
  87. /**
  88. * Overwrites the existting default graph.
  89. *
  90. * @param MemModel
  91. */
  92. function setDefaultGraph(&$graph)
  93. {
  94. $this->defaultGraph=&$graph;
  95. }
  96. /**
  97. * Returns a reference to the defaultGraph
  98. * @return Model
  99. */
  100. function &getDefaultGraph()
  101. {
  102. return ($this->defaultGraph);
  103. }
  104. /**
  105. * Returns true, if an defaultGraph exists. False otherwise
  106. * @return boolean
  107. */
  108. function hasDefaultGraph()
  109. {
  110. return ($this->defaultGraph!=null);
  111. }
  112. /**
  113. * Removes a NamedGraph from the set. Nothing happens
  114. * if no graph with that name is contained in the set.
  115. * @param string
  116. */
  117. function removeNamedGraph($graphName)
  118. {
  119. unset($this->graphs[$graphName]);
  120. }
  121. /**
  122. * Tells wether the Dataset contains a NamedGraph.
  123. * @param string
  124. * @return boolean
  125. */
  126. function containsNamedGraph($graphName)
  127. {
  128. return (isset($this->graphs[$graphName])===true);
  129. }
  130. /**
  131. * Returns the NamedGraph with a specific name from the Dataset.
  132. * Changes to the graph will be reflected in the set.
  133. * @param string
  134. * @return NamedGraphMem or NULL
  135. */
  136. function &getNamedGraph($graphName)
  137. {
  138. if (!isset($this->graphs[$graphName])) return NULL;
  139. return ($this->graphs[$graphName]);
  140. }
  141. /**
  142. * Returns the names of the namedGraphs in this set as strings in an array.
  143. * @return Array
  144. */
  145. function listGraphNames()
  146. {
  147. return array_keys($this->graphs);
  148. }
  149. /**
  150. * Creates a new NamedGraph and adds it to the set. An existing
  151. * graph with the same name will be replaced.The name of the NamedGraph to be created ; must be an URI
  152. * @param string
  153. * @param string
  154. * @return NamedGraphMem
  155. */
  156. function &createGraph($graphName,$baseURI = null)
  157. {
  158. $this->graphs[$graphName]=new NamedGraphMem($graphName,$baseURI);
  159. return $this->getNamedGraph($graphName);
  160. }
  161. /**
  162. * Deletes all NamedGraphs from the set.
  163. */
  164. function clear()
  165. {
  166. $this->graphs = array();
  167. }
  168.  
  169. /**
  170. * Returns the number of NamedGraphs in the set. Empty graphs
  171. * are counted.
  172. * @return int
  173. */
  174. function countGraphs()
  175. {
  176. return count($this->graphs);
  177. }
  178. /**
  179. * Returns the NamedGraph with a specific offset in the dataset.
  180. * Changes to the graph will be reflected in the set.
  181. * @param int
  182. * @return NamedGraphMem or null
  183. * @access private
  184. */
  185. function &getGraphWithOffset($offset)
  186. {
  187. $i=0;
  188. foreach ($this->graphs as $graph)
  189. {
  190. if (($i++)==$offset)
  191. return $graph;
  192. }
  193. return NULL;
  194. }
  195. /**
  196. * Returns an iterator over all {@link NamedGraph}s in the set.
  197. * @return IteratorAllGraphsMem
  198. */
  199. function &listNamedGraphs()
  200. {
  201. return (new IteratorAllGraphsMem($this));
  202. }
  203. /**
  204. * Tells wether the set contains any NamedGraphs.
  205. * @return boolean
  206. */
  207. function isEmpty()
  208. {
  209. return ($this->countGraphs()==0);
  210. }
  211. /**
  212. * Add all named graphs of the other dataset to this dataset
  213. * @param Dataset
  214. */
  215. function addAll($otherDataset)
  216. {
  217. for($iterator = $otherDataset->listNamedGraphs(); $iterator->valid(); $iterator->next())
  218. {
  219. $current=$iterator->current();
  220. $this->graphs[$current->getGraphName()]=$current;
  221. };
  222. if ($otherDataset->hasDefaultGraph())
  223. {
  224. $this->defaultGraph = $this->defaultGraph->unite($otherDataset->getDefaultGraph());
  225. }
  226. }
  227. // === Quad level methods ========================
  228.  
  229. /**
  230. * Adds a quad to the Dataset. The argument must not contain any
  231. * wildcards. If the quad is already present, nothing happens. A new
  232. * named graph will automatically be created if necessary.
  233. * @param Quad
  234. */
  235. function addQuad(&$quad)
  236. {
  237. $graphName=$quad->getGraphName();
  238. if ($this->containsNamedGraph($graphName->getLabel())===false)
  239. $this->createGraph($graphName->getLabel());
  240. $this->graphs[$graphName->getLabel()]->add($quad->getStatement());
  241. }
  242.  
  243. /**
  244. * Tells wether the Dataset contains a quad or
  245. * quads matching a pattern.
  246. *
  247. * @param Resource $graphName
  248. * @param Resource $subject
  249. * @param Resource $predicate
  250. * @param Resource $object
  251. * @return boolean
  252. */
  253. function containsQuad($graphName,$subject,$predicate,$object)
  254. {
  255. if($graphName!=null)
  256. {
  257. if ($this->containsNamedGraph($graphName->getLabel())!==true)
  258. return false;
  259. return ($this->graphs[$graphName->getLabel()]->findFirstMatchingStatement($subject,$predicate,$object)!=null);
  260. }
  261. foreach ($this->graphs as $graph)
  262. {
  263. if ($graph->findFirstMatchingStatement($subject,$predicate,$object)!=null)
  264. return true;
  265. };
  266. return false;
  267. }
  268. /**
  269. * Deletes a Quad from the RDF dataset.
  270. * @param Quad
  271. */
  272. function removeQuad($quad)
  273. {
  274. $graphName=$quad->getGraphName();
  275. if($graphName!=null)
  276. {
  277. if ($this->containsNamedGraph($graphName->getLabel())!==true)
  278. return;
  279. return ($this->graphs[$graphName->getLabel()]->remove($quad->getStatement())!=null);
  280. }
  281. foreach ($this->graphs as $graph)
  282. {
  283. $graph->remove($quad->getStatement());
  284. };
  285. }
  286. /**
  287. * Counts the Quads in the RDF dataset. Identical Triples in
  288. * different NamedGraphs are counted individually.
  289. * @return int
  290. */
  291. function countQuads()
  292. {
  293. $count=0;
  294. foreach ($this->graphs as $graph)
  295. {
  296. $count+=$graph->size();
  297. }
  298. return $count;
  299. }
  300. /**
  301. * Finds Statements that match a quad pattern. The argument may contain
  302. * wildcards.
  303. * @param Resource or Null
  304. * @param Resourceor Null
  305. * @param Resource or Null
  306. * @param Resource or Null
  307. * @return Iterator
  308. */
  309. function &findInNamedGraphs($graph,$subject,$predicate,$object,$returnAsTriples = false)
  310. {
  311. if ($graph!=null)
  312. {
  313. $findGraph=&$this->getNamedGraph($graph->getLabel());
  314. if($findGraph==null)
  315. $findGraph=new MemModel();
  316.  
  317. return $findGraph->iterFind($subject,$predicate,$object);
  318. }
  319. return new IteratorFindQuadsMem($subject,$predicate,$object,$this->listNamedGraphs(),$returnAsTriples);
  320. }
  321. /**
  322. * Finds Statements that match a pattern in the default Graph. The argument may contain
  323. * wildcards.
  324. * @param Resource or Null
  325. * @param Resource or Null
  326. * @param Resource or Null
  327. * @return Iterator
  328. */
  329. function &findInDefaultGraph($subject,$predicate,$object)
  330. {
  331. return $this->defaultGraph->iterFind($subject,$predicate,$object);
  332. }
  333. }
  334. ?>

Documentation generated on Fri, 13 Jan 2006 07:48:03 +0100 by phpDocumentor 1.3.0RC4