public function testSelectSerializeAndDelete() { global $EndPoint4store, $modeDebug, $prefixSparql, $prefixTurtle, $graph1, $graph2; $s = new Endpoint($EndPoint4store, false, $modeDebug); $this->checkIfInitialState($s); $r = $s->set($graph1, $prefixTurtle . "\n@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n\t\t\t\t\ta:A b:Name \"Test2\"@en.\n\t\t\t\t\ta:A b:Name \"Test3\"@fr.\n\t\t\t\t\ta:A b:Name \"Test4\".\n\t\t\t\t\ta:A b:date \"2010-03-09T22:30:00Z\"^^xsd:dateTime .\n\t\t\t\t\t"); $q = $prefixSparql . "\n select * where {GRAPH <" . $graph1 . "> {a:A ?p ?o.}} "; $triples = $s->query($q, 'rows'); $err = $s->getErrors(); if ($err) { print_r($err); $this->assertTrue(false); } for ($i = 0, $i_max = count($triples); $i < $i_max; $i++) { $triples[$i]['s'] = "http://example.com/test/a/A"; $triples[$i]['s type'] = "uri"; } //print_r($triples); /* Serializer instantiation */ $ser = ARC2::getNTriplesSerializer(); /* Serialize a triples array */ $docd = $ser->getSerializedTriples($triples, 1); $q = "DELETE DATA { \n\t\t\t\tGRAPH <" . $graph1 . "> { \n\t\t\t\t{$docd} \n \t\t}}"; //print_r($q); $res = $s->query($q, 'raw'); $err = $s->getErrors(); if ($err) { print_r($err); $this->assertTrue(false); } $this->assertTrue($res); $this->checkIfInitialState($s); }
/** * Perform the load. * * @param EasyRdf_Graph $chunk * @return void */ public function execute(&$chunk) { if (!$chunk->isEmpty()) { // Don't use EasyRdf's ntriple serializer, as something might be wrong with its unicode byte characters // After serializing with semsol/arc and easyrdf, the output looks the same (with unicode characters), but after a // binary utf-8 conversion (see $this->serialize()) the outcome is very different, leaving easyrdf's encoding completely different // from the original utf-8 characters, and the semsol/arc encoding correct as the original. $ttl = $chunk->serialise('turtle'); $arc_parser = \ARC2::getTurtleParser(); $ser = \ARC2::getNTriplesSerializer(); $arc_parser->parse('', $ttl); $triples = $ser->getSerializedTriples($arc_parser->getTriples()); preg_match_all("/(<.*\\.)/", $triples, $matches); if ($matches[0]) { $this->buffer = array_merge($this->buffer, $matches[0]); } $triple_count = count($matches[0]); $this->log("Added {$triple_count} triples to the load buffer."); while (count($this->buffer) >= $this->loader->buffer_size) { // Log the time it takes to load the triples into the store $start = microtime(true); $buffer_size = $this->loader->buffer_size; $triples_to_send = array_slice($this->buffer, 0, $buffer_size); $this->addTriples($triples_to_send); $this->buffer = array_slice($this->buffer, $buffer_size); $duration = round((microtime(true) - $start) * 1000, 2); $this->log("Took {$buffer_size} triples from the load buffer, loading them took {$duration} ms."); } } }
static function deleteTriples($iri, $graph, $endpoint) { /* Serializer instantiation */ $ser = ARC2::getNTriplesSerializer(); $sp_readonly = new Endpoint($endpoint); //DELETE OLD TRIPLES WITH THE SUBJECT $uri $q = "select * where {GRAPH <" . $graph . "> {<" . $iri . "> ?p ?o.}} "; $oldTriples = $sp_readonly->query($q, 'rows'); $err = $sp_readonly->getErrors(); if ($err) { throw new Exception(self::buildMessage($err)); } if (count($oldTriples) > 0) { for ($i = 0, $i_max = count($oldTriples); $i < $i_max; $i++) { $t =& $oldTriples[$i]; $t['s'] = $iri; $t['s type'] = "uri"; } /* Serialize a triples array */ $docd = $ser->getSerializedTriples($oldTriples, 1); $sp = new Endpoint($endpoint, false); $q = "DELETE DATA { \n\t\t\t\t\t\t\tGRAPH <" . $graph . "> { \n\t\t\t\t\t\t\t{$docd} \n\t\t\t \t\t}}"; $res = $sp->query($q, 'raw'); $err = $sp->getErrors(); if ($err) { throw new Exception(self::buildMessage($err)); } if (!$res) { $msg = "Query delete old triples return: False without errors"; throw new Exception($msg); } } }
public function printGraph() { /* Serializer instantiation */ $ser = \ARC2::getNTriplesSerializer(); foreach ($this->objectToPrint as $class => $prop) { $triples = $prop->getTriples(); } /* Serialize a triples array */ echo $ser->getSerializedTriples($triples); }
public function testLiteral() { $ser = ARC2::getNTriplesSerializer(); $v = array(); $v['type'] = 'literal'; $v['value'] = 'basilic'; $res = $ser->getTerm($v); $this->assertEquals("\"basilic\"", $res); $v['value'] = 'fraise'; $res = $ser->getTerm($v); $this->assertEquals("\"fraise\"", $res); $v['value'] = 'rat'; $res = $ser->getTerm($v); $this->assertEquals("\"rat\"", $res); }
/** * getRdf get the full BIBO RDF serialization of the collection of entries * @param $format the serialization format * @throws Exception if PHP_ZOTERO_ENTRIES_ARC_PATH is not defined */ public function getRdf($format = 'rdf/xml') { if (!defined('PHP_ZOTERO_ENTRIES_ARC_PATH')) { throw new Exception('PHP_ZOTERO_ENTRIES_ARC_PATH must be defined and valid to use RDF methods'); } require_once PHP_ZOTERO_ENTRIES_ARC_PATH; switch ($format) { case 'rdf/xml': $ser = ARC2::getRDFXMLSerializer($this->arcConf); break; case 'rdf/json': $ser = ARC2::getRDFJsonSerializer($this->arcConf); break; case 'turtle': $ser = ARC2::getTurtleSerializer($this->arcConf); break; case 'ntriples': $ser = ARC2::getNTriplesSerializer($this->arcConf); break; } return $ser->getSerializedIndex($this->getArcIndex()); }
/** * Store triples into our local store * * @param integer $id The id of the configured semantic source * @param array $config The configuration needed to extract the triples */ public function cacheTriples($id, array $config) { // Fetch the ARC2 triplestore $store = $this->setUpArc2Store(); // Fetch the data extractor for the given type $type = $config['type']; $source_type = strtolower($type); $graph = ''; $caching_necessary = true; switch ($source_type) { case 'turtle': $rdf_reader = \App::make('\\Tdt\\Core\\DataControllers\\RDFController'); $configuration = array('uri' => $config['uri'], 'format' => 'turtle'); $data = $rdf_reader->readData($configuration, array()); $graph = $data->data; break; case 'rdf': $rdf_reader = \App::make('\\Tdt\\Core\\DataControllers\\RDFController'); $configuration = array('uri' => $config['uri'], 'format' => 'xml'); $data = $rdf_reader->readData($configuration, array()); $graph = $data->data; break; case 'sparql': // Do nothing, the sparql endpoint is already optimized for read operations $caching_necessary = false; break; case 'ldf': // Do nothing the ldf endpoint is a queryable endpoint itself $caching_necessary = false; break; default: \App::abort(400, "The source type, {$source_type}, was configured, but no reader has been found\n to extract semantic data from it."); break; } // If the semantic source needs caching in our local store@ if ($caching_necessary) { // Make the graph name to cache the triples into $graph_name = self::$graph_name . $id; // Serialise the triples into a turtle string $ttl = $graph->serialise('turtle'); // Parse the turtle into an ARC graph $arc_parser = \ARC2::getTurtleParser(); $ser = \ARC2::getNTriplesSerializer(); // Parse the turtle string $arc_parser->parse('', $ttl); // Serialize the triples again, this is because an EasyRdf_Graph has // troubles with serializing some unicode characters. The underlying bytes are // not properly converted to utf8 // A dump shows that all unicode encodings through serialization are the same (in easyrdf and arc) // however when we convert the string (binary) into a utf8, only the arc2 serialization // comes out correctly, hence something beneath the encoding (byte sequences?) must hold some wrongs in the EasyRdf library. $triples = $ser->getSerializedTriples($arc_parser->getTriples()); preg_match_all("/(<.*\\.)/", $triples, $matches); $triples_buffer = array(); if ($matches[0]) { $triples_buffer = $matches[0]; } \Log::info("--------------- CACHING TRIPLES -------------------------"); \Log::info("Starting insertion of triples into the ARC2 RDF Store into the graph with the name " . $graph_name); // Insert the triples in a chunked manner (not all triples at once) $buffer_size = 20; while (count($triples_buffer) >= $buffer_size) { $triples_to_cache = array_slice($triples_buffer, 0, $buffer_size); \Log::info("Caching " . count($triples_to_cache) . " triples into the store."); $this->addTriples($graph_name, $triples_to_cache, $store); $triples_buffer = array_slice($triples_buffer, $buffer_size); } // Insert the last triples in the buffer \Log::info("Caching " . count($triples_buffer) . " triples into the store."); $this->addTriples($graph_name, $triples_buffer, $store); \Log::info("--------------- DONE CACHING TRIPLES -------------------"); } }
public static function serializeRdf($data, $extension = 'rdf') { global $conf; global $lodspk; $ser; $dPointer; $docs = Utils::travelTree($data); require_once $conf['home'] . 'lib/arc2/ARC2.php'; $parser = ARC2::getRDFParser(); $triples = array(); foreach ($docs as $d) { $parser->parse($conf['basedir'], $d); $t = $parser->getTriples(); $triples = array_merge($triples, $t); } if ($lodspk['mirror_external_uris']) { global $uri; global $localUri; $t = array(); $t['s'] = $localUri; $t['s_type'] = 'uri'; $t['p'] = "http://www.w3.org/2002/07/owl#sameAs"; $t['o'] = $uri; $t['o_type'] = 'uri'; array_push($triples, $t); $t['p'] = "http://www.w3.org/2000/10/swap/pim/contact#preferredURI"; array_push($triples, $t); } switch ($extension) { case 'ttl': $ser = ARC2::getTurtleSerializer(); break; case 'nt': $ser = ARC2::getNTriplesSerializer(); break; case 'json': $ser = ARC2::getRDFJSONSerializer(); break; case 'rdf': $ser = ARC2::getRDFXMLSerializer(); break; case 'html': return array("content" => $triples, "serialized" => false); break; default: $ser = null; } if ($ser != null) { $doc = $ser->getSerializedTriples($triples); } else { $doc = var_export($data, true); } return array("content" => $doc, "serialized" => true); }
function getNTriplesDescribeResultDoc($r) { $this->setHeader('content-type', 'Content-Type: text/plain'); $index = $r['result']; $ser = ARC2::getNTriplesSerializer($this->a); $dur = $r['query_time']; return '# query time: ' . $dur . "\n" . $ser->getSerializedIndex($index) . "\n"; }
* Out * ****/ $ns = array('p' => trim($prefix), 'nif' => NIF, 'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#', 'owl' => 'http://www.w3.org/2002/07/owl#', 'rlog' => 'http://persistence.uni-leipzig.org/nlp2rdf/ontologies/rlog#', 'dc' => 'http://purl.org/dc/elements/1.1/'); $ser = null; switch ($outformat) { case "turtle": $ser = ARC2::getTurtleSerializer(array('ns' => $ns)); break; case "rdfxml": $ser = ARC2::getRDFXMLSerializer(array('ns' => $ns)); break; case "json": $ser = ARC2::getRDFJSONSerializer(array('ns' => $ns)); break; case "ntriples": $ser = ARC2::getNTriplesSerializer(array('ns' => $ns)); break; default: $ser = ARC2::getTurtleSerializer(array('ns' => $ns)); break; } $output = ""; if ($outformat == "turtle") { $output = $ser->getSerializedTriples($triples); header("Content-Type: text/turtle"); } else { if ($outformat == "rdfxml") { $output = $ser->getSerializedTriples($triples); header("Content-Type: application/rdf+xml"); } else { if ($outformat == "json") {
function toNTriples() { $index = $this->toIndex(); $ser = ARC2::getNTriplesSerializer(array('ns' => $this->ns)); return $ser->getSerializedIndex($index); }
function journo_emitN3(&$j) { global $_conf; $ser = ARC2::getNTriplesSerializer($_conf); $triples = journo_asARC2Triples($j); $doc = $ser->getSerializedTriples($triples); print $doc; }
/** * getItemAsRdf returns the BIBO RDF for the entire Zotero Item * @param $format default 'rdf/xml' the rdf serialization to use * @return string the RDF serialization * @throw Exception if PHP_ZOTERO_ENTRIES_ARC_PATH is not defined */ public function getItemAsRdf($format = 'rdf/xml') { if (!defined('PHP_ZOTERO_ENTRIES_ARC_PATH')) { throw new Exception('PHP_ZOTERO_ENTRIES_ARC_PATH must be defined and valid to use RDF methods'); } if (!$this->arcIndex) { $this->setRdf(); } switch ($format) { case 'rdf/xml': $ser = ARC2::getRDFXMLSerializer($this->arcConf); break; case 'rdf/json': $ser = ARC2::getRDFJsonSerializer($this->arcConf); break; case 'turtle': $ser = ARC2::getTurtleSerializer($this->arcConf); break; case 'ntriples': $ser = ARC2::getNTriplesSerializer($this->arcConf); break; } return $ser->getSerializedIndex(array($this->itemUri => $this->arcIndex[$this->itemUri])); }