[12] | 1 | <?php
|
---|
| 2 | require_once RDFAPI_INCLUDE_DIR . 'dataset/Quad.php';
|
---|
| 3 | // ----------------------------------------------------------------------------------
|
---|
| 4 | // Class: IteratorFindQuadsMem
|
---|
| 5 | // ----------------------------------------------------------------------------------
|
---|
| 6 |
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | /**
|
---|
| 10 | * Implementation of a quad iterator.
|
---|
| 11 | *
|
---|
| 12 | * This Iterator should be used like:
|
---|
| 13 | * for($iterator = $dataset->findInNamedGraphs(null,null,null,null); $iterator->valid(); $iterator->next())
|
---|
| 14 | * {
|
---|
| 15 | * $currentQuad=$it->current();
|
---|
| 16 | * };
|
---|
| 17 | *
|
---|
| 18 | * @version $Id$
|
---|
| 19 | * @author Daniel Westphal (http://d-westphal.de)
|
---|
| 20 | *
|
---|
| 21 | *
|
---|
| 22 | * @package dataset
|
---|
| 23 | * @access public
|
---|
| 24 | **/
|
---|
| 25 | class IteratorFindQuadsMem
|
---|
| 26 | {
|
---|
| 27 | /**
|
---|
| 28 | * key value in the current graph.
|
---|
| 29 | *
|
---|
| 30 | * @var dataset
|
---|
| 31 | * @access private
|
---|
| 32 | */
|
---|
| 33 | var $graphKey;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * boolean value, if the results should be returned as triples.
|
---|
| 37 | *
|
---|
| 38 | * @var boolean
|
---|
| 39 | * @access private
|
---|
| 40 | */
|
---|
| 41 | var $returnAsTriples;
|
---|
| 42 |
|
---|
| 43 | /**
|
---|
| 44 | * The current position.
|
---|
| 45 | *
|
---|
| 46 | * @var integer
|
---|
| 47 | * @access private
|
---|
| 48 | */
|
---|
| 49 | var $key;
|
---|
| 50 |
|
---|
| 51 | /**
|
---|
| 52 | * If the current resource is valid.
|
---|
| 53 | *
|
---|
| 54 | * @var boolean
|
---|
| 55 | * @access private
|
---|
| 56 | */
|
---|
| 57 | var $valid;
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * The current NamedGraph.
|
---|
| 61 | *
|
---|
| 62 | * @var NamedGraph
|
---|
| 63 | * @access private
|
---|
| 64 | */
|
---|
| 65 | var $current;
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * The graphName Resource to search for.
|
---|
| 69 | *
|
---|
| 70 | * @var string
|
---|
| 71 | * @access private
|
---|
| 72 | */
|
---|
| 73 | var $findGraphName;
|
---|
| 74 |
|
---|
| 75 | /**
|
---|
| 76 | * The subject Resource to search for.
|
---|
| 77 | *
|
---|
| 78 | * @var string
|
---|
| 79 | * @access private
|
---|
| 80 | */
|
---|
| 81 | var $findSubject;
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * The predicate Resource to search for.
|
---|
| 85 | *
|
---|
| 86 | * @var string
|
---|
| 87 | * @access private
|
---|
| 88 | */
|
---|
| 89 | var $findPredicate;
|
---|
| 90 |
|
---|
| 91 | /**
|
---|
| 92 | * The object Resource to search for.
|
---|
| 93 | *
|
---|
| 94 | * @var string
|
---|
| 95 | * @access private
|
---|
| 96 | */
|
---|
| 97 | var $findObject;
|
---|
| 98 |
|
---|
| 99 | /**
|
---|
| 100 | * Iterator over all graphs of the RDF dataset.
|
---|
| 101 | *
|
---|
| 102 | * @var string
|
---|
| 103 | * @access private
|
---|
| 104 | */
|
---|
| 105 | var $graphIterator;
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * Constructor.
|
---|
| 110 | *
|
---|
| 111 | * $subject, $predicate, and $object are used like find().
|
---|
| 112 | * $getSPO supports the strings 's', 'p', and 'o' to return
|
---|
| 113 | * either the subject, predicate, or object of the result statements.
|
---|
| 114 | *
|
---|
| 115 | *
|
---|
| 116 | * @param Resource
|
---|
| 117 | * @param Resource
|
---|
| 118 | * @param Resource
|
---|
| 119 | * @param dataset
|
---|
| 120 | * @param Boolean
|
---|
| 121 | * @access public
|
---|
| 122 | */
|
---|
| 123 | function IteratorFindQuadsMem($subject,$predicate,$object,&$graphIterator, $returnAsTriples=false)
|
---|
| 124 | {
|
---|
| 125 | $this->findSubject=$subject;
|
---|
| 126 | $this->findPredicate=$predicate;
|
---|
| 127 | $this->findObject=$object;
|
---|
| 128 | $this->graphIterator=&$graphIterator;
|
---|
| 129 | $this->rewind();
|
---|
| 130 | $this->returnAsTriples=$returnAsTriples;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /**
|
---|
| 134 | * Resets iterator list to start.
|
---|
| 135 | *
|
---|
| 136 | * @access public
|
---|
| 137 | */
|
---|
| 138 | function rewind()
|
---|
| 139 | {
|
---|
| 140 | $this->graphIterator->rewind();
|
---|
| 141 | $this->key = -1;
|
---|
| 142 | $this->graphKey=-1;
|
---|
| 143 | $this->next();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | /**
|
---|
| 147 | * Says if there are additional items left in the list.
|
---|
| 148 | *
|
---|
| 149 | * @return boolean
|
---|
| 150 | * @access public
|
---|
| 151 | */
|
---|
| 152 | function valid()
|
---|
| 153 | {
|
---|
| 154 | return $this->valid;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Moves Iterator to the next item in the list.
|
---|
| 159 | *
|
---|
| 160 | * @access public
|
---|
| 161 | */
|
---|
| 162 | function next()
|
---|
| 163 | {
|
---|
| 164 | if($this->graphIterator->valid()===false)
|
---|
| 165 | {
|
---|
| 166 | $this->valid=false;
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | $currentGraph=&$this->graphIterator->current();
|
---|
| 171 | $this->current= $currentGraph->findFirstMatchingStatement($this->findSubject,$this->findPredicate,$this->findObject,++$this->graphKey);
|
---|
| 172 | if($this->current==null)
|
---|
| 173 | {
|
---|
| 174 | do
|
---|
| 175 | {
|
---|
| 176 | $this->graphIterator->next();
|
---|
| 177 | if($this->graphIterator->valid()===false)
|
---|
| 178 | {
|
---|
| 179 | $this->valid=false;
|
---|
| 180 | return;
|
---|
| 181 | }
|
---|
| 182 | $currentGraph=&$this->graphIterator->current();
|
---|
| 183 | $this->graphKey=-1;
|
---|
| 184 | $this->current= $currentGraph->findFirstMatchingStatement($this->findSubject,$this->findPredicate,$this->findObject,++$this->graphKey);
|
---|
| 185 |
|
---|
| 186 | } while ($this->current==null);
|
---|
| 187 | }
|
---|
| 188 | $this->key++;
|
---|
| 189 | $this->valid=true;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * Returns the current item.
|
---|
| 194 | *
|
---|
| 195 | * @return mixed
|
---|
| 196 | * @access public
|
---|
| 197 | */
|
---|
| 198 | function current()
|
---|
| 199 | {
|
---|
| 200 | if($this->returnAsTriples) return $this->current;
|
---|
| 201 |
|
---|
| 202 | $currentGraph=&$this->graphIterator->current();
|
---|
| 203 | return new Quad(new Resource($currentGraph->getGraphName()),$this->current->getSubject(),$this->current->getPredicate(),$this->current->getObject());
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | /**
|
---|
| 207 | * Returns the key of the current item.
|
---|
| 208 | *
|
---|
| 209 | * @return integer
|
---|
| 210 | * @access public
|
---|
| 211 | */
|
---|
| 212 | function key()
|
---|
| 213 | {
|
---|
| 214 | return $this->key;
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 | ?> |
---|