/**
  * Parses an RDF collection from the response data.
  *
  * @return null
  */
 protected function loadEntity()
 {
     $input = (string) $this->getResponse()->getBody();
     $options = array("");
     $this->rdfCollection = new RdfCollection(JsonLD::toRdf($input, $options));
     return null;
 }
 /**
  * Parse a JSON-LD document into an EasyRdf_Graph
  *
  * Attention: Since JSON-LD supports datasets, a document may contain
  * multiple graphs and not just one. This parser returns only the
  * default graph. An alternative would be to merge all graphs.
  *
  * @param object EasyRdf_Graph $graph   the graph to load the data into
  * @param string               $data    the RDF document data
  * @param string               $format  the format of the input data
  * @param string               $baseUri the base URI of the data being parsed
  * @return integer             The number of triples added to the graph
  */
 public function parse($graph, $data, $format, $baseUri)
 {
     parent::checkParseParams($graph, $data, $format, $baseUri);
     if ($format != 'jsonld') {
         throw new EasyRdf_Exception("EasyRdf_Parser_JsonLd does not support {$format}");
     }
     try {
         $quads = \ML\JsonLD\JsonLD::toRdf($data, array('base' => $baseUri));
     } catch (\ML\JsonLD\Exception\JsonLdException $e) {
         throw new EasyRdf_Parser_Exception($e->getMessage());
     }
     foreach ($quads as $quad) {
         // Ignore named graphs
         if (null !== $quad->getGraph()) {
             continue;
         }
         $subject = (string) $quad->getSubject();
         if ('_:' === substr($subject, 0, 2)) {
             $subject = $this->remapBnode($subject);
         }
         $predicate = (string) $quad->getProperty();
         if ($quad->getObject() instanceof \ML\IRI\IRI) {
             $object = array('type' => 'uri', 'value' => (string) $quad->getObject());
             if ('_:' === substr($object['value'], 0, 2)) {
                 $object = array('type' => 'bnode', 'value' => $this->remapBnode($object['value']));
             }
         } else {
             $object = array('type' => 'literal', 'value' => $quad->getObject()->getValue());
             if ($quad->getObject() instanceof \ML\JsonLD\LanguageTaggedString) {
                 $object['lang'] = $quad->getObject()->getLanguage();
             } else {
                 $object['datatype'] = $quad->getObject()->getType();
             }
         }
         $this->addTriple($subject, $predicate, $object);
     }
     return $this->tripleCount;
 }
Beispiel #3
0
 private function parseJsonLD($uri)
 {
     $quads = JsonLD::toRdf($uri);
     $nquads = new NQuads();
     $graph = new Graph();
     foreach ($quads as $quad) {
         $subject = (string) $quad->getSubject();
         if ('_:' === substr($subject, 0, 2)) {
             $subject = $this->remapBnode($subject, $graph);
         }
         $predicate = (string) $quad->getProperty();
         if ($quad->getObject() instanceof \ML\IRI\IRI) {
             $object = array('type' => 'uri', 'value' => (string) $quad->getObject());
             if ('_:' === substr($object['value'], 0, 2)) {
                 $object = array('type' => 'bnode', 'value' => $this->remapBnode($object['value'], $graph));
             }
         } else {
             $object = array('type' => 'literal', 'value' => $quad->getObject()->getValue());
             if ($quad->getObject() instanceof \ML\JsonLD\LanguageTaggedString) {
                 $object['lang'] = $quad->getObject()->getLanguage();
             } else {
                 $object['datatype'] = $quad->getObject()->getType();
             }
         }
         $graph->add($subject, $predicate, $object);
     }
     return $graph;
 }
Beispiel #4
0
 /**
  * Tests conversion to RDF quads.
  *
  * @param string $name    The test name.
  * @param object $test    The test definition.
  * @param object $options The options to configure the algorithms.
  *
  * @group toRdf
  * @dataProvider toRdfProvider
  */
 public function testToRdf($name, $test, $options)
 {
     $expected = trim(file_get_contents($this->basedir . $test->{'expect'}));
     $quads = JsonLD::toRdf($this->basedir . $test->{'input'}, $options);
     $serializer = new NQuads();
     $result = $serializer->serialize($quads);
     // Sort quads (the expected quads are already sorted)
     $result = explode("\n", trim($result));
     sort($result);
     $result = implode("\n", $result);
     $this->assertEquals($expected, $result);
 }