source: Dev/branches/rest-dojo-ui/jos-branch/server/rdfapi/dataset/DatasetMem.php @ 312

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