Why not adopt me?
NAME
RDF::Core::Helper - A wrapper around RDF::Core to simplify RDF-related tasks
SYNOPSIS
my $rdf = new RDF::Core::Helper(
Model => new RDF::Core::Model(
Storage => new RDF::Core::Storage::Postgres(
ConnectStr => 'dbi:Pg:dbname=rdf',
DBUser => 'dbuser',
DBPassword => 'dbpassword',
Model => 'rdf-model',
)
),
BaseURI => 'http://domain/NS/2004/09/03-url#'
);
DESCRIPTION
This module intends to simplify many of the methods and objects needed for interacting with an RDF datastore. URI handling, resource creation, and node insertion/updating/deletion is all simplified in this object. RDF::Core itself, is quite simple, though it is due to this simplicity that one's code must be verbose in order to use it effectively. To ease the process of using an RDF datastore, RDF::Core::Helper simplifies the commonly-used methods for accessing RDF data.
CONSTRUCTOR
my $helper = new RDF::Core::Helper( Model => $model );
Constructor; instantiates a new instance of the Helper class. By supplying arguments to this constructor you can bind this instance to an existing RDF model and/or a "Node Factory".
METHODS
- blank_node
-
my $new_node = $helper->blank_node;
Creates and returns a new node that can be used to create and add new resources.
- get_triples
-
my $node_array = $helper->get_triples($subject, $predicate, $object);
Retrieves triples from the datastore based on the values supplied (similar to "get_statements"), except the statement's individual elements - the subject, predicate and object - are expanded into an array reference.
- get_statements
-
my $stmt_array = $helper->get_statements($subject, $predicate, $object); foreach (@$stmt_array) { # Magic happens here }
Returns an array representation of the enumerator returned by "get_enumerator".
- new_resource
-
my $resource = $helper->new_resource("urn:root:name");
Creates and returns a new resource based on the supplied value. This is mainly useful for calling the various RDF::Core methods that require a properly-formed object be supplied.
- new_literal
-
my $literal = $helper->new_literal("RDF Rocks");
Creates and returns a new literal based on the supplied value. This is mainly useful for calling the various RDF::Core methods that require a properly-formed object be supplied.
- get_enumerator
-
my $enum = $helper->get_enumerator($subject, $predicate, $object);
Retrieves statements from the datastore based on the values supplied. Each value could be either a proper RDF::Core object, a URI, or
undef
. Whatever the values are however, they are automatically converted to RDF::Core::Resource or RDF::Core::Literal objects.This method is used by several of the other RDF::Core::Helper methods, and is thus built to be generic.
- remove_statements
-
my $count = $helper->remove_statements($subject, $predicate, $object);
Removes statements from the datastore that match the supplied triple. The number of statements removed is returned.
- assert_literal
-
$helper->assert_literal($subject, $predicate, $literal);
Asserts the supplied statement into the datastore as a literal value.
- update_literal
-
$helper->update_literal($subject, $predicate, $old_literal, $new_literal);
Changes the value of the literal identified by the supplied statement. This is achieved by first removing the old statement, and then asserting the statement with the new literal.
- assert_resource
-
$helper->assert_resource($subject, $predicate, $resource);
Asserts the supplied statement into the datastore as a resource.
- update_resource
-
$helper->update_resource($subject, $predicate, $old_resource, $new_resource);
Changes the resource identified by the supplied statement. This is similar in functionality to "update_literal".
- include_model
-
$helper->include_model($model);
This method can be used to merge multiple models into one. If an additional model is created as a RDF::Core::Model object, it's statements can be extracted and added to the model the current Helper object encapsulates.
- include_rdfxml
-
$helper->include_rdfxml(filename => $file); $helper->include_rdfxml(xml => $xml_string);
Includes the RDF statements present in the given file or XML string into the current RDF model.
- serialize_model
-
my $xml_string = $helper->serialize_model();
Serializes the RDF model as XML, and returns it as a string.
- count
-
my $num_stmts = $helper->count($subject, $predicate, $object);
Returns the number of statements currently stored in the RDF model that match the given statement.
- exists
-
if ($helper->exists($subject, $predicate, $object)) { # Magic happens here }
Returns a boolean value indicating if any statements matching the given values exist in the RDF model.
- get_uri
-
my $uri = $helper->get_uri('rdf', 'type'); # Returns "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" $helper->assert('urn:subject', $uri, $classType);
This helper method makes creating predicate and other URIs easier. Instead of having to copy-and-paste the same namespace URI when asserting statements, this can let you combine the namespace prefix with the predicate type to create a fully-formed URI.
There are other methods for doing this though, using constants to define both the namespace and specific predicate URIs. TIMTOWTDI.
- model
-
my $model = $helper->model; $helper->model($new_model);
Accessor method providing access to the internal RDF model object. This can be used to change which model this helper object uses.
- node_factory
-
my $node_factory = $helper->node_factory; $helper->node_factory($new_node_factory);
Accessor method providing access to the internal RDF node factory object. This can be used to change the factory this helper object uses.
- ns
-
$helper->ns('foaf', 'http://xmlns.com/foaf/0.1/'); my $rdf_ns = $helper->ns('rdf'); # Returns "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
Accessor method providing access to the internal Namespaces hash. The "constructor" allows you to supply as many namespace prefix to URI definitions, but it may sometimes be necessary to change this after the fact.
By invoking this method with two arguments, it sets a new or updates a current namespace prefix with a URI. If supplied with only one argument, it will return the URI for that namespace prefix if available.
AUTHOR
Kip Hampton, khampton@totalcinema.com
COPYRIGHT
Copyright (c) 2004 Kip Hampton. All rights reserved.
LICENSE
This module is free sofrware; you can redistribute it and/or modify it under the same terms as Perl itself.