#!/bin/perl
#
# a simple Perl RDF script, load using some parser, navigate in-memory
# --danbri
 
BEGIN {unshift@INC,('../../../','../../','../');}
use strict;
use RDF::API::Node;
use RDF::XRDFDataSource;
use RDF::RDFWeb::MemDB;

my $PARSERURI = 'http://cara.sourceforge.net/';# 'http://www.w3.org/Perllib/';
my $mem = new RDF::RDFWeb::MemDB; ## somewhere to store our data...

# register some useful namespaces with $mem
my $RSS = ns $mem 'RSS', 'http://purl.org/rss/1.0/';
my $DC  = ns $mem 'DC',  'http://purl.org/dc/elements/1.1/';
my $RDF = ns $mem 'RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
my $A = ns $mem 'a', 'http://ilrt.org/discovery/harmony/2001/05/a#';
my $WN = ns $mem 'wn',  'http://xmlns.com/wordnet/1.6/';
my $FOAF = ns $mem 'foaf', 'http://xmlns.com/foaf/0.1/';
my $RDFS = ns $mem 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#';

my $dataurl = shift || '../samples/harmony1.rdf';
my $ds = new RDF::XRDFDataSource( $PARSERURI, $mem );
eval {  $ds->get( $dataurl );  };
if ($@) { print STDERR "Error loading data from $dataurl .\n".$@."\n"; }

#############################################################################
#
# harmony examples

# for each event described inline or in a 
# separate file, create a new RDF graph in memory and show some data
# extracted from it

# print a list of all the events we know about
foreach ( $mem->GetSources( $A.'Event', $RDF.'type') ) {
  my $event = new RDF::API::Node( $_, $mem);
  print "Found an event: ", dc_title $event, "\n";
}


# todo: I need a better API for RDF containers. in progress.
# this code is pretty painful to read. ignore. assume it works...
 
print "Looking for quoted graphs...\n";
my $LI=$RDF.'_'; # container code should be moved into RDF API

foreach ( $mem->GetSources( $A.'StateSummary', $RDF.'type') ) {
  my $ss = new RDF::API::Node($_,$mem);

  print "\n\nFound a state summary: ", dc_title $ss, "\n";
  my $inner = new RDF::RDFWeb::MemDB;

  # all arcs out of the sequence
  my @statements =  queryAndReturn $mem undef, a_data $ss, undef; 
    while (@statements) {
    my $p = shift @statements;
    next unless $p =~ m/$LI/; # skip unless an ordinal property 
    my $s = shift @statements;
    my $o = shift @statements;

    my $statement = new RDF::API::Node($o, $mem) if $o; # points to a statement

    print sprintf( "quoted statement: p: %s s: %s o: %s type: %s \n",
        rdf_predicate $statement, rdf_subject $statement, 
	rdf_object $statement, rdf_type $statement) if $statement;

    $inner->assert( $statement->rdf_predicate, 
	$statement->rdf_subject, $statement->rdf_object );

   } 

   print "The quoted graph is now in '$inner'. here's a dump of all 
          the triples in it...\n";
   print "------------------------\n";
   print $inner->report(); ## we could query it with node API, store...
   print "========================\n";

} 









#foreach my $s ( @{allStatements $mem } ) {
#  my %s = %{$s}; print (sprintf ("p: %s s: %s o: %s ", $s{'p'}, $s{'s'}, $s{'o'})."\n");
#}

