/** * @ignore */ protected function serialiseSubjects(Graph $graph, $filterType) { $turtle = ''; foreach ($graph->resources() as $resource) { /** @var $resource Resource */ // If the resource has no properties - don't serialise it $properties = $resource->propertyUris(); if (count($properties) == 0) { continue; } // Is this node of the right type? $thisType = $resource->isBNode() ? 'bnode' : 'uri'; if ($thisType != $filterType) { continue; } if ($thisType == 'bnode') { $id = $resource->getBNodeId(); if (isset($this->outputtedBnodes[$id])) { // Already been serialised continue; } $this->outputtedBnodes[$id] = true; $rpcount = $this->reversePropertyCount($resource); if ($rpcount == 0) { $turtle .= '[]'; } else { $turtle .= $this->serialiseResource($resource); } } else { $turtle .= $this->serialiseResource($resource); } $turtle .= $this->serialiseProperties($resource); $turtle .= "\n"; } return $turtle; }
/** * Method to serialise an EasyRdf\Graph to RDF/XML * * @param Graph $graph An EasyRdf\Graph object. * @param string $format The name of the format to convert to. * @param array $options * * @return string The RDF in the new desired format. * @throws Exception */ public function serialise(Graph $graph, $format, array $options = array()) { parent::checkSerialiseParams($format); if ($format != 'rdfxml') { throw new Exception("EasyRdf\\Serialiser\\RdfXml does not support: {$format}"); } // store of namespaces to be appended to the rdf:RDF tag $this->prefixes = array('rdf' => true); // store of the resource URIs we have serialised $this->outputtedResources = array(); $xml = ''; // Serialise URIs first foreach ($graph->resources() as $resource) { if (!$resource->isBnode()) { $xml .= $this->rdfxmlResource($resource, true); } } // Serialise bnodes afterwards foreach ($graph->resources() as $resource) { if ($resource->isBnode()) { $xml .= $this->rdfxmlResource($resource, true); } } // iterate through namepsaces array prefix and output a string. $namespaceStr = ''; foreach ($this->prefixes as $prefix => $count) { $url = RdfNamespace::get($prefix); if (strlen($namespaceStr)) { $namespaceStr .= "\n "; } if (strlen($prefix) === 0) { $namespaceStr .= ' xmlns="' . htmlspecialchars($url) . '"'; } else { $namespaceStr .= ' xmlns:' . $prefix . '="' . htmlspecialchars($url) . '"'; } } return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" . "<rdf:RDF" . $namespaceStr . ">\n" . $xml . "\n</rdf:RDF>\n"; }
/** * Internal function to serialise an EasyRdf\Graph into a DOT formatted string * * @ignore */ protected function serialiseDot(Graph $graph) { $result = "digraph {\n"; // Write the graph attributes foreach ($this->attributes as $k => $v) { $result .= ' ' . $this->escape($k) . '=' . $this->escape($v) . ";\n"; } // Go through each of the properties and write the edges $nodes = array(); $result .= "\n // Edges\n"; foreach ($graph->resources() as $resource) { $name1 = $this->nodeName($resource); foreach ($resource->propertyUris() as $property) { $label = null; if ($this->useLabels) { $label = $graph->resource($property)->label(); } if ($label === null) { if ($this->onlyLabelled == true) { continue; } else { $label = RdfNamespace::shorten($property); } } foreach ($resource->all("<{$property}>") as $value) { $name2 = $this->nodeName($value); $nodes[$name1] = $resource; $nodes[$name2] = $value; $result .= $this->serialiseRow($name1, $name2, array('label' => $label)); } } } ksort($nodes); $result .= "\n // Nodes\n"; foreach ($nodes as $name => $node) { $type = substr($name, 0, 1); $label = ''; if ($type == 'R') { if ($this->useLabels) { $label = $node->label(); } if (!$label) { $label = $node->shorten(); } if (!$label) { $label = $node->getURI(); } $result .= $this->serialiseRow($name, null, array('URL' => $node->getURI(), 'label' => $label, 'shape' => 'ellipse', 'color' => 'blue')); } elseif ($type == 'B') { if ($this->useLabels) { $label = $node->label(); } $result .= $this->serialiseRow($name, null, array('label' => $label, 'shape' => 'circle', 'color' => 'green')); } else { $result .= $this->serialiseRow($name, null, array('label' => strval($node), 'shape' => 'record')); } } $result .= "}\n"; return $result; }