You are viewing a plain text version of this content. The canonical link for it is here.
Posted to triplesoup-commits@incubator.apache.org by le...@apache.org on 2007/04/13 08:56:16 UTC
svn commit: r528394 [16/35] - in
/incubator/triplesoup/donations/TRIPLES-3-RDFStore: ./ dbms/ dbms/client/
dbms/client/t/ dbms/dbmsproxy/ dbms/deamon/ dbms/doc/ dbms/include/
dbms/libdbms/ dbms/utils/ doc/ include/ lib/ lib/DBD/ lib/RDFStore/
lib/RDFSt...
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Model.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Model.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Model.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Model.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,337 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - 2000/11/03 at 04:30 CEST
+# * version 0.2
+# * - Init() now setSourceURI() for the model
+# * - now the result set is a SetModel
+# * version 0.3
+# * - fixed bug in Assert() checking if $st is a ref and valid RDFStore::Statement
+# * version 0.31
+# * - updated documentation
+# * version 0.4
+# * - modified Assert() to print only new statements
+# * - fixed a few warnings
+# * - updated accordingly to new RDFStore::Model
+# * version 0.41
+# * - renamed
+# * - added Context option to the storage
+# * - fixed typing error when passing the nodeFactory to the model
+# * - allows to specify an existing RDFStore::Model as input
+# * - added owl:imports support
+# *
+
+package RDFStore::Parser::Styles::RDFStore::Model;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.41';
+
+use RDFStore::Model;
+use RDFStore::Vocabulary::OWL;
+use Carp;
+
+sub Init {
+ my $expat = shift;
+
+ my $context;
+ if( (exists $expat->{'style_options'}->{'store_options'}->{'Context'}) &&
+ (ref($expat->{'style_options'}->{'store_options'}->{'Context'})) &&
+ ($expat->{'style_options'}->{'store_options'}->{'Context'}->isa("RDFStore::Resource")) ) {
+ $context = $expat->{'style_options'}->{'store_options'}->{'Context'};
+ delete($expat->{'style_options'}->{'store_options'}->{'Context'});
+ };
+
+ if( (exists $expat->{'style_options'}->{'delete'}) &&
+ (defined $expat->{'style_options'}->{'delete'}) ) {
+ my $storename = $expat->{'style_options'}->{'store_options'}->{'Name'};
+ my $in_context = ($context) ? " in context '".$context->toString."'" : '';
+ my $yes = ( ($expat->{'style_options'}->{'confirm'}) && ($expat->{'style_options'}->{'confirm'} =~ m/1|yes|on/) ) ? 1 : 0;
+ confirm("\n*WARNINIG* This operation can not be undone!!\n\nAre you sure you want to remove statements from '$storename' database$in_context? (^C to kill, any key to continue)\n\n")
+ unless($yes);
+ };
+
+
+ # take an existing model if passed
+ my $not_override = (exists $expat->{'RDFStore_model'}) ? 1 : 0 ;
+ if( (exists $expat->{'style_options'}->{'store_options'}->{'sourceModel'}) &&
+ (ref($expat->{'style_options'}->{'store_options'}->{'sourceModel'})) &&
+ ($expat->{'style_options'}->{'store_options'}->{'sourceModel'}->isa("RDFStore::Model")) ) {
+ $expat->{'RDFStore_model'} = $expat->{'style_options'}->{'store_options'}->{'sourceModel'}
+ unless($not_override);
+ } else {
+ $expat->{'RDFStore_model'} = new RDFStore::Model(
+ nodeFactory => $expat->{'NodeFactory'},
+ %{$expat->{'style_options'}->{'store_options'}} )
+ unless($not_override);
+ };
+
+ unless($not_override) {
+ $expat->{'RDFStore_model'}->setContext($context)
+ if(defined $context);
+ $expat->{'RDFStore_model'}->setSourceURI($expat->{'sSource'})
+ if( (exists $expat->{'sSource'}) &&
+ (defined $expat->{'sSource'}) );
+ };
+ $expat->{'imports'} = {}
+ unless(exists $expat->{'imports'});
+ };
+
+sub Final {
+ my $expat = shift;
+
+ return $expat->{'RDFStore_model'};
+};
+
+# input: either Expat valid QNames or "assertions" (statements)
+# output: "assertions" (statements)
+# David Megginson saying that this is bad it is better Start/Stop Resource/Property
+# anyway it should look like: Assert(subjectType,subject,predicate,objectType,object,lang)
+# (see http://lists.w3.org/Archives/Public/www-rdf-interest/1999Dec/0045.html)
+sub Assert {
+ my ($expat,$st) = @_;
+
+ if( (exists $expat->{'style_options'}->{'delete'}) &&
+ (defined $expat->{'style_options'}->{'delete'}) ) {
+ # problem is wiht contexts now....when are we going to zap the context specific triples now?????
+ if($expat->{'RDFStore_model'}->remove($st)) {
+ # we should print just the new ones
+ print "Removed statement ".$st->toString,"\n"
+ if( (defined $st) && (ref($st)) && ($st->isa("RDFStore::Statement")) && (defined $expat->{'style_options'}->{'seevalues'}) );
+ };
+ } else {
+ if($expat->{'RDFStore_model'}->add($st)) {
+ # we should print just the new ones
+ print "Added statement ".$st->toString,"\n"
+ if( (defined $st) && (ref($st)) && ($st->isa("RDFStore::Statement")) && (defined $expat->{'style_options'}->{'seevalues'}) );
+ };
+ };
+
+ if( (defined $expat->{'style_options'}->{'owl:imports'}) &&
+ ($st->predicate->equals($RDFStore::Vocabulary::OWL::imports)) && #take any in it
+ ($st->object->toString ne $expat->{'Source'}) && #try to avoid recursion :)
+ (! exists $expat->{'imports'}->{ $st->object->toString } ) ) {
+ my $current_ctx = $expat->{'RDFStore_model'}->getContext;
+ $expat->{'RDFStore_model'}->setContext( $st->object ); #owl:imports is the context of what is imported
+ # parse the target as RDF/XML
+ my $owl_p = new RDFStore::Parser::SiRPAC(
+ Style => 'RDFStore::Parser::Styles::RDFStore::Model',
+ NodeFactory => $expat->{'RDFStore_model'}->getNodeFactory,
+ Source => $st->object->toString,
+ store => {
+ 'seevalues' => $expat->{'style_options'}->{'seevalues'},
+ 'delete' => ( (exists $expat->{'style_options'}->{'delete'}) &&
+ (defined $expat->{'style_options'}->{'delete'}) ) ? $expat->{'style_options'}->{'delete'} : undef,
+ 'confirm' => ( (exists $expat->{'style_options'}->{'confirm'}) &&
+ (defined $expat->{'style_options'}->{'confirm'}) ) ? $expat->{'style_options'}->{'confirm'} : undef
+ },
+ RDFStore_model => $expat->{'RDFStore_model'}, #import into current one
+ imports => $expat->{'imports'}
+ );
+
+ # avoid to fail the main parsing for the moment....
+ eval {
+ # add all those triples to current one
+ $owl_p->parsefile( $st->object->toString );
+ };
+
+ $expat->{'RDFStore_model'}->setContext( $current_ctx )
+ if($current_ctx); #restore old context if any
+
+ $expat->{'imports'}->{ $st->object->toString } = ($@) ? 2 : 1; #not propagating to sub-parser above still - avoid multiples only on one level
+ };
+};
+
+# we might use this callback for XSLT tansofrmations of xml-blobs :)
+sub Start_XML_Literal {
+ my $expat = shift;
+ my $el = shift;
+
+ $expat->{'XML_Literal_processed_namespaces'} = {}
+ unless(exists $expat->{'XML_Literal_processed_namespaces'});
+
+ my @current_ns_prefixes = $expat->current_ns_prefixes;
+
+ my $ns_index = 1;
+
+ my $xmlcn='';
+ my $elns = $expat->namespace($el);
+ if (defined $elns) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $elns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n' . $ns_index++
+ unless($pfx);
+
+ if( exists $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$elns } ) {
+ $xmlcn .= "<$el";
+ } else {
+ $xmlcn .= ( $pfx eq '#default' ) ? "<$el xmlns=\"$elns\"" : "<$pfx:$el xmlns:$pfx=\"$elns\"";
+ $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$elns } = 1;
+ };
+ } else {
+ $xmlcn .= "<$el";
+ };
+
+ if (@_) {
+ for (my $i = 0; $i < @_; $i += 2) {
+ my $nm = $_[$i];
+ my $ns = $expat->namespace($nm);
+ $_[$i] = defined($ns) ? "$ns\01$nm" : "\01$nm";
+ };
+
+ my %atts = @_;
+ my @ids = sort keys %atts;
+ foreach my $id (@ids) {
+ my ($ns, $nm) = split(/\01/, $id);
+ my $val = $expat->xml_escape($atts{$id}, '"', "\x9", "\xA", "\xD");
+ if (length($ns)) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $ns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n' . $ns_index++
+ unless($pfx);
+
+ if( exists $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$ns } ) {
+ $xmlcn .= " $nm=\"$val\"";
+ } else {
+ $xmlcn .= " $pfx:$nm=\"$val\" xmlns:$pfx=\"$ns\""; # '#default' namespace does not apply to attributes
+ };
+ } else {
+ $xmlcn .= " $nm=\"$val\"";
+ };
+ };
+ };
+
+ $xmlcn .= '>';
+
+ return $xmlcn;
+ };
+
+sub Stop_XML_Literal {
+ my ($expat,$el) = @_;
+
+ my @current_ns_prefixes = $expat->current_ns_prefixes;
+
+ my $elns = $expat->namespace($el);
+ if (defined $elns) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $elns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n1' #wrong anyway
+ unless($pfx);
+ return ( $pfx eq '#default' ) ? "</$el>" : "</$pfx:$el>";
+ } else {
+ return "</$el>";
+ };
+ };
+
+sub Char_Literal {
+ my ($expat,$literal_text) = @_;
+
+ return $expat->xml_escape($literal_text, '>', "\xD");
+ };
+
+sub confirm {
+ my ($msg) = @_;
+
+ print $msg;
+
+ return <STDIN>;
+ };
+
+1;
+}
+
+__END__
+
+=head1 NAME
+
+RDFStore::Parser::Styles::RDFStore::Model - This module is a RDFStore::Parser::SiRPAC(3) filter to ingest records into an RDFStore::Model(3).
+
+=head1 SYNOPSIS
+
+
+use RDFStore::Parser::SiRPAC;
+use RDFStore::Parser::Styles::RDFStore::Model;
+use RDFStore::NodeFactory;
+my $p=new RDFStore::Parser::SiRPAC(
+ ErrorContext => 2,
+ Style => 'RDFStore::Parser::Styles::RDFStore::Model',
+ NodeFactory => new RDFStore::NodeFactory()
+ );
+
+if(defined $ENV{GATEWAY_INTERFACE}) {
+ print "Content-type: text/html
+
+";
+ $p->parsefile($ENV{QUERY_STRING});
+} else {
+ my $input = shift;
+ if($input =~ /^-/) {
+ $p->parse(*STDIN);
+ } else {
+ $p->parsefile($input);
+ };
+};
+
+=head1 DESCRIPTION
+
+In the samples directory of the distribution you can find a set of a sample scripts to play with :)
+
+=head1 METHODS
+
+=over 4
+
+=item new
+
+This is a class method, the constructor for RDFStore::Parser::SiRPAC. B<Options> are passed as key/value pairs. RDFStore::Parser::Styles::MagicTie supports B<all> the RDFStore::Parser::SiRPAC options plus the following:
+
+=over 5
+
+=item * store
+
+This option if present must point to an HASH reference. Recognized options are:
+
+=item * seevalues
+
+This options is a SCALAR with possible values of 0/1 and flags whether the parsing is verbose or not (print triples)
+
+=item * options
+
+This option if present must point to an HASH reference and allows to the user specifying the RDFStore::Model(3) options about storage of the statements of the type generated by the corresponding RDFStore::NodeFactory(3)
+
+=head1 NOTES
+
+This module will probably be renamed to XML::SAX::RDF::RDFStore::Model when proper SAX2 support is added to the main RDFStore::Parser::SiRPAC parser
+
+=head1 SEE ALSO
+
+RDFStore::Parser::SiRPAC(3) and RDFStore::Model(3)
+
+=head1 AUTHOR
+
+Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Statement.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Statement.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Statement.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Parser/Styles/RDFStore/Statement.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,197 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - Tue Aug 24 16:15:10 CEST 2004
+# *
+
+package RDFStore::Parser::Styles::RDFStore::Statement;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.1';
+
+use Carp;
+
+sub Final {
+ my $expat = shift;
+
+ return 1;
+ };
+
+sub Assert {
+ my ($expat,$st) = @_;
+
+ print $st->toString."\n"
+ if( (defined $st) && (ref($st)) && ($st->isa("RDFStore::Statement")) && (defined $expat->{'style_options'}->{'seevalues'}) );
+ };
+
+# we might use this callback for XSLT tansofrmations of xml-blobs :)
+sub Start_XML_Literal {
+ my $expat = shift;
+ my $el = shift;
+
+ $expat->{'XML_Literal_processed_namespaces'} = {}
+ unless(exists $expat->{'XML_Literal_processed_namespaces'});
+
+ my @current_ns_prefixes = $expat->current_ns_prefixes;
+
+ my $ns_index = 1;
+
+ my $xmlcn='';
+ my $elns = $expat->namespace($el);
+ if (defined $elns) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $elns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n' . $ns_index++
+ unless($pfx);
+
+ if( exists $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$elns } ) {
+ $xmlcn .= "<$el";
+ } else {
+ $xmlcn .= ( $pfx eq '#default' ) ? "<$el xmlns=\"$elns\"" : "<$pfx:$el xmlns:$pfx=\"$elns\"";
+ $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$elns } = 1;
+ };
+ } else {
+ $xmlcn .= "<$el";
+ };
+
+ if (@_) {
+ for (my $i = 0; $i < @_; $i += 2) {
+ my $nm = $_[$i];
+ my $ns = $expat->namespace($nm);
+ $_[$i] = defined($ns) ? "$ns\01$nm" : "\01$nm";
+ };
+
+ my %atts = @_;
+ my @ids = sort keys %atts;
+ foreach my $id (@ids) {
+ my ($ns, $nm) = split(/\01/, $id);
+ my $val = $expat->xml_escape($atts{$id}, '"', "\x9", "\xA", "\xD");
+ if (length($ns)) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $ns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n' . $ns_index++
+ unless($pfx);
+
+ if( exists $expat->{'XML_Literal_processed_namespaces'}->{ $pfx.$ns } ) {
+ $xmlcn .= " $nm=\"$val\"";
+ } else {
+ $xmlcn .= " $pfx:$nm=\"$val\" xmlns:$pfx=\"$ns\""; # '#default' namespace does not apply to attributes
+ };
+ } else {
+ $xmlcn .= " $nm=\"$val\"";
+ };
+ };
+ };
+
+ $xmlcn .= '>';
+
+ return $xmlcn;
+ };
+
+sub Stop_XML_Literal {
+ my ($expat,$el) = @_;
+
+ my @current_ns_prefixes = $expat->current_ns_prefixes;
+
+ my $elns = $expat->namespace($el);
+ if (defined $elns) {
+ my $pfx;
+ for my $p ( @current_ns_prefixes ) {
+ if( $expat->expand_ns_prefix($p) eq $elns ) {
+ $pfx = $p;
+ last;
+ };
+ };
+ $pfx = 'n1' #wrong anyway
+ unless($pfx);
+ return ( $pfx eq '#default' ) ? "</$el>" : "</$pfx:$el>";
+ } else {
+ return "</$el>";
+ };
+ };
+
+sub Char_Literal {
+ my ($expat,$literal_text) = @_;
+
+ return $expat->xml_escape($literal_text, '>', "\xD");
+ };
+
+1;
+}
+
+__END__
+
+=head1 NAME
+
+RDFStore::Parser::Styles::RDFStore::Statement - This module is a RDFStore::Parser::SiRPAC(3) filter to generate RDFStore::Statements
+
+=head1 SYNOPSIS
+
+
+use RDFStore::Parser::SiRPAC;
+use RDFStore::NodeFactory;
+my $p=new RDFStore::Parser::SiRPAC(
+ ErrorContext => 2,
+ Style => 'RDFStore::Parser::Styles::RDFStore::Statement',
+ NodeFactory => new RDFStore::NodeFactory()
+ );
+
+if(defined $ENV{GATEWAY_INTERFACE}) {
+ print "Content-type: text/html
+
+";
+ $p->parsefile($ENV{QUERY_STRING});
+} else {
+ my $input = shift;
+ if($input =~ /^-/) {
+ $p->parse(*STDIN);
+ } else {
+ $p->parsefile($input);
+ };
+};
+
+=head1 DESCRIPTION
+
+In the samples directory of the distribution you can find a set of a sample scripts to play with :)
+
+=head1 METHODS
+
+=over 4
+
+=item new
+
+This is a class method, the constructor for RDFStore::Parser::SiRPAC. B<Options> are passed as key/value pairs. RDFStore::Parser::Styles::MagicTie supports B<all> the RDFStore::Parser::SiRPAC options plus the following:
+
+=head1 NOTES
+
+This module will probably be renamed to XML::SAX::RDF::RDFStore::Model when proper SAX2 support is added to the main RDFStore::Parser::SiRPAC parser
+
+=head1 SEE ALSO
+
+RDFStore::Parser::SiRPAC(3) RDFStore::Parser::Styles::RDFStore::Statement and RDFStore::Statement(3)
+
+=head1 AUTHOR
+
+Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/RDFNode.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/RDFNode.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/RDFNode.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/RDFNode.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,136 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - 2000/11/03 at 04:30 CEST
+# * version 0.2
+# * - modified getDigest() equals() methods accordingly to rdf-api-2000-10-30
+# * version 0.4
+# * - updated accordingly to rdf-api-2001-01-19
+# * - fixed bug in hashCode() to avoid bulding the digest each time
+# * - added inheritance from RDFStore::Digest::Digestable
+# * version 0.41
+# * - updated accordingly to new RDFStore API
+# * - removed BLOB support
+# *
+
+package RDFStore::RDFNode;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.41';
+
+use Carp;
+use RDFStore; # load the underlying C code in RDFStore.xs because it is all in one module file
+use RDFStore::Digest::Digestable;
+
+sub toString {
+ return $_[0]->getLabel();
+};
+
+sub equals {
+ return 0
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::RDFNode")) );
+
+ return ( $_[0]->getDigest() eq $_[1]->getDigest() ) ? 1 : 0
+ if($_[1]->can('getDigest'));
+
+ return ( $_[0]->getLabel() eq $_[1]->getLabel() ) ? 1 : 0;
+};
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::RDFNode - An RDF graph node
+
+=head1 SYNOPSIS
+
+ package myNode;
+
+ use RDFStore::RDFNode;
+ @myNode::ISA = qw ( RDFStore::RDFNode );
+
+ sub new {
+ my $self = $_[0]->SUPER::new();
+ $self->{mylabel} = $_[1];
+ bless $self,$_[0];
+ };
+
+ sub getLabel {
+ return $_[0]->{mylabel};
+ };
+
+ package main;
+
+ my $node = new myNode('My generic node');
+ my $node1 = new myNode('Your generic node');
+
+ print $node->toString." is ";
+ print "not "
+ unless $node->equals($node1);
+ print " equal to ".$node1->toString."\n";
+
+
+=head1 DESCRIPTION
+
+RDFStore::RDFNode is the base abstract class for RDFStore::Literal and RDFStore::Resource.
+
+=head1 METHODS
+
+=over 4
+
+=item new
+
+This is a class method, the constructor for RDFStore::RDFNode.
+
+=item getLabel
+
+Return the label of the RDFNode as perl scalar
+
+=item toString
+
+Return the textual represention of the RDFNode
+
+=item getDigest
+
+Return a Cryptographic Digest (SHA-1 by default) of the node label - see RDFStore::Digest::Digestable(3)
+
+=item equals
+
+Compare two RDFNodes.
+
+=head1 SEE ALSO
+
+RDFStore::Literal(3) RDFStore::Resource(3) RDFStore(3) RDFStore::Digest::Digestable(3)
+
+=head1 ABOUT RDF
+
+ http://www.w3.org/TR/rdf-primer/
+
+ http://www.w3.org/TR/rdf-mt
+
+ http://www.w3.org/TR/rdf-syntax-grammar/
+
+ http://www.w3.org/TR/rdf-schema/
+
+ http://www.w3.org/TR/1999/REC-rdf-syntax-19990222 (obsolete)
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Resource.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Resource.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Resource.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Resource.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,201 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - 2000/11/03 at 04:30 CEST
+# * version 0.2
+# * - modified new() getURI() getLabel() and added getNamespace()
+# * getLocalName()methods accordingly to rdf-api-2000-10-30
+# * - modified toString()
+# * version 0.3
+# * - fixed bugs when checking references/pointers (defined and ref() )
+# * version 0.4
+# * - added check on local name when create a new Resource
+# * - updated accordingly to rdf-api-2001-01-19
+# * - allow creation of resources from URI(3) objects or strings using XMLNS LocalPart
+# * - hashCode() and getDigest() return separated values for localName and namespace if requested
+# * version 0.41
+# * - added anonymous resources support - see also RDFStore::NodeFactory(3) and RDFStore::Model(3)
+# * - added isAnonymous() and isbNode()
+# * - updated accordingly to new RDFStore API
+# * - removed BLOB support
+# *
+
+package RDFStore::Resource;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.41';
+
+use Carp;
+use RDFStore; # load the underlying C code in RDFStore.xs because it is all in one module file
+use RDFStore::RDFNode;
+
+sub isbNode {
+ return $_[0]->isAnonymous;
+};
+
+sub getURI {
+ return
+ if($_[0]->isAnonymous); #bNodes do not have a URI
+
+ return $_[0]->getLabel;
+};
+
+sub getNodeID {
+ return
+ unless($_[0]->isAnonymous);
+
+ return $_[0]->getLabel;
+ };
+
+sub equals {
+ return 0
+ unless(defined $_[1]);
+
+ return 0
+ if ( ref($_[1]) =~ /^(SCALAR|ARRAY|HASH|CODE|REF|GLOB|LVALUE)/ ); #see perldoc perlfunc ref()
+
+ return 0
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::Resource")) );
+
+ return ( $_[0]->isAnonymous && $_[1]->isAnonymous ) ? ( $_[0]->getNodeID eq $_[1]->getNodeID ) : $_[0]->SUPER::equals($_[1])
+ if( ($_[0]->isAnonymous) ||
+ ($_[1]->isAnonymous) );
+
+ # resources are equal if $_[0]->getURI() eq $_[1]->getURI() unless anonymous; then the digest is checked instead - see RDFStore::RDFNode(3)
+ unless( $_[0]->getNamespace() ) {
+ unless($_[1]->getNamespace()) {
+ return ( $_[0]->getLocalName() eq $_[1]->getLocalName() ) ? 1 : 0;
+ } else { # maybe $_[1] did not detect names
+ return ($_[0]->getLocalName() eq $_[1]->getURI()) ? 1 : 0;
+ };
+ } else {
+ if($_[1]->getNamespace()) {
+ return ( ( $_[0]->getLocalName() eq $_[1]->getLocalName() ) &&
+ ( $_[0]->getNamespace() eq $_[1]->getNamespace()) ) ? 1 : 0;
+ } else { # maybe $_[1] did not detect names
+ return ($_[0]->getURI() eq $_[1]->getURI()) ? 1 : 0;
+ };
+ };
+ return $_[0]->SUPER::equals($_[1]);
+};
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::Resource - An RDF Resource Node implementation
+
+=head1 SYNOPSIS
+
+ use RDFStore::Resource;
+ my $resource = new RDFStore::Resource("http://pen.jrc.it/index.html");
+ my $resource1 = new RDFStore::Resource("http://pen.jrc.it/","index.html");
+
+ print $resource->toString." is ";
+ print "not"
+ unless $resource->equals($resource1);
+ print " equal to ".$resource1->toString."\n";
+
+ # or from URI object
+ use URI;
+ $resource = new RDFStore::Resource("http://www.w3.org/1999/02/22-rdf-syntax-ns#","Description");
+ $resource1 = new RDFStore::Resource( new URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#Description") );
+
+ print $resource->toString." is ";
+ print "not"
+ unless $resource->equals($resource1);
+ print " equal to ".$resource1->toString."\n";
+
+ my $resource = new RDFStore::Resource([ a,{ d => 'value'}, [ 1,2,3] ]);
+
+ print $resource->toString." is ";
+ print "not"
+ unless($resource->isbNode);
+ print " anonymous\n";
+
+=head1 DESCRIPTION
+
+An RDF Resource Node implementation which support the so called anonymous-resources or bNodes (blankNodes)
+
+=head1 METHODS
+
+=over 4
+
+=item new ( LOCALNAME_NAMESPACE [, LOCALNAME ] )
+
+This is a class method, the constructor for RDFStore::Resource. In case the method is called with a single perl scalar as parameter a new RDF Resource is created with the string passed as indentifier (LOCALNAME); a fully qualified RDF resource can be constructed by invoching the constructor with B<two> paramters where the former is the NAMESPACE and the latter is the LOCALNAME. By RDF definition we assume that B<LOCALNAME can not be undefined>. If LOCALNAME is a perl reference the new Resource is flagged as anonymous-resource or bNode :)
+
+bNodes can be created either passing a perl reference to the constructor or by using the RDFStore::NodeFactory(3) createbNode() or createAnonymousResource() methods; the latter is actually setting the RDFStore::Resource internal bNode flag.
+
+=item isAnonymous
+
+Return true if the RDF Resource is anonymous or is a bNode
+
+=item isbNode
+
+Return true if the RDF Resource is anonymous or is a bNode
+
+=item getURI
+
+Return the URI identifing the RDF Resource; an undefined values i returned if the node is blank or anonymous.
+
+=item getNamespace
+
+Return the Namespace identifier of the Resource
+
+=item getLocalName
+
+Return the LocalName identifier of the Resource; if the Resource is anonymous (bNode) the Storable(3) representation of the label is returned instead. This will allow to distinguish bNodes between normal resources and give them a kind of unique identity. B<NOTE> See RDFStore::Model(3) to see how actually bNodes are being stored and retrieved in RDFStore(3).
+
+=item getLabel
+
+Return the URI identifing the RDF Resource.
+
+=item equals
+
+Compare two RDF Resources either textual
+
+=item getNodeID
+
+Return the rdf:nodeID if the Resource is anonymous (bNode).
+
+=item getbNode
+
+Return the bNode conent.
+
+=head1 SEE ALSO
+
+RDFStore::RDFNode(3)
+
+=head1 ABOUT RDF
+
+ http://www.w3.org/TR/rdf-primer/
+
+ http://www.w3.org/TR/rdf-mt
+
+ http://www.w3.org/TR/rdf-syntax-grammar/
+
+ http://www.w3.org/TR/rdf-schema/
+
+ http://www.w3.org/TR/1999/REC-rdf-syntax-19990222 (obsolete)
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/SchemaModel.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/SchemaModel.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/SchemaModel.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/SchemaModel.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,1068 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - 2000/11/03 at 04:30 CEST
+# * version 0.2
+# * - general fixing and improvements
+# * * instances and closure are Model
+# * version 0.3
+# * - added getLocalName() and getNamespace() to delegate to instances
+# * - changed checking to RDFStore::Model type
+# * - modified toString()
+# * - fixed bugs when checking references/pointers (defined and ref() )
+# * - fixed miss-spell in validate()
+# * version 0.4
+# * - complete review of the code
+# * - updated accordingly to new RDFStore::Model
+# *
+
+package RDFStore::SchemaModel;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.4';
+
+use Carp;
+use RDFStore;
+use RDFStore::VirtualModel;
+use RDFStore::Resource;
+use RDFStore::Literal;
+use RDFStore::Statement;
+use RDFStore::NodeFactory;
+use RDFStore::Vocabulary::RDF;
+use RDFStore::Vocabulary::RDFS;
+
+@RDFStore::SchemaModel::ISA = qw( RDFStore::VirtualModel );
+
+# Creates a schema model, closure must contain transitive closures of rdfs:subClassOf and rdfs:subPropertyOf
+sub new {
+ my ($pkg,$factory_or_instances,$instances_or_closure,$closure) = @_;
+
+ my $self = $pkg->SUPER::new();
+
+ #to emulate typed parameters
+ if ( (defined $factory_or_instances) &&
+ (ref($factory_or_instances)) &&
+ ($factory_or_instances->isa("RDFStore::Model")) ) {
+ $self->{nodeFactory}=new RDFStore::NodeFactory();
+ $self->{instances}=$factory_or_instances;
+ if( (defined $instances_or_closure) &&
+ (ref($instances_or_closure)) &&
+ ($instances_or_closure->isa("RDFStore::Model")) ) {
+ $self->{closure}=$instances_or_closure;
+ };
+ } elsif( (defined $factory_or_instances) &&
+ (ref($factory_or_instances)) &&
+ ($factory_or_instances->isa("RDFStore::NodeFactory")) ) {
+ $self->{nodeFactory}=$factory_or_instances;
+ if ( (defined $instances_or_closure) &&
+ (ref($instances_or_closure)) &&
+ ($instances_or_closure->isa("RDFStore::Model")) ) {
+ $self->{instances}=$instances_or_closure;
+ };
+ if( (defined $closure) &&
+ (ref($closure)) &&
+ ($closure->isa("RDFStore::Model")) ) {
+ $self->{closure}=$closure;
+ };
+ } else {
+ $self->{nodeFactory}=new RDFStore::NodeFactory();
+ if ( (defined $instances_or_closure) &&
+ (ref($instances_or_closure)) &&
+ ($instances_or_closure->isa("RDFStore::Model")) ) {
+ $self->{instances}=$instances_or_closure;
+ };
+ if( (defined $closure) &&
+ (ref($closure)) &&
+ ($closure->isa("RDFStore::Model")) ) {
+ $self->{closure}=$closure;
+ };
+ };
+
+ warn "Missing ground or schema model" and return
+ unless( defined $self->{instances} and defined $self->{closure} );
+
+ bless $self,$pkg;
+ };
+
+# we alwasy use instances (data) model for generic reference operations
+sub getNamespace {
+ return $_[0]->{instances}->getNamespace();
+ };
+
+sub getLocalName {
+ return $_[0]->{instances}->getLocalName();
+ };
+
+sub getLabel {
+ return $_[0]->{instances}->getLabel();
+ };
+
+sub getURI {
+ return $_[0]->{instances}->getURI();
+ };
+
+# return model contains the fact basis of this model
+sub getGroundModel {
+ return $_[0]->{instances};
+ };
+
+sub setSourceURI {
+ $_[0]->{instances}->setSourceURI($_[1]);
+ };
+
+sub getSourceURI {
+ return $_[0]->{instances}->getSourceURI();
+ };
+
+sub setContext {
+ return $_[0]->{instances}->setContext($_[1]);
+ };
+
+sub resetContext {
+ return $_[0]->{instances}->resetContext;
+ };
+
+sub getContext {
+ return $_[0]->{instances}->getContext;
+ };
+
+sub getOptions {
+ return $_[0]->{instances}->getOptions;
+ };
+
+sub isAnonymous {
+ return 0;
+ };
+
+sub isConnected {
+ return $_[0]->{instances}->isConnected;
+ };
+
+sub isRemote {
+ return $_[0]->{instances}->isRemote;
+ };
+
+sub namespaces {
+ return $_[0]->{instances}->namespaces;
+ };
+
+# return number of triples (unknown due to the RDF Schema business)
+sub size {
+ return -1; #unknown
+ };
+
+sub isEmpty {
+ return $_[0]->{instances}->isEmpty();
+ };
+
+sub isMutable {
+ $_[0]->{instances}->isMutable();
+ };
+
+# Enumerates all triples (including derived) for a given model and schema
+sub elements {
+ my ($class) = @_;
+
+ #something special here...like merge the instances and closure with Model?
+ return RDFStore::SchemaModel::Iterator->new( $class->getNodeFactory,
+ $class->{instances}->{rdfstore}->elements,
+ $class->{closure}->{rdfstore}->elements );
+ };
+
+sub contains {
+ return 0
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::Statement")) );
+
+ croak "Statement context '".$_[2]."' is not instance of RDFStore::Resource"
+ unless( (not(defined $_[2])) ||
+ ( (defined $_[2]) &&
+ (ref($_[2])) &&
+ ($_[2]->isa('RDFStore::Resource')) ) );
+
+ my $context;
+ if(defined $_[2]) {
+ $context = $_[2];
+ } else {
+ $context = $_[1]->context
+ if($_[1]->context);
+ };
+
+ # FIXME: efficiency?
+ return !($_[0]->find( $_[1]->subject(),
+ $_[1]->predicate(),
+ $_[1]->object(),
+ $context
+ )->isEmpty());
+};
+
+sub add {
+ my ( $class ) = shift;
+
+ $class->{instances}->add(@_);
+ };
+
+# Removes the triple from the model
+sub remove {
+ my ( $class ) = shift;
+
+ $class->{instances}->remove(@_);
+ };
+
+# General method to search for triples.
+# null input for any parameter will match anything.
+# Example: Model result = m.find( null, RDF.type, new Resource("http://...#MyClass") )
+# finds all instances of the class MyClass
+# NOTE: AR want DAML here now :-)
+sub find {
+ my ($class) = shift;
+ my ($subject,$predicate,$object,$context,$words_operator,@words) = @_;
+
+ croak "Subject '".$subject."' is not instance of RDFStore::Resource"
+ unless( (not(defined $subject)) ||
+ ( (defined $subject) &&
+ (ref($subject)) &&
+ ($subject->isa('RDFStore::Resource')) ) );
+ croak "Predicate '".$predicate."' is not instance of RDFStore::Resource"
+ unless( (not(defined $predicate)) ||
+ ( (defined $predicate) &&
+ (ref($predicate)) &&
+ ($predicate->isa('RDFStore::Resource')) ) );
+ croak "Object '".$object."' is not instance of RDFStore::RDFNode"
+ unless( (not(defined $object)) ||
+ ( (defined $object) &&
+ (ref($object)) &&
+ ($object->isa('RDFStore::RDFNode')) ) );
+
+ croak "Statement context '".$context."' is not instance of RDFStore::Resource"
+ unless( (not(defined $context)) ||
+ ( (defined $context) &&
+ (ref($context)) &&
+ ($context->isa('RDFStore::Resource')) ) );
+
+ # only two special cases for now but need more entailment/inferencing here
+
+ my $res;
+
+ # asking for instances - to me it looks really like PEN.pm ;-)
+ if ((defined $object) && ($RDFStore::Vocabulary::RDF::type->equals($predicate))) {
+ $res = $class->{instances}->find($subject,$predicate,$object,$context,$words_operator,@words);
+
+ # collect subclasses
+ my $subclass = $class->{closure}->find(undef,$RDFStore::Vocabulary::RDFS::subClassOf,$object)->elements;
+ while ( my $s = $subclass->each_subject ) {
+ # find instances
+ my $subclass_type = $class->{instances}->find( $subject,$RDFStore::Vocabulary::RDF::type,$s ); #slow and inefficient (most searches will fail) !!
+ while ( my $s1 = $subclass_type->each ) {
+ $res->add( $s1 );
+ };
+ };
+ } elsif($RDFStore::Vocabulary::RDFS::subClassOf->equals($predicate)) {
+ $res = $class->{closure}->find($subject,$predicate,$object,undef,$words_operator,@words); #we do not consider context!!!
+ } elsif(defined $predicate) {
+ $res = $class->{instances}->find($subject,$predicate,$object,$context,$words_operator,@words);
+
+ # collect subproperties
+ my $subprop = $class->{closure}->find(undef, $RDFStore::Vocabulary::RDFS::subPropertyOf,$predicate)->elements;
+ while ( my $s = $subprop->each_subject ) {
+ # find properties
+ my $subprop_type = $class->{instances}->find( $subject,$s, $predicate ); #slow and inefficient (most searches will fail) !!
+ while ( my $s1 = $subprop_type->each ) {
+ $res->add( $s1 );
+ };
+ };
+ } else {
+ # normal search into instances
+ $res = $class->{instances}->find($subject,$predicate,$object,$context,$words_operator,@words);
+ };
+
+ return $res;
+};
+
+sub duplicate {
+ # creates a model that shares ONLY the closure with this model
+ return new RDFStore::SchemaModel($_[0]->{nodeFactory},$_[0]->{instances}->duplicate(), $_[0]->{closure});
+ };
+
+sub create {
+ return new RDFStore::SchemaModel($_[0]->{instances}->create(), $_[0]->{closure});
+ };
+
+sub getNodeFactory {
+ return $_[0]->{nodeFactory};
+ };
+
+sub toString {
+ return "[RDFSchemaModel ".$_[0]->{instances}->getSourceURI()."]";
+ };
+
+sub intersect {
+ my ($class,$other) = @_;
+
+ return
+ unless($other);
+
+ croak "Model '".$other."' is not instance of RDFStore::SchemaModel"
+ unless( (defined $other) && (ref($other)) &&
+ ($other->isa('RDFStore::Model')) );
+
+ croak "Models can not be intersected"
+ unless( ( ( $class->{instances}->{Shared} == $class->{instances}->{Shared} ) &&
+ ( $class->{closure}->{Shared} == $class->{closure}->{Shared} ) ) ||
+ ( ( $class->{instances}->{rdfstore} == $other->{instances}->{rdfstore} ) &&
+ ( $class->{closure}->{rdfstore} == $other->{closure}->{rdfstore} ) ) );
+
+ my $res = $class->create(); #EMPTY MODEL in-memory
+
+ $res->{Shared} = $class; # share storage (the other is sharing it anyway by definition :-)
+
+ $res->{query_iterator} = $class->elements->intersect( $other->elements ); # that easy :)
+
+ # set default context if any was set
+ my $sg = $class->getContext;
+ $res->setContext( $sg )
+ if(defined $sg);
+
+ return $res;
+ };
+
+sub subtract {
+ };
+
+sub unite {
+ };
+
+sub complement {
+ };
+
+sub exor {
+ };
+
+sub serialize {
+ };
+
+sub computeRDFSClosure {
+ croak "Model ".$_[1]." is not instance of RDFStore::Model"
+ unless( (defined $_[1]) && (ref($_[1])) &&
+ ($_[1]->isa('RDFStore::Model')) );
+
+ #closure must be Model!!!
+ my $closure = $_[0]->computeClosure($_[1],$RDFStore::Vocabulary::RDFS::subClassOf);
+ $closure->unite($_[0]->computeClosure($_[1],$RDFStore::Vocabulary::RDFS::subPropertyOf)); # works???
+ return $closure;
+ };
+
+# Computes a transitive closure on a given predicate. If allowLoops is set to false, an error is thrown if a loop is encountered
+sub computeClosure {
+ my ($class, $model, $property, $allowLoops ) = @_;
+
+ croak "Model ".$model." is not instance of RDFStore::Model"
+ unless( (defined $model) && (ref($model)) &&
+ ($model->isa('RDFStore::Model')) );
+
+ croak "Property ".$property." is not instance of RDFStore::Resource"
+ unless( (defined $property) && (ref($property)) &&
+ ($property->isa('RDFStore::Resource')) );
+
+ # disallow loops by default
+ $allowLoops = 0
+ unless( ($allowLoops) && (int($allowLoops)) );
+
+ my $closure = $model->create(); #in-memory by default???
+
+ # find all roots
+ my $all = $model->find(undef, $property, undef)->elements;
+
+ # compute closure
+ my %processedNodes = ();
+ my %stack = ();
+ while ( my $o = $all->each_object ) {
+ if( (!(exists $processedNodes{ $o->toString })) &&
+ ($o->isa("RDFStore::Resource")) ) {
+ %stack = ();
+ croak "[RDFSchemaModel] found invalid loop in transitive closure of ",$property->getLabel," Loop node: ",$o->getLabel
+ if( ($class->traverseClosure( \%processedNodes, $o, $property, \%stack, $closure, $model, 0)) &&
+ (!($allowLoops)) );
+ };
+ };
+
+ return $closure;
+ };
+
+# traverse down the tree, maintains stack and adds shortcuts to the model. Returns true if a loop is found - false otherwise
+sub traverseClosure {
+ my ($class, $processedNodes, $object, $property, $stack, $closure, $model, $depth ) = @_;
+
+ croak "Hash ".$processedNodes." is not an HASH reference"
+ unless( (defined $processedNodes) &&
+ (ref($processedNodes) =~ /HASH/) );
+
+ croak "Resource ".$object." is not instance of RDFStore::Resource"
+ unless( (defined $object) && (ref($object)) &&
+ ($object->isa('RDFStore::Resource')) );
+
+ croak "Resource ".$property." is not instance of RDFStore::Resource"
+ unless( (defined $property) && (ref($property)) &&
+ ($property->isa('RDFStore::Resource')) );
+
+ croak "Hash ".$stack." is not an HASH reference"
+ unless( (defined $stack) &&
+ (ref($stack) =~ /HASH/) );
+
+ croak "Model ".$closure." is not instance of RDFStore::Model"
+ unless( (defined $closure) && (ref($closure)) &&
+ ($closure->isa('RDFStore::Model')) );
+
+ croak "Model ".$model." is not instance of RDFStore::Model"
+ unless( (defined $model) && (ref($model)) &&
+ ($model->isa('RDFStore::Model')) );
+
+ croak "Integer ".$depth." is not a valid INTEGER "
+ unless( ($depth == 0) || ( (defined $depth) && (int($depth))) );
+
+ $processedNodes->{ $object->toString } = 1;
+
+ my $isOnStack = (exists $stack->{ $object->toString });
+ my $isLoop = $isOnStack;
+ if(!($isOnStack)) {
+ $stack->{ $object->toString } = 1;
+
+ # get all children of this node
+ my $children = $model->find(undef, $property, undef)->elements;
+
+ while ( my $s = $children->each_subject ) {
+ # uauuu!! recursive here :)
+ $isLoop |= $_[0]->traverseClosure($processedNodes, $s, $property, $stack, $closure, $model, $depth+1 );
+ };
+
+ delete $stack->{ $object->toString }
+ if(!($isLoop));
+ };
+
+ # add everything from stack
+ if(!($isOnStack)) {
+ while ( my ($k,$parent) = each %{$stack} ) {
+ my $factory = $model->getNodeFactory();
+ $closure->add( $factory->createStatement($object, $property, $factory->createResource($parent)) );
+ };
+ };
+
+ return $isLoop;
+ };
+
+# validates instances model against a schema model
+sub validateRawSchema {
+ my ($class, $instances_model, $schema_model) = @_;
+
+ croak "Model ".$instances_model." is not instance of RDFStore::Model"
+ unless( (defined $instances_model) && (ref($instances_model)) &&
+ ($instances_model->isa('RDFStore::Model')) );
+
+ croak "Model ".$schema_model." is not instance of RDFStore::Model"
+ unless( (defined $schema_model) && (ref($schema_model)) &&
+ ($schema_model->isa('RDFStore::Model')) );
+
+ my $closure = $class->computeRDFSClosure($schema_model);
+
+ my $schema = new RDFStore::SchemaModel($schema_model, $closure); #kinda recursive here...
+ my $instances = new RDFStore::SchemaModel($instances_model, $closure);
+
+ $class->validate($instances, $schema);
+ };
+
+# converts an ordinal property to an integer
+sub getOrd {
+ croak "Resource ".$_[1]." is not instance of RDFStore::Resource"
+ unless( (defined $_[1]) && (ref($_[1])) &&
+ ($_[1]->isa('RDFStore::Resource')) );
+
+ return -1
+ unless(defined $_[1]);
+
+ my $uri = $_[1]->toString();
+
+ #is RDF?
+ return -1
+ if(!((defined $uri) && ($uri =~ m|^$RDFStore::Vocabulary::RDF::_Namespace|)));
+
+ # Position of the namespace end
+ my $pos;
+ if ( ($uri =~ m|#$|g) ||
+ ($uri =~ m|:$|g) ||
+ ($uri =~ m|\/$|g) ) {
+ $pos=pos($uri);
+ } else {
+ $pos=length($uri);
+ };
+
+ if(($pos > 0) && ($pos + 1 < length($uri))) {
+ #parseInt in Perl....
+ my $n = unpack("i*",substring($uri,$pos + 1));
+ return $n
+ if($n >= 1);
+ };
+
+ return -1;
+ };
+
+# validates the model. schema should be a RDFStore::SchemaModel
+sub validate {
+ my ($class, $instances, $schema ) = @_;
+
+ croak "Model ".$instances." is not instance of RDFStore::Model"
+ unless( (defined $instances) && (ref($instances)) &&
+ ($instances->isa('RDFStore::Model')) );
+
+ croak "Model ".$schema." is not instance of RDFStore::SchemaModel"
+ unless( (defined $schema) && (ref($schema)) &&
+ ($schema->isa('RDFStore::SchemaModel')) );
+
+ my %containers = (); # triples containing collections and ordinals
+ my @errors = ();
+ my $ele = $instances->elements;
+ while ( my $t = $ele->each ) {
+ # rdf:type
+ if($RDFStore::Vocabulary::RDF::type->equals($t->predicate())) {
+ # ensure that the target is of type rdf:Class
+ if($t->object()->isa("RDFStore::Literal")) {
+ $class->invalid( \@errors, $t, "Literals cannot be used for typing - object must be a RDF resource of some kind" );
+ };
+ # cast is skipped in Perl.....
+ my $res = $schema->find( $t->object(), $RDFStore::Vocabulary::RDF::type, $RDFStore::Vocabulary::RDFS::Class );
+ if($res->isEmpty()) {
+ if($class->noSchema(\@errors, $t->object())) {
+ last;
+ } else {
+ $class->invalid( \@errors, $t, $t->object->toString . " must be an instance of ". $RDFStore::Vocabulary::RDFS::Class->toString );
+ };
+ };
+ } elsif($class->getOrd($t->predicate()) > 0) {
+ # save for later
+ $containers{ $t->subject->toString }= $t;
+ } else {
+ # check rdfs:domain and rdfs:range
+ my @expected = ();
+
+ # find all allowed domains of the Property
+ my $domains = $schema->find( $t->predicate(), $RDFStore::Vocabulary::RDFS::domain, undef );
+ if(!($domains->isEmpty())) {
+ my $domainOK = 0;
+ # go through all valid domains and check whether the subject is an instance of a valid domain Class
+ my $dd = $domains->elements;
+ while( my $o = $dd->each_object ) {
+ push @expected, $o;
+ if(!($instances->find($t->subject(),$RDFStore::Vocabulary::RDF::type, $o)->isEmpty())) {
+ $domainOK = 1;
+ last;
+ };
+ };
+ if(!($domainOK)) {
+ if($class->noSchema(\@errors, $t->subject())) {
+ last;
+ } else {
+ $class->invalid( \@errors, $t, "Subject must be instance of ".join(' or ', map { $_->toString } @expected ) );
+ };
+ };
+ };
+ @expected=();
+
+ # find all allowed ranges of the Property
+ my $ranges = $schema->find( $t->predicate(), $RDFStore::Vocabulary::RDFS::range, undef );
+ if($ranges->size() == 1) { # there can be only one range property!!! (See specs)
+ my $rangeOK = 0;
+ # go through all valid ranges and check whether
+ # the object() is an instance of a valid range Class
+ my $rr = $ranges->elements;
+ while( my $o = $rr->each_object ) {
+ push @expected, $o;
+ # special treatment for Literals
+ if($RDFStore::Vocabulary::RDFS::Literal->equals($o)) {
+ if( $t->object()->isa("RDFStore::Literal")) {
+ $rangeOK = 1;
+ last;
+ } else {
+ $class->invalid( \@errors, $t, $t->object() ." must be a literal");
+ };
+ } elsif ( ($t->object()->isa("RDFStore::Resource")) &&
+ (!($instances->find( $t->object(), $RDFStore::Vocabulary::RDF::type, $o )->isEmpty())) ) {
+ $rangeOK = 1;
+ last;
+ };
+ };
+ if(!($rangeOK)) {
+ if($class->noSchema(\@errors,$t->object())) {
+ last;
+ } else {
+ $class->invalid( \@errors, $t, "Object must be instance of ".join(' or ', map { $_->toString } @expected ) );
+ };
+ };
+ } elsif($ranges->size() > 1) {
+ $class->invalid( \@errors, undef, "Invalid schema. Multiple ranges for ".$t->predicate->toString );
+ };
+ };
+ };
+
+ croak "InvalidModel ".join(' , ', @errors)
+ if(scalar(@errors)>0);
+ };
+
+sub noSchema {
+ return 0;
+ };
+
+sub invalid {
+ croak "Parameter ".$_[1]." is not an ARRAY reference"
+ unless( (defined $_[1]) &&
+ (ref($_[1])=~ /ARRAY/) );
+ croak "Statement ".$_[2]." is not instance of RDFStore::Statement"
+ unless( (defined $_[2]) && (ref($_[2])) &&
+ ($_[2]->isa('RDFStore::Statement')) );
+
+ if(scalar(@{$_[1]}) > 0) {
+ push @{$_[1]},"\n";
+ if(defined $_[2]) {
+ push @{$_[1]},"Invalid statement:\n\t".$_[2].".\n\t";
+ };
+ };
+
+ push @{$_[1]},$_[3];
+ };
+
+# simple front-end to RDFStore::Iterator using a the given nodeFactory
+package RDFStore::SchemaModel::Iterator;
+
+use vars qw ( $VERSION );
+use strict;
+
+$VERSION = '0.1';
+
+sub new {
+ my ($pkg,$factory,$iterator) = @_;
+
+ return
+ unless( (defined $iterator) && (defined $factory) );
+
+ return bless {
+ factory => $factory,
+ iterator => $iterator
+ },$pkg;
+ };
+
+sub size {
+ return $_[0]->{iterator}->size;
+ };
+
+sub duplicate {
+ return $_[0]->{iterator}->duplicate;
+ };
+
+sub hasnext {
+ return $_[0]->{iterator}->hasnext;
+ };
+
+sub remove {
+ return $_[0]->{iterator}->remove;
+ };
+
+sub intersect {
+ return
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::SchemaModel::Iterator")) );
+
+ return new RDFStore::SchemaModel::Iterator( $_[0]->{factory},
+ $_[0]->{iterator}->intersect( $_[1]->{iterator} ) );
+ };
+
+sub unite {
+ return
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::SchemaModel::Iterator")) );
+
+ return new RDFStore::SchemaModel::Iterator( $_[0]->{factory},
+ $_[0]->{iterator}->unite( $_[1]->{iterator} ) );
+ };
+
+sub subtract {
+ return
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::SchemaModel::Iterator")) );
+
+ return new RDFStore::SchemaModel::Iterator( $_[0]->{factory},
+ $_[0]->{iterator}->subtract( $_[1]->{iterator} ) );
+ };
+
+sub complement {
+ return
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::SchemaModel::Iterator")) );
+
+ return new RDFStore::SchemaModel::Iterator( $_[0]->{factory},
+ $_[0]->{iterator}->complement( $_[1]->{iterator} ) );
+ };
+
+sub exor {
+ return
+ unless( (defined $_[1]) &&
+ (ref($_[1])) &&
+ ($_[1]->isa("RDFStore::SchemaModel::Iterator")) );
+
+ return new RDFStore::SchemaModel::Iterator( $_[0]->{factory},
+ $_[0]->{iterator}->exor( $_[1]->{iterator} ) );
+ };
+
+sub next {
+ my ($st) = $_[0]->{iterator}->next;
+
+ return
+ unless($st);
+
+ return $_[0]->{factory}->createStatement(
+ ( $st->subject->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->subject->toString ) :
+ $_[0]->{factory}->createResource( $st->subject->toString ),
+ ( $st->predicate->isbNode ) ? # I know that is not possible but we allow it anyway ;-/
+ $_[0]->{factory}->createAnonymousResource( $st->predicate->toString ) :
+ $_[0]->{factory}->createResource( $st->predicate->toString ),
+ ( $st->object->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $st->object->getLabel,
+ $st->object->getParseType,
+ $st->object->getLang,
+ $st->object->getDataType ) :
+ ( $st->object->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->object->toString ) :
+ $_[0]->{factory}->createResource( $st->object->toString ),
+ ( $st->context ) ? ( $st->context->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->context->toString ) :
+ $_[0]->{factory}->createResource( $st->context->toString ) : undef );
+ };
+
+sub next_subject {
+ my ($n) = $_[0]->{iterator}->next_subject;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub next_predicate {
+ my ($n) = $_[0]->{iterator}->next_predicate;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub next_object {
+ my ($n) = $_[0]->{iterator}->next_object;
+
+ return
+ unless($n);
+
+ return ( $n->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $n->getLabel,
+ $n->getParseType,
+ $n->getLang,
+ $n->getDataType ) :
+ ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub next_context {
+ my ($n) = $_[0]->{iterator}->next_context;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub current {
+ my ($st) = $_[0]->{iterator}->current;
+
+ return
+ unless($st);
+
+ return $_[0]->{factory}->createStatement(
+ ( $st->subject->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->subject->toString ) :
+ $_[0]->{factory}->createResource( $st->subject->toString ),
+ ( $st->predicate->isbNode ) ? # I know that is not possible but we allow it anyway ;-/
+ $_[0]->{factory}->createAnonymousResource( $st->predicate->toString ) :
+ $_[0]->{factory}->createResource( $st->predicate->toString ),
+ ( $st->object->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $st->object->getLabel,
+ $st->object->getParseType,
+ $st->object->getLang,
+ $st->object->getDataType ) :
+ ( $st->object->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->object->toString ) :
+ $_[0]->{factory}->createResource( $st->object->toString ),
+ ( $st->context ) ? ( $st->context->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->context->toString ) :
+ $_[0]->{factory}->createResource( $st->context->toString ) : undef );
+ };
+
+sub current_subject {
+ my ($n) = $_[0]->{iterator}->current_subject;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub current_predicate {
+ my ($n) = $_[0]->{iterator}->current_predicate;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub current_object {
+ my ($n) = $_[0]->{iterator}->current_object;
+
+ return
+ unless($n);
+
+ return ( $n->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $n->getLabel,
+ $n->getParseType,
+ $n->getLang,
+ $n->getDataType ) :
+ ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub current_context {
+ my ($n) = $_[0]->{iterator}->current_context;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub first {
+ my ($st) = $_[0]->{iterator}->first;
+
+ return
+ unless($st);
+
+ return $_[0]->{factory}->createStatement(
+ ( $st->subject->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->subject->toString ) :
+ $_[0]->{factory}->createResource( $st->subject->toString ),
+ ( $st->predicate->isbNode ) ? # I know that is not possible but we allow it anyway ;-/
+ $_[0]->{factory}->createAnonymousResource( $st->predicate->toString ) :
+ $_[0]->{factory}->createResource( $st->predicate->toString ),
+ ( $st->object->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $st->object->getLabel,
+ $st->object->getParseType,
+ $st->object->getLang,
+ $st->object->getDataType ) :
+ ( $st->object->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->object->toString ) :
+ $_[0]->{factory}->createResource( $st->object->toString ),
+ ( $st->context ) ? ( $st->context->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->context->toString ) :
+ $_[0]->{factory}->createResource( $st->context->toString ) : undef );
+ };
+
+sub first_subject {
+ my ($n) = $_[0]->{iterator}->first_subject;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub first_predicate {
+ my ($n) = $_[0]->{iterator}->first_predicate;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub first_object {
+ my ($n) = $_[0]->{iterator}->first_object;
+
+ return
+ unless($n);
+
+ return ( $n->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $n->getLabel,
+ $n->getParseType,
+ $n->getLang,
+ $n->getDataType ) :
+ ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub first_context {
+ my ($n) = $_[0]->{iterator}->first_context;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub each {
+ my ($st) = $_[0]->{iterator}->each;
+
+ return
+ unless($st);
+
+ return $_[0]->{factory}->createStatement(
+ ( $st->subject->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->subject->toString ) :
+ $_[0]->{factory}->createResource( $st->subject->toString ),
+ ( $st->predicate->isbNode ) ? # I know that is not possible but we allow it anyway ;-/
+ $_[0]->{factory}->createAnonymousResource( $st->predicate->toString ) :
+ $_[0]->{factory}->createResource( $st->predicate->toString ),
+ ( $st->object->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $st->object->getLabel,
+ $st->object->getParseType,
+ $st->object->getLang,
+ $st->object->getDataType ) :
+ ( $st->object->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->object->toString ) :
+ $_[0]->{factory}->createResource( $st->object->toString ),
+ ( $st->context ) ? ( $st->context->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $st->context->toString ) :
+ $_[0]->{factory}->createResource( $st->context->toString ) : undef );
+ };
+
+sub each_subject {
+ my ($n) = $_[0]->{iterator}->each_subject;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub each_predicate {
+ my ($n) = $_[0]->{iterator}->each_predicate;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub each_object {
+ my ($n) = $_[0]->{iterator}->each_object;
+
+ return
+ unless($n);
+
+ return ( $n->isa("RDFStore::Literal") ) ?
+ $_[0]->{factory}->createLiteral( $n->getLabel,
+ $n->getParseType,
+ $n->getLang,
+ $n->getDataType ) :
+ ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+sub each_context {
+ my ($n) = $_[0]->{iterator}->each_context;
+
+ return
+ unless($n);
+
+ return ( $n->isbNode ) ?
+ $_[0]->{factory}->createAnonymousResource( $n->toString ) :
+ $_[0]->{factory}->createResource( $n->toString );
+ };
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::SchemaModel - implementation of the SchemaModel RDF API
+
+=head1 SYNOPSIS
+
+ use RDFStore::SchemaModel;
+ my $schema_validator = new RDFStore::SchemaModel();
+ my $valid = $schema_validator->validateRawSchema($m,$rawSchema);
+
+=head1 DESCRIPTION
+
+This is an incomplete package and it provides basic RDF Schema support accordingly to the Draft API of Sergey Melnik at http://www-db.stanford.edu/~melnik/rdf/api.html.
+Please use it as a prototype and/or just to get the idea. It provide basic 'closure' support and validation of a given RDF instance against an RDF Schema.
+
+=head1 SEE ALSO
+
+ RDFStore::Model(3) RDFStore::VirtualModel(3)
+
+ http://www.w3.org/TR/rdf-primer/
+
+ http://www.w3.org/TR/rdf-mt
+
+ http://www.w3.org/TR/rdf-syntax-grammar/
+
+ http://www.w3.org/TR/rdf-schema/
+
+ http://www.w3.org/TR/1999/REC-rdf-syntax-19990222 (obsolete)
+
+ DARPA Agent Markup Language (DAML) - http://www.daml.org/
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,207 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - Tue Jan 28 15:30:00 CET 2003
+# *
+
+package RDFStore::Serializer;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.1';
+
+use Carp;
+
+# common vocabularies
+use RDFStore::Vocabulary::RDF;
+use RDFStore::Vocabulary::RDFS;
+use RDFStore::Vocabulary::DC;
+use RDFStore::Vocabulary::DCQ;
+use RDFStore::Vocabulary::DCT;
+use RDFStore::Vocabulary::DAML;
+use RDFStore::Vocabulary::RDFStoreContext;
+
+# it could return a kind of tied filehandle/stream :) i.e. TIEHANLDE see perltie(8)
+sub new {
+ my ($pkg) = @_;
+
+ my $self = {};
+
+ #load default ones
+ $self->{Namespaces} = {};
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::RDF::_Namespace } = 'rdf';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::RDFS::_Namespace } = 'rdfs';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::DC::_Namespace } = 'dc';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::DCQ::_Namespace } = 'dcq';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::DCT::_Namespace } = 'dct';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::DAML::_Namespace } = 'daml';
+ $self->{Namespaces}->{ $RDFStore::Vocabulary::RDFStoreContext::_Namespace } = 'rdfstore';
+
+ $self->{string} = '';
+
+ $self->{'options'} = {};
+
+ bless $self,$pkg;
+
+ return $self;
+ };
+
+sub setProperty {
+ my ($class, $name, $value) = @_;
+
+ $class->{'options'}->{ $name } = $value;
+ };
+
+sub getProperty {
+ my ($class, $name) = @_;
+
+ return $class->{'options'}->{ $name };
+ };
+
+sub setNamespacePrefix {
+ my ($class, $ns_uri, $ns_prefix ) = @_;
+
+ $class->{Namespaces}->{ $ns_uri } = $ns_prefix;
+ };
+
+sub getNamespacePrefix {
+ my ($class, $ns_uri ) = @_;
+
+ return $class->{Namespaces}->{ $ns_uri };
+ };
+
+sub serialize {
+ my ($class) = shift;
+
+ return $class->write( @_ );
+ };
+
+sub write {
+ my ($class, $model, $fh, $namespaces, $base ) = @_;
+
+ croak "Model is not defined"
+ unless( (defined $model) && (ref($model)) &&
+ ($model->isa('RDFStore::Model')) );
+
+ $class->{'ioref'} = \$fh
+ if( defined $fh );
+
+ if ( ( defined $namespaces ) &&
+ (ref($namespaces) =~ /HASH/) ) {
+ foreach my $ns_uri ( keys %{$namespaces} ) {
+ $class->setNamespacePrefix( $ns_uri, $namespaces->{ $ns_uri } );
+ };
+ };
+
+ # init string
+ $class->{string} = '';
+ };
+
+sub printContent {
+ my ($class) = shift;
+
+ if(exists $class->{'ioref'}) {
+ print ${ $class->{'ioref'} } (@_);
+ } else {
+ $class->{string} .= join('',@_);
+ };
+ };
+
+sub returnContent {
+ return (exists $_[0]->{'ioref'}) ? 1 : $_[0]->{string};
+ };
+
+sub xml_escape {
+ my $class = shift;
+ my $text = shift;
+
+ $text =~ s/\&/\&/g;
+ $text =~ s/</\</g;
+ foreach (@_) {
+ croak "xml_escape: '$_' isn't a single character" if length($_) > 1;
+
+ if ($_ eq '>') {
+ $text =~ s/>/\>/g;
+ } elsif ($_ eq '"') {
+ $text =~ s/\"/\"/g;
+ } elsif ($_ eq "'") {
+ $text =~ s/\'/\'/g;
+ } else {
+ my $rep = '&#' . sprintf('x%X', ord($_)) . ';';
+ if (/\W/) {
+ my $ptrn = "\\$_";
+ $text =~ s/$ptrn/$rep/g;
+ } else {
+ $text =~ s/$_/$rep/g;
+ };
+ };
+ };
+ return $text;
+ };
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::Serializer - Interface to an RDF model/graph serializer
+
+=head1 SYNOPSIS
+
+ use RDFStore::Serializer;
+
+ my $model= new RDFStore::Model();
+ $model->add($statement);
+ $model->add($statement1);
+ $model->add($statement2);
+
+ my $serializer = new RDFStore::Serializer;
+
+ my $rdf = $serializer->serialize( $model ); # serialise model to a string in-memory
+ my $rdf = $serializer->serialize( $model, undef, {}, $base ); # using xml:base
+ my $rdf = $serializer->serialize( $model, undef, { 'http://mynamespace.org/blaaa/' => blaa } ); # using my blaa namespace
+
+ $serializer->serialize( $model, *STREAM ); # serialise model to a given file descriptor (stream)
+
+=head1 DESCRIPTION
+
+An RDFStore::Model serializer.
+
+=head1 CONSTRUCTORS
+
+The following methods construct RDFStore::Serializer:
+
+=item new ()
+
+ Create an new RDFStore::Serializer object to serialize and RDFStore::Model.
+
+=head1 METHODS
+
+=item write ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Write out the given MODEL to FILEHANDLE_REF (or in-memory string if not passed) using a given list of NAMESPACES and xml:base BASE if passed. The NAMESPACES hash ref contains a list of namespace values (URI refs) and prefix names - see RDFStore::Vocabulary::Generator(3). By default the output is returned from the method into a string otheriwse a valid (and opened) FILEHANLDE_REF can be passed, which will be being printed to.
+
+=item serialize ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Same as write method above.
+
+=head1 SEE ALSO
+
+RDFStore::Model(3) RDFStore::Serializer::Strawman(3) RDFStore::Serializer::RDFXML(3)
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/NTriples.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/NTriples.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/NTriples.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/NTriples.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,113 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - Tue Jan 28 15:30:00 CET 2003
+# *
+
+package RDFStore::Serializer::NTriples;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.1';
+
+use Carp;
+
+use RDFStore::Serializer;
+use RDFStore;
+
+@RDFStore::Serializer::NTriples::ISA = qw( RDFStore::Serializer );
+
+sub new {
+ my ($pkg) = shift;
+
+ my $self = $pkg->SUPER::new(@_);
+
+ bless $self,$pkg;
+ };
+
+sub write {
+ my ($class, $model, $fh, $namespaces ) = @_;
+
+ $model = $class->{'model'}
+ unless($model);
+
+ # init
+ $class->SUPER::write( $model, $fh, $namespaces );
+
+ my $itr = $model->elements;
+ while ( my $st = $itr->each ) {
+ $class->printContent( $st->toString . "\n" );
+ };
+
+ return $class->returnContent;
+ };
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::Serilizer::NTriples - Serialise a model/graph to W3C RDF Test Cases N-Triples syntax
+
+=head1 SYNOPSIS
+
+ use RDFStore::Serializer::NTriples;
+
+ my $model= new RDFStore::Model();
+ $model->add($statement);
+ $model->add($statement1);
+ $model->add($statement2);
+
+ my $serializer = new RDFStore::Serializer::NTriples;
+ my $rdf_strawman = $serializer->serialize( $model ); # serialise model to a string in-memory
+
+ $serializer->serialize( $model, *STREAM ); # serialise model to a given descriptor (stream)
+
+=head1 DESCRIPTION
+
+An RDFStore::Model serializer to W3C RDF Test Cases N-Triples syntax - see http://www.w3.org/TR/rdf-testcases/#ntriples
+
+=head1 CONSTRUCTORS
+
+The following methods construct RDFStore::Serializer::NTriples :
+
+=item new ()
+
+ Create an new RDFStore::Serializer object to serialize and RDFStore::Model.
+
+=head1 METHODS
+
+=item write ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Write out the given MODEL to FILEHANDLE_REF (or in-memory string if not passed) using a given list of NAMESPACES and xml:base BASE if passed. The NAMESPACES hash ref contains a list of namespace values (URI refs) and prefix names - see RDFStore::Vocabulary::Generator(3). By default the output is returned from the method into a string otheriwse a valid (and opened) FILEHANLDE_REF can be passed, which will be being printed to.
+
+=item serialize ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Same as write method above.
+
+=head1 SEE ALSO
+
+ RDFStore::Model(3) RDFStore::Serializer(3)
+
+ http://www.w3.org/TR/rdf-testcases/#ntriples
+
+ http://www.w3.org/TR/rdf-syntax-grammar/
+
+ http://www.w3.org/TR/rdf-schema/
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>
Added: incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/RDFXML.pm
URL: http://svn.apache.org/viewvc/incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/RDFXML.pm?view=auto&rev=528394
==============================================================================
--- incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/RDFXML.pm (added)
+++ incubator/triplesoup/donations/TRIPLES-3-RDFStore/lib/RDFStore/Serializer/RDFXML.pm Fri Apr 13 01:56:01 2007
@@ -0,0 +1,267 @@
+# *
+# * Copyright (c) 2000-2006 Alberto Reggiori <ar...@webweaving.org>
+# * Dirk-Willem van Gulik <di...@webweaving.org>
+# *
+# * NOTICE
+# *
+# * This product is distributed under a BSD/ASF like license as described in the 'LICENSE'
+# * file you should have received together with this source code. If you did not get a
+# * a copy of such a license agreement you can pick up one at:
+# *
+# * http://rdfstore.sourceforge.net/LICENSE
+# *
+# * Changes:
+# * version 0.1 - Tue Jan 28 15:30:00 CET 2003
+# * version 0.2
+# * - fixed bug when model context is set
+# *
+
+package RDFStore::Serializer::RDFXML;
+{
+use vars qw ($VERSION);
+use strict;
+
+$VERSION = '0.2';
+
+use Carp;
+
+use RDFStore::Serializer;
+
+@RDFStore::Serializer::RDFXML::ISA = qw( RDFStore::Serializer );
+
+sub new {
+ my ($pkg) = shift;
+
+ my $self = $pkg->SUPER::new(@_);
+
+ bless $self,$pkg;
+ };
+
+sub write {
+ my ($class, $model, $fh, $namespaces, $base ) = @_;
+
+ $model = $class->{'model'}
+ unless($model);
+
+ # ignore model its context while dumping
+ my $ctx = $model->getContext();
+ $model->resetContext();
+
+ # init
+ $class->SUPER::write( $model, $fh, $namespaces );
+
+ $class->{subjects_done} = {};
+
+ # header
+ $class->printContent( "<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":RDF" );
+ $class->printContent( "\n\txmlns:" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . "='". $RDFStore::Vocabulary::RDF::_Namespace . "'");
+
+ # rdfstore namespace is alwasys put there
+ $class->printContent( "\n\txmlns:" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDFStoreContext::_Namespace) . "='". $RDFStore::Vocabulary::RDFStoreContext::_Namespace . "'");
+
+ $class->printContent( "\n\txml:base='$base'")
+ if( (defined $base) &&
+ ($base ne '') );
+
+ my $cc=0;
+ foreach my $ns ( $model->namespaces ) {
+ next
+ if( $ns eq $RDFStore::Vocabulary::RDF::_Namespace );
+
+ $class->setNamespacePrefix( $ns, 'voc'.($cc++) )
+ unless( $class->getNamespacePrefix( $ns ) );
+
+ $class->printContent( "\n\txmlns:" . $class->getNamespacePrefix($ns) . "='". $ns . "'");
+ };
+ $class->printContent( ">");
+
+ # we should try to group descriptions about the same thing (same subject)
+ my $itr = $model->elements;
+ while ( my $st = $itr->each ) {
+ $class->_processDescription( $model, $st );
+ };
+
+ # footer
+ $class->printContent( "\n</" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":RDF>" );
+
+ $class->{subjects_done} = {};
+
+ # restore context
+ $model->setContext( $ctx )
+ if($ctx);
+
+ return $class->returnContent;
+ };
+
+sub _processDescription {
+ my ($class, $model, $statement ) = @_;
+
+ my $context = $statement->context;
+ my $ctx = $context->toString
+ if($context);
+
+ return
+ if(exists $class->{subjects_done}->{ $statement->subject->toString . $ctx });
+
+ $class->{subjects_done}->{ $statement->subject->toString . $ctx } = 1; # for very large model this might be a problem!!
+
+ # group by subject in context
+ my $itr = $model->find( $statement->subject, undef, undef, $context )->elements;
+
+ return
+ unless($itr->size > 0 );
+
+ $class->printContent( "\n<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":Description " );
+ if ( $statement->subject->isbNode ) {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":nodeID='" . $statement->subject->getLabel );
+ } else {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":about='" . $class->xml_escape( $statement->subject->getURI,"'" ) );
+ };
+ if ( $context ) {
+ if ( $context->isbNode ) {
+ $class->printContent( "' " . $class->getNamespacePrefix($RDFStore::Vocabulary::RDFStoreContext::_Namespace) . ":contextnodeID='" . $context->getLabel );
+ } else {
+ $class->printContent( "' " . $class->getNamespacePrefix($RDFStore::Vocabulary::RDFStoreContext::_Namespace) . ":context='" . $class->xml_escape( $context->getURI,"'" ) );
+ };
+ };
+ $class->printContent("'>");
+
+ # properly un-asserted statement?
+ if ( ($statement->subject->isa("RDFStore::Statement")) &&
+ (! $model->contains( $statement->subject) ) ) { #it must be un-asserted
+ $class->printContent( "\n\t<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":type " .
+ $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":resource='".$RDFStore::Vocabulary::RDF::_Namespace ."Statement' />");
+
+ $class->printContent( "\n\t<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":subject ");
+ if ( $statement->subject->subject->isbNode ) {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":nodeID='" . $statement->subject->subject->getLabel );
+ } else {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":resource='" . $class->xml_escape( $statement->subject->subject->getURI,"'" ) );
+ };
+ $class->printContent( "' />");
+ $class->printContent( "\n\t<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":predicate ");
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":resource='" . $class->xml_escape( $statement->subject->predicate->getURI,"'" ) . "' />" );
+
+ $class->printContent( "\n\t<" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":object ");
+ if( $statement->subject->object->isa("RDFStore::Resource") ) {
+ if ( $statement->subject->object->isbNode ) {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":nodeID='" . $statement->subject->object->getLabel );
+ } else {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":resource='" . $class->xml_escape( $statement->subject->object->getURI,"'" ) );
+ };
+ $class->printContent( "' />");
+ } else {
+ $class->printContent( "xml:lang='" . $statement->subject->object->getLang . "'")
+ if($statement->subject->object->getLang);
+ if($statement->subject->object->getParseType) {
+ $class->printContent( " " . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":parseType='Literal'>");
+ $class->printContent( $statement->subject->object->getLabel );
+ } else {
+ $class->printContent( " rdf:datatype='" . $statement->subject->object->getDataType . "'")
+ if($statement->subject->object->getDataType);
+ $class->printContent( ">" . $class->xml_escape( $statement->subject->object->getLabel ) );
+ };
+ $class->printContent( "</" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":object>");
+ };
+ };
+
+ while ( my $st = $itr->each ) {
+ $class->printContent( "\n\t<" . $class->getNamespacePrefix( $st->predicate->getNamespace ).":".$st->predicate->getLocalName );
+ $class->printContent( " " . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":ID='" . $st->getURI . "'" )
+ if( $st->isReified );
+
+ if( $st->object->isa("RDFStore::Resource") ) {
+ $class->printContent( " " );
+ if ( $st->object->isbNode ) {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":nodeID='" . $st->object->getLabel );
+ } else {
+ $class->printContent( $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":resource='" . $class->xml_escape( $st->object->getURI,"'" ) );
+ };
+ $class->printContent( "' />" );
+ } else {
+ $class->printContent( " xml:lang='" . $st->object->getLang . "'")
+ if($st->object->getLang);
+ if($st->object->getParseType) {
+ $class->printContent( " " . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":parseType='Literal'>");
+ $class->printContent( $st->object->getLabel );
+ } else {
+ $class->printContent( " rdf:datatype='" . $st->object->getDataType . "'")
+ if($st->object->getDataType);
+ $class->printContent( ">" . $class->xml_escape( $st->object->getLabel ) );
+ };
+ $class->printContent( "</" . $class->getNamespacePrefix( $st->predicate->getNamespace ) .":".$st->predicate->getLocalName . ">" );
+ };
+ };
+ $class->printContent( "\n</" . $class->getNamespacePrefix($RDFStore::Vocabulary::RDF::_Namespace) . ":Description>" );
+
+ if ($statement->subject->isa("RDFStore::Statement")) {
+ if ( ($statement->subject->subject->isa("RDFStore::Statement")) &&
+ (! $model->contains( $statement->subject->subject) ) ) {
+ $class->_processDescription( $model, $statement->subject );
+ };
+ if ( ($statement->subject->object->isa("RDFStore::Statement")) &&
+ (! $model->contains( $statement->subject->object) ) ) {
+ $class->_processDescription( $model, $statement->object );
+ };
+ };
+ };
+
+1;
+};
+
+__END__
+
+=head1 NAME
+
+RDFStore::Serilizer::RDFXML - Serialise a model/graph to W3C RDF/XML syntax
+
+=head1 SYNOPSIS
+
+ use RDFStore::Serializer::RDFXML;
+
+ my $model= new RDFStore::Model();
+ $model->add($statement);
+ $model->add($statement1);
+ $model->add($statement2);
+
+ my $serializer = new RDFStore::Serializer::RDFXML;
+
+ my $rdf_strawman = $serializer->serialize( $model ); # serialise model to a string in-memory
+ my $rdf_strawman = $serializer->serialize( $model, undef, {}, $base ); # using xml:base
+ my $rdf_strawman = $serializer->serialize( $model, undef, { 'http://mynamespace.org/blaaa/' => blaa } ); # using my blaa namespace
+
+ $serializer->serialize( $model, *STREAM ); # serialise model to a given descriptor (stream)
+
+=head1 DESCRIPTION
+
+An RDFStore::Model serializer to W3C RDF/XML syntax - see http://www.w3.org/TR/rdf-syntax-grammar/
+
+=head1 CONSTRUCTORS
+
+The following methods construct RDFStore::Serializer::RDFXML :
+
+=item new ()
+
+ Create an new RDFStore::Serializer object to serialize and RDFStore::Model.
+
+=head1 METHODS
+
+=item write ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Write out the given MODEL to FILEHANDLE_REF (or in-memory string if not passed) using a given list of NAMESPACES and xml:base BASE if passed. The NAMESPACES hash ref contains a list of namespace values (URI refs) and prefix names - see RDFStore::Vocabulary::Generator(3). By default the output is returned from the method into a string otheriwse a valid (and opened) FILEHANLDE_REF can be passed, which will be being printed to.
+
+=item serialize ( MODEL [ , FILEHANDLE_REF, NAMESPACES, BASE ] )
+
+ Same as write method above.
+
+=head1 SEE ALSO
+
+ RDFStore::Model(3) RDFStore::Serializer(3)
+
+ http://www.w3.org/TR/rdf-syntax-grammar/
+
+ http://www.w3.org/TR/rdf-schema/
+
+=head1 AUTHOR
+
+ Alberto Reggiori <ar...@webweaving.org>