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

/extensions/patternmanager/classes/PatternFunction.php

https://code.google.com/p/ontowiki/
PHP | 316 lines | 146 code | 56 blank | 114 comment | 24 complexity | c22e5cd4d3596750acb7271f42d51ccc MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Class for Builtin Functions in Patterns
  4. *
  5. * @copyright Copyright (c) 2010 {@link http://aksw.org AKSW}
  6. * @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
  7. * @package
  8. * @subpackage
  9. * @author Christoph Rie?&#x; <c.riess.dev@googlemail.com>
  10. */
  11. class PatternFunction {
  12. /**
  13. *
  14. * Enter description here ...
  15. * @var array
  16. */
  17. private $_cache = array();
  18. /**
  19. *
  20. * Enter description here ...
  21. * @var array
  22. */
  23. private $_cacheSize = 0;
  24. /**
  25. *
  26. * Enter description here ...
  27. * @var int
  28. */
  29. const CACHE_MAX_ENTRIES = 20000;
  30. /**
  31. * Singleton instance
  32. * @var PatternFunction
  33. */
  34. private static $_instance = null;
  35. /**
  36. * Constructor
  37. */
  38. private function __construct()
  39. {
  40. $this->_cache = array();
  41. }
  42. /**
  43. * Singleton instance
  44. *
  45. * @return PatternFunction
  46. */
  47. public static function getInstance()
  48. {
  49. if (null === self::$_instance) {
  50. self::$_instance = new self();
  51. }
  52. return self::$_instance;
  53. }
  54. /**
  55. *
  56. * Pattern function internal cache: test for value
  57. * @param string $cId Identifier
  58. */
  59. private function cacheTest($cId) {
  60. return ( array_key_exists($cId,$this->_cache) );
  61. }
  62. /**
  63. *
  64. * Pattern function internal cache: load value
  65. * @param string $cId Identifier
  66. */
  67. private function cacheLoad($cId) {
  68. return ( $this->_cache[$cId] );
  69. }
  70. /**
  71. *
  72. * Pattern function internal cache: load value
  73. * @param string $cId Identifier
  74. */
  75. private function cacheSave($cId,$payload) {
  76. if ($this->_cacheSize > PatternFunction::CACHE_MAX_ENTRIES) {
  77. $this->_cacheSize = 0;
  78. $this->_cache = array();
  79. }
  80. $this->_cache[$cId] = $payload;
  81. $this->_cacheSize++;
  82. }
  83. /**
  84. *
  85. * Enter description here ...
  86. * @param $data
  87. * @param $bind
  88. * @param $asString
  89. */
  90. public function executeFunction ($data, $bind = array(), $asString = false) {
  91. $funcOptions = array();
  92. $options = array();
  93. foreach ($data['param'] as $param) {
  94. if ($param['type'] === 'function') {
  95. $options[] = $this->executeFunction($param, $bind, true);
  96. } elseif ( $param['type'] === 'temp') {
  97. $options[] = $bind[$param['value']]['value'];
  98. } else {
  99. $options[] = $param['value'];
  100. }
  101. }
  102. if ($this->isFunctionAvailable($data['name'])) {
  103. $callback = 'call' . ucfirst($data['name']);
  104. // caching all function results in memory
  105. $cacheId = $callback . implode('//',$options);
  106. if ( $this->cacheTest($cacheId) ) {
  107. $ret = $this->cacheLoad($cacheId);
  108. } else {
  109. $ret = $this->$callback($options);
  110. $this->cacheSave($cacheId,$ret);
  111. }
  112. } else {
  113. throw new RuntimeException('call for PatternFunction ' . $data['name'] . ' not available');
  114. }
  115. if ($asString) {
  116. return $ret['value'];
  117. } else {
  118. return $ret;
  119. }
  120. }
  121. /**
  122. * Checks whether a function with a given name $funcName is available
  123. * inside this instance and is callable.
  124. *
  125. * @param string $funcName function name to check for callability
  126. */
  127. public function isFunctionAvailable($funcName) {
  128. return is_callable( array($this,'call' . $funcName));
  129. }
  130. /**
  131. *
  132. * Enter description here ...
  133. * @param unknown_type $options
  134. */
  135. private function callGetnamespace($options = array()) {
  136. $data = array('value' => 'http://fancy-ns.com/' , 'type' => 'uri');
  137. return $data;
  138. }
  139. /**
  140. *
  141. * Enter description here ...
  142. * @param unknown_type $options
  143. */
  144. private function callGettempuri($options = array()) {
  145. $str = '';
  146. $prefix = 'http://defaultgraph/';
  147. foreach ($options as $key => $value) {
  148. if ($key === 0) {
  149. $prefix = $value;
  150. } else {
  151. $str .= md5($value);
  152. }
  153. }
  154. if ( !preg_match('/\/$/',$prefix) && !preg_match('/#$/',$prefix) ) {
  155. $prefix .= '/';
  156. }
  157. return array ('value' => $prefix . $str, 'type' => 'uri');
  158. }
  159. /**
  160. *
  161. * Enter description here ...
  162. * @param unknown_type $options
  163. */
  164. private function callGetlang($options = array('de')) {
  165. $ret = array('value' => $options[0]);
  166. return $ret;
  167. }
  168. /**
  169. *
  170. * Enter description here ...
  171. * @param unknown_type $options
  172. */
  173. private function callGetprefixeduri ($options) {
  174. $prefix = $options[0];
  175. $matches = array();
  176. preg_match_all('/([A-Z]|[a-z]|[0-9]|%|[-_])+$/',$options[1],$matches,PREG_PATTERN_ORDER);
  177. return array( 'value' => $prefix . $matches[0][0] );
  178. }
  179. /**
  180. *
  181. * Enter description here ...
  182. * @param unknown_type $options
  183. */
  184. private function callGetsmarturi($options) {
  185. require_once ONTOWIKI_ROOT . 'plugins/resourcecreationuri/classes/ResourceUriGenerator.php';
  186. $gen = new ResourceUriGenerator();
  187. $uri = $gen->generateUri($options[0], ResourceUriGenerator::FORMAT_SPARQL);
  188. return array('type' => 'uri' , 'value' => $uri);
  189. }
  190. /**
  191. * Returns the localname of an URI. If the URI contains # symbol, the string after it, else
  192. * the string after the last slash.
  193. *
  194. * @author Marvin Frommhold
  195. */
  196. private function callGetlocalname($options) {
  197. $uri = $options[0];
  198. $pos = strpos($uri, "#");
  199. if ( $pos === false ) {
  200. // no # found, use last slash
  201. $localName = substr($uri, strrpos($uri, "/") + 1);
  202. }
  203. else {
  204. $localName = substr($uri, strrpos($uri, "#") + 1);
  205. }
  206. return array('value' => $localName, 'type' => 'literal');
  207. }
  208. /**
  209. * Creates an URI by concatenating the given localname ($options[0]), prefix ($options[1]) and
  210. * optional suffix ($options[2]). If the prefix is not empty and has no ending slash, one will be appended.
  211. * The suffix, if available, will be concatenated to the localname without further editing.
  212. *
  213. * @author Marvin Frommhold
  214. */
  215. private function callCreateuri($options = array()) {
  216. $localname = $options[0];
  217. $prefix = $options[1];
  218. if ( count($options) == 3 ) {
  219. // append suffix
  220. $localname .= $options[2];
  221. }
  222. if ( !empty($prefix) ) {
  223. // add slash to the end of the prefix, if it does not exist
  224. if ( !preg_match('/\/$/',$prefix) && !preg_match('/#$/',$prefix) ) {
  225. $prefix .= '/';
  226. }
  227. }
  228. return array('value' => $prefix . $localname, 'type' => 'uri');
  229. }
  230. /**
  231. * Sets the language of the given literal to the given language.
  232. */
  233. private function callSetlanguage($options = array()) {
  234. $literal = $options[0];
  235. $language = $options[1];
  236. return array('value' => $literal, 'type' => 'literal', 'lang' => $language, 'datatype' => NULL);
  237. }
  238. /**
  239. * Creates a literal with the given value ($options[0]) and typed as the given datatype ($options[1]).
  240. *
  241. * @author Marvin Frommhold
  242. */
  243. private function callCreatetypedliteral($options = array()) {
  244. $value = $options[0];
  245. $datatype = $options[1];
  246. switch ( $datatype ) {
  247. case "http://www.w3.org/2001/XMLSchema#boolean":
  248. if ( $value > 0 ) {
  249. return array('value' => "true", 'type' => 'literal', "datatype" => $datatype);
  250. }
  251. else {
  252. return array('value' => "false", 'type' => 'literal', "datatype" => $datatype);
  253. }
  254. break;
  255. default:
  256. return array('value' => $value, 'type' => 'literal', "datatype" => $datatype);
  257. break;
  258. }
  259. }
  260. }