/** * Serialise an EasyRdf_Graph to the RDF format of choice. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); $ntriples = parent::serialise($graph, 'ntriples'); // Open a pipe to the rapper command $descriptorspec = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w")); // Hack to produce more concise RDF/XML if ($format == 'rdfxml') { $format = 'rdfxml-abbrev'; } $process = proc_open(escapeshellcmd($this->_rapperCmd) . " --quiet " . " --input ntriples " . " --output " . escapeshellarg($format) . " - " . 'unknown://', $descriptorspec, $pipes, '/tmp', null); if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // 2 => readable handle connected to child stderr fwrite($pipes[0], $ntriples); fclose($pipes[0]); $output = stream_get_contents($pipes[1]); fclose($pipes[1]); $error = stream_get_contents($pipes[2]); fclose($pipes[2]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $returnValue = proc_close($process); if ($returnValue) { throw new EasyRdf_Exception("Failed to convert RDF: " . $error); } } else { throw new EasyRdf_Exception("Failed to execute rapper command."); } return $output; }
/** * Serialise an EasyRdf_Graph to the RDF format of choice. * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); $ntriples = parent::serialise($graph, 'ntriples'); // Hack to produce more concise RDF/XML if ($format == 'rdfxml') { $format = 'rdfxml-abbrev'; } return EasyRdf_Utils::execCommandPipe($this->_rapperCmd, array('--quiet', '--input', 'ntriples', '--output', $format, '-', 'unknown://'), $ntriples); }
/** * Serialise an EasyRdf_Graph into an array of N-Triples objects * * @param object EasyRdf_Graph $graph An EasyRdf_Graph object. * @param string $format The name of the format to convert to. * @return string The RDF in the new desired format. */ public function serialise($graph, $format) { parent::checkSerialiseParams($graph, $format); $triples = array(); foreach ($graph->toArray() as $resource => $properties) { foreach ($properties as $property => $values) { foreach ($values as $value) { array_push($triples, array('s' => $this->ntriplesResource($resource), 'p' => "<" . $this->escapeString($property) . ">", 'o' => $this->ntriplesValue($value))); } } } // Sort the triples into a consistent order usort($triples, array($this, 'compareTriples')); return $triples; }