RDF::Query::Plan::Extend - Executable query plan for Extends.


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

Index


Code Index:

NAME

Top

RDF::Query::Plan::Extend - Executable query plan for Extends.

VERSION

Top

This document describes RDF::Query::Plan::Extend version 2.907.

METHODS

Top

Beyond the methods documented below, this class inherits methods from the RDF::Query::Plan class.

new ( $plan, \@keys )
execute ( $execution_context )
next
close
pattern

Returns the query plan that will be used to produce the data to be extended.

distinct

Returns true if the pattern is guaranteed to return distinct results.

ordered

Returns true if the pattern is guaranteed to return ordered results.

plan_node_name

Returns the string name of this plan node, suitable for use in serialization.

plan_prototype

Returns a list of scalar identifiers for the type of the content (children) nodes of this plan node. See RDF::Query::Plan for a list of the allowable identifiers.

plan_node_data

Returns the data for this plan node that corresponds to the values described by the signature returned by plan_prototype.

graph ( $g )

AUTHOR

Top

 Gregory Todd Williams <gwilliams@cpan.org>


RDF-Query documentation Contained in the RDF-Query distribution.
# RDF::Query::Plan::Extend
# -----------------------------------------------------------------------------

package RDF::Query::Plan::Extend;

use strict;
use warnings;
use base qw(RDF::Query::Plan);
use RDF::Query::Error qw(:try);
use Scalar::Util qw(blessed);

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

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

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

sub new {
	my $class	= shift;
	my $plan	= shift;
	my $keys	= shift;
	my (@vars, @exprs);
	foreach my $k (@$keys) {
		push(@exprs, $k) if ($k->isa('RDF::Query::Expression'));
		push(@vars, $k->name) if ($k->isa('RDF::Query::Node::Variable'));
		push(@vars, $k) if (not(ref($k)));
	}
	my $self	= $class->SUPER::new( $plan, \@vars, \@exprs );
	$self->[0]{referenced_variables}	= [ $plan->referenced_variables ];
	return $self;
}

sub execute ($) {
	my $self	= shift;
	my $context	= shift;
	my $l		= Log::Log4perl->get_logger("rdf.query.plan.extend");
	$l->trace( "executing extend plan: " . $self->sse );
	if ($self->state == $self->OPEN) {
		throw RDF::Query::Error::ExecutionError -text => "EXTEND plan can't be executed while already open";
	}
	my $plan	= $self->[1];
	$plan->execute( $context );
	
	if ($plan->state == $self->OPEN) {
		$self->[0]{context}	= $context;
		$self->state( $self->OPEN );
	} else {
		warn "could not execute plan in PROJECT";
	}
	$self;
}

sub next {
	my $self	= shift;
	my $ctx		= $self->[0]{context};
	unless ($self->state == $self->OPEN) {
		throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open PROJECT";
	}
	
	my $l		= Log::Log4perl->get_logger("rdf.query.plan.extend");
	my $plan	= $self->[1];
	while (1) {
		my $row		= $plan->next;
		unless (defined($row)) {
			$l->trace("no remaining rows in extend");
			if ($self->[1]->state == $self->[1]->OPEN) {
				$self->[1]->close();
			}
			return;
		}
		if ($l->is_trace) {
			$l->trace( "extend on row $row" );
		}
		
		my $keys	= $self->[2];
		my $exprs	= $self->[3];
		my $query	= $self->[0]{context}->query;
		
		local($query->{_query_row_cache})	= {};
#		my $proj	= $row->project( @{ $keys } );
		my $ok	= 1;
		try {
			foreach my $e (@$exprs) {
				my $name			= $e->name;
				my $var_or_expr	= $e->expression;
				if ($l->is_trace) {
					$l->trace( "- extend alias " . $var_or_expr->sse . " -> $name" );
				}
				my $value		= $query->var_or_expr_value( $row, $var_or_expr, $ctx );
				if ($l->is_trace) {
					$l->trace( "- extend value $name -> $value" );
				}
				$row->{ $name }	= $value;
			}
		} catch RDF::Query::Error with {
			$l->trace( "- evaluating extend expression resulted in an error; dropping the variable binding" );
		} otherwise {
			$ok	= 0;
		};
		next unless ($ok);
		$l->trace( "Extended result: $row" );
		return $row;
	}
}

sub close {
	my $self	= shift;
	unless ($self->state == $self->OPEN) {
		throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open PROJECT";
	}
	delete $self->[0]{context};
	if (blessed($self->[1]) and $self->[1]->state == $self->OPEN) {
		$self->[1]->close();
	}
	$self->SUPER::close();
}

sub pattern {
	my $self	= shift;
	return $self->[1];
}

sub distinct {
	my $self	= shift;
	return $self->pattern->distinct;
}

sub ordered {
	my $self	= shift;
	return $self->pattern->ordered;
}

sub plan_node_name {
	return 'extend';
}

sub plan_prototype {
	my $self	= shift;
	return qw(\J P);
}

sub plan_node_data {
	my $self	= shift;
	my @vars	= map { RDF::Query::Node::Variable->new( $_ ) } @{$self->[2]};
	my @exprs	= @{$self->[3]};
	return ([ @vars, @exprs ], $self->pattern);
}

sub graph {
	my $self	= shift;
	my $g		= shift;
	my $c		= $self->pattern->graph( $g );
	my $expr	= join(' ', @{$self->[2]}, map { blessed($_) ? $_->sse( {}, "" ) : $_ } @{$self->[3]});
	$g->add_node( "$self", label => "Extend ($expr)" . $self->graph_labels );
	$g->add_edge( "$self", $c );
	return "$self";
}



1;

__END__