RdfPerlLib

From W3C Wiki

Some hints and tips for using Rdf Perl Lib code

look in rdfapp.pm see how it prepares sax etc... v complex. or subclass RdfApp and just call prepare parser.

see the algae script for that.

algae query of an sql-backed rdf store

'We have a local RDF database (with config setup described in a .prop file) that we'd like to send (Algae) RDF queries to, and get back a table of bindings or serialize results in RDF/XML.


#!/usr/bin/perl 

use strict;
use W3C::Rdf::RdfDB;
use W3C::Rdf::Atoms;
use W3C::Util::Exception;

eval {
    my $atoms = new W3C::Rdf::Atoms();
    my $db = new W3C::Rdf::RdfDB(-atomDictionary => $atoms);
    my $algae = $db->getAlgaeInterface;
    my $query = "(attach '(\"W3C::Rdf::ObjectDB\" (\"properties:../../rdf.prop\" \"name:db:rdf.prop\" limit:5)) ask '(db:rdf.prop (?p ?s ?o)) collect '(?p))";
    my ($nodes, $selects, $messages, $proofs) = $algae->algae($query, undef, {-uniqueResults => 1});
    for (my $rowNo = 0; $rowNo < @$nodes; $rowNo++) {
	for (my $colNo = 0; $colNo < @{$nodes->[$rowNo]}; $colNo++) {
	    my $datum = $nodes->[$rowNo][$colNo]->show;
	    my $heading = $selects->[$colNo];
	    print "$heading: $datum\n";
	}
    }
}; if ($@) {if (my $ex = &catch('W3C::Util::Exception')) {
	die $ex->toString;
    } else {
	die $@;
    }
}


What's this 'attach' business?

<ericP> getSourceByName: if someone uses "attach" to connect algae to a database, they will specify a name, eg. 'db:rdf.prop'.
You can then use the API $algae->getSourceByName to get that database as a perl object.


populate an sql-backed rdf store

You can either use Algae, or an API-based approach.

This is a sketch of what you need for the latter.


use W3C::Rdf::ObjectDB;
use W3C::Util::Object;
use W3C::Rdf::Atoms;

use strict;

# you can store database connectivity details in a properties file: 
#my $sqldb = new W3C::Rdf::ObjectDB(-atomDictionary => $self->{-atomDictionary}, -properties => "props file"); 
 
# or here it is inline: 
my $atoms = new W3C::Rdf::Atoms;
my $sqldb = new W3C::Rdf::ObjectDB(-atomDictionary => $atoms,  
				-properties => new W3C::Util::Properties({'user' => 'root', 'password' => '',  'database' => 'rdf'}));
 
# Basic idea: $selqdb->addTriple(some triple from proofs) 
# creating a p/s/o to assert into the db... (or we could get these from parsing/querying RDF) 
 
my $attribUri = $atoms->getUri("http://mysource.example.com/s1");
my $attribution = $atoms->getAttribution($W3C::Rdf::Atoms::AttributionType_SOURCE, $attribUri, undef, undef, undef, 1, 1); 

# args: type, uri, auth, parent, source(undef!), trusted, clear 
# 'clear resets all triples with same attribURI' regardless of attr type or author 
 
my $predicate = $atoms->getUri("http://xmlns.com/foaf/0.1/img") ;
my $subject = $atoms->createGenId($attribution)  ;
my $object = $atoms->getUri("http://example.com/mugshot.jpg") ;
 
# also getUri("#foo", "http://..."),  getString("text", PLAIN | XML), createGenId($attribution) 
 
$sqldb->createStatement($predicate, $subject, $object, undef, undef, $attribution);
 


 
#Eric: "$auth is just a string; just use undef. An identity that tags a subgraph in the database. Used in Annotea work, not much used elsewhere. In Annotea it is a string, the Basic Auth name from HTTP. 
#genid takes an attribution - reason is to assign mininun bnode id for that object. 
# creates a BaseTriple and calls addTriple()/ reification and container can be undef. see above re attribution. 


check mysql to see what got loaded...

tail -0f /var/log/mysql.log &


serialize an sql-backed rdf store to rdf/xml

How can I use the algae commandline tool to dump a database into RDF/XML?


algae -a"(attach '(\"W3C::Rdf::ObjectDB\" (\"properties:rdf.prop\" \"name:db:rdf.prop\")) ask '(db:rdf.prop (?p ?s ?o)) collect '(?p))" -sClassRDFXML -sParm-createNamespaces=1


What if the database is huge? Can I limit it to the first n (eg '100') answers?

algae -a"(attach '(\"W3C::Rdf::ObjectDB\" (\"properties:rdf.prop\" \"name:db:rdf.prop\") limit:100) ask '(db:rdf.prop (?p ?s ?o)) collect '(?p))" -sClassRDFXML -sParm-createNamespaces=1


How do I do this through an API instead? (WriteMe)

load RDF/XML from a URI

#!/usr/bin/perl
use strict;
package testRdfParser;
use W3C::Util::Exception;
use W3C::Rdf::RdfApp;
use vars qw(@ISA);
@ISA = qw(W3C::Rdf::RdfApp);

eval {
    my $tester = new testRdfParser;
    $tester->execute(\@ARGV);
}; if ($@) {if (my $ex = &catch('W3C::Util::Exception')) {
        die $ex->toString;
    } else { die $@; }
}

sub render {
    my ($self) = @_;
    my $queryHandler = $self->{RDF_DB}->getAlgaeInterface;

    foreach my $query (@{$self->{ARGS}{-algae}}) {
        my ($nodes, $selects, $messages, $proofs) = $queryHandler->algae($query, undef, {-uniqueResults => 1});
        my $rows = @$nodes;
        print "\"$query\" got $rows rows.\n";
    }
}

# ./simpleRdfApp.pm ../test/medium.rdf -a"(ask '((?p ?s ?o)) collect '(?p ?s ?o))"


random notes

<danbri> mappedtriple subclasses basetriple


 my ($self, $type, $uri, $auth, $parent, $source, $trusted, $clear) = @_;