PageRenderTime 38ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/rdfapi-php/api/model/ModelFactory.php

https://github.com/koja13/DSi2.0
PHP | 288 lines | 116 code | 26 blank | 146 comment | 4 complexity | 46b7ba2f10215ddf859a14fce7f13a0e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. require_once RDFAPI_INCLUDE_DIR . 'model/DbStore.php';
  3. // ----------------------------------------------------------------------------------
  4. // Class: ModelFactory
  5. // ----------------------------------------------------------------------------------
  6. /**
  7. * ModelFactory is a static class which provides methods for creating different
  8. * types of RAP models. RAP models have to be created trough a ModelFactory
  9. * instead of creating them directly with the 'new' operator because of RAP's
  10. * dynamic code inclusion mechanism.
  11. *
  12. * @version $Id: ModelFactory.php 524 2007-08-14 11:12:45Z kobasoft $
  13. * @author Daniel Westphal <mail at d-westphal.de>
  14. * @author Richard Cyganiak <richard@cyganiak.de>
  15. *
  16. *
  17. * @package model
  18. * @access public
  19. **/
  20. class ModelFactory
  21. {
  22. /**
  23. * Returns a MemModel.
  24. * You can supply a base URI
  25. *
  26. * @param string $baseURI
  27. * @return object MemModel
  28. * @access public
  29. */
  30. function & getDefaultModel($baseURI = null)
  31. {
  32. return ModelFactory::getMemModel($baseURI);
  33. }
  34. /**
  35. * Returns a NamedGraphSetMem.
  36. * You can supply a GraphSet name.
  37. *
  38. * @param string $graphSetId
  39. * @param string $uri
  40. * @access public
  41. */
  42. function & getDatasetMem($graphSetId = null)
  43. {
  44. require_once RDFAPI_INCLUDE_DIR . 'dataset/DatasetMem.php';
  45. $m = new DatasetMem($graphSetId);
  46. return $m;
  47. }
  48. /**
  49. * Returns a MemModel.
  50. * You can supply a base URI
  51. *
  52. * @param string $baseURI
  53. * @return object MemModel
  54. * @access public
  55. */
  56. function & getMemModel($baseURI = null)
  57. {
  58. require_once RDFAPI_INCLUDE_DIR . 'model/MemModel.php';
  59. $m = new MemModel($baseURI);
  60. return $m;
  61. }
  62. /**
  63. * Returns a DbModel with the database connection
  64. * defined in constants.php.
  65. * You can supply a base URI. If a model with the given base
  66. * URI exists in the DbStore, it'll be opened.
  67. * If not, a new model will be created.
  68. *
  69. * @param string $baseURI
  70. * @return object DbModel
  71. * @access public
  72. */
  73. function & getDefaultDbModel($baseURI = null)
  74. {
  75. $dbStore = ModelFactory::getDbStore();
  76. $m = ModelFactory::getDbModel($dbStore,$baseURI);
  77. return $m;
  78. }
  79. /**
  80. * Returns a new DbModel using the database connection
  81. * supplied by $dbStore.
  82. * You can supply a base URI. If a model with the given base
  83. * URI exists in the DbStore, it'll be opened.
  84. * If not, a new model will be created.
  85. *
  86. * @param object DbStore $dbStore
  87. * @param string $baseURI
  88. * @return object DbModel
  89. * @access public
  90. */
  91. function & getDbModel($dbStore, $baseURI = null)
  92. {
  93. if ($dbStore->modelExists($baseURI)) {
  94. return $dbStore->getModel($baseURI);
  95. }
  96. return $dbStore->getNewModel($baseURI);
  97. }
  98. /**
  99. * Returns a database connection with the given parameters.
  100. * Paramters, which are not defined are taken from the constants.php
  101. *
  102. * @param string $dbDriver
  103. * @param string $host
  104. * @param string $dbName
  105. * @param string $user
  106. * @param string $password
  107. * @return object DbStore
  108. * @access public
  109. */
  110. function & getDbStore($dbDriver=ADODB_DB_DRIVER, $host=ADODB_DB_HOST, $dbName=ADODB_DB_NAME,
  111. $user=ADODB_DB_USER, $password=ADODB_DB_PASSWORD)
  112. {
  113. $dbs = new DbStore($dbDriver, $host, $dbName,$user, $password);
  114. return $dbs;
  115. }
  116. /**
  117. * Returns a InfModelF.
  118. * (MemModel with forward chaining inference engine)
  119. * Configurations can be done in constants.php
  120. * You can supply a base URI
  121. *
  122. * @param string $baseURI
  123. * @return object MemModel
  124. * @access public
  125. */
  126. function & getInfModelF($baseURI = null)
  127. {
  128. require_once RDFAPI_INCLUDE_DIR . 'infModel/InfModelF.php';
  129. $mod = new InfModelF($baseURI);
  130. return $mod;
  131. }
  132. /**
  133. * Returns a InfModelB.
  134. * (MemModel with backward chaining inference engine)
  135. * Configurations can be done in constants.php
  136. * You can supply a base URI
  137. *
  138. * @param string $baseURI
  139. * @return object MemModel
  140. * @access public
  141. */
  142. function & getInfModelB($baseURI = null)
  143. {
  144. require_once RDFAPI_INCLUDE_DIR . 'infModel/InfModelB.php';
  145. $mod = new InfModelB($baseURI);
  146. return $mod;
  147. }
  148. /**
  149. * Returns a ResModel.
  150. * $modelType has to be one of the following constants:
  151. * MEMMODEL,DBMODEL,INFMODELF,INFMODELB to create a resmodel with a new
  152. * model from defined type.
  153. * You can supply a base URI
  154. *
  155. * @param constant $modelType
  156. * @param string $baseURI
  157. * @return object ResModel
  158. * @access public
  159. */
  160. function & getResModel($modelType, $baseURI = null)
  161. {
  162. switch ($modelType) {
  163. case DBMODEL:
  164. $baseModel = ModelFactory::getDefaultDbModel($baseURI);
  165. break;
  166. case INFMODELF:
  167. $baseModel = ModelFactory::getInfModelF($baseURI);
  168. break;
  169. case INFMODELB:
  170. $baseModel = ModelFactory::getInfModelB($baseURI);
  171. break;
  172. default:
  173. $baseModel = ModelFactory::getMemModel($baseURI);
  174. break;
  175. }
  176. return ModelFactory::getResModelForBaseModel($baseModel);
  177. }
  178. /**
  179. * Creates a ResModel that wraps an existing base model.
  180. *
  181. * @param object Model $baseModel
  182. * @return object ResModel
  183. * @access public
  184. */
  185. function &getResModelForBaseModel(&$baseModel) {
  186. require_once RDFAPI_INCLUDE_DIR . 'resModel/ResModel.php';
  187. $mod = new ResModel($baseModel);
  188. return $mod;
  189. }
  190. /**
  191. * Returns an OntModel.
  192. * $modelType has to be one of the following constants:
  193. * MEMMODEL, DBMODEL, INFMODELF, INFMODELB to create a OntModel
  194. * with a new model from defined type.
  195. * $vocabulary defines the ontology language. Currently only
  196. * RDFS_VOCABULARY is supported. You can supply a model base URI.
  197. *
  198. * @param constant $modelType
  199. * @param constant $vocabulary
  200. * @param string $baseURI
  201. * @return object OntModel
  202. * @access public
  203. */
  204. function & getOntModel($modelType,$vocabulary, $baseURI = null)
  205. {
  206. switch ($modelType)
  207. {
  208. case DBMODEL:
  209. $baseModel = ModelFactory::getDefaultDbModel($baseURI);
  210. break;
  211. case INFMODELF:
  212. $baseModel = ModelFactory::getInfModelF($baseURI);
  213. break;
  214. case INFMODELB:
  215. $baseModel = ModelFactory::getInfModelB($baseURI);
  216. break;
  217. default:
  218. $baseModel = ModelFactory::getMemModel($baseURI);;
  219. }
  220. $mod = ModelFactory::getOntModelForBaseModel($baseModel, $vocabulary);
  221. return $mod;
  222. }
  223. /**
  224. * Creates an OntModel that wraps an existing base model.
  225. * $vocabulary defines the ontology language. Currently only
  226. * RDFS_VOCABULARY is supported.
  227. *
  228. * @param object Model $baseModel
  229. * @param constant $vocabulary
  230. * @return object OntModel
  231. * @access public
  232. */
  233. function &getOntModelForBaseModel(&$baseModel, $vocabulary)
  234. {
  235. require_once RDFAPI_INCLUDE_DIR . 'ontModel/OntModel.php';
  236. switch ($vocabulary)
  237. {
  238. case RDFS_VOCABULARY:
  239. require_once(RDFAPI_INCLUDE_DIR.'ontModel/'.RDFS_VOCABULARY);
  240. $vocab_object = new RdfsVocabulary();
  241. break;
  242. default:
  243. trigger_error("Unknown vocabulary constant '$vocabulary'; only RDFS_VOCABULARY is supported", E_USER_WARNING);
  244. $vocab_object = null;
  245. break;
  246. }
  247. $mod = new OntModel($baseModel, $vocab_object);
  248. return $mod;
  249. }
  250. /**
  251. * Creates a SparqlClient.
  252. *
  253. * @param String $server Link to a SPARQL endpoint.
  254. * @return SparqlClient the SparqlClient object.
  255. * @access public
  256. */
  257. function & getSparqlClient($server){
  258. $cl = new SparqlClient($server);
  259. return $cl;
  260. }
  261. }
  262. ?>