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