Fetching contributors…
Cannot retrieve contributors at this time
executable file 183 lines (147 sloc) 5.75 KB
# - Redland eaxmple Perl program
# Copyright (C) 2000-2005 David Beckett -
# Copyright (C) 2000-2005 University of Bristol -
# This package is Free Software and part of Redland
# It is licensed under the following three licenses as alternatives:
# 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
# 2. GNU General Public License (GPL) V2 or any newer version
# 3. Apache License, V2.0 or any newer version
# You may not use this file except in compliance with at least one of
# the above three licenses.
# See LICENSE.html or LICENSE.txt at the top of this package for the
# full license terms.
use RDF::Redland;
warn "Creating storage\n";
my $storage=new RDF::Redland::Storage("hashes", "test",
die "Failed to create RDF::Redland::Storage\n" unless $storage;
warn "\nCreating model\n";
my $model=new RDF::Redland::Model($storage, "");
die "Failed to create RDF::Redland::Model for storage\n" unless $model;
my $creator_uri=new RDF::Redland::URI("");
warn "\nCreating statement\n";
my $statement=new RDF::Redland::Statement(RDF::Redland::Node->new_from_uri(""),
new RDF::Redland::Node("Dave Beckett"));
die "Failed to create RDF::Redland::Statement\n" unless $statement;
warn "\nAdding statement to model\n";
my $n=new RDF::Redland::URI('');
my $statementn=new RDF::Redland::Statement($n, $n, $n);
warn "\nAdding statement (new Statement(n, n, n)) to model\n";
warn "\nAdding statement (n,n,n) to model\n";
$model->add_statement($n, $n, $n);
die "Data file $test_file not found - cannot parse it.\n Try editing \$test_file variable to point to a file.\n"
unless -r $test_file;
warn "\nParsing URI (file) $test_file\n";
my $uri=new RDF::Redland::URI("file:$test_file");
# Use any rdf/xml parser that is available
my $parser=new RDF::Redland::Parser("rdfxml", "application/rdf+xml");
die "Failed to find parser\n" if !$parser;
my $count=0;
while(!$stream->end) {
warn "Parsing added $count statements\n";
warn "\nNamespace declarations seen during parsing:\n";
while(my($prefix,$uri)=each %namespaces) {
warn " prefix '$prefix' URI ".$uri->as_string."\n";
warn "\nPrinting all statements\n";
while(!$stream->end) {
print "Statement: ",$stream->current->as_string,"\n";
warn "\nSearching model for statements matching predicate\n";
$statement=new RDF::Redland::Statement(undef, $creator_uri, undef);
my $stream=$model->find_statements($statement);
while(!$stream->end) {
my $statement2=$stream->current;
print "Matching Statement: ",$statement2->as_string,"\n";
my $subject=$statement2->subject;
print " Subject: ",$subject->as_string,"\n";
print " Predicate: ",$statement2->predicate->as_string,"\n";
print " Object: ",$statement2->object->as_string,"\n";
my $home=RDF::Redland::Node->new_from_uri("");
warn "\nSearching model for targets of subject ",$home->uri->as_string," predicate ", $creator_uri->as_string, "\n";
my(@nodes)=$model->targets($home, new RDF::Redland::Node($creator_uri));
die "Failed to find any targets matching\n"
unless @nodes;
for my $node (@nodes) {
print "Matching Node: ",$node->as_string,"\n";
my $q = new RDF::Redland::Query("PREFIX dc: <>\nSELECT ?a ?c WHERE { ?a dc:title ?c }", undef, undef, "sparql");
print "Querying for dc:titles:\n";
my $results=$model->query_execute($q);
my $count=1;
while(!$results->finished) {
print "result $count: {\n";
for(my $i=0; $i < $results->bindings_count; $i++) {
my $val=$results->binding_value($i);
next unless defined $val; # optionals
print " ",$results->binding_name($i),"=",$val->as_string,"\n";
print "}\n";
warn "Returned $count results\n";
print "\nExecuting query again\n";
my $results=$model->query_execute($q);
my $str=$results->to_string;
print "Query results serialized to an XML string size ".length($str)." bytes\n";
print "\nExecuting SPARQL construct query\n";
my $q2 = new RDF::Redland::Query("CONSTRUCT { ?a ?b ?c } WHERE { ?a ?b ?c }", undef, undef, "sparql");
my $results=$model->query_execute($q2);
while(!$stream->end) {
print "Statement: ",$stream->current->as_string,"\n";
warn "Returned $count triples\n";
warn "\nWriting model to test-out.rdf as rdf/xml\n";
# Use any rdf/xml parser that is available
my $serializer=new RDF::Redland::Serializer("rdfxml");
die "Failed to find serializer\n" if !$serializer;
$serializer->set_namespace("dc", new RDF::Redland::URI(""));
$serializer->serialize_model_to_file("test-out.rdf", $uri, $model);
my $str1=$serializer->serialize_model_to_string($uri, $model);
warn "\nSerialized to RDF/XML as a string size ",length($str1)," bytes\n";
my $str=$model->to_string(new RDF::Redland::URI(""), "ntriples");
warn "\nSerialized to ntriples as a string size ",length($str)," bytes\n";
warn "\nDone\n";
# Required in order to ensure storage is correctly flushed to disk