#!/bin/perl
#
# a simple Perl RDF script, load using some parser, navigate in-memory
# --danbri
# 
BEGIN {unshift@INC,('../../../','../../','../');}

## Note: This has a dependency on our having loaded the XMLHack data. 
## See README for a commandline that'll do this (and load all the other samples)

use strict;
use RDF::RDFWeb::Node;
use RDF::RDFWeb::XRDFDataSource;
use RDF::RDFWeb::AggregateDS;
use RDF::RDFWeb::MemDB;

my $mem = new RDF::RDFWeb::MemDB; 

# 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 $m2 = new RDF::RDFWeb::MemDB; 	# register some useful namespaces with $m2
$RSS = ns $m2 'RSS', 'http://purl.org/rss/1.0/';
$DC  = ns $m2 'DC',  'http://purl.org/dc/elements/1.1/';
$RDF = ns $m2 'RDF', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';


my $todo = shift || '../samples/xmlhack.rss';

# get data
my $ds = new RDF::RDFWeb::XRDFDataSource( 'http://cara.sourceforge.net/');
$ds->{'baseuri'} = $todo; # should sensibly default
eval {  $ds->get( $todo );  };
if ($@) { print STDERR "Error loading data from $todo .\n".$@."\n"; }

# store (the XRDFDataSource should create our MemDB instead...)
foreach my $t ( @{ data $ds } ) {
  my %s = %{$t};
  my ($p,$s,$o) = ( $s{'p'}, $s{'s'}, $s{'o'} );
  assert $mem $p->value, $s->value, $o->value;
}

## APPLICATION SPECIFIC CODE LIVES HERE:
# print a description of any RSS 1.0 Channels we find

print "\n\nLooking in aggregate datasource.\n";

my $ag = new RDF::RDFWeb::AggregateDS;
addDS $ag $mem;
addDS $ag $m2;
ns $ag 'RSS', 'http://purl.org/rss/1.0/';
ns $ag 'DC',  'http://purl.org/dc/elements/1.1/';

assert $m2 $RDF.'type', 'http://channel.org', $RSS.'channel';
assert $m2 $RSS.'title', 'http://channel.org', 'some other channel';
assert $m2 $RSS.'description', 'http://channel.org', 'made up, does not really exist';
assert $m2 $DC.'publisher', 'http://channel.org', 'dan brickley';
assert $m2 $DC.'rights', 'http://channel.org', 'public domain';

foreach my $c ( $ag->GetSources($RSS.'channel', $RDF.'type')) {
  my $channel = new RDF::RDFWeb::Node ( $c, $ag );
 
  print "\n[ $channel ] \nTitle: ", rss_title $channel, 
	"\nDescription: ", rss_description $channel, "\n";
 
print sprintf ("publisher: %s \nrights: %s ", 
	dc_publisher $channel, dc_rights $channel);

} 

print "\n\nReport...\n";
print "===============================\n";
print $mem->report();
print "===============================\n";
print $m2->report();
print "===============================\n";
print $ag->report();
print "===============================\n";

