Source for file DatasetMem.php

Documentation is available at DatasetMem.php

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

Documentation generated on Thu, 7 Jul 2005 13:41:14 +0200 by phpDocumentor 1.3.0RC3