Source for file ResIterator.php

Documentation is available at ResIterator.php

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

Documentation generated on Fri, 17 Dec 2004 16:17:49 +0100 by phpDocumentor 1.3.0RC3