PageRenderTime 1145ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/rdfapi-php/api/util/RdfUtil.php

https://github.com/koja13/DSi2.0
PHP | 563 lines | 362 code | 55 blank | 146 comment | 86 complexity | 150538d72312104b706e96ff770f88d0 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // ----------------------------------------------------------------------------------
  3. // Class: RDFUtil
  4. // ----------------------------------------------------------------------------------
  5. /**
  6. * Useful utility methods.
  7. * Static class.
  8. *
  9. * @version $Id: RdfUtil.php 295 2006-06-23 06:45:53Z tgauss $
  10. * @author Chris Bizer <chris@bizer.de>, Daniel Westphal <dawe@gmx.de>
  11. * @author Anton Köstlbacher <anton1@koestlbacher.de>
  12. * @package utility
  13. * @access public
  14. **/
  15. class RDFUtil extends Object {
  16. /**
  17. * Extracts the namespace prefix out of a URI.
  18. *
  19. * @param String $uri
  20. * @return string
  21. * @access public
  22. */
  23. function guessNamespace($uri) {
  24. $l = RDFUtil::getNamespaceEnd($uri);
  25. return $l > 1 ? substr($uri ,0, $l) : "";
  26. }
  27. /**
  28. * Delivers the name out of the URI (without the namespace prefix).
  29. *
  30. * @param String $uri
  31. * @return string
  32. * @access public
  33. */
  34. function guessName($uri) {
  35. return substr($uri,RDFUtil::getNamespaceEnd($uri));
  36. }
  37. /**
  38. * Extracts the namespace prefix out of the URI of a Resource.
  39. *
  40. * @param Object Resource $resource
  41. * @return string
  42. * @access public
  43. */
  44. function getNamespace($resource) {
  45. return RDFUtil::guessNamespace($resource->getURI());
  46. }
  47. /**
  48. * Delivers the Localname (without the namespace prefix) out of the URI of a Resource.
  49. *
  50. * @param Object Resource $resource
  51. * @return string
  52. * @access public
  53. */
  54. function getLocalName($resource) {
  55. return RDFUtil::guessName($resource->getURI());
  56. }
  57. /**
  58. * Position of the namespace end
  59. * Method looks for # : and /
  60. * @param String $uri
  61. * @access private
  62. */
  63. function getNamespaceEnd($uri) {
  64. $l = strlen($uri)-1;
  65. do {
  66. $c = substr($uri, $l, 1);
  67. if($c == '#' || $c == ':' || $c == '/')
  68. break;
  69. $l--;
  70. } while ($l >= 0);
  71. $l++;
  72. return $l;
  73. }
  74. /**
  75. * Short Prefix for known Namespaces by given URI
  76. * @param String $uri
  77. * @access public
  78. */
  79. function getPrefix($uri) {
  80. switch (RDFUtil::guessNamespace($uri))
  81. {
  82. case RDF_NAMESPACE_URI:
  83. $prefix = RDF_NAMESPACE_PREFIX;
  84. break;
  85. case RDF_SCHEMA_URI:
  86. $short_p = RDF_SCHEMA_PREFIX;
  87. break;
  88. case OWL_URI:
  89. $short_p = OWL_PREFIX;
  90. break;
  91. default:
  92. $short_p = $statement->getLabelPredicate();
  93. }
  94. return $short_p;
  95. }
  96. /**
  97. * Tests if the URI of a resource belongs to the RDF syntax/model namespace.
  98. *
  99. * @param Object Resource $resource
  100. * @return boolean
  101. * @access public
  102. */
  103. function isRDF($resource) {
  104. return ($resource != NULL && RDFUtil::getNamespace($resource) == RDF_NAMESPACE_URI);
  105. }
  106. /**
  107. * Escapes < > and &
  108. *
  109. * @param String $textValue
  110. * @return String
  111. * @access public
  112. */
  113. function escapeValue($textValue) {
  114. $textValue = str_replace('<', '&lt;', $textValue);
  115. $textValue = str_replace('>', '&gt;', $textValue);
  116. $textValue = str_replace('&', '&amp;', $textValue);
  117. return $textValue;
  118. }
  119. /**
  120. * Converts an ordinal RDF resource to an integer.
  121. * e.g. Resource(RDF:_1) => 1
  122. *
  123. * @param object Resource $resource
  124. * @return Integer
  125. * @access public
  126. */
  127. function getOrd($resource) {
  128. if($resource == NULL || !is_a($resource, 'Resource') || !RDFUtil::isRDF($resource))
  129. return -1;
  130. $name = RDFUtil::getLocalName($resource);
  131. echo substr($name, 1).' '.RDFUtil::getLocalName($resource);
  132. $n = substr($name, 1);
  133. //noch rein : chekcen ob $n Nummer ist !!!!!!!!!!!!!!!!!!!!!!if($n)
  134. return $n;
  135. return -1;
  136. }
  137. /**
  138. * Creates ordinal RDF resource out of an integer.
  139. *
  140. * @param Integer $num
  141. * @return object Resource
  142. * @access public
  143. */
  144. function createOrd($num) {
  145. return new Resource(RDF_NAMESPACE_URI . '_' . $num);
  146. }
  147. /**
  148. * Prints a MemModel as HTML table.
  149. * You can change the colors in the configuration file.
  150. *
  151. * @param object MemModel &$model
  152. * @access public
  153. */
  154. function writeHTMLTable(&$model) {
  155. $nms = $model->getParsedNamespaces();
  156. $names = '';
  157. $pre = '';
  158. echo '<table border="1" cellpadding="3" cellspacing="0" width="100%">' . LINEFEED;
  159. echo INDENTATION . '<tr bgcolor="' . HTML_TABLE_HEADER_COLOR . '">' . LINEFEED . INDENTATION . INDENTATION . '<td td width="68%" colspan="3">';
  160. echo '<p><b>Base URI:</b> ' . $model->getBaseURI() . '</p></td>' . LINEFEED;
  161. echo INDENTATION . INDENTATION . '<td width="32%"><p><b>Size:</b> ' . $model->size() . '</p></td>' . LINEFEED . INDENTATION . '</tr>';
  162. echo '<tr><td><b>Prefix:</b>'.'<br/></td><td colspan="3"><b>Namespace:</b>'.'<br/></td></tr>';
  163. $i=0;
  164. if($nms != false){
  165. foreach($nms as $namespace => $prefix){
  166. if($i==0){
  167. $col = HTML_TABLE_NS_ROW_COLOR0;
  168. }else{
  169. $col = HTML_TABLE_NS_ROW_COLOR1;
  170. }
  171. echo '<tr bgcolor="'.$col.'"><td>'.$prefix.'</td><td colspan="3">'.$namespace.'</td></tr>';
  172. $i++;
  173. $i%=2;
  174. }
  175. }else{
  176. echo '<tr><td>-</td><td colspan="3">-</td></tr>';
  177. }
  178. echo INDENTATION . '<tr bgcolor="' . HTML_TABLE_HEADER_COLOR . '">' . LINEFEED . INDENTATION . INDENTATION . '<td width="4%"><p align=center><b>No.</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Subject</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Predicate</b></p></td>' . LINEFEED . INDENTATION . INDENTATION . '<td width="32%"><p><b>Object</b></p></td>' . LINEFEED . INDENTATION . '</tr>' . LINEFEED;
  179. $i = 1;
  180. foreach($model->triples as $key => $statement) {
  181. $infered='';
  182. if (is_a($statement,'InfStatement')) $infered='<small>(infered)</small>';
  183. echo INDENTATION . '<tr valign="top">' . LINEFEED . INDENTATION . INDENTATION . '<td><p align=center>' . $i . '.<BR>'.$infered.'</p></td>' . LINEFEED;
  184. // subject
  185. echo INDENTATION . INDENTATION . '<td bgcolor="';
  186. echo RDFUtil::chooseColor($statement->getSubject());
  187. echo '">';
  188. echo '<p>' . RDFUtil::getNodeTypeName($statement->getSubject());
  189. if(is_a($statement->subj,'Resource')){
  190. $ns = $statement->subj->getNamespace();
  191. if(isset($nms[$ns])){
  192. echo $nms[$ns].':'.RDFUtil::getLocalName($statement->subj);
  193. }else{
  194. echo $statement->subj->getLabel();
  195. }
  196. }
  197. echo '</p></td>' . LINEFEED;
  198. // predicate
  199. echo INDENTATION . INDENTATION . '<td bgcolor="';
  200. echo RDFUtil::chooseColor($statement->getPredicate());
  201. echo '">';
  202. echo '<p>' . RDFUtil::getNodeTypeName($statement->getPredicate());
  203. if(is_a($statement->pred,'Resource')){
  204. $ns = $statement->pred->getNamespace();
  205. if(isset($nms[$ns])){
  206. echo $nms[$ns].':'.RDFUtil::getLocalName($statement->pred);
  207. }else{
  208. echo $statement->pred->getLabel();
  209. }
  210. }
  211. echo '</p></td>' . LINEFEED;
  212. // object
  213. echo INDENTATION . INDENTATION . '<td bgcolor="';
  214. echo RDFUtil::chooseColor($statement->getObject());
  215. echo '">';
  216. echo '<p>';
  217. if (is_a($statement->getObject(), 'Literal')) {
  218. if ($statement->obj->getLanguage() != null) {
  219. $lang = ' <b>(xml:lang="' . $statement->obj->getLanguage() . '") </b> ';
  220. } ELSE $lang = '';
  221. if ($statement->obj->getDatatype() != null) {
  222. $dtype = ' <b>(rdf:datatype="' . $statement->obj->getDatatype() . '") </b> ';
  223. } ELSE $dtype = '';
  224. } else {
  225. $lang = '';
  226. $dtype = '';
  227. }
  228. $label = $statement->obj->getLabel();
  229. if(is_a($statement->obj,'Resource')){
  230. $ns = $statement->obj->getNamespace();
  231. if(isset($nms[$ns])){
  232. $label = $nms[$ns].':'.RDFUtil::getLocalName($statement->obj);
  233. }else{
  234. $label = $statement->obj->getLabel();
  235. }
  236. }
  237. echo RDFUtil::getNodeTypeName($statement->getObject())
  238. .nl2br(htmlspecialchars($label)) . $lang . $dtype;
  239. echo '</p></td>' . LINEFEED;
  240. echo INDENTATION . '</tr>' . LINEFEED;
  241. $i++;
  242. }
  243. echo '</table>' . LINEFEED;
  244. }
  245. /**
  246. * Chooses a node color.
  247. * Used by RDFUtil::writeHTMLTable()
  248. *
  249. * @param object Node $node
  250. * @return object Resource
  251. * @access private
  252. */
  253. function chooseColor($node) {
  254. if (is_a($node, 'BlankNode'))
  255. return HTML_TABLE_BNODE_COLOR;
  256. elseif (is_a($node, 'Literal'))
  257. return HTML_TABLE_LITERAL_COLOR;
  258. else {
  259. if (RDFUtil::getNamespace($node) == RDF_NAMESPACE_URI ||
  260. RDFUtil::getNamespace($node) == RDF_SCHEMA_URI ||
  261. RDFUtil::getNamespace($node) == OWL_URI
  262. )
  263. return HTML_TABLE_RDF_NS_COLOR;
  264. }
  265. return HTML_TABLE_RESOURCE_COLOR;
  266. }
  267. /**
  268. * Get Node Type.
  269. * Used by RDFUtil::writeHTMLTable()
  270. *
  271. * @param object Node $node
  272. * @return object Resource
  273. * @access private
  274. */
  275. function getNodeTypeName($node) {
  276. if (is_a($node, "BlankNode"))
  277. return 'Blank Node: ';
  278. elseif (is_a($node, 'Literal'))
  279. return 'Literal: ';
  280. else {
  281. if (RDFUtil::getNamespace($node) == RDF_NAMESPACE_URI ||
  282. RDFUtil::getNamespace($node) == RDF_SCHEMA_URI ||
  283. RDFUtil::getNamespace($node) == OWL_URI)
  284. return 'RDF Node: ';
  285. }
  286. return 'Resource: ';
  287. }
  288. /**
  289. * Short Prefix for known and/or parsed Namespaces by given URI and Model
  290. * Uses $default_prefixes defined in constants.php and getParsedNamespaces()
  291. * Returns FALSE if no matching prefix is found
  292. *
  293. * @author Anton Köstlbacher <anton1@koestlbacher.de>
  294. * @param string $uri
  295. * @param object $model
  296. * @return string, boolean
  297. * @access public
  298. * @throws PhpError
  299. */
  300. function guessPrefix($uri, &$model)
  301. {
  302. global $default_prefixes;
  303. $namespace = RDFUtil::guessNamespace($uri);
  304. $par_nms = $model->getParsedNamespaces();
  305. if (isset($par_nms[$namespace]))
  306. {
  307. $prefix = $par_nms[$namespace];
  308. }
  309. else
  310. {
  311. $prefix = array_search($namespace, $default_prefixes);
  312. }
  313. if($prefix !== false)
  314. {
  315. return $prefix;
  316. }
  317. else
  318. {
  319. return false;
  320. }
  321. }
  322. /**
  323. * Generates a dot-file for drawing graphical output with the
  324. * graphviz-application which can be downloaded at http://www.graphviz.org
  325. * If the graphviz-application is installed and its path is set to the
  326. * correct value in constants.php we can directly generate any
  327. * file format graphviz supports, e.g. SVG, PNG
  328. * Parameters: model to visualize, output format, use prefixes
  329. *
  330. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  331. * WARNING: Graphviz can be slow with large models.
  332. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  333. *
  334. * @author Anton Köstlbacher <anton1@koestlbacher.de>
  335. * @param object Model
  336. * @param string $format
  337. * @param boolean $short_prefix
  338. * @return string, binary
  339. * @access public
  340. * @throws PhpError
  341. */
  342. function visualizeGraph(&$model, $format = "input_dot", $short_prefix = TRUE)
  343. {
  344. global $graphviz_param;
  345. $i = 0;
  346. foreach ($model->triples as $key => $statement)
  347. {
  348. $subject = $statement->getLabelSubject();
  349. $predicate = $statement->getLabelPredicate();
  350. $object = $statement->getLabelObject();
  351. // format subject
  352. if (!isset($attrib[$subject]))
  353. {
  354. if (is_a($statement->subject(),'BlankNode'))
  355. {
  356. $attrib[$subject] = $graphviz_param['BLANKNODE_STYLE'];
  357. }
  358. else
  359. {
  360. if ($short_prefix == TRUE && RDFUtil::guessPrefix($subject, $model) != FALSE)
  361. {
  362. $prefix = RDFUtil::guessPrefix($subject, $model);
  363. $subject_label = $prefix.":".RDFUtil::guessName($subject);
  364. $attrib[$subject] = "label=\"".$subject_label."\" ";
  365. if(!isset($prefix_array[$prefix]))
  366. {
  367. $prefix_array[$prefix] = RDFUtil::guessNamespace($subject);
  368. }
  369. }
  370. if (GRAPHVIZ_URI == TRUE)
  371. {
  372. $attrib[$subject] .= "URL=\"".$subject."\"";
  373. }
  374. }
  375. }
  376. // format predicate
  377. if ($short_prefix == TRUE && RDFUtil::guessPrefix($predicate, $model) != FALSE)
  378. {
  379. $prefix = RDFUtil::guessPrefix($predicate, $model);
  380. $predicate_label = "label=\"".$prefix.":".RDFUtil::guessName($predicate)."\"";
  381. if(!isset($prefix_array[$prefix]))
  382. {
  383. $prefix_array[$prefix] = RDFUtil::guessNamespace($predicate);
  384. }
  385. }
  386. else
  387. {
  388. $predicate_label = "label=\"".$predicate."\"";
  389. }
  390. if (is_a($statement,'InfStatement'))
  391. {
  392. $predicate_label .= " ".$graphviz_param['INFERRED_STYLE'];
  393. }
  394. else
  395. {
  396. if (GRAPHVIZ_URI == TRUE)
  397. {
  398. $predicate_label .= "URL=\"".$predicate."\"";
  399. }
  400. }
  401. // format object
  402. if (!isset($attrib[$object]))
  403. {
  404. if (is_a($statement->object(),'BlankNode'))
  405. {
  406. $attrib[$object] = $graphviz_param['BLANKNODE_STYLE'];
  407. }
  408. elseif (is_a($statement->object(),'Literal'))
  409. {
  410. $object_label = $object;
  411. $object = "literal".$i;
  412. $i++;
  413. $attrib[$object] = "label=\"$object_label\" ".$graphviz_param['LITERAL_STYLE'];
  414. }
  415. else
  416. {
  417. if ($short_prefix == TRUE && RDFUtil::guessPrefix($object, $model) != FALSE)
  418. {
  419. $prefix = RDFUtil::guessPrefix($object, $model);
  420. $object_label = $prefix.":".RDFUtil::guessName($object);
  421. $attrib[$object] = "label=\"".$object_label."\" ";
  422. if(!isset($prefix_array[$prefix]))
  423. {
  424. $prefix_array[$prefix] = RDFUtil::guessNamespace($object);
  425. }
  426. }
  427. if (GRAPHVIZ_URI == TRUE)
  428. {
  429. $attrib[$object] .= "URL=\"".$object."\"";
  430. }
  431. }
  432. }
  433. // fill graph array
  434. $graph[] = "\"".$subject."\" -> \"".$object."\" [".$predicate_label."];";
  435. }
  436. //generate DOT-file
  437. $dot = "digraph G { ".$graphviz_param['GRAPH_STYLE']."\n edge [".$graphviz_param['PREDICATE_STYLE']."]\n node [".$graphviz_param['RESOURCE_STYLE']."]\n";
  438. if (isset($attrib))
  439. {
  440. foreach ($attrib AS $key => $value)
  441. {
  442. $dot .= "\"$key\" [$value];\n";
  443. }
  444. }
  445. if (!isset($graph))
  446. {
  447. $dot .= "error [shape=box,label=\"No Statements found!\"]";
  448. }
  449. else
  450. {
  451. $dot .= implode("\n", $graph);
  452. }
  453. if (GRAPHVIZ_STAT == TRUE)
  454. {
  455. $stat_label = "| ".$model->size()." Statements drawn";
  456. }
  457. if ((strstr($graphviz_param['GRAPH_STYLE'], 'rankdir="LR"') === FALSE) && (strstr($graphviz_param['GRAPH_STYLE'], 'rankdir=LR') === FALSE))
  458. {
  459. $sep1 = "}";
  460. $sep2 = "";
  461. }
  462. else
  463. {
  464. $sep1 = "";
  465. $sep2 = "}";
  466. }
  467. if ($short_prefix == TRUE && isset($prefix_array))
  468. {
  469. $struct_label = "{ { Base URI: ".$model->getBaseURI()." $sep1 | { { ".implode("|", array_keys($prefix_array))." } | { ".implode("|", $prefix_array)." } } $stat_label } $sep2";
  470. }
  471. else
  472. {
  473. $struct_label = "{ { Base URI: ".$model->getBaseURI()."$sep1 ".$stat_label." } }";
  474. }
  475. $dot .= "\n struct [shape=Mrecord,label=\"$struct_label\",".$graphviz_param['BOX_STYLE']."];\n";
  476. $dot .= " vocabulary [style=invis];\n struct -> vocabulary [style=invis];\n}";
  477. // if needed call dot.exe
  478. if (($format != "input_dot") && (defined('GRAPHVIZ_PATH')) && (strstr(GRAPHVIZ_FORMAT, $format) !== FALSE))
  479. {
  480. mt_srand((double)microtime()*1000000);
  481. $filename=GRAPHVIZ_TEMP.md5(uniqid(mt_rand())).".dot";
  482. $file_handle = @fopen($filename, 'w');
  483. if ($file_handle)
  484. {
  485. fwrite($file_handle, $dot);
  486. fclose($file_handle);
  487. }
  488. $dotinput = " -T".$format." ".$filename;
  489. ob_start();
  490. passthru(GRAPHVIZ_PATH.$dotinput);
  491. $output = ob_get_contents();
  492. ob_end_clean();
  493. unlink($filename);
  494. echo $output;
  495. return TRUE;
  496. }
  497. elseif ($format == "input_dot")
  498. {
  499. echo $dot;
  500. return TRUE;
  501. }
  502. else
  503. {
  504. return FALSE;
  505. }
  506. }
  507. } // end: RDfUtil
  508. ?>