1 | <?php
|
---|
2 | /**
|
---|
3 | * ----------------------------------------------------------------------------------
|
---|
4 | * Class: ResIterator
|
---|
5 | * ----------------------------------------------------------------------------------
|
---|
6 | *
|
---|
7 | * @package resModel
|
---|
8 | */
|
---|
9 |
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Implementation of a resource iterator.
|
---|
13 | *
|
---|
14 | * This Iterator should be used in a for-loop like:
|
---|
15 | * $it = $ontClass->listInstances();
|
---|
16 | * for ($it->rewind(); $it->valid(); $it->next())
|
---|
17 | * {
|
---|
18 | * $currentResource=$it->current();
|
---|
19 | * };
|
---|
20 | *
|
---|
21 | *
|
---|
22 | * @version $Id: ResIterator.php 522 2007-08-14 09:22:22Z cweiske $
|
---|
23 | * @author Daniel Westphal <mail at d-westphal dot de>
|
---|
24 | *
|
---|
25 | *
|
---|
26 | * @package resModel
|
---|
27 | * @access public
|
---|
28 | **/
|
---|
29 | class ResIterator
|
---|
30 | {
|
---|
31 | /**
|
---|
32 | * Holds a reference to the assoiated ResModel / OntModel
|
---|
33 | * @var object Model
|
---|
34 | * @access private
|
---|
35 | */
|
---|
36 | var $associatedModel;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * The current position
|
---|
40 | * @var integer
|
---|
41 | * @access private
|
---|
42 | */
|
---|
43 | var $key;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * If the current resource is valid
|
---|
47 | * @var boolean
|
---|
48 | * @access private
|
---|
49 | */
|
---|
50 | var $valid;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The current resource
|
---|
54 | * @var obejct ResResource
|
---|
55 | * @access private
|
---|
56 | */
|
---|
57 | var $currentResource;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The subject to search for.
|
---|
61 | * @var object ResResource
|
---|
62 | * @access private
|
---|
63 | */
|
---|
64 | var $searchSubject;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * The predicate to search for.
|
---|
68 | * @var object ResResource
|
---|
69 | * @access private
|
---|
70 | */
|
---|
71 | var $searchPredicate;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * The object to search for.
|
---|
75 | * @var object ResResource
|
---|
76 | * @access private
|
---|
77 | */
|
---|
78 | var $searchObject;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * If the resource, we're intrested in is the subject (s), predicate(p),
|
---|
82 | * or object (o) of the found statements
|
---|
83 | *
|
---|
84 | * @var string
|
---|
85 | * @access private
|
---|
86 | */
|
---|
87 | var $getSPO;
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Defines the type of resource, we'd like to receive.
|
---|
91 | *
|
---|
92 | * @var string
|
---|
93 | * @access private
|
---|
94 | */
|
---|
95 | var $returnType;
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * If set, each resource will first be checked, if it's
|
---|
99 | * language fits.
|
---|
100 | *
|
---|
101 | * @var string
|
---|
102 | * @access private
|
---|
103 | */
|
---|
104 | var $findLiteralWithLang;
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Constructor.
|
---|
109 | *
|
---|
110 | * $subject, $predicate, and $object are used like inf find().
|
---|
111 | * $getSPO supports the strings 's', 'p', and 'o' to return
|
---|
112 | * either the subject, predicate, or object of the result statements.
|
---|
113 | * $returnType supports the strings 'ResProperty', 'ResLiteral',
|
---|
114 | * 'OntProperty', 'OntClass', and 'Individual' and returns the resources
|
---|
115 | * as the matching type.
|
---|
116 | *
|
---|
117 | * @param object ResResource $subject
|
---|
118 | * @param object ResResource $predicate
|
---|
119 | * @param object ResResource $object
|
---|
120 | * @param string $getSPO
|
---|
121 | * @param object ResModel $associatedModel
|
---|
122 | * @param string $returnType
|
---|
123 | * @access public
|
---|
124 | */
|
---|
125 | function ResIterator($subject,$predicate,$object,$getSPO,& $associatedModel,$returnType = false)
|
---|
126 | {
|
---|
127 | $this->searchSubject =& $subject;
|
---|
128 | $this->searchPredicate =& $predicate;
|
---|
129 | $this->searchObject =& $object;
|
---|
130 | $this->getSPO = $getSPO;
|
---|
131 | $this->returnType = $returnType;
|
---|
132 | $this->associatedModel =& $associatedModel;
|
---|
133 | $this->findLiteralWithLang = false;
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Resets iterator list to start
|
---|
138 | *
|
---|
139 | * @access public
|
---|
140 | */
|
---|
141 | function rewind()
|
---|
142 | {
|
---|
143 | $this->key = -1;
|
---|
144 | $this->next();
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Says if there are additional items left in the list
|
---|
149 | *
|
---|
150 | * @return boolean
|
---|
151 | * @access public
|
---|
152 | */
|
---|
153 | function valid()
|
---|
154 | {
|
---|
155 | return $this->valid;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Moves Iterator to the next item in the list
|
---|
160 | *
|
---|
161 | * @access public
|
---|
162 | */
|
---|
163 | function next()
|
---|
164 | {
|
---|
165 | $this->key++;
|
---|
166 | $this->valid=($this->_getNextResource());
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Returns the current item
|
---|
171 | *
|
---|
172 | * @return mixed
|
---|
173 | * @access public
|
---|
174 | */
|
---|
175 | function current()
|
---|
176 | {
|
---|
177 | return $this->currentResource;
|
---|
178 | }
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Returns the next Resource (subject, predicate,
|
---|
182 | * or object of the next matching statement).
|
---|
183 | *
|
---|
184 | * @return object resResource
|
---|
185 | * @access private
|
---|
186 | */
|
---|
187 | function _getNextResource()
|
---|
188 | {
|
---|
189 | if ($this->findLiteralWithLang)
|
---|
190 | {
|
---|
191 | do
|
---|
192 | {
|
---|
193 | $nextStatement = $this->associatedModel->findFirstMatchingStatement($this->searchSubject,$this->searchPredicate,$this->searchObject,$this->key);
|
---|
194 | if ($nextStatement === null)
|
---|
195 | return false;
|
---|
196 |
|
---|
197 | $object = $nextStatement->getObject();
|
---|
198 | if ($object->getLanguage() != $this->findLiteralWithLang)
|
---|
199 | {
|
---|
200 | $hasCorrectLanguage=false;
|
---|
201 | $this->key++;
|
---|
202 | } else
|
---|
203 | {
|
---|
204 | $hasCorrectLanguage=true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | } while (!$hasCorrectLanguage);
|
---|
208 | } else
|
---|
209 | {
|
---|
210 | $nextStatement = $this->associatedModel->findFirstMatchingStatement($this->searchSubject,$this->searchPredicate,$this->searchObject,$this->key);
|
---|
211 | }
|
---|
212 | if ($nextStatement === null)
|
---|
213 | return false;
|
---|
214 |
|
---|
215 | switch ($this->getSPO)
|
---|
216 | {
|
---|
217 | case 's':
|
---|
218 | $this->currentResource = $this->_getResourceAs($nextStatement->getSubject());
|
---|
219 | break;
|
---|
220 |
|
---|
221 | case 'p':
|
---|
222 | $this->currentResource = $this->_getResourceAs($nextStatement->getPredicate());
|
---|
223 | break;
|
---|
224 |
|
---|
225 | case 'o':
|
---|
226 | $this->currentResource = $this->_getResourceAs($nextStatement->getObject());
|
---|
227 | break;
|
---|
228 | }
|
---|
229 | return (true);
|
---|
230 | }
|
---|
231 |
|
---|
232 | /**
|
---|
233 | * Returns the key of the current item
|
---|
234 | *
|
---|
235 | * @return integer
|
---|
236 | * @access public
|
---|
237 | */
|
---|
238 | function key()
|
---|
239 | {
|
---|
240 | return $this->key;
|
---|
241 | }
|
---|
242 |
|
---|
243 | /**
|
---|
244 | * Sets that only Literals with the matching
|
---|
245 | * language should be returned
|
---|
246 | *
|
---|
247 | * @param string
|
---|
248 | * @access public
|
---|
249 | */
|
---|
250 | function setFindLiteralWithLang($language)
|
---|
251 | {
|
---|
252 | $this->findLiteralWithLang = $language;
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Returns the $resource as an instance of the type
|
---|
257 | * specified in $this->returnType.
|
---|
258 | *
|
---|
259 | * @param object ResResource
|
---|
260 | * @return object ResResource
|
---|
261 | * @access private
|
---|
262 | */
|
---|
263 | function _getResourceAs($resource)
|
---|
264 | {
|
---|
265 | if ($this->findLiteralWithLang && $resource->getLanguage() != $this->findLiteralWithLang)
|
---|
266 | $this->_getNextResource();
|
---|
267 |
|
---|
268 | if($this->returnType)
|
---|
269 | switch ($this->returnType) {
|
---|
270 |
|
---|
271 | case 'ResProperty':
|
---|
272 | return $this->associatedModel->createProperty($resource->getLabel());
|
---|
273 | break;
|
---|
274 |
|
---|
275 | case 'ResLiteral':
|
---|
276 | $newLiteral = $this->associatedModel->createLiteral($resource->getLabel(),$resource->getLanguage());
|
---|
277 | $newLiteral->setDatatype($resource->getDatatype());
|
---|
278 | return $newLiteral;
|
---|
279 | break;
|
---|
280 |
|
---|
281 | case 'OntProperty':
|
---|
282 |
|
---|
283 | return $this->associatedModel->createOntProperty($resource->getLabel());
|
---|
284 | break;
|
---|
285 |
|
---|
286 | case 'OntClass':
|
---|
287 | return $this->associatedModel->createOntClass($resource->getLabel());
|
---|
288 | break;
|
---|
289 |
|
---|
290 | case 'Individual':
|
---|
291 | return $this->associatedModel->createIndividual($resource->getLabel());
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | return $resource;
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | ?> |
---|