/usr/share/perl5/RDF/Query/Plan/ComputedStatement.pm is in librdf-query-perl 2.918-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | # RDF::Query::Plan::ComputedStatement
# -----------------------------------------------------------------------------
=head1 NAME
RDF::Query::Plan::ComputedStatement - Executable query plan for computed triples.
=head1 VERSION
This document describes RDF::Query::Plan::ComputedStatement version 2.918.
=head1 METHODS
Beyond the methods documented below, this class inherits methods from the
L<RDF::Query::Plan> class.
=over 4
=cut
package RDF::Query::Plan::ComputedStatement;
use strict;
use warnings;
use base qw(RDF::Query::Plan);
use Log::Log4perl;
use Scalar::Util qw(blessed);
use Time::HiRes qw(gettimeofday tv_interval);
use RDF::Query::ExecutionContext;
use RDF::Query::VariableBindings;
######################################################################
our ($VERSION);
BEGIN {
$VERSION = '2.918';
}
######################################################################
=item C<< new ( @triple ) >>
=cut
sub new {
my $class = shift;
my @nodes = splice(@_, 0, 4);
my $quad = shift;
my $keys = shift || {};
my $self = $class->SUPER::new( \@nodes, $quad );
$self->[0]{logging_keys} = $keys;
my %var_to_position;
my @methodmap = qw(subject predicate object);
my %counts;
my $dup_var;
foreach my $idx (0 .. 3) {
my $node = $nodes[ $idx ];
if (blessed($node) and $node->isa('RDF::Trine::Node::Variable')) {
my $name = $node->name;
$var_to_position{ $name } = $methodmap[ $idx ];
$counts{ $name }++;
if ($counts{ $name } >= 2) {
$dup_var = $name;
}
}
}
$self->[0]{referenced_variables} = [ keys %counts ];
my @positions;
if (defined($dup_var)) {
foreach my $idx (0 .. 2) {
my $var = $nodes[ $idx ];
if (blessed($var) and $var->isa('RDF::Trine::Node::Variable')) {
my $name = $var->name;
if ($name eq $dup_var) {
push(@positions, $methodmap[ $idx ]);
}
}
}
}
$self->[0]{mappings} = \%var_to_position;
if (@positions) {
$self->[0]{dups} = \@positions;
}
return $self;
}
=item C<< execute ( $execution_context ) >>
=cut
sub execute ($) {
my $self = shift;
my $context = shift;
$self->[0]{delegate} = $context->delegate;
if ($self->state == $self->OPEN) {
throw RDF::Query::Error::ExecutionError -text => "COMPUTEDSTATEMENT plan can't be executed while already open";
}
my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
$l->trace( "executing RDF::Query::Plan::ComputedStatement" );
$self->[0]{start_time} = [gettimeofday];
my @nodes = @{ $self->[1] };
unless ($self->[2]) {
pop(@nodes);
}
my $bound = $context->bound;
if (%$bound) {
foreach my $i (0 .. $#nodes) {
next unless ($nodes[$i]->isa('RDF::Trine::Node::Variable'));
next unless (blessed($bound->{ $nodes[$i]->name }));
$nodes[ $i ] = $bound->{ $nodes[$i]->name };
}
}
$l->trace( "computed statement pattern after pre-binding: " . join(' ', map { $_->as_string } @nodes));
my $query = $context->query;
my $csg = $query->get_computed_statement_generators( $nodes[1]->uri_value );
unless (scalar(@$csg)) {
throw RDF::Query::Error::ExecutionError -text => "No computed statement generator found for predicate " . $nodes[1]->uri_value;
}
my $iter;
{
local($query->{model}) = $context->model;
$iter = $csg->[0]->( $query, $bound, @nodes );
}
if (blessed($iter)) {
$self->[0]{iter} = $iter;
$self->[0]{bound} = $bound;
$self->[0]{logger} = $context->logger;
$self->[0]{count} = 0;
$self->state( $self->OPEN );
} else {
warn "no iterator in execute()";
}
$self;
}
=item C<< next >>
=cut
sub next {
my $self = shift;
unless ($self->state == $self->OPEN) {
throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open COMPUTEDSTATEMENT";
}
my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
my $iter = $self->[0]{iter};
LOOP: while (my $row = $iter->next) {
if ($l->is_trace) {
$l->trace( "- got triple from model: " . $row->as_string );
}
if (my $pos = $self->[0]{dups}) {
$l->trace( "- checking for duplicate variables in triple" );
my @pos = @$pos;
my $first_method = shift(@pos);
my $first = $row->$first_method();
foreach my $p (@pos) {
unless ($first->equal( $row->$p() )) {
next LOOP;
}
}
}
my $binding = {};
foreach my $key (keys %{ $self->[0]{mappings} }) {
my $method = $self->[0]{mappings}{ $key };
$binding->{ $key } = $row->$method();
}
my $pre_bound = $self->[0]{bound};
my $bindings = RDF::Query::VariableBindings->new( $binding );
if ($row->can('label')) {
if (my $o = $row->label('origin')) {
$bindings->label( origin => [ $o ] );
}
}
@{ $bindings }{ keys %$pre_bound } = values %$pre_bound;
$self->[0]{count}++;
if (my $d = $self->delegate) {
$d->log_result( $self, $bindings );
}
return $bindings;
}
return;
}
=item C<< close >>
=cut
sub close {
my $self = shift;
unless ($self->state == $self->OPEN) {
throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open TRIPLE";
}
# my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
my $t0 = delete $self->[0]{start_time};
my $count = delete $self->[0]{count};
delete $self->[0]{iter};
$self->SUPER::close();
}
=item C<< nodes >>
Returns a list of the three node objects that comprise the triple pattern this plan will return.
=cut
sub nodes {
my $self = shift;
if ($self->[2]) {
return @{ $self->[1] }[0..3];
} else {
return @{ $self->[1] }[0..2];
}
}
=item C<< triple >>
Returns a RDF::Trine::Statement object representing the triple pattern this plan will return.
=cut
sub triple {
my $self = shift;
my @nodes = $self->nodes;
if ($self->[2]) {
return RDF::Trine::Statement::Quad->new( @nodes );
} else {
return RDF::Trine::Statement->new( @nodes );
}
}
=item C<< bf () >>
Returns a string representing the state of the nodes of the triple (bound or free).
=cut
sub bf {
my $self = shift;
my $context = shift;
my $bf = '';
my $bound = $context->bound;
foreach my $n (@{ $self->[1] }[0..3]) {
if ($n->isa('RDF::Trine::Node::Variable')) {
if (my $b = $bound->{ $n->name }) {
$bf .= 'b';
} else {
$bf .= 'f';
}
} else {
$bf .= 'b';
}
}
return $bf;
}
=item C<< distinct >>
Returns true if the pattern is guaranteed to return distinct results.
=cut
sub distinct {
return 0;
}
=item C<< ordered >>
Returns true if the pattern is guaranteed to return ordered results.
=cut
sub ordered {
return [];
}
=item C<< plan_node_name >>
Returns the string name of this plan node, suitable for use in serialization.
=cut
sub plan_node_name {
return 'computedstatement';
}
=item C<< plan_prototype >>
Returns a list of scalar identifiers for the type of the content (children)
nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
identifiers.
=cut
sub plan_prototype {
my $self = shift;
return qw(N N N N i);
}
=item C<< plan_node_data >>
Returns the data for this plan node that corresponds to the values described by
the signature returned by C<< plan_prototype >>.
=cut
sub plan_node_data {
my $self = shift;
return ($self->nodes);
}
=item C<< graph ( $g ) >>
=cut
sub graph {
my $self = shift;
my $g = shift;
my $label = $self->graph_labels;
$g->add_node( "$self", label => "Computed Statement" . $self->graph_labels );
my @names = qw(subject predicate object graph);
foreach my $i (0 .. 3) {
my $n = $self->[ $i + 1 ];
my $rel = $names[ $i ];
my $str = $n->sse( {}, '' );
$g->add_node( "${self}$n", label => $str );
$g->add_edge( "$self" => "${self}$n", label => $names[ $i ] );
}
return "$self";
}
1;
__END__
=back
=head1 AUTHOR
Gregory Todd Williams <gwilliams@cpan.org>
=cut
|