/** * @author "Lionel Lecaque, <*****@*****.**>" * @param string $namespace * @param string $data xml content */ public function createModel($namespace, $data) { $modelId = $this->getModelId($namespace); if ($modelId === false) { common_Logger::d('modelId not found, need to add namespace ' . $namespace); $this->addNewModel($namespace); //TODO bad way, need to find better $modelId = $this->getModelId($namespace); } $modelDefinition = new EasyRdf_Graph($namespace); if (is_file($data)) { $modelDefinition->parseFile($data); } else { $modelDefinition->parse($data); } $graph = $modelDefinition->toRdfPhp(); $resources = $modelDefinition->resources(); $format = EasyRdf_Format::getFormat('php'); $data = $modelDefinition->serialise($format); foreach ($data as $subjectUri => $propertiesValues) { foreach ($propertiesValues as $prop => $values) { foreach ($values as $k => $v) { $this->addStatement($modelId, $subjectUri, $prop, $v['value'], isset($v['lang']) ? $v['lang'] : null); } } } return true; }
public function testParseQuery() { $graph = new EasyRdf_Graph(); $graph->parse(readFixture('query/describe.ttl'), 'turtle'); $query = $graph->resource('test:describe'); $this->assertClass('EasySpinRdf_Query_Describe', $query); $this->assertStringEquals("DESCRIBE ?value WHERE { ?this test:uncle ?value }", $query->getSparql()); }
public function testParseQuery() { $graph = new EasyRdf_Graph(); $graph->parse(readFixture('query/ask.ttl'), 'turtle'); $query = $graph->resource('test:ask'); $this->assertClass('EasySpinRdf_Query_Ask', $query); $this->assertStringEquals("# must be 18 years old\nASK WHERE { ?this test:age 18 }", $query->getSparql()); }
public function testParseQuery() { $graph = new EasyRdf_Graph(); $graph->parse(readFixture('query/construct.ttl'), 'turtle'); $query = $graph->resource('test:construct'); $this->assertClass('EasySpinRdf_Query_Construct', $query); $this->assertStringEquals("CONSTRUCT { ?this test:grandParent ?grandParent } WHERE { ?parent test:child ?this. ?grandParent test:child ?parent }", $query->getSparql()); }
public function saveAdditionalRdf($userId) { try { $serializedRdf = trim(stripslashes($_POST['additionalRdf'])); if (!empty($serializedRdf)) { $graph = new \EasyRdf_Graph(); $graph->parse($serializedRdf); // parsing if done to check if syntax is valid update_user_meta($userId, 'additionalRdf', $serializedRdf); } else { update_user_meta($userId, 'additionalRdf', ''); } } catch (\Exception $ex) { wp_die('Addtional RDF Triples could not be saved. Cause: ' . $ex->getMessage()); } }
/** * Get a certain dataset * * @param string $id The id of the dataset * * @return EasyRdf_Resource */ public function get($id) { $uri = \URL::to('/users/' . $id); $collection = $this->getMongoCollection(); $cursor = $collection->find(['@id' => $uri]); if ($cursor->hasNext()) { $jsonLd = $cursor->getNext(); unset($jsonLd['_id']); $expand = JsonLD::expand(json_encode($jsonLd)); $jsonLd = (array) $expand; $graph = new \EasyRdf_Graph(); $json_ld = json_encode($jsonLd); $graph->parse($json_ld, 'jsonld'); return $graph; } else { return []; } }
/** * Merge two graphs and return the resulting merged graph * * @param EasyRdf_Graph $graph * @param EasyRdf_Graph $input_graph * * @return EasyRdf_Graph */ private function mergeGraph($graph, $input_graph) { $turtle_graph = $input_graph->serialise('turtle'); $graph->parse($turtle_graph, 'turtle'); return $graph; }
/** * @see https://github.com/njh/easyrdf/issues/115 */ public function testIssue115() { $triples = <<<RDF <http://example.com/id/1> <http://www.w3.org/2000/01/rdf-schema#type> <http://example.com/ns/animals/dog> . <http://example.com/id/2> <http://www.w3.org/2000/01/rdf-schema#type> <http://example.com/ns/animals/cat> . <http://example.com/id/3> <http://www.w3.org/2000/01/rdf-schema#type> <http://example.com/ns/animals/bird> . <http://example.com/id/4> <http://www.w3.org/2000/01/rdf-schema#type> <http://example.com/ns/animals/reptiles/snake> . RDF; EasyRdf_Namespace::set('id', 'http://example.com/id/'); EasyRdf_Namespace::set('animals', 'http://example.com/ns/animals/'); // parse graph $graph = new EasyRdf_Graph(); $graph->parse($triples, 'ntriples'); // dump as text/turtle $turtle = $graph->serialise('turtle'); $this->assertEquals(readFixture('turtle/gh115-nested-namespaces.ttl'), $turtle); }
foreach (EasyRdf_Format::getFormats() as $format) { if ($format->getSerialiserClass()) { $output_format_options[$format->getLabel()] = $format->getName(); } if ($format->getParserClass()) { $input_format_options[$format->getLabel()] = $format->getName(); } } if (get_magic_quotes_gpc() and isset($fileContent)) { $fileContent = stripslashes($fileContent); } $_REQUEST['output_format'] = 'dot'; $_REQUEST['input_format'] = 'guess'; $graph = new EasyRdf_Graph(); $returnValue = array(); try { $graph->parse($fileContent, $_REQUEST['input_format']); } catch (EasyRdf_Exception $e) { $returnValue['status'] = "failure"; $returnValue['message'] = $e->getMessage(); echo json_encode($returnValue); return; } $format = EasyRdf_Format::getFormat($_REQUEST['output_format']); $output = $graph->serialise($format); if (!is_scalar($output)) { $output = var_export($output, true); } $returnValue['status'] = "success"; $returnValue['content'] = $output; echo json_encode($returnValue);
public function testParseDataGuess() { $graph = new EasyRdf_Graph(); $data = readFixture('foaf.json'); $graph->parse($data, 'guess'); $name = $graph->get('http://www.example.com/joe#me', 'foaf:name'); $this->assertEquals('EasyRdf_Literal', get_class($name)); $this->assertEquals('Joe Bloggs', $name->getValue()); $this->assertEquals('en', $name->getLang()); $this->assertEquals(null, $name->getDatatype()); }
/** * Shows a train or train data, based on the accept-header. * @param $station_id * @param $liveboard_id * @return array * @throws EasyRdf_Exception */ public function specificTrain($station_id, $liveboard_id) { $negotiator = new \Negotiation\FormatNegotiator(); $acceptHeader = Request::header('accept'); $priorities = array('application/json', 'text/html', '*/*'); $result = $negotiator->getBest($acceptHeader, $priorities); $val = $result->getValue(); //get the right date-time to query $datetime = substr($liveboard_id, 0, 12); $datetime = strtotime($datetime); $archived = false; if ($datetime < strtotime("now")) { $archived = true; } switch ($val) { case "text/html": // Convert id to string for interpretation by old API $stationStringName = \hyperRail\StationString::convertToString($station_id); if (!$archived) { // Set up path to old api $URL = "http://api.irail.be/liveboard/?station=" . urlencode($stationStringName->name) . "&date=" . date("mmddyy", $datetime) . "&time=" . date("Hi", $datetime) . "&fast=true&lang=nl&format=json"; // Get the contents of this path $data = file_get_contents($URL); // Convert the data to the new liveboard object $newData = \hyperRail\FormatConverter::convertLiveboardData($data, $station_id); // Read new liveboard object and return the page but load data foreach ($newData['@graph'] as $graph) { if (strpos($graph['@id'], $liveboard_id) !== false) { return View::make('stations.departuredetail')->with('station', $graph)->with('departureStation', $stationStringName); } } App::abort(404); } else { // If no match is found, attempt to look in the archive // Fetch file using curl $ch = curl_init("http://archive.irail.be/" . 'irail?subject=' . urlencode('http://irail.be/stations/NMBS/' . $station_id . '/departures/' . $liveboard_id)); curl_setopt($ch, CURLOPT_HEADER, 0); $request_headers[] = 'Accept: text/turtle'; curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $turtle = curl_exec($ch); curl_close($ch); // Convert turtle to json-ld // Create a new graph $graph = new EasyRdf_Graph(); if (empty($_REQUEST['data'])) { // Load the sample information $graph->parse($turtle, 'turtle'); } // Export to JSON LD $format = EasyRdf_Format::getFormat('jsonld'); $output = $graph->serialise($format); if (!is_scalar($output)) { $output = var_export($output, true); } // First, define the context $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id"), "seeAlso" => array("@id" => "http://www.w3.org/2000/01/rdf-schema#seeAlso", "@type" => "@id")); // Next, encode the context as JSON $jsonContext = json_encode($context); // Compact the JsonLD by using @context $compacted = JsonLD::compact($output, $jsonContext); // Print the resulting JSON-LD! $urlToFind = 'NMBS/' . $station_id . '/departures/' . $liveboard_id; $stationDataFallback = json_decode(JsonLD::toString($compacted, true)); foreach ($stationDataFallback->{'@graph'} as $graph) { if (strpos($graph->{'@id'}, $urlToFind) !== false) { return View::make('stations.departurearchive')->with('station', $graph)->with('departureStation', $stationStringName); } } App::abort(404); } break; case "application/json": case "application/ld+json": default: $stationStringName = \hyperRail\StationString::convertToString($station_id); if (!$archived) { $URL = "http://api.irail.be/liveboard/?station=" . urlencode($stationStringName->name) . "&date=" . date("mmddyy", $datetime) . "&time=" . date("Hi", $datetime) . "&fast=true&lang=nl&format=json"; $data = file_get_contents($URL); $newData = \hyperRail\FormatConverter::convertLiveboardData($data, $station_id); foreach ($newData['@graph'] as $graph) { if (strpos($graph['@id'], $liveboard_id) !== false) { $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id")); return array("@context" => $context, "@graph" => $graph); } } App::abort(404); } else { // If no match is found, attempt to look in the archive // Fetch file using curl $ch = curl_init("http://archive.irail.be/" . 'irail?subject=' . urlencode('http://irail.be/stations/NMBS/' . $station_id . '/departures/' . $liveboard_id)); curl_setopt($ch, CURLOPT_HEADER, 0); $request_headers[] = 'Accept: text/turtle'; curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $turtle = curl_exec($ch); curl_close($ch); // Convert turtle to json-ld // Create a new graph $graph = new EasyRdf_Graph(); if (empty($_REQUEST['data'])) { // Load the sample information $graph->parse($turtle, 'turtle'); } // Export to JSON LD $format = EasyRdf_Format::getFormat('jsonld'); $output = $graph->serialise($format); if (!is_scalar($output)) { $output = var_export($output, true); } // First, define the context $context = array("delay" => "http://semweb.mmlab.be/ns/rplod/delay", "platform" => "http://semweb.mmlab.be/ns/rplod/platform", "scheduledDepartureTime" => "http://semweb.mmlab.be/ns/rplod/scheduledDepartureTime", "headsign" => "http://vocab.org/transit/terms/headsign", "routeLabel" => "http://semweb.mmlab.be/ns/rplod/routeLabel", "stop" => array("@id" => "http://semweb.mmlab.be/ns/rplod/stop", "@type" => "@id"), "seeAlso" => array("@id" => "http://www.w3.org/2000/01/rdf-schema#seeAlso", "@type" => "@id")); // Next, encode the context as JSON $jsonContext = json_encode($context); // Compact the JsonLD by using @context $compacted = JsonLD::compact($output, $jsonContext); // Print the resulting JSON-LD! $urlToFind = 'NMBS/' . $station_id . '/departures/' . $liveboard_id; $stationDataFallback = json_decode(JsonLD::toString($compacted, true)); foreach ($stationDataFallback->{'@graph'} as $graph) { if (strpos($graph->{'@id'}, $urlToFind) !== false) { return array("@context" => $context, "@graph" => $graph); } } App::abort(404); } break; } }
break; default: returnHttpError(300); break; } } } EasyRdf_Namespace::set('adms', 'http://www.w3.org/ns/adms#'); EasyRdf_Namespace::set('cnt', 'http://www.w3.org/2011/content#'); EasyRdf_Namespace::set('dc', 'http://purl.org/dc/elements/1.1/'); EasyRdf_Namespace::set('dcat', 'http://www.w3.org/ns/dcat#'); EasyRdf_Namespace::set('gsp', 'http://www.opengis.net/ont/geosparql#'); EasyRdf_Namespace::set('locn', 'http://www.w3.org/ns/locn#'); EasyRdf_Namespace::set('prov', 'http://www.w3.org/ns/prov#'); $graph = new EasyRdf_Graph(); $graph->parse($rdf, "rdfxml", $rdfxmlURL); header("Content-type: " . $outputFormat); echo $graph->serialise($outputFormats[$outputFormat][1]); exit; /* if ($outputFormat == 'text/html') { $xml = new DOMDocument; $xml->loadXML($rdf) or die(); $xsl = new DOMDocument; $xsl->load(""); $proc = new XSLTProcessor(); $proc->importStyleSheet($xsl); echo $proc->transformToXML($xml); exit; } else {
public function testMockParser() { EasyRdf_Format::registerParser('mock', 'Mock_RdfParser'); $graph = new EasyRdf_Graph(); $graph->parse('data', 'mock'); $this->assertStringEquals('Joseph Bloggs', $graph->get('http://www.example.com/joe#me', 'foaf:name')); }
/** * Merge two graphs and return the result * * @param EasyRdf_Graph $graph * @param EasyRdf_Graph $input_graph * * @return EasyRdf_Graph */ private function mergeGraph($graph, $input_graph) { $rdfxml_string = $input_graph->serialise('rdfxml'); $graph->parse($rdfxml_string, 'rdfxml'); return $graph; }
/** * Gets RDF metadata from Fedora. * * @param string $uri Resource URI * @param array $headers HTTP Headers * @param string $transaction Transaction id * * @return EasyRdf_Graph */ public function getGraph($uri = "", $headers = [], $transaction = "") { $headers['Accept'] = 'application/ld+json'; $rdf = (string) $this->getResource($uri, $headers, $transaction); if (empty($rdf)) { return null; } $graph = new \EasyRdf_Graph(); $graph->parse($rdf, 'jsonld'); return $graph; }
?> <br /> <?php echo reset_tag(); ?> <?php echo submit_tag(); ?> <?php echo form_end_tag(); ?> </div> <?php if (isset($_REQUEST['uri']) or isset($_REQUEST['data'])) { $graph = new EasyRdf_Graph($_REQUEST['uri']); if (empty($_REQUEST['data'])) { $graph->load(); } else { $graph->parse($_REQUEST['data'], $_REQUEST['input_format'], $_REQUEST['uri']); } $output = $graph->serialise($_REQUEST['output_format']); if (!is_scalar($output)) { $output = var_export($output, true); } print "<pre>" . htmlspecialchars($output) . "</pre>"; } ?> </body> </html>
/** * @covers Model::getRDF * @depends testConstructorWithConfig */ public function testGetRDFShouldIncludeLists() { $model = new Model(new GlobalConfig('/../tests/testconfig.inc')); $result = $model->getRDF('test', 'http://www.skosmos.skos/test/ta124', 'text/turtle'); $resultGraph = new EasyRdf_Graph(); $resultGraph->parse($result, "turtle"); $expected = '@prefix test: <http://www.skosmos.skos/test/> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix mads: <http://www.loc.gov/mads/rdf/v1#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . skos:prefLabel rdfs:label "preferred label"@en . test:ta124 a mads:ComplexSubject, skos:Concept ; skos:prefLabel "Vadefugler : Europa"@nb ; mads:componentList ( test:ta125 test:ta126 ) . test:ta125 a mads:Topic, skos:Concept ; skos:prefLabel "Vadefugler"@nb . test:ta126 a mads:Geographic, skos:Concept ; skos:prefLabel "Europa"@nb . '; $expectedGraph = new EasyRdf_Graph(); $expectedGraph->parse($expected, "turtle"); $this->assertTrue(EasyRdf_Isomorphic::isomorphic($resultGraph, $expectedGraph)); }