RDF::Trine::Statement::Quad - Class for quads and quad patterns


RDF-Trine documentation Contained in the RDF-Trine distribution.

Index


Code Index:

NAME

Top

RDF::Trine::Statement::Quad - Class for quads and quad patterns

VERSION

Top

This document describes RDF::Trine::Statement::Quad version 0.135

METHODS

Top

Beyond the methods documented below, this class inherits methods from the RDF::Trine::Statement class.

new ( $s, $p, $o, $c )

Returns a new Quad structure.

nodes

Returns the subject, predicate and object of the triple pattern.

node_names

Returns the method names for accessing the nodes of this statement.

context

Returns the context node of the quad pattern.

sse

Returns the SSE string for this algebra expression.

type

Returns the type of this algebra expression.

clone
from_redland ( $statement, $name )

Given a RDF::Redland::Statement object and a graph name, returns a perl-native RDF::Trine::Statement::Quad object.

AUTHOR

Top

Gregory Todd Williams <gwilliams@cpan.org>

COPYRIGHT

Top


RDF-Trine documentation Contained in the RDF-Trine distribution.
# RDF::Trine::Statement::Quad
# -----------------------------------------------------------------------------

package RDF::Trine::Statement::Quad;

use strict;
use warnings;
no warnings 'redefine';
use base qw(RDF::Trine::Statement);

use Scalar::Util qw(blessed);

######################################################################

our ($VERSION);
BEGIN {
	$VERSION	= '0.135';
}

######################################################################

sub new {
	my $class	= shift;
	my @nodes	= @_;
	unless (scalar(@nodes) == 4) {
		throw RDF::Trine::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 ])) {
			$nodes[ $i ]	= RDF::Trine::Node::Variable->new($names[ $i ]);
		}
	}
	
	return bless( [ @nodes ], $class );
}

sub nodes {
	my $self	= shift;
	return @$self;
}

sub node_names {
	return qw(subject predicate object context);
}

sub context {
	my $self	= shift;
	if (@_) {
		$self->[3]	= shift;
	}
	return $self->[3];
}

sub sse {
	my $self	= shift;
	my $context	= shift;
	
	my @nodes	= $self->nodes;
	my @sse		= map { $_->sse( $context ) } (@nodes);
	return sprintf( '(quad %s %s %s %s)', @sse );
}

sub type {
	return 'QUAD';
}

sub clone {
	my $self	= shift;
	my $class	= ref($self);
	return $class->new( $self->nodes );
}

sub from_redland {
	my $self	= shift;
	my $rstmt	= shift;
	my $graph	= shift;
	
	my $rs		= $rstmt->subject;
	my $rp		= $rstmt->predicate;
	my $ro		= $rstmt->object;
	
	my $cast	= sub {
		my $node	= shift;
		my $type	= $node->type;
		if ($type == $RDF::Redland::Node::Type_Resource) {
			return RDF::Trine::Node::Resource->new( $node->uri->as_string );
		} elsif ($type == $RDF::Redland::Node::Type_Blank) {
			return RDF::Trine::Node::Blank->new( $node->blank_identifier );
		} elsif ($type == $RDF::Redland::Node::Type_Literal) {
			my $lang	= $node->literal_value_language;
			my $dturi	= $node->literal_datatype;
			my $dt		= ($dturi)
						? $dturi->as_string
						: undef;
			return RDF::Trine::Node::Literal->new( $node->literal_value, $lang, $dt );
		} else {
			die;
		}
	};
	
	my @nodes;
	foreach my $n ($rs, $rp, $ro) {
		push(@nodes, $cast->( $n ));
	}
	my $st	= $self->new( @nodes, $graph );
	return $st;
}




1;

__END__