Source for file RdfUtil.php

Documentation is available at RdfUtil.php

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

Documentation generated on Fri, 13 Jan 2006 07:49:09 +0100 by phpDocumentor 1.3.0RC4