public function retrieve() { $sm = MwRdf::StoredModel(); $model = MwRdf::Model(); $context = $this->Agent->getContextNode($this->getName()); $stream = librdf_model_context_as_stream($sm->getModel(), $context->getNode()); librdf_model_add_statements($model->getModel(), $stream); librdf_free_stream($stream); $sm->rewind(); return $model; }
/** * Free the stream's resources. * * @return void * @access public */ public function __destruct() { if ($this->stream) { librdf_free_stream($this->stream); } }
/** * Parse an RDF document into an EasyRdf_Graph * * @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 boolean true if parsing was successful */ public function parse($graph, $data, $format, $baseUri) { parent::checkParseParams($graph, $data, $format, $baseUri); $parser = librdf_new_parser($this->_world, $format, null, null); if (!$parser) { throw new EasyRdf_Exception("Failed to create librdf_parser of type: {$format}"); } $rdfUri = librdf_new_uri($this->_world, $baseUri); if (!$rdfUri) { throw new EasyRdf_Exception("Failed to create librdf_uri from: {$baseUri}"); } $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri); if (!$stream) { throw new EasyRdf_Exception("Failed to parse RDF stream for: {$rdfUri}"); } do { $statement = librdf_stream_get_object($stream); if ($statement) { $subject = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_subject($statement)); $predicate = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_predicate($statement)); $object = EasyRdf_Parser_Redland::nodeToArray(librdf_statement_get_object($statement)); $graph->add($subject, $predicate, $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); // Success return true; }
/** * Parse an RDF document * * @param string $uri the base URI of the data * @param string $data the document data * @param string $format the format of the input data * @return array the parsed data */ public function parse($uri, $data, $format) { if (!is_string($uri) or $uri == null or $uri == '') { throw new InvalidArgumentException("\$uri should be a string and cannot be null or empty"); } if (!is_string($data) or $data == null or $data == '') { throw new InvalidArgumentException("\$data should be a string and cannot be null or empty"); } if (!is_string($format) or $format == null or $format == '') { throw new InvalidArgumentException("\$format should be a string and cannot be null or empty"); } $parser = librdf_new_parser($this->_world, $format, null, null); if (!$parser) { throw new EasyRdf_Exception("Failed to create librdf_parser of type: {$format}"); } $rdfUri = librdf_new_uri($this->_world, $uri); if (!$rdfUri) { throw new EasyRdf_Exception("Failed to create librdf_uri from: {$uri}"); } $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri); if (!$stream) { throw new EasyRdf_Exception("Failed to parse RDF stream for: {$rdfUri}"); } $rdfphp = array(); do { $statement = librdf_stream_get_object($stream); if ($statement) { $subject = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_subject($statement)); $predicate = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_predicate($statement)); $object = EasyRdf_Parser_Redland::rdfPhpObject(librdf_statement_get_object($statement)); if (!isset($rdfphp[$subject])) { $rdfphp[$subject] = array(); } if (!isset($rdfphp[$subject][$predicate])) { $rdfphp[$subject][$predicate] = array(); } array_push($rdfphp[$subject][$predicate], $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); return $rdfphp; }
public function retrieveModel($par) { if (gettype($par) == 'string') { $par = array($par); } if (!gettype($par) == 'array') { throw new Exception('retrieveModel takes either a model ' . 'name string or an array of modelnames'); } $model = MwRdf::Model(); foreach ($par as $name) { $mm = $this->ModelMakers[$name]; ### TODO Retrieve from Cache if it's there $sub_model = $mm->retrieve($name); ### TODO Cache the sub_model $stream = librdf_model_as_stream($sub_model->getModel()); librdf_model_add_statements($model->getModel(), $stream); librdf_free_stream($stream); $sub_model->rewind(); } return $model; }
/** * Parse an RDF document into an EasyRdf_Graph * * @param string $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 boolean true if parsing was successful */ public function parse($graph, $data, $format, $baseUri) { parent::checkParseParams($graph, $data, $format, $baseUri); $parser = librdf_new_parser($this->_world, $format, null, null); if (!$parser) { throw new EasyRdf_Exception("Failed to create librdf_parser of type: {$format}"); } $rdfUri = librdf_new_uri($this->_world, $baseUri); if (!$rdfUri) { throw new EasyRdf_Exception("Failed to create librdf_uri from: {$baseUri}"); } $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri); if (!$stream) { throw new EasyRdf_Exception("Failed to parse RDF stream for: {$rdfUri}"); } $rdfphp = array(); do { $statement = librdf_stream_get_object($stream); if ($statement) { $subject = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_subject($statement)); $predicate = EasyRdf_Parser_Redland::nodeUriString(librdf_statement_get_predicate($statement)); $object = EasyRdf_Parser_Redland::rdfPhpObject(librdf_statement_get_object($statement)); if (!isset($rdfphp[$subject])) { $rdfphp[$subject] = array(); } if (!isset($rdfphp[$subject][$predicate])) { $rdfphp[$subject][$predicate] = array(); } array_push($rdfphp[$subject][$predicate], $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new EasyRdf_Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); // FIXME: remove this second parse step return parent::parse($graph, $rdfphp, 'php', $baseUri); }
function to_jsonld_string() { $r = array(); $stream = librdf_model_as_stream($this->_model); while (!librdf_stream_end($stream)) { $elt = $this->_statement(librdf_stream_get_object($stream)); $d = new \stdClass(); $d->{'@subject'} = $elt[0]['value']; if ($elt[2]['type'] == 'literal') { if (isset($elt[2]['datatype'])) { $d->{$elt[1]['value']} = (object) array('@literal' => $elt[2]['value'], '@datatype' => $elt[2]['datatype']); } else { $d->{$elt[1]['value']} = $elt[2]['value']; } } else { $d->{$elt[1]['value']} = (object) array('@iri' => $elt[2]['value']); } $r[] = $d; librdf_stream_next($stream); } librdf_free_stream($stream); return str_replace('\\/', '/', json_encode(jsonld_normalize($r))); }
/** * Parse an RDF document into an EasyRdf\Graph * * @param 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 * * @throws Exception * @throws \EasyRdf\Exception * @return integer The number of triples added to the graph */ public function parse($graph, $data, $format, $baseUri) { parent::checkParseParams($graph, $data, $format, $baseUri); $parser = librdf_new_parser($this->world, $format, null, null); if (!$parser) { throw new \EasyRdf\Exception("Failed to create librdf_parser of type: {$format}"); } $rdfUri = librdf_new_uri($this->world, $baseUri); if (!$rdfUri) { throw new \EasyRdf\Exception("Failed to create librdf_uri from: {$baseUri}"); } $stream = librdf_parser_parse_string_as_stream($parser, $data, $rdfUri); if (!$stream) { throw new Exception("Failed to parse RDF stream"); } do { $statement = librdf_stream_get_object($stream); if ($statement) { $subject = self::nodeUriString(librdf_statement_get_subject($statement)); $predicate = self::nodeUriString(librdf_statement_get_predicate($statement)); $object = self::nodeToRdfPhp(librdf_statement_get_object($statement)); $this->addTriple($subject, $predicate, $object); } } while (!librdf_stream_next($stream)); $errorCount = $this->parserErrorCount($parser); if ($errorCount) { throw new Exception("{$errorCount} errors while parsing."); } librdf_free_uri($rdfUri); librdf_free_stream($stream); librdf_free_parser($parser); return $this->tripleCount; }