| RDF-Query documentation | Contained in the RDF-Query distribution. |
RDF::Query::Algebra::Quad - Algebra class for Quad patterns
This document describes RDF::Query::Algebra::Quad version 2.907.
Beyond the methods documented below, this class inherits methods from the RDF::Query::Algebra class.
new ( $s, $p, $o, $g )Returns a new Quad structure.
as_sparqlReturns the SPARQL string for this algebra expression.
as_hashReturns the query as a nested set of plain data structures (no objects).
referenced_blanksReturns a list of the blank node names used in this algebra expression.
qualify_uris ( \%namespaces, $base_uri )Returns a new algebra pattern where all referenced Resource nodes representing QNames (ns:local) are qualified using the supplied %namespaces.
bf ()Returns a string representing the state of the nodes of the triple (bound or free).
distinguish_bnode_variablesReturns a new Quad object with blank nodes replaced by distinguished variables.
label ( $label => $value )Sets the named $label to $value for this quad object.
If no $value is given, returns the current label value, or undef if none
exists.
Gregory Todd Williams <gwilliams@cpan.org>
| RDF-Query documentation | Contained in the RDF-Query distribution. |
# RDF::Query::Algebra::Quad # -----------------------------------------------------------------------------
package RDF::Query::Algebra::Quad; use strict; use warnings; no warnings 'redefine'; use base qw(RDF::Query::Algebra RDF::Trine::Statement::Quad); use Data::Dumper; use Carp qw(carp croak confess); use Scalar::Util qw(blessed reftype refaddr); use RDF::Trine::Iterator qw(smap sgrep swatch); ###################################################################### my %QUAD_LABELS; our ($VERSION); BEGIN { $VERSION = '2.907'; } ######################################################################
sub new { my $class = shift; my @nodes = @_; unless (scalar(@nodes) == 4) { throw RDF::Query::Error::MethodInvocationError -text => "Quad constructor must have four node arguments"; } my @names = qw(subject predicate object context); foreach my $i (0 .. 3) { unless (defined($nodes[ $i ]) and blessed($nodes[ $i ])) { $nodes[ $i ] = RDF::Query::Node::Variable->new($names[ $i ]); } unless ($nodes[ $i ]->isa('RDF::Query::Node')) { $nodes[ $i ] = RDF::Query::Node->from_trine( $nodes[ $i ] ); } } return $class->SUPER::new( @nodes ); }
sub as_sparql { my $self = shift; my $context = shift || {}; my $indent = shift; my $pred = $self->predicate; if ($pred->isa('RDF::Trine::Node::Resource') and $pred->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') { $pred = 'a'; } else { $pred = $pred->as_sparql( $context ); } my $string = sprintf( "%s %s %s .", $self->subject->as_sparql( $context ), $pred, $self->object->as_sparql( $context ), ); return $string; }
sub as_hash { my $self = shift; my $context = shift; return { type => lc($self->type), nodes => [ map { $_->as_hash } $self->nodes ], }; }
sub referenced_blanks { my $self = shift; my @nodes = $self->nodes; my @blanks = grep { $_->isa('RDF::Trine::Node::Blank') } @nodes; return map { $_->blank_identifier } @blanks; }
sub qualify_uris { my $self = shift; my $class = ref($self); my $ns = shift; my $base_uri = shift; my @nodes; foreach my $n ($self->nodes) { my $blessed = blessed($n); if ($blessed and $n->isa('RDF::Query::Node::Resource')) { my $uri = $n->uri; if (ref($uri)) { my ($n,$l) = @$uri; unless (exists($ns->{ $n })) { throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined"; } my $resolved = RDF::Query::Node::Resource->new( join('', $ns->{ $n }, $l), $base_uri ); push(@nodes, $resolved); } else { push(@nodes, $n); } } elsif ($blessed and $n->isa('RDF::Query::Node::Literal')) { my $node = $n; my $dt = $node->literal_datatype; if (ref($dt)) { my ($n,$l) = @$dt; unless (exists($ns->{ $n })) { throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined"; } my $resolved = RDF::Query::Node::Resource->new( join('', $ns->{ $n }, $l), $base_uri ); my $lit = RDF::Query::Node::Literal->new( $node->literal_value, undef, $resolved->uri_value ); push(@nodes, $lit); } else { push(@nodes, $node); } } else { push(@nodes, $n); } } return $class->new( @nodes ); }
sub bf { my $self = shift; my $bf = ''; foreach my $n ($self->nodes) { $bf .= ($n->isa('RDF::Query::Node::Variable')) ? 'f' : 'b'; } return $bf; }
sub distinguish_bnode_variables { my $self = shift; my $class = ref($self); my @nodes = $self->nodes; foreach my $i (0 .. $#nodes) { if ($nodes[$i]->isa('RDF::Query::Node::Blank')) { $nodes[$i] = $nodes[$i]->make_distinguished_variable; } } return $class->new( @nodes ); }
sub label { my $self = shift; my $addr = refaddr($self); my $label = shift; if (@_) { my $value = shift; $QUAD_LABELS{ $addr }{ $label } = $value; } if (exists $QUAD_LABELS{ $addr }) { return $QUAD_LABELS{ $addr }{ $label }; } else { return; } } sub DESTROY { my $self = shift; my $addr = refaddr( $self ); delete $QUAD_LABELS{ $addr }; } 1; __END__