function triples2string() {
		$xml = '<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">';
		foreach ($this->triples as $s => $po) {
			# Start with subject.
			$xml .= "\n<rdf:Description";
			if ('_:' == substr($s, 0, 2))
				$xml .= ' rdf:nodeID="' . htmlspecialchars(substr($s,2)) . '"';
			else
				$xml .= ' rdf:about="' . htmlspecialchars($s) . '"';
			$xml .= ">";
			# Loop through predicate/object pairs.
			foreach($po as $x) {
				list($p, $o) = $x;
				# Output predicate.
				$nsuri = RDFUtil::guessNamespace($p);
				$local = RDFUtil::guessName($p);
				if ('http://www.w3.org/1999/02/22-rdf-syntax-ns#' != $nsuri)
					$xml .= "\n  <ns:" . $local . ' xmlns:ns="' . $nsuri . '"';
				else
					$xml .= "\n  <rdf:" . $local;
				# Output object.
				if (is_array($o)) {
					if ('' != $o[1])
						$xml .= ' xml:lang="' . htmlspecialchars($o[1]) . '"';
					if ('http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral' == $o[2])
						$xml .= ' rdf:parseType="Literal">' . str_replace('\"', '"', $o[0]);
					else if ('' != $o[2])
						$xml .= ' rdf:datatype="' . htmlspecialchars($o[2]) . '">' . $o[0];
					else
						$xml .= '>' . htmlspecialchars($o[0]);
					if ('http://www.w3.org/1999/02/22-rdf-syntax-ns#' != $nsuri)
						$xml .= '</ns:' . $local;
					else
						$xml .= '</rdf:' . $local;
				} else if ('_:' == substr($o, 0, 2))
					$xml .= ' rdf:nodeID="' . htmlspecialchars(substr($o, 2)) . '"/';
				else
					$xml .= ' rdf:resource="' . htmlspecialchars($o) . '"/';
				$xml.=">";
			};
			$xml.="\n</rdf:Description>";
		}
		$xml.="\n</rdf:RDF>";
		return $xml;
	}
示例#2
0
 /**
  * Prints a query result as HTML table.
  * You can change the colors in the configuration file.
  *
  * @param array $queryResult [][?VARNAME] = object Node
  * @access private
  */
 function writeQueryResultAsHtmlTable($queryResult)
 {
     // Import Package Utility
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_UTILITY;
     if (current($queryResult[0]) == NULL) {
         echo 'no match<br>';
         return;
     }
     echo '<table border="1" cellpadding="3" cellspacing="0"><tr><td><b>No.</b></td>';
     foreach ($queryResult[0] as $varName => $value) {
         echo "<td align='center'><b>{$varName}</b></td>";
     }
     echo '</tr>';
     foreach ($queryResult as $n => $var) {
         echo '<tr><td width="20" align="right">' . ($n + 1) . '.</td>';
         foreach ($var as $varName => $value) {
             echo INDENTATION . INDENTATION . '<td bgcolor="';
             echo RDFUtil::chooseColor($value);
             echo '">';
             echo '<p>';
             $lang = NULL;
             $dtype = NULL;
             if (is_a($value, 'Literal')) {
                 if ($value->getLanguage() != NULL) {
                     $lang = ' <b>(xml:lang="' . $value->getLanguage() . '") </b> ';
                 }
                 if ($value->getDatatype() != NULL) {
                     $dtype = ' <b>(rdf:datatype="' . $value->getDatatype() . '") </b> ';
                 }
             }
             echo RDFUtil::getNodeTypeName($value) . $value->getLabel() . $lang . $dtype . '</p>';
         }
         echo '</tr>';
     }
     echo '</table>';
 }
示例#3
0
 /**
  * Returns the local name of the resource.
  * @access	public
  * @return string
  */
 function getLocalName()
 {
     // Import Package Utility
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_UTILITY;
     return RDFUtil::guessName($this->uri);
 }
示例#4
0
         }
         $comment_string .= "Object = " . $_POST['query_object'] . "<BR>";
     }
     // Execute query and display what has been done
     $model = $model->find($query_subj, $query_pred, $query_obj);
     echo $comment_string;
 }
 //  Reify the model if checked in submitted form
 if (isset($_POST['reify']) and $_POST['reify'] == "1") {
     $model =& $model->reify();
     echo "<BR><BR><h3>Your original model has been refied.</h3><BR>";
 }
 // Output Triples as Table if checked in submitted form
 if ($_POST['view_triple'] == "1") {
     echo "<BR><BR><h3>View input as HTML table:</h3><BR>";
     RDFUtil::writeHTMLTable($model);
     echo "<P>";
 }
 //serialize model to RDF with default configuration if checked in submitted form
 if ($_POST['serialize'] == '1') {
     // Create Serializer
     $ser = new RDFSerializer();
     $msg_string = '';
     if (isset($_POST['serial_attributes']) and $_POST['serial_attributes'] == '1') {
         $ser->configUseAttributes(TRUE);
         $msg_string .= 'Use Attributes ';
     }
     if (isset($_POST['serial_entities']) and $_POST['serial_entities'] == '1') {
         $ser->configUseEntities(TRUE);
         $msg_string .= 'Use XML Entities ';
     }
 /**
  *  Call-back function for uksort()
  */
 function _sortURIbyQName($a, $b)
 {
     return strcasecmp(RDFUtil::guessName($a), RDFUtil::guessName($b));
 }
示例#6
0
 /**
  * Writes the RDF serialization of the Model including ALL inferable 
  * triples as HTML table.
  *
  * @access	public 
  */
 function writeAsHtmlTable()
 {
     // Import Package Utility
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_UTILITY;
     RDFUtil::writeHTMLTable($this->getMemModel());
 }
示例#7
0
 /**
  * Generates a dot-file for drawing graphical output with the
  * graphviz-application which can be downloaded at http://www.graphviz.org
  * If the graphviz-application is installed and its path is set to the
  * correct value in constants.php we can directly generate any
  * file format graphviz supports, e.g. SVG, PNG
  * Parameters: model to visualize, output format, use prefixes
  *
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  * WARNING: Graphviz can be slow with large models.
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  *
  * @author   Anton Köstlbacher <*****@*****.**>
  * @param    object  Model
  * @param    string  $format
  * @param    boolean $short_prefix
  * @return   string, binary
  * @access   public
  * @throws   PhpError
  */
 function visualizeGraph(&$model, $format = "input_dot", $short_prefix = TRUE)
 {
     global $graphviz_param;
     $i = 0;
     foreach ($model->triples as $key => $statement) {
         $subject = $statement->getLabelSubject();
         $predicate = $statement->getLabelPredicate();
         $object = $statement->getLabelObject();
         // format subject
         if (!isset($attrib[$subject])) {
             if (is_a($statement->subject(), 'BlankNode')) {
                 $attrib[$subject] = $graphviz_param['BLANKNODE_STYLE'];
             } else {
                 if ($short_prefix == TRUE && RDFUtil::guessPrefix($subject, $model) != FALSE) {
                     $prefix = RDFUtil::guessPrefix($subject, $model);
                     $subject_label = $prefix . ":" . RDFUtil::guessName($subject);
                     $attrib[$subject] = "label=\"" . $subject_label . "\" ";
                     if (!isset($prefix_array[$prefix])) {
                         $prefix_array[$prefix] = RDFUtil::guessNamespace($subject);
                     }
                 }
                 if (GRAPHVIZ_URI == TRUE) {
                     $attrib[$subject] .= "URL=\"" . $subject . "\"";
                 }
             }
         }
         // format predicate
         if ($short_prefix == TRUE && RDFUtil::guessPrefix($predicate, $model) != FALSE) {
             $prefix = RDFUtil::guessPrefix($predicate, $model);
             $predicate_label = "label=\"" . $prefix . ":" . RDFUtil::guessName($predicate) . "\"";
             if (!isset($prefix_array[$prefix])) {
                 $prefix_array[$prefix] = RDFUtil::guessNamespace($predicate);
             }
         } else {
             $predicate_label = "label=\"" . $predicate . "\"";
         }
         if (is_a($statement, 'InfStatement')) {
             $predicate_label .= " " . $graphviz_param['INFERRED_STYLE'];
         } else {
             if (GRAPHVIZ_URI == TRUE) {
                 $predicate_label .= "URL=\"" . $predicate . "\"";
             }
         }
         // format object
         if (!isset($attrib[$object])) {
             if (is_a($statement->object(), 'BlankNode')) {
                 $attrib[$object] = $graphviz_param['BLANKNODE_STYLE'];
             } elseif (is_a($statement->object(), 'Literal')) {
                 $object_label = $object;
                 $object = "literal" . $i;
                 $i++;
                 $attrib[$object] = "label=\"{$object_label}\" " . $graphviz_param['LITERAL_STYLE'];
             } else {
                 if ($short_prefix == TRUE && RDFUtil::guessPrefix($object, $model) != FALSE) {
                     $prefix = RDFUtil::guessPrefix($object, $model);
                     $object_label = $prefix . ":" . RDFUtil::guessName($object);
                     $attrib[$object] = "label=\"" . $object_label . "\" ";
                     if (!isset($prefix_array[$prefix])) {
                         $prefix_array[$prefix] = RDFUtil::guessNamespace($object);
                     }
                 }
                 if (GRAPHVIZ_URI == TRUE) {
                     $attrib[$object] .= "URL=\"" . $object . "\"";
                 }
             }
         }
         // fill graph array
         $graph[] = "\"" . $subject . "\" -> \"" . $object . "\" [" . $predicate_label . "];";
     }
     //generate DOT-file
     $dot = "digraph G { " . $graphviz_param['GRAPH_STYLE'] . "\n edge [" . $graphviz_param['PREDICATE_STYLE'] . "]\n node [" . $graphviz_param['RESOURCE_STYLE'] . "]\n";
     if (isset($attrib)) {
         foreach ($attrib as $key => $value) {
             $dot .= "\"{$key}\" [{$value}];\n";
         }
     }
     if (!isset($graph)) {
         $dot .= "error [shape=box,label=\"No Statements found!\"]";
     } else {
         $dot .= implode("\n", $graph);
     }
     if (GRAPHVIZ_STAT == TRUE) {
         $stat_label = "| " . $model->size() . " Statements drawn";
     }
     if (strstr($graphviz_param['GRAPH_STYLE'], 'rankdir="LR"') === FALSE && strstr($graphviz_param['GRAPH_STYLE'], 'rankdir=LR') === FALSE) {
         $sep1 = "}";
         $sep2 = "";
     } else {
         $sep1 = "";
         $sep2 = "}";
     }
     if ($short_prefix == TRUE && isset($prefix_array)) {
         $struct_label = "{ { Base URI: " . $model->getBaseURI() . " {$sep1} | { { " . implode("|", array_keys($prefix_array)) . " } | { " . implode("|", $prefix_array) . " } } {$stat_label} } {$sep2}";
     } else {
         $struct_label = "{ { Base URI: " . $model->getBaseURI() . "{$sep1} " . $stat_label . " } }";
     }
     $dot .= "\n struct [shape=Mrecord,label=\"{$struct_label}\"," . $graphviz_param['BOX_STYLE'] . "];\n";
     $dot .= " vocabulary [style=invis];\n struct -> vocabulary [style=invis];\n}";
     // if needed call dot.exe
     if ($format != "input_dot" && defined('GRAPHVIZ_PATH') && strstr(GRAPHVIZ_FORMAT, $format) !== FALSE) {
         mt_srand((double) microtime() * 1000000);
         $filename = GRAPHVIZ_TEMP . md5(uniqid(mt_rand())) . ".dot";
         $file_handle = @fopen($filename, 'w');
         if ($file_handle) {
             fwrite($file_handle, $dot);
             fclose($file_handle);
         }
         $dotinput = " -T" . $format . " " . $filename;
         ob_start();
         passthru(GRAPHVIZ_PATH . $dotinput);
         $output = ob_get_contents();
         ob_end_clean();
         unlink($filename);
         echo $output;
         return TRUE;
     } elseif ($format == "input_dot") {
         echo $dot;
         return TRUE;
     } else {
         return FALSE;
     }
 }
示例#8
0
 /**
  * Write the RDF serialization of the DbModel as HTML table.
  *
  * @access	public
  */
 function writeAsHtmlTable()
 {
     include_once RDFAPI_INCLUDE_DIR . PACKAGE_UTILITY;
     $memModel =& $this->getMemModel();
     RDFUtil::writeHTMLTable($memModel);
 }
示例#9
0
 /**
  * @param object Resource $resource
  * @access   private
  */
 function collectNamespace($resource)
 {
     $namespace = RDFUtil::getNamespace($resource);
     if (!in_array($namespace, $this->m_namespaces) && $namespace != '') {
         $prefix = array_search($namespace, $this->m_defaultNamespaces);
         if ($prefix === FALSE) {
             $prefix = $this->getNextNamespacePrefix();
         }
         $this->m_namespaces[$prefix] = $namespace;
     }
 }
示例#10
0
 /**
  * Alias for RDFUtil::visualiseGraph(&$model, $format, $short_prefix)
  *
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  * Note: See RDFUtil for further Information.
  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  *
  * @author   Anton K�tlbacher <*****@*****.**>
  * @param    string  $format
  * @param    boolean $short_prefix
  * @return   string, binary
  * @access   public
  * @throws   PhpError
  */
 function visualize($format = "dot", $short_prefix = TRUE)
 {
     return RDFUtil::visualizeGraph($this, $format, $short_prefix);
 }
示例#11
0
 /**
  * Returns all tripels of a certain vocabulary.
  * $vocabulary is the namespace of the vocabulary inluding a # : / char at the end.
  * e.g. http://www.w3.org/2000/01/rdf-schema#
  * Returns an empty MemModel if nothing is found.
  *
  * @param	string	$vocabulary
  * @return	object MemModel
  * @access	public
  */
 function findVocabulary($vocabulary)
 {
     if ($this->size() == 0) {
         return new MemModel();
     }
     if ($vocabulary == NULL || $vocabulary == '') {
         return $this;
     }
     $res = new MemModel($this->getBaseURI());
     if ($this->indexed == 0) {
         foreach ($this->indexArr[5] as $key => $value) {
             $pos = strpos($key, '#') + 1;
             if (substr($key, 0, $pos) == $vocabulary) {
                 for ($i = 1; $i <= $value[0]; $i++) {
                     $res->add($this->triples[$value[$i]]);
                 }
             }
         }
         return $res;
     } else {
         // Import Package Utility
         include_once RDFAPI_INCLUDE_DIR . PACKAGE_UTILITY;
         foreach ($this->triples as $value) {
             if (RDFUtil::getNamespace($value->getPredicate()) == $vocabulary) {
                 $res->add($value);
             }
         }
         return $res;
     }
 }