/** * @ignore */ private static function statement2rdf(PDOStatement $statement) { $graph = new EasyRdf_Graph(); while ($r = $statement->fetch()) { if (isset($r['l_language']) && !empty($r['l_language'])) { $graph->addLiteral($r['subject'], $r['predicate'], $r['object'], $r['l_language']); } elseif (common_Utils::isUri($r['object'])) { $graph->add($r['subject'], $r['predicate'], $r['object']); } else { $graph->addLiteral($r['subject'], $r['predicate'], $r['object']); } } $format = EasyRdf_Format::getFormat('rdfxml'); return $graph->serialise($format); }
public static function toFile($filePath, $triples) { $graph = new \EasyRdf_Graph(); foreach ($triples as $triple) { if (!empty($triple->lg)) { $graph->addLiteral($triple->subject, $triple->predicate, $triple->object, $triple->lg); } elseif (\common_Utils::isUri($triple->object)) { $graph->add($triple->subject, $triple->predicate, $triple->object); } else { $graph->addLiteral($triple->subject, $triple->predicate, $triple->object); } } $format = \EasyRdf_Format::getFormat('rdfxml'); return file_put_contents($filePath, $graph->serialise($format)); }
/** * Create a new dataset * * @param array $config The config of the new dataset * * @return void */ public function add($config) { // Create a auto-generated subject URI $id = $this->getIncrementalId(); $uri = \URL::to('/users/' . $id); $context = $this->getContext(); // Add the dataset resource $graph = new \EasyRdf_Graph(); $user = $graph->resource($uri . '#agent'); $user->addType('foaf:Agent'); foreach ($this->getFields() as $field) { if ($field['domain'] == 'foaf:Agent') { if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { if (filter_var(trim($config[$field['var_name']]), FILTER_VALIDATE_URL)) { $graph->addResource($user, $field['sem_term'], trim($config[$field['var_name']])); } else { $graph->add($user, $field['sem_term'], trim($config[$field['var_name']])); } } else { if (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { if (filter_var($val, FILTER_VALIDATE_URL)) { $graph->addResource(${$user}, $field['sem_term'], $val); } else { $graph->add(${$user}, $field['sem_term'], $val); } } } } } } } $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $collection = $this->getMongoCollection(); $collection->insert($compact_document); }
/** * Create a new dataset * * @param array $config The config of the new dataset * * @return void */ public function add($config) { \EasyRdf_Namespace::set('odapps', 'http://semweb.mmlab.be/ns/odapps#'); // Create a auto-generated subject URI $id = $this->getIncrementalId(); $uri = \URL::to('/apps/' . $id); $context = $this->getContext(); $graph = new \EasyRdf_Graph(); $application = $graph->resource($uri . '#application'); $application->addType('odapps:Application'); foreach ($this->getFields() as $field) { if ($field['domain'] == 'odapps:Application') { if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { if (filter_var(trim($config[$field['var_name']]), FILTER_VALIDATE_URL)) { $graph->addResource($application, $field['sem_term'], trim($config[$field['var_name']])); } else { $graph->add($application, $field['sem_term'], trim($config[$field['var_name']])); } } else { if (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { if (filter_var($val, FILTER_VALIDATE_URL)) { $graph->addResource($application, $field['sem_term'], $val); } else { $graph->add($application, $field['sem_term'], $val); } } } } } } } $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $collection = $this->getMongoCollection(); $collection->insert($compact_document); }
public function testDumpResource() { $graph = new EasyRdf_Graph(); $graph->addResource('http://example.com/joe#me', 'rdf:type', 'foaf:Person'); $graph->addResource('http://example.com/joe#me', 'foaf:homepage', 'http://example.com/'); $graph->add('http://example.com/joe#me', 'foaf:knows', $graph->newBnode()); $text = $graph->dumpResource('http://example.com/joe#me', false); $this->assertContains('http://example.com/joe#me', $text); $this->assertContains('-> rdf:type -> foaf:Person', $text); $this->assertContains('-> foaf:homepage -> http://example.com/', $text); $this->assertContains('-> foaf:knows -> _:genid1', $text); $html = $graph->dumpResource('http://example.com/joe#me', true); $this->assertContains('http://example.com/joe#me', $html); $this->assertContains('>rdf:type</span>', $html); $this->assertContains('>foaf:Person</a>', $html); $this->assertContains('>foaf:homepage</span>', $html); $this->assertContains('>http://example.com/</a>', $html); $this->assertContains('>foaf:knows</span>', $html); $this->assertContains('>_:genid1</a>', $html); }
/** * Transforms the statements of a StatementIterator instance into a stream, a file for instance. * * @param StatementIterator $statements The StatementIterator containing all the Statements which * should be serialized by the serializer. * @param string|resource $outputStream Filename or file pointer to the stream to where the serialization * should be written. * @throws \Exception if unknown serilaization was given. */ public function serializeIteratorToStream(StatementIterator $statements, $outputStream) { /* * check parameter $outputStream */ if (is_resource($outputStream)) { // use it as it is } elseif (is_string($outputStream)) { $outputStream = fopen($outputStream, 'w'); } else { throw new \Exception('Parameter $outputStream is neither a string nor resource.'); } $graph = new \EasyRdf_Graph(); // go through all statements foreach ($statements as $statement) { /* * Handle subject */ $stmtSubject = $statement->getSubject(); if ($stmtSubject->isNamed()) { $s = $stmtSubject->getUri(); } elseif ($stmtSubject->isBlank()) { $s = $stmtSubject->getBlankId(); } else { throw new \Exception('Subject can either be a blank node or an URI.'); } /* * Handle predicate */ $stmtPredicate = $statement->getPredicate(); if ($stmtPredicate->isNamed()) { $p = $stmtPredicate->getUri(); } else { throw new \Exception('Predicate can only be an URI.'); } /* * Handle object */ $stmtObject = $statement->getObject(); if ($stmtObject->isNamed()) { $o = array('type' => 'uri', 'value' => $stmtObject->getUri()); } elseif ($stmtObject->isBlank()) { $o = array('type' => 'bnode', 'value' => $stmtObject->getBlankId()); } elseif ($stmtObject->isLiteral()) { $o = array('type' => 'literal', 'value' => $stmtObject->getValue()); } else { throw new \Exception('Object can either be a blank node, an URI or literal.'); } $graph->add($s, $p, $o); } fwrite($outputStream, $graph->serialise($this->serialization) . PHP_EOL); }
public function testReplaceDirectJson() { $graph = new EasyRdf_Graph('http://localhost:8080/data/new.rdf'); $graph->add('urn:subject', 'urn:predicate', 'object'); $this->_client->addMock('PUT', '/data/?graph=http%3A%2F%2Ffoo.com%2Fbar.rdf', 'OK', array('callback' => array($this, 'checkTurtleRequest'))); $response = $this->_graphStore->replace($graph, "http://foo.com/bar.rdf", 'json'); $this->assertEquals('200', $response->getStatus()); }
/** * Add a resource to the graph * * @param EasyRdf_Graph $graph * @param core_kernel_classes_Resource $resource * @ignore */ private function addResource(EasyRdf_Graph $graph, core_kernel_classes_Resource $resource) { foreach ($resource->getRdfTriples() as $triple) { if (!empty($triple->lg)) { $graph->addLiteral($triple->subject, $triple->predicate, $triple->object, $triple->lg); } elseif (common_Utils::isUri($triple->object)) { $graph->add($triple->subject, $triple->predicate, $triple->object); } else { $graph->addLiteral($triple->subject, $triple->predicate, $triple->object); } } }
/** * Rebase the graph on triples with the start fragment to our own base URI * * @param string $start_fragment * @param EasyRdf_Graph $graph * * @return EasyRdf_Graph */ private function rebaseGraph($start_fragment, $graph) { // Filter out the #dataset meta-data (if present) and change the URI's to our base URI $collections = $graph->allOfType('hydra:Collection'); // Fetch all of the subject URI's that bring forth hydra meta-data (and are thus irrelevant) $ignore_subjects = array(); if (empty($collection)) { $collections = $graph->allOfType('hydra:PagedCollection'); } if (!empty($collections)) { foreach ($collections as $collection) { array_push($ignore_subjects, $collection->getUri()); } } // Fetch the bnode of the hydra mapping (property is hydra:search) $hydra_mapping = $graph->getResource($start_fragment . '#dataset', 'hydra:search'); if (!empty($hydra_mapping)) { // Hydra mapping's will be a bnode structure array_push($ignore_subjects, '_:' . $hydra_mapping->getBNodeId()); $mapping_nodes = $hydra_mapping->all('hydra:mapping'); foreach ($mapping_nodes as $mapping_node) { if ($mapping_node->isBNode()) { array_push($ignore_subjects, '_:' . $mapping_node->getBNodeId()); } } $graph->deleteResource($start_fragment . '#dataset', 'hydra:search', '_:genid1'); $graph->deleteResource('_:genid1', 'hydra:mapping', '_:genid2'); // Delete all of the mapping related resources $triples = $graph->toRdfPhp(); } else { // Change all of the base (startfragment) URI's to our own base URI $triples = $graph->toRdfPhp(); } // Unset the #dataset unset($triples[$start_fragment . '#dataset']); foreach ($ignore_subjects as $ignore_subject) { unset($triples[$ignore_subject]); } $adjusted_graph = new \EasyRdf_Graph(); foreach ($triples as $subject => $triple) { foreach ($triple as $predicate => $objects) { foreach ($objects as $object) { $adjusted_graph->add($subject, $predicate, $object['value']); } } } return $adjusted_graph; }
* running on your local machine in order to test this example. * * @package EasyRdf * @copyright Copyright (c) 2009-2011 Nicholas J Humfrey * @license http://unlicense.org/ */ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/'); require_once "EasyRdf.php"; ?> <html> <head> <title>GraphStore example</title> </head> <body> <?php // Use a local SPARQL 1.1 Graph Store (eg RedStore) $gs = new EasyRdf_GraphStore('http://localhost:8080/data/'); // Add the current time in a graph $graph1 = new EasyRdf_Graph(); $graph1->add('http://example.com/test', 'rdfs:label', 'Test'); $graph1->add('http://example.com/test', 'dc:date', time()); $gs->insert($graph1, 'time.rdf'); // Get the graph back out of the graph store and display it $graph2 = $gs->get('time.rdf'); print $graph2->dump(); ?> </body> </html>
/** * @see https://github.com/njh/easyrdf/issues/209 */ public function testIssue209() { $g = new EasyRdf_Graph(); $g->add('http://example.com/resource', 'rdf:type', new EasyRdf_Resource('foaf:Person')); $g->add('http://example.com/resource', 'rdf:type', new EasyRdf_Resource('http://example.com/TypeA')); $xml = $g->serialise('rdfxml'); $g2 = new EasyRdf_Graph('http://example.com/', $xml, 'rdfxml'); $types = $g2->resource('http://example.com/resource')->typesAsResources(); $expected = array('http://example.com/TypeA', 'http://xmlns.com/foaf/0.1/Person'); $this->assertCount(2, $types); $this->assertContains($types[0]->getUri(), $expected); $this->assertContains($types[1]->getUri(), $expected); }
*/ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/'); require_once "EasyRdf.php"; ?> <html> <head> <title>Example of using EasyRdf_Graph directly</title> </head> <body> <?php $graph = new EasyRdf_Graph(); $graph->addResource("http://example.com/joe", "rdf:type", "foaf:Person"); $graph->addLiteral("http://example.com/joe", "foaf:name", "Joe Bloggs"); $graph->addLiteral("http://example.com/joe", "foaf:name", "Joseph Bloggs"); $graph->add("http://example.com/joe", "rdfs:label", "Joe"); $graph->setType("http://njh.me/", "foaf:Person"); $graph->add("http://njh.me/", "rdfs:label", "Nick"); $graph->addLiteral("http://njh.me/", "foaf:name", "Nicholas Humfrey"); $graph->addResource("http://njh.me/", "foaf:homepage", "http://www.aelius.com/njh/"); ?> <p> <b>Name:</b> <?php echo $graph->get("http://example.com/joe", "foaf:name"); ?> <br /> <b>Names:</b> <?php echo $graph->join("http://example.com/joe", "foaf:name"); ?> <br />
/** * Create a new dataset * * @param array $config The config of the new dataset * * @return void */ public function add($config) { // Create a auto-generated subject URI $id = $this->getIncrementalId(); $uri = \URL::to('/datasets/' . $id); $context = $this->getContext(); // Add the dataset resource $graph = new \EasyRdf_Graph(); $dataset = $graph->resource($uri . '#dataset'); $dataset->addType('dcat:Dataset'); foreach ($this->getFields() as $field) { if ($field['domain'] == 'dcat:Dataset') { if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { $graph->add($dataset, $field['sem_term'], trim($config[$field['var_name']])); } elseif (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { $graph->add($dataset, $field['sem_term'], $val); } } } } } $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); // Add the datarecord resource $datarecord = $graph->resource($uri); $datarecord->addType('dcat:CatalogRecord'); $created = time(); $datarecord->addLiteral('http://purl.org/dc/terms/issued', $created); $datarecord->addLiteral('http://purl.org/dc/terms/modified', $created); $datarecord->addResource('http://purl.org/dc/terms/creator', \URL::to('/users/' . strtolower(str_replace(" ", "", $config['user'])))); foreach ($this->getFields() as $field) { if ($field['domain'] == 'dcat:CatalogRecord') { if ($field['single_value'] && in_array($field['type'], ['string', 'text', 'list'])) { if (filter_var(trim($config[$field['var_name']]), FILTER_VALIDATE_URL)) { $graph->addResource($datarecord, $field['sem_term'], trim($config[$field['var_name']])); } else { $graph->add($datarecord, $field['sem_term'], trim($config[$field['var_name']])); } } elseif (!$field['single_value'] && in_array($field['type'], ['string', 'list'])) { if (!empty($config[$field['var_name']])) { foreach ($config[$field['var_name']] as $val) { if (filter_var($val, FILTER_VALIDATE_URL)) { $graph->addResource($datarecord, $field['sem_term'], $val); } else { $graph->add($datarecord, $field['sem_term'], $val); } } } } } } // Add the relationship with the dataset $graph->addResource($datarecord, 'http://xmlns.com/foaf/spec/primaryTopic', $uri . '#dataset'); // Add the distribution resource foreach ($config['distributions'] as $distribution) { $id = $this->getIncrementalId(); $distr_uri = $uri . '#distribution' . $id; $distributionResource = $graph->resource($distr_uri); $distributionResource->addType('dcat:Distribution'); if (!empty($distribution['license'])) { $graph->addResource($distributionResource, 'dct:license', $distribution['license']); } if (!empty($distribution['usecases'])) { foreach ($distribution['usecases'] as $usecase) { $graph->addResource($distributionResource, 'linda:useFor', $usecase); } } // Add the distribution to the dataset $graph->addResource($dataset, 'dcat:distribution', $distr_uri); } $serializer = new \EasyRdf_Serialiser_JsonLd(); $jsonld = $serializer->serialise($graph, 'jsonld'); $compact_document = (array) JsonLD::compact($jsonld, $context); $collection = $this->getMongoCollection(); $collection->insert($compact_document); }
<?php if (isset($_REQUEST['uri'])) { $graph = new EasyRdf_Graph(); # 1st Technique $me = $graph->resource($_REQUEST['uri'], 'foaf:Person'); $me->set('foaf:name', $_REQUEST['title'] . ' ' . $_REQUEST['given_name'] . ' ' . $_REQUEST['family_name']); if ($_REQUEST['email']) { $email = $graph->resource("mailto:" . $_REQUEST['email']); $me->add('foaf:mbox', $email); } if ($_REQUEST['homepage']) { $homepage = $graph->resource($_REQUEST['homepage']); $me->add('foaf:homepage', $homepage); } # 2nd Technique $graph->add($_REQUEST['uri'], array('foaf:title' => $_REQUEST['title'], 'foaf:givenname' => $_REQUEST['given_name'], 'foaf:family_name' => $_REQUEST['family_name'], 'foaf:nick' => $_REQUEST['nickname'])); # Add friends for ($i = 1; $i <= 4; $i++) { if ($_REQUEST["person_{$i}"]) { $person = $graph->resource($_REQUEST["person_{$i}"]); $graph->add($me, 'foaf:knows', $person); } } # Finally output the graph $data = $graph->serialise($_REQUEST['format']); if (!is_scalar($data)) { $data = var_export($data, true); } print "<pre>" . htmlspecialchars($data) . "</pre>"; } ?>
$email = $graph->resource("mailto:" . $_REQUEST['email']); $me->add('foaf:mbox', $email); } if ($_REQUEST['homepage']) { $homepage = $graph->resource($_REQUEST['homepage']); $me->add('foaf:homepage', $homepage); } # 2nd Technique $graph->addLiteral($_REQUEST['uri'], 'foaf:title', $_REQUEST['title']); $graph->addLiteral($_REQUEST['uri'], 'foaf:givenname', $_REQUEST['given_name']); $graph->addLiteral($_REQUEST['uri'], 'foaf:family_name', $_REQUEST['family_name']); $graph->addLiteral($_REQUEST['uri'], 'foaf:nick', $_REQUEST['nickname']); # Add friends for ($i = 1; $i <= 4; $i++) { if ($_REQUEST["person_{$i}"]) { $person = $graph->resource($_REQUEST["person_{$i}"]); $graph->add($me, 'foaf:knows', $person); } } # Finally output the graph $data = $graph->serialise($_REQUEST['format']); if (!is_scalar($data)) { $data = var_export($data, true); } print "<pre>" . htmlspecialchars($data) . "</pre>"; } ?> </body> </html>
public function testDump() { $graph = new EasyRdf_Graph(); $graph->add('http://example.com/joe#me', 'foaf:name', 'Joe'); $text = $graph->dump(false); $this->assertContains('http://example.com/joe#me', $text); $this->assertContains('-> foaf:name -> "Joe"', $text); $html = $graph->dump(true); $this->assertContains('http://example.com/joe#me', $html); $this->assertContains('>foaf:name</span>', $html); $this->assertContains('>"Joe"</span>', $html); }
*/ set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/'); require_once "EasyRdf.php"; ?> <html> <head> <title>Example of using EasyRdf_Graph directly</title> </head> <body> <?php $graph = new EasyRdf_Graph(); $graph->addResource("http://example.com/joe", "rdf:type", "foaf:Person"); $graph->addLiteral("http://example.com/joe", "foaf:name", "Joe Bloggs"); $graph->addLiteral("http://example.com/joe", "foaf:name", "Joseph Bloggs"); $graph->add("http://example.com/joe", "rdfs:label", "Joe"); $graph->setType("http://aelius.com/njh#me", "foaf:Person"); $graph->add("http://aelius.com/njh#me", "rdfs:label", "Nick"); $graph->addLiteral("http://aelius.com/njh#me", "foaf:name", "Nicholas Humfrey"); $graph->addResource("http://aelius.com/njh#me", "foaf:homepage", "http://aelius.com/njh"); ?> <p> <b>Name:</b> <?php echo $graph->get("http://example.com/joe", "foaf:name"); ?> <br /> <b>Names:</b> <?php echo $graph->join("http://example.com/joe", "foaf:name"); ?> <br />