Commits

Toby Inkster committed 084c36d

statement() and pattern() functions. Still nowhere near usable.

  • Participants
  • Parent commits 75a0ebc

Comments (0)

Files changed (5)

lib/RDF/Shortcuts.pm

 
 BEGIN {
 	$RDF::Shortcuts::AUTHORITY = 'cpan:TOBYINK';
-}
-BEGIN {
 	$RDF::Shortcuts::VERSION   = '0.001';
 }
 
 use Exporter;
+use RDF::Trine;
 use Scalar::Util qw[blessed refaddr];
 
 our $Namespaces;
 }
 
 my @modules = qw(Parse Serialise Query Node Statement Pattern);
-our (@EXPORT, @EXPORT_OK, @EXPORT_TAGS, %GOT);
+our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS, %GOT);
+
 BEGIN
 {
 	foreach my $module (@modules)
 			push @{ $EXPORT_TAGS{$tag} }, @$names
 				if ref $names eq 'ARRAY';
 		}
+		$EXPORT_TAGS{lc "$class\_all"} = [ eval "@${class}::EXPORT_OK" ];
+		$EXPORT_TAGS{lc "$class"} = [ eval "@${class}::EXPORT" ];
+		$class->import("@${class}::EXPORT_OK");
 	}
+	$EXPORT_TAGS{'all'}      = \@EXPORT_OK;
+	$EXPORT_TAGS{'standard'} = \@EXPORT;
+	$EXPORT_TAGS{'default'}  = \@EXPORT;
 }
 
 TRUE;

lib/RDF/Shortcuts/Node.pm

 
 BEGIN {
 	$RDF::Shortcuts::Node::AUTHORITY = 'cpan:TOBYINK';
-}
-BEGIN {
 	$RDF::Shortcuts::Node::VERSION   = '0.001';
 }
 
 use base qw[Exporter];
 use Scalar::Util qw[blessed];
 
-our (@EXPORT, @EXPORT_OK, @EXPORT_TAGS, %GOT);
+our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS, %GOT);
 BEGIN
 {
 	@EXPORT    = qw[];

lib/RDF/Shortcuts/Parser.pm

+1;

lib/RDF/Shortcuts/Pattern.pm

+package RDF::Shortcuts::Pattern;
+
+use 5.010;
+use common::sense;
+use constant { FALSE => 0, TRUE => 1 };
+use utf8;
+
+BEGIN {
+	$RDF::Shortcuts::Pattern::AUTHORITY = 'cpan:TOBYINK';
+	$RDF::Shortcuts::Pattern::VERSION   = '0.001';
+}
+
+use base qw[Exporter];
+use Scalar::Util qw[blessed];
+
+our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
+BEGIN
+{
+	@EXPORT    = qw[];
+	@EXPORT_OK = qw[pattern rdf_pattern];
+}
+
+use RDF::Shortcuts::Statement qw(statement);
+
+sub pattern
+{
+	my @p;
+	if (scalar @_ == 1 and blessed($_[0]) and $_[0]->isa('RDF::Trine::Pattern'))
+	{
+		return $_[0];
+	}
+	while (my $st = shift)
+	{
+		if (blessed($st) and $st->isa('RDF::Trine::Statement'))
+		{
+			push @p, $st;
+		}
+		elsif (blessed($st) and $st->isa('RDF::Trine::Pattern'))
+		{
+			push @p, $st->triples;
+		}
+		elsif (blessed($st) and $st->isa('RDF::Trine::Model'))
+		{
+			push @p, $st->as_stream->get_all;
+		}
+		elsif (ref($st) eq 'ARRAY')
+		{
+			push @p, statement(@$st);
+		}
+	}
+	return RDF::Trine::Pattern->new(@p);
+}
+
+*rdf_pattern   = \&pattern;
+
+TRUE;#DAT
+
+__END__
+
+=over 4
+
+=item C<< pattern(@statements) >>
+
+Creates an RDF::Trine::Pattern object.
+
+Each argument should be either an RDF::Trine::Statement object or an arrayref
+of arguments for C<statement>.
+
+=item C<< rdf_pattern(@statements) >>
+
+Alias.
+
+=back

lib/RDF/Shortcuts/Statement.pm

+package RDF::Shortcuts::Statement;
+
+use 5.010;
+use common::sense;
+use constant { FALSE => 0, TRUE => 1 };
+use utf8;
+
+BEGIN {
+	$RDF::Shortcuts::Statement::AUTHORITY = 'cpan:TOBYINK';
+	$RDF::Shortcuts::Statement::VERSION   = '0.001';
+}
+
+use base qw[Exporter];
+use Scalar::Util qw[blessed];
+
+our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
+BEGIN
+{
+	@EXPORT    = qw[];
+	@EXPORT_OK = qw[statement quad triple rdf_statement rdf_quad rdf_triple];
+}
+
+use RDF::Shortcuts::Node   qw(node);
+use RDF::Shortcuts::Parser qw(rdf_parse);
+
+sub statement
+{
+	my ($s, $p, $o, $g) = @_;
+	
+	unless (defined $o or defined $g)
+	{
+		(my $ntriple, $g, $s, $p) = ($s, $p, undef, undef);
+		
+		if (blessed($ntriple) and $ntriple->isa('RDF::Trine::Statement'))
+		{
+			my ($s, $p, $o, $g2) = $ntriple->nodes;
+			return statement($s, $p, $o, ($g//$g2));
+		}
+		
+		$g = (ref $g eq 'ARRAY') ? node(@$g) : node($g);
+		$ntriple .= " ." unless $ntriple =~ /\.[\s\r\n]*(#[^\r\n]*)?$/;
+		$ntriple .= "\n";		
+		my $model = rdf_parse($ntriple, type=>'ntriples', context=>$g);
+		my $iter  = $model->get_statements(undef,undef,undef,undef);
+		if (my $st = $iter->next)
+		{
+			return $st;
+		}
+		else
+		{
+			die "Could not parse N-Triples statement";
+		}
+	}
+
+	$s = (ref $s eq 'ARRAY') ? node(@$s) : node($s);
+	$p = (ref $p eq 'ARRAY') ? node(@$p) : node($p);
+	$o = (ref $o eq 'ARRAY') ? node(@$o) : node($o);
+	$g = (ref $g eq 'ARRAY') ? node(@$g) : node($g);
+
+	return defined $g
+		? RDF::Trine::Statement->new($s, $p, $o, $g)
+		: RDF::Trine::Statement->new($s, $p, $o);
+}
+
+*rdf_statement   = \&statement;
+*rdf_triple      = \&statement;
+*rdf_quad        = \&statement;
+*triple          = \&statement;
+*quad            = \&statement;
+
+TRUE;#DAT
+
+__END__
+
+=over 4
+
+=item C<< statement($st, $g) >>
+=item C<< statement($s, $p, $o, $g) >>
+
+Creates an RDF::Trine::Statement object.
+
+C<$st> can be an N-Triples string, or an existing RDF::Trine::Statement.
+C<$s>, C<$p>, C<$o> and C<$g> can be anything that can be passed to
+C<node>.
+
+=item C<< quad >>,
+C<< triple >>, 
+C<< rdf_statement >>, 
+C<< rdf_quad >>,
+C<< rdf_triple >>
+
+Aliases.
+
+=back