| 1 | # | ||
| 2 | # The contents of this file are subject to the Mozilla Public | ||
| 3 | # License Version 1.1 (the "License"); you may not use this file | ||
| 4 | # except in compliance with the License. You may obtain a copy of | ||
| 5 | # the License at http://www.mozilla.org/MPL/ | ||
| 6 | # | ||
| 7 | # Software distributed under the License is distributed on an "AS | ||
| 8 | # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or | ||
| 9 | # implied. See the License for the specific language governing | ||
| 10 | # rights and limitations under the License. | ||
| 11 | # | ||
| 12 | # The Original Code is the RDF::Core module | ||
| 13 | # | ||
| 14 | # The Initial Developer of the Original Code is Ginger Alliance Ltd. | ||
| 15 | # Portions created by Ginger Alliance are | ||
| 16 | # Copyright (C) 2001 Ginger Alliance Ltd. | ||
| 17 | # All Rights Reserved. | ||
| 18 | # | ||
| 19 | # Contributor(s): | ||
| 20 | # | ||
| 21 | # Alternatively, the contents of this file may be used under the | ||
| 22 | # terms of the GNU General Public License Version 2 or later (the | ||
| 23 | # "GPL"), in which case the provisions of the GPL are applicable | ||
| 24 | # instead of those above. If you wish to allow use of your | ||
| 25 | # version of this file only under the terms of the GPL and not to | ||
| 26 | # allow others to use your version of this file under the MPL, | ||
| 27 | # indicate your decision by deleting the provisions above and | ||
| 28 | # replace them with the notice and other provisions required by | ||
| 29 | # the GPL. If you do not delete the provisions above, a recipient | ||
| 30 | # may use your version of this file under either the MPL or the | ||
| 31 | # GPL. | ||
| 32 | # | ||
| 33 | |||
| 34 | package RDF::Core::Evaluator; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | require Exporter; | ||
| 38 | |||
| 39 | use RDF::Core::Query qw(:syntax); | ||
| 40 | use RDF::Core::Constants qw(:rdf); | ||
| 41 | require RDF::Core::Function; | ||
| 42 | require RDF::Core::Statement; | ||
| 43 | require RDF::Core::Resource; | ||
| 44 | require RDF::Core::Enumerator::Memory; | ||
| 45 | use Carp; | ||
| 46 | |||
| 47 | sub new { | ||
| 48 | my ($pkg,%options) = @_; | ||
| 49 | $pkg = ref $pkg || $pkg; | ||
| 50 | my $self = {}; | ||
| 51 | $self->{_options} = \%options; | ||
| 52 | bless $self, $pkg; | ||
| 53 | } | ||
| 54 | sub getOptions { | ||
| 55 | my $self = shift; | ||
| 56 | return $self->{_options}; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub evaluate { | ||
| 61 | my ($self, $query, $substitutions) = @_; | ||
| 62 | my $rs = [[undef]]; | ||
| 63 | my $descr = {}; | ||
| 64 | $self->{_query} = $query; | ||
| 65 | $self->{_subst} = $substitutions; | ||
| 66 | $self->_namespaces($query); | ||
| 67 | my @result; | ||
| 68 | my $wantResult = $self->_prepareHandler($self->getOptions->{Row}, | ||
| 69 | sub {push @result, \@_}); | ||
| 70 | if ($self->getOptions->{TURBO}) { | ||
| 71 | $self->_prepareResultSet_new($query, $rs, $descr) | ||
| 72 | } else { | ||
| 73 | ($rs, $descr) = $self->_prepareResultSet($query); | ||
| 74 | $self->_applyConditions($rs, $descr, $query->{+Q_CONDITION}->[0]); | ||
| 75 | $self->_formatResult($rs, $descr, $query); | ||
| 76 | } | ||
| 77 | return $wantResult ? \@result : undef; | ||
| 78 | } | ||
| 79 | |||
| 80 | ############################################################ | ||
| 81 | # Result set setup | ||
| 82 | |||
| 83 | sub _prepareResultSet { | ||
| 84 | #get result set and it's description | ||
| 85 | my ($self, $query) = @_; | ||
| 86 | |||
| 87 | my $resultSets = []; | ||
| 88 | my $descriptions = []; | ||
| 89 | my $idx = 0; | ||
| 90 | my $description = $descriptions->[0] = {}; | ||
| 91 | my $rs = $resultSets->[0] = [[undef]]; | ||
| 92 | my @condSet = $self->_analyzeCondition($query->{+Q_CONDITION}->[0]); | ||
| 93 | my $wantNewSet; | ||
| 94 | foreach (@{$query->{+Q_SOURCE}->[0]->{+Q_SOURCEPATH}}) { | ||
| 95 | $wantNewSet = @$rs > 100 ? 1 : 0; | ||
| 96 | if ($wantNewSet) { | ||
| 97 | $idx++; | ||
| 98 | $description = $descriptions->[$idx] = {}; | ||
| 99 | $rs = $resultSets->[$idx] = [[undef]]; | ||
| 100 | @condSet = $self->_analyzeCondition($query->{+Q_CONDITION}->[0]); | ||
| 101 | } | ||
| 102 | |||
| 103 | #process $subject - a beginning of the path | ||
| 104 | my $subject = $self->_extractElement($_->{+Q_ELEMENT}->[0], | ||
| 105 | $_->{+Q_BINDING}->[0]); | ||
| 106 | if ($subject->[0]{type} eq Q_FUNCTION) { | ||
| 107 | $self->_funcParams($rs,$description,undef,$_->{+Q_ELEMENT}->[0]); | ||
| 108 | } | ||
| 109 | #process class info (rdf:type) | ||
| 110 | if (exists $_->{+Q_CLASS}) { | ||
| 111 | my $type = $self->getOptions->{Factory}-> | ||
| 112 | newResource(RDF_NS, 'type'); | ||
| 113 | my $class = $self->_extractElement | ||
| 114 | ($_->{+Q_CLASS}->[0]->{+Q_ELEMENT}->[0], | ||
| 115 | $_->{+Q_CLASS}->[0]->{+Q_BINDING}); | ||
| 116 | $self->_funcParams($rs,$description,undef, | ||
| 117 | $class->[0]{elementpath}) | ||
| 118 | if $class->[0]{type} eq Q_FUNCTION; | ||
| 119 | $self->_expandResult($rs, $description, 'S', $subject, | ||
| 120 | [{object=>$type}], $class); | ||
| 121 | } | ||
| 122 | |||
| 123 | #process $element - the first property found on the path | ||
| 124 | my $element; | ||
| 125 | if (exists $_->{+Q_ELEMENT}->[1] ) { | ||
| 126 | $element = $self->_extractElement($_->{+Q_ELEMENT}->[1], | ||
| 127 | $_->{+Q_BINDING}->[1]); | ||
| 128 | $self->_funcParams($rs,$description,$subject,$_->{+Q_ELEMENT}->[1]) | ||
| 129 | if $element->[0]{type} eq Q_FUNCTION; | ||
| 130 | |||
| 131 | #if the path has only one property and leads to target,process here | ||
| 132 | if ($_->{+Q_HASTARGET} && @{$_->{+Q_ELEMENT}} == 2) { | ||
| 133 | my $target; | ||
| 134 | if ($_->{+Q_TARGET}[0]{+Q_EXPRESSION}) { | ||
| 135 | $target = $self->_evalExpression(undef,undef, | ||
| 136 | $_->{+Q_TARGET}[0]); | ||
| 137 | $target = [{object=>$target}]; | ||
| 138 | } else { | ||
| 139 | $target = $self->_extractElement | ||
| 140 | ($_->{+Q_TARGET}[0],undef); | ||
| 141 | $self->_funcParams($rs,$description,undef, | ||
| 142 | $_->{+Q_TARGET}->[0]) | ||
| 143 | if $target->[0]{type} eq Q_FUNCTION; | ||
| 144 | } | ||
| 145 | $self->_expandResult($rs, $description, 'O', $subject, | ||
| 146 | $element, $target); | ||
| 147 | } else { | ||
| 148 | $self->_expandResult($rs, $description, 'O', $subject, | ||
| 149 | $element); | ||
| 150 | } | ||
| 151 | |||
| 152 | } else { | ||
| 153 | $self->_singularResult($rs, $description, $subject); | ||
| 154 | } | ||
| 155 | $self->_checkConditions ($rs, $description, \@condSet); | ||
| 156 | |||
| 157 | for (my $i = 2; $i < @{$_->{+Q_ELEMENT}} ; $i++) { | ||
| 158 | #iterate through sourcepath elements | ||
| 159 | #make "step" over the element | ||
| 160 | |||
| 161 | $element = $self->_extractElement($_->{+Q_ELEMENT}->[$i], | ||
| 162 | $_->{+Q_BINDING}->[$i]); | ||
| 163 | $self->_funcParams($rs,$description,undef,$_->{+Q_ELEMENT}->[$i]) | ||
| 164 | if $element->[0]{type} eq Q_FUNCTION; | ||
| 165 | |||
| 166 | if ($_->{+Q_HASTARGET} && @{$_->{+Q_ELEMENT}} == $i+1) { | ||
| 167 | my $target = $self-> | ||
| 168 | _extractElement($_->{+Q_TARGET}->[0], undef); | ||
| 169 | |||
| 170 | $self->_funcParams($rs,$description,undef, | ||
| 171 | $_->{+Q_TARGET}->[0]) | ||
| 172 | if $target->[0]{type} eq Q_FUNCTION; | ||
| 173 | |||
| 174 | $self->_expandResult($rs, $description, 'O', undef, $element, | ||
| 175 | $target); | ||
| 176 | } else { | ||
| 177 | $self->_expandResult($rs,$description, 'O', undef, $element); | ||
| 178 | } | ||
| 179 | $self->_checkConditions ($rs, $description, \@condSet); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | return $self->_joinResults($resultSets, $descriptions); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub _prepareResultSet_new { | ||
| 186 | #get result set and it's description | ||
| 187 | my ($self, $query, $rs, $description, $position) = @_; | ||
| 188 | return unless $self->getOptions->{TURBO}; | ||
| 189 | warn "Entering _prepareResultSet\n" if $self->getOptions->{Debug}; | ||
| 190 | |||
| 191 | #position determines what's already done in SourcePath | ||
| 192 | $position ||= {}; | ||
| 193 | #an index of path to process | ||
| 194 | $position->{path} ||= 0; | ||
| 195 | #was class processed? | ||
| 196 | $position->{class} ||= 0; | ||
| 197 | #an index of element in path to process | ||
| 198 | $position->{element} ||= 0; | ||
| 199 | |||
| 200 | #make your own copy | ||
| 201 | my %position = %$position; | ||
| 202 | |||
| 203 | my @condSet = $self->_analyzeCondition($query->{+Q_CONDITION}->[0]); | ||
| 204 | # unless defined @condSet ; | ||
| 205 | $self->_checkConditions ($rs, $description, \@condSet); | ||
| 206 | |||
| 207 | #check position, you may be finished already | ||
| 208 | my $finished; | ||
| 209 | my $path = $query->{+Q_SOURCE}[0]{+Q_SOURCEPATH}[$position{path}]; | ||
| 210 | if ($position{element} >= @{$path->{+Q_ELEMENT}}) { | ||
| 211 | #next path if possible | ||
| 212 | $position{path}++; | ||
| 213 | if ($position{path} < @{$query->{+Q_SOURCE}->[0]->{+Q_SOURCEPATH}}) { | ||
| 214 | $position{element} = 0; | ||
| 215 | $position{class} = 0; | ||
| 216 | $path = $query->{+Q_SOURCE}[0]{+Q_SOURCEPATH}[$position{path}]; | ||
| 217 | } else { | ||
| 218 | $finished = 1; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | if ($finished) { | ||
| 222 | $self->_formatResult($rs, $description, $query); | ||
| 223 | warn "Leaving _prepareResultSet\n" if $self->getOptions->{Debug}; | ||
| 224 | return; | ||
| 225 | } | ||
| 226 | |||
| 227 | #process $subject - a beginning of the path | ||
| 228 | my $subject; | ||
| 229 | if ($position{element} == 0) { | ||
| 230 | $subject = $self->_extractElement($path->{+Q_ELEMENT}->[0], | ||
| 231 | $path->{+Q_BINDING}->[0]); | ||
| 232 | if ($subject->[0]{type} eq Q_FUNCTION) { | ||
| 233 | $self->_funcParams($rs,$description,undef, | ||
| 234 | $path->{+Q_ELEMENT}->[0]); | ||
| 235 | } | ||
| 236 | $position{element}++; | ||
| 237 | } else { | ||
| 238 | $subject = undef; | ||
| 239 | } | ||
| 240 | #process class info (rdf:type) | ||
| 241 | if (exists $path->{+Q_CLASS} and !$position{class}) { | ||
| 242 | my $type = $self->getOptions->{Factory}-> | ||
| 243 | newResource(RDF_NS, 'type'); | ||
| 244 | my $class = $self->_extractElement | ||
| 245 | ($path->{+Q_CLASS}->[0]->{+Q_ELEMENT}->[0], | ||
| 246 | $path->{+Q_CLASS}->[0]->{+Q_BINDING}->[0]); | ||
| 247 | $self->_funcParams($rs,$description,undef, $class->[0]{elementpath}) | ||
| 248 | if $class->[0]{type} eq Q_FUNCTION; | ||
| 249 | $position{class} = 1; | ||
| 250 | $self->_expandResult($rs, $description, 'S', $subject, | ||
| 251 | [{object=>$type}], $class, \%position); | ||
| 252 | } else { | ||
| 253 | #process $element - the first property found on the path | ||
| 254 | my $element; | ||
| 255 | if (exists $path->{+Q_ELEMENT}->[$position{element}] ) { | ||
| 256 | $element = $self->_extractElement | ||
| 257 | ($path->{+Q_ELEMENT}->[$position{element}], | ||
| 258 | $path->{+Q_BINDING}->[$position{element}]); | ||
| 259 | $self->_funcParams($rs,$description,$subject, | ||
| 260 | $path->{+Q_ELEMENT}->[$position{element}]) | ||
| 261 | if $element->[0]{type} eq Q_FUNCTION; | ||
| 262 | $position{element}++; | ||
| 263 | |||
| 264 | #if the path has only one property and leads to target,process here | ||
| 265 | if ($path->{+Q_HASTARGET} && @{$path->{+Q_ELEMENT}} | ||
| 266 | == $position{element}) { | ||
| 267 | my $target; | ||
| 268 | if ($path->{+Q_TARGET}[0]{+Q_EXPRESSION}) { | ||
| 269 | $target = $self->_evalExpression(undef,undef, | ||
| 270 | $path->{+Q_TARGET}[0]); | ||
| 271 | $target = [{object=>$target}]; | ||
| 272 | } else { | ||
| 273 | $target = $self->_extractElement | ||
| 274 | ($path->{+Q_TARGET}[0],undef); | ||
| 275 | $self->_funcParams($rs,$description,undef, | ||
| 276 | $path->{+Q_TARGET}->[0]) | ||
| 277 | if $target->[0]{type} eq Q_FUNCTION; | ||
| 278 | } | ||
| 279 | $self->_expandResult($rs, $description, 'O', $subject, | ||
| 280 | $element, $target, \%position); | ||
| 281 | } else { | ||
| 282 | $self->_expandResult($rs, $description, 'O', $subject, | ||
| 283 | $element, undef, \%position); | ||
| 284 | } | ||
| 285 | } else { | ||
| 286 | $self->_singularResult($rs, $description, $subject, \%position); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | warn "Leaving _prepareResultSet\n" if $self->getOptions->{Debug}; | ||
| 291 | |||
| 292 | return ; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _expandResult { | ||
| 296 | my ($self, $rs, $description, $join, $roots, | ||
| 297 | $elements, $targets, $position) = @_; | ||
| 298 | my @newRS; | ||
| 299 | my %newDescr = %$description; | ||
| 300 | my $targets = $targets || [{object=>undef}]; | ||
| 301 | #iterate through partial result's rows | ||
| 302 | for (my $i = 0; $i < @$rs; $i++) { | ||
| 303 | my $subjects = $roots || [{object=>$rs->[$i][0]}]; | ||
| 304 | foreach my $root (@$subjects) { | ||
| 305 | next unless $self->_bindingPreCheck($rs->[$i], | ||
| 306 | $description,$root); | ||
| 307 | my $pushSubject; | ||
| 308 | if ($roots && $root->{type} eq Q_VARIABLE) { | ||
| 309 | $newDescr{$root->{name}} = @{$rs->[$i]}; | ||
| 310 | $pushSubject = 1; | ||
| 311 | } | ||
| 312 | if ($roots && $root->{binding}) { | ||
| 313 | croak "Multiple binding is not allowed near ".$root->{name} | ||
| 314 | if @{$root->{binding}} > 1; | ||
| 315 | if (!($description->{$root->{binding}[0]})) { | ||
| 316 | $newDescr{$root->{binding}[0]} = @{$rs->[$i]}; | ||
| 317 | $pushSubject = 1; | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | |||
| 322 | foreach my $element (@$elements) { | ||
| 323 | my $pushPredicate; | ||
| 324 | if ($element->{type} eq Q_VARIABLE && | ||
| 325 | !($description->{$element->{name}})) { | ||
| 326 | $newDescr{$element->{name}} = @{$rs->[$i]} + $pushSubject; | ||
| 327 | $pushPredicate = 1; | ||
| 328 | } | ||
| 329 | if (my $bnd = $element->{binding}[1]) { | ||
| 330 | if (!($description->{$bnd})) { | ||
| 331 | $newDescr{$bnd} = @{$rs->[$i]} + $pushSubject; | ||
| 332 | $pushPredicate = 1; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | my $pushObject; | ||
| 336 | if (my $bnd = $element->{binding}[0]) { | ||
| 337 | if (!($description->{$bnd})) { | ||
| 338 | $newDescr{$bnd} = @{$rs->[$i]} + $pushSubject + | ||
| 339 | $pushPredicate; | ||
| 340 | $pushObject = 1; | ||
| 341 | } | ||
| 342 | } | ||
| 343 | foreach my $target (@$targets) { | ||
| 344 | if ($target->{type} eq Q_VARIABLE && | ||
| 345 | !($description->{$target->{name}})) { | ||
| 346 | $newDescr{$target->{name}} = @{$rs->[$i]} + | ||
| 347 | $pushSubject + $pushPredicate; | ||
| 348 | $pushObject = 1; | ||
| 349 | } | ||
| 350 | if ($target->{binding}) { | ||
| 351 | croak "Multiple binding is not allowed near ". | ||
| 352 | $target->{name} | ||
| 353 | if @{$target->{binding}} > 1; | ||
| 354 | my $bnd = $target->{binding}[0]; | ||
| 355 | if (!($description->{$bnd})) { | ||
| 356 | $newDescr{$bnd} = @{$rs->[$i]} + | ||
| 357 | $pushSubject + $pushPredicate; | ||
| 358 | $pushObject = 1; | ||
| 359 | } | ||
| 360 | |||
| 361 | } | ||
| 362 | my $found = $self->_getStmts($rs->[$i], $description, | ||
| 363 | $root, $element, $target); | ||
| 364 | foreach my $enum (@{$found}) { | ||
| 365 | while (my $st = $enum->getNext) { | ||
| 366 | next unless $self->_bindingCheck($rs->[$i], | ||
| 367 | $description, | ||
| 368 | $root, | ||
| 369 | $st->getSubject); | ||
| 370 | next unless $self->_bindingCheck($rs->[$i], | ||
| 371 | $description, | ||
| 372 | $element, | ||
| 373 | $st->getObject); | ||
| 374 | my @row = @{$rs->[$i]}; | ||
| 375 | push @row, $st->getSubject if $pushSubject; | ||
| 376 | push @row, $st->getPredicate if $pushPredicate; | ||
| 377 | push @row, $st->getObject if $pushObject; | ||
| 378 | if ($join eq 'S') { | ||
| 379 | @row[0] = $st->getSubject; | ||
| 380 | } elsif ($join eq 'O') { | ||
| 381 | @row[0] = $st->getObject; | ||
| 382 | } else { | ||
| 383 | croak "Join point not defined\n"; | ||
| 384 | } | ||
| 385 | ########## | ||
| 386 | if ($position) { | ||
| 387 | my @subRS = [@row]; | ||
| 388 | my %subDescr = %newDescr; | ||
| 389 | $self->_prepareResultSet_new | ||
| 390 | ($self->{_query}, \@subRS, \%subDescr, | ||
| 391 | $position); | ||
| 392 | } | ||
| 393 | ########## | ||
| 394 | push @newRS, \@row; | ||
| 395 | } | ||
| 396 | $enum->close; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | } | ||
| 402 | } | ||
| 403 | %$description = %newDescr; | ||
| 404 | return @$rs = @newRS; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub _singularResult { | ||
| 408 | #get all nodes from the model | ||
| 409 | my ($self, $rs, $description, $elements, $position) = @_; | ||
| 410 | my %newDescr = %$description; | ||
| 411 | my @newRS; | ||
| 412 | |||
| 413 | my $pushElement = 0; | ||
| 414 | my $lastIndex; | ||
| 415 | if (defined $rs->[0]) { | ||
| 416 | $lastIndex = @{$rs->[0]} ; | ||
| 417 | } else { | ||
| 418 | $lastIndex = 0; | ||
| 419 | } | ||
| 420 | if ($elements->[0]{type} eq Q_VARIABLE && | ||
| 421 | !exists $description->{$elements->[0]{name}}) { | ||
| 422 | $pushElement = 1; | ||
| 423 | $newDescr{$elements->[0]{name}} = $lastIndex; | ||
| 424 | } | ||
| 425 | if (my $bnd = $elements->[0]{binding}) { | ||
| 426 | croak "Multiple binding is not allowed near ".$elements->[0]->{name} | ||
| 427 | if @{$bnd} > 1; | ||
| 428 | if (!($description->{$bnd->[0]})) { | ||
| 429 | $newDescr{$bnd->[0]} = $lastIndex; | ||
| 430 | $pushElement = 1; | ||
| 431 | } | ||
| 432 | } | ||
| 433 | for (my $i = 0; $i < @$rs; $i++) { | ||
| 434 | my %res; | ||
| 435 | my %lit; | ||
| 436 | foreach my $element (@$elements) { | ||
| 437 | #check for conflict between bound variables and values | ||
| 438 | next unless $self->_bindingPreCheck($rs->[$i], | ||
| 439 | $description,$element); | ||
| 440 | #return current result set if variable is already resolved | ||
| 441 | # - this doesn't work with binding | ||
| 442 | # return @$rs if $self->_evalVar($rs->[$i],$description, | ||
| 443 | # $element->{name}, 'RELAX'); | ||
| 444 | |||
| 445 | my $found = $self->_getStmts($rs->[$i], $description, | ||
| 446 | $element, undef, undef); | ||
| 447 | foreach my $enum (@{$found}) { | ||
| 448 | while (my $st = $enum->getNext) { | ||
| 449 | next unless $self->_bindingCheck | ||
| 450 | ($rs->[$i], $description,$element, $st->getSubject); | ||
| 451 | my @row = @{$rs->[$i]}; | ||
| 452 | unless ($res{$st->getSubject->getURI}) { | ||
| 453 | if ($pushElement) { | ||
| 454 | push @row, $st->getSubject; | ||
| 455 | } | ||
| 456 | ########## | ||
| 457 | if ($position) { | ||
| 458 | my @subRS = [@row]; | ||
| 459 | my %subDescr = %newDescr; | ||
| 460 | $self->_prepareResultSet_new | ||
| 461 | ($self->{_query}, \@subRS, \%subDescr, | ||
| 462 | $position); | ||
| 463 | } | ||
| 464 | ########## | ||
| 465 | $res{$st->getSubject->getURI} = 1; | ||
| 466 | push @newRS, \@row; | ||
| 467 | } | ||
| 468 | } | ||
| 469 | $enum->close; | ||
| 470 | } | ||
| 471 | |||
| 472 | $found = $self->_getStmts($rs->[$i], $description, | ||
| 473 | undef, $element, undef); | ||
| 474 | foreach my $enum (@{$found}) { | ||
| 475 | while (my $st = $enum->getNext) { | ||
| 476 | next unless $self->_bindingCheck | ||
| 477 | ($rs->[$i],$description,$element,$st->getPredicate); | ||
| 478 | my @row = @{$rs->[$i]}; | ||
| 479 | unless ($res{$st->getPredicate->getURI}) { | ||
| 480 | if ($pushElement) { | ||
| 481 | push @row, $st->getPredicate; | ||
| 482 | } | ||
| 483 | ########## | ||
| 484 | if ($position) { | ||
| 485 | my @subRS = [@row]; | ||
| 486 | my %subDescr = %newDescr; | ||
| 487 | $self->_prepareResultSet_new | ||
| 488 | ($self->{_query}, \@subRS, \%subDescr, | ||
| 489 | $position); | ||
| 490 | } | ||
| 491 | ########## | ||
| 492 | $res{$st->getPredicate->getURI} = 1; | ||
| 493 | push @newRS, \@row; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | $enum->close; | ||
| 497 | } | ||
| 498 | |||
| 499 | |||
| 500 | $found = $self->_getStmts($rs->[$i], $description, | ||
| 501 | undef, undef, $element); | ||
| 502 | foreach my $enum (@{$found}) { | ||
| 503 | while (my $st = $enum->getNext) { | ||
| 504 | next unless $self->_bindingCheck | ||
| 505 | ($rs->[$i],$description,$element,$st->getObject); | ||
| 506 | my @row = @{$rs->[$i]}; | ||
| 507 | if ($st->getObject->isLiteral) { | ||
| 508 | unless ($lit{$st->getObject->getValue}) { | ||
| 509 | if ($pushElement) { | ||
| 510 | push @row, $st->getObject; | ||
| 511 | } | ||
| 512 | ########## | ||
| 513 | if ($position) { | ||
| 514 | my @subRS = [@row]; | ||
| 515 | my %subDescr = %newDescr; | ||
| 516 | $self->_prepareResultSet_new | ||
| 517 | ($self->{_query}, \@subRS, \%subDescr, | ||
| 518 | $position); | ||
| 519 | } | ||
| 520 | ########## | ||
| 521 | $lit{$st->getObject->getValue} = 1; | ||
| 522 | push @newRS, \@row; | ||
| 523 | } | ||
| 524 | } else { | ||
| 525 | unless ($res{$st->getObject->getURI}) { | ||
| 526 | if ($pushElement) { | ||
| 527 | push @row, $st->getObject; | ||
| 528 | } | ||
| 529 | ########## | ||
| 530 | if ($position) { | ||
| 531 | my @subRS = [@row]; | ||
| 532 | my %subDescr = %newDescr; | ||
| 533 | $self->_prepareResultSet_new | ||
| 534 | ($self->{_query}, \@subRS, \%subDescr, | ||
| 535 | $position); | ||
| 536 | } | ||
| 537 | ########## | ||
| 538 | $res{$st->getObject->getURI} = 1; | ||
| 539 | push @newRS, \@row; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | } | ||
| 543 | $enum->close; | ||
| 544 | } | ||
| 545 | } | ||
| 546 | } | ||
| 547 | %$description = %newDescr; | ||
| 548 | return @$rs = @newRS; | ||
| 549 | } | ||
| 550 | sub _extractElement { | ||
| 551 | my ($self,$node, $binding) = @_; | ||
| 552 | my $elements = []; | ||
| 553 | my $element = {}; | ||
| 554 | |||
| 555 | ($element->{type}) = keys %$node; | ||
| 556 | if ($element->{type} eq Q_VARIABLE) { | ||
| 557 | $element->{name} = $node->{+Q_VARIABLE}->[0]->{+Q_NAME}->[0]; | ||
| 558 | $element->{object} = undef; | ||
| 559 | $element->{binding} = $self->_extractBinding($binding); | ||
| 560 | push @$elements, $element; | ||
| 561 | } elsif ($element->{type} eq Q_NODE) { | ||
| 562 | if ($node->{+Q_NODE}->[0]->{+Q_URI}) { | ||
| 563 | $element->{name} = $node->{+Q_NODE}->[0]->{+Q_URI}->[0]; | ||
| 564 | $element->{object} = $self->getOptions->{Factory}-> | ||
| 565 | newResource($element->{name}); | ||
| 566 | } elsif (my $name = $node->{+Q_NODE}->[0]->{+Q_NAME}) { | ||
| 567 | if (@$name > 1) { | ||
| 568 | my $ns = $self->getOptions->{_localNamespaces}->{$name->[0]}; | ||
| 569 | $ns = $self->getOptions->{Namespaces}->{$name->[0]} | ||
| 570 | unless defined $ns; | ||
| 571 | $element->{name} = $ns.$name->[1]; | ||
| 572 | } else { | ||
| 573 | my $ns = $self->getOptions->{_localNamespaces}->{Default}; | ||
| 574 | $ns = $self->getOptions->{Namespaces}->{Default} | ||
| 575 | unless defined $ns; | ||
| 576 | $element->{name} = $ns.$name->[0]; | ||
| 577 | } | ||
| 578 | $element->{object} = $self->getOptions->{Factory}-> | ||
| 579 | newResource($element->{name}); | ||
| 580 | } | ||
| 581 | $element->{binding} = $self->_extractBinding($binding); | ||
| 582 | push @$elements, $element; | ||
| 583 | } elsif ($element->{type} eq Q_FUNCTION) { | ||
| 584 | $element->{name} = $node->{+Q_FUNCTION}->[0]->{+Q_NAME}->[0]; | ||
| 585 | $element->{elementpath} = $node->{+Q_FUNCTION}->[0]-> | ||
| 586 | {+Q_ELEMENTPATH}; | ||
| 587 | $element->{binding} = $self->_extractBinding($binding); | ||
| 588 | push @$elements, $element; | ||
| 589 | } elsif ($element->{type} eq Q_ELEMENT) { | ||
| 590 | foreach (@{$node->{+Q_ELEMENT}}) { | ||
| 591 | my $subEls = $self->_extractElement($_, $binding); | ||
| 592 | push @$elements , @$subEls; | ||
| 593 | } | ||
| 594 | } elsif ($element->{type} eq Q_SUBSTITUTION) { | ||
| 595 | my $substName = $node->{+Q_SUBSTITUTION}->[0]->{+Q_NAME}->[0]; | ||
| 596 | $element->{object} = $self->{_subst}{$substName} || | ||
| 597 | croak "Substitution not defined for $substName.\n"; | ||
| 598 | $element->{binding} = $self->_extractBinding($binding); | ||
| 599 | $element->{type} = $element->{object}->isLiteral ? Q_LITERAL : Q_NODE; | ||
| 600 | $element->{name} = $element->{object}->getLabel; | ||
| 601 | push @$elements, $element; | ||
| 602 | } | ||
| 603 | return $elements; | ||
| 604 | } | ||
| 605 | sub _extractBinding { | ||
| 606 | my ($self, $binding) = @_; | ||
| 607 | my @retVal; | ||
| 608 | return undef unless defined $binding; | ||
| 609 | foreach (@{$binding->{+Q_VARIABLE}}) { | ||
| 610 | push @retVal, $_->{+Q_NAME}[0]; | ||
| 611 | } | ||
| 612 | return \@retVal | ||
| 613 | } | ||
| 614 | sub _bindingPreCheck { | ||
| 615 | my ($self, $row, $descr, $element) = @_; | ||
| 616 | #if element has a binding, it should conform its value | ||
| 617 | #this doesn't apply to properties | ||
| 618 | my $retVal = 1; | ||
| 619 | if ($element->{binding} && $element->{binding}[0]) { | ||
| 620 | my $bound = $self->_evalVar($row, $descr, | ||
| 621 | $element->{binding}[0], 'RELAX'); | ||
| 622 | if ($bound) { | ||
| 623 | if ($element->{type} eq Q_VARIABLE) { | ||
| 624 | my $val = $self->_evalVar($row, $descr, | ||
| 625 | $element->{name}, 'RELAX'); | ||
| 626 | $retVal = 0 unless !$val || | ||
| 627 | $bound->isLiteral == $val->isLiteral && | ||
| 628 | $bound->getLabel eq $val->getLabel; | ||
| 629 | } elsif ($element->{type} eq Q_FUNCTION) { | ||
| 630 | #do nothing here, check bindings after _getStmts | ||
| 631 | } else { | ||
| 632 | my $val =$element->{object}; | ||
| 633 | $retVal = 0 unless !defined $val || | ||
| 634 | $bound->isLiteral == $val->isLiteral && | ||
| 635 | $bound->getLabel eq $val->getLabel; | ||
| 636 | } | ||
| 637 | } | ||
| 638 | } | ||
| 639 | return $retVal; | ||
| 640 | } | ||
| 641 | sub _bindingCheck { | ||
| 642 | my ($self, $row, $descr, $element, $result) = @_; | ||
| 643 | my $retVal = 1; | ||
| 644 | if ($element->{binding} && $element->{binding}[0]) { | ||
| 645 | my $bound = $self->_evalVar($row, $descr, | ||
| 646 | $element->{binding}[0], 'RELAX'); | ||
| 647 | $retVal = 0 unless !$bound || | ||
| 648 | $result->isLiteral==$bound->isLiteral && | ||
| 649 | $result->getLabel eq $bound->getLabel; | ||
| 650 | } | ||
| 651 | return $retVal; | ||
| 652 | } | ||
| 653 | |||
| 654 | sub _funcParams { | ||
| 655 | #Find variables resolved not yet in function subject and parameters | ||
| 656 | #and resolve them. | ||
| 657 | my ($self, $rs, $descr, $subjects, $prmNode) = @_; | ||
| 658 | my @vars; | ||
| 659 | if (defined $subjects) { | ||
| 660 | foreach (@$subjects) { | ||
| 661 | if ($_->{type} eq Q_VARIABLE) { | ||
| 662 | push @vars, $_->{name} unless defined $descr->{$_->{name}}; | ||
| 663 | } | ||
| 664 | } | ||
| 665 | } | ||
| 666 | $self->_findVars($prmNode, \@vars); | ||
| 667 | foreach (@vars) { | ||
| 668 | $self->_singularResult($rs, $descr, [{name=>$_, type=>Q_VARIABLE}]) | ||
| 669 | unless defined $descr->{$_}; | ||
| 670 | } | ||
| 671 | |||
| 672 | } | ||
| 673 | |||
| 674 | sub _joinResults { | ||
| 675 | my ($self, $resultSets, $descriptions) = @_; | ||
| 676 | my $toJoin ; | ||
| 677 | my $joined = $resultSets->[0]; | ||
| 678 | my $joinedDescr = $descriptions->[0]; | ||
| 679 | for (my $i = 1; $i < @$resultSets; $i++) { | ||
| 680 | my @attachements; | ||
| 681 | my @extensions; | ||
| 682 | $toJoin = $joined; | ||
| 683 | undef $joined; | ||
| 684 | #descriptions | ||
| 685 | my $lastIndex; | ||
| 686 | if (defined $toJoin->[0]) { | ||
| 687 | $lastIndex = @{$toJoin->[0]}; | ||
| 688 | } else { | ||
| 689 | $lastIndex = 0; | ||
| 690 | } | ||
| 691 | foreach (keys %{$descriptions->[$i]}) { | ||
| 692 | if (exists $joinedDescr->{$_}) { | ||
| 693 | push @attachements, [$joinedDescr->{$_}, | ||
| 694 | $descriptions->[$i]->{$_} ]; | ||
| 695 | } else { | ||
| 696 | push @extensions, $descriptions->[$i]->{$_}; | ||
| 697 | $joinedDescr->{$_} = $lastIndex++; | ||
| 698 | } | ||
| 699 | } | ||
| 700 | |||
| 701 | #data | ||
| 702 | foreach my $rowTo (@$toJoin) { | ||
| 703 | foreach my $rowFrom (@{$resultSets->[$i]}) { | ||
| 704 | my $fit = 1; | ||
| 705 | foreach (@attachements) { | ||
| 706 | $fit = 0 unless $rowTo->[$_->[0]]->getLabel eq | ||
| 707 | $rowFrom->[$_->[1]]->getLabel; | ||
| 708 | } | ||
| 709 | if ($fit) { | ||
| 710 | my @newRow = @$rowTo; | ||
| 711 | foreach (@extensions) { | ||
| 712 | push @newRow, $rowFrom->[$_]; | ||
| 713 | } | ||
| 714 | push @$joined, \@newRow; | ||
| 715 | } | ||
| 716 | } | ||
| 717 | } | ||
| 718 | } | ||
| 719 | return ($joined, $joinedDescr); | ||
| 720 | } | ||
| 721 | |||
| 722 | sub _analyzeCondition { | ||
| 723 | my ($self, $condition) = @_; | ||
| 724 | #returns list of conjunctions that apply independently plus which | ||
| 725 | #variables are in them. Variables are sorted by name. | ||
| 726 | #The form is: ({node=>$queryNode, vars=>[name1,name2,...]},...) | ||
| 727 | |||
| 728 | my @retVal; | ||
| 729 | return @retVal unless $condition; | ||
| 730 | |||
| 731 | my $isConjunction = 0; | ||
| 732 | if (exists $condition->{+Q_CONNECTION}) { | ||
| 733 | $isConjunction = 1; | ||
| 734 | foreach (@{$condition->{+Q_CONNECTION}}) { | ||
| 735 | $isConjunction = 0 if /^or$/i; | ||
| 736 | } | ||
| 737 | |||
| 738 | } | ||
| 739 | if ($isConjunction) { | ||
| 740 | foreach (@{$condition->{+Q_CONDITION}}) { | ||
| 741 | push @retVal, $self->_analyzeCondition($_); | ||
| 742 | } | ||
| 743 | } elsif (exists $condition->{+Q_CONDITION} && | ||
| 744 | !exists $condition->{+Q_CONNECTION}) { | ||
| 745 | #a single condition, step into it | ||
| 746 | push @retVal, $self->_analyzeCondition($condition->{+Q_CONDITION}[0]); | ||
| 747 | } else { | ||
| 748 | #a match or disjunction | ||
| 749 | my @vars; | ||
| 750 | croak "Condition not defined in analyzeCondition\n" unless defined $condition; | ||
| 751 | $self->_findVars($condition , \@vars); | ||
| 752 | push @retVal,{node=>$condition, vars=>\@vars}; | ||
| 753 | } | ||
| 754 | return @retVal; | ||
| 755 | } | ||
| 756 | |||
| 757 | sub _checkConditions { | ||
| 758 | my ($self, $rs, $descr, $condSet) = @_; | ||
| 759 | #tries to apply some sub-conditions | ||
| 760 | #condSet is got from _analyzeCondition | ||
| 761 | |||
| 762 | my @newCondSet; | ||
| 763 | foreach my $cond (@$condSet) { | ||
| 764 | my $apply = 1; | ||
| 765 | foreach (@{$cond->{vars}}) { | ||
| 766 | $apply = 0 unless $descr->{$_}; | ||
| 767 | last unless $apply; | ||
| 768 | } | ||
| 769 | if ($apply) { | ||
| 770 | $self->_applyConditions($rs, $descr, $cond->{node}); | ||
| 771 | } else { | ||
| 772 | push @newCondSet, $cond; | ||
| 773 | } | ||
| 774 | |||
| 775 | } | ||
| 776 | #throw away applied conditions | ||
| 777 | @$condSet = @newCondSet; | ||
| 778 | } | ||
| 779 | |||
| 780 | ############################################################ | ||
| 781 | # Result formatting | ||
| 782 | |||
| 783 | sub _formatResult { | ||
| 784 | my ($self, $rs, $descr, $query) = @_; | ||
| 785 | |||
| 786 | foreach my $rsRow (@$rs) { | ||
| 787 | my $rows; | ||
| 788 | $rows = $self->_evalRow($rsRow, $descr,$query-> | ||
| 789 | {+Q_RESULTSET}->[0]->{+Q_ELEMENTPATH}); | ||
| 790 | foreach my $row (@$rows) { | ||
| 791 | &{$self->{_handler}}(@$row); | ||
| 792 | } | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | sub _evalRow { | ||
| 797 | # Apply element path expressions to a row (a set of variables) | ||
| 798 | # It may return one or more rows | ||
| 799 | my ($self, $row, $descr, $elementPath) = @_; | ||
| 800 | my $result = [[]]; | ||
| 801 | if (defined $elementPath) { | ||
| 802 | foreach (@$elementPath) { | ||
| 803 | my @newResult; | ||
| 804 | my $values = $self->_evalPath($row, $descr, $_); | ||
| 805 | foreach my $rw (@$result) { | ||
| 806 | foreach my $value (@$values) { | ||
| 807 | push @newResult, [@$rw, $value]; | ||
| 808 | } | ||
| 809 | push (@newResult,[@$rw, undef]) unless @$values; | ||
| 810 | } | ||
| 811 | $result = \@newResult; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | return $result; | ||
| 815 | } | ||
| 816 | |||
| 817 | ############################################################ | ||
| 818 | # Narrowing result according to conditions | ||
| 819 | sub _applyConditions { | ||
| 820 | my ($self, $rs, $descr, $condition) = @_; | ||
| 821 | my @newResult; | ||
| 822 | #my $condition = $query->{+Q_CONDITION}->[0]; | ||
| 823 | return $rs unless $condition; | ||
| 824 | foreach my $row (@$rs) { | ||
| 825 | if ($self->_evalCondition($row, $descr, $condition)) { | ||
| 826 | push @newResult, $row; | ||
| 827 | } | ||
| 828 | } | ||
| 829 | @$rs = @newResult; | ||
| 830 | return $rs; | ||
| 831 | } | ||
| 832 | |||
| 833 | sub _evalCondition { | ||
| 834 | my ($self, $row, $descr, $condition) = @_; | ||
| 835 | my $fit; | ||
| 836 | |||
| 837 | if (exists $condition->{+Q_MATCH}) { | ||
| 838 | $fit = $self->_evalMatch($row, $descr, $condition->{+Q_MATCH}->[0]); | ||
| 839 | } else { | ||
| 840 | for (my $i = 0; $i < @{$condition->{+Q_CONDITION}}; $i++) { | ||
| 841 | if (defined ( $condition->{+Q_CONNECTION}->[$i]) && | ||
| 842 | $condition->{+Q_CONNECTION}->[$i] =~ /^or$/i) { | ||
| 843 | $fit = (defined $fit ? $fit : 1) && | ||
| 844 | $self->_evalCondition($row, $descr, | ||
| 845 | $condition->{+Q_CONDITION}->[$i]); | ||
| 846 | last if $fit; | ||
| 847 | undef $fit; | ||
| 848 | } else { | ||
| 849 | $fit = (defined $fit ? $fit : 1) && | ||
| 850 | $self->_evalCondition($row, $descr, | ||
| 851 | $condition->{+Q_CONDITION}->[$i]); | ||
| 852 | } | ||
| 853 | } | ||
| 854 | } | ||
| 855 | return $fit; | ||
| 856 | } | ||
| 857 | |||
| 858 | sub _evalMatch { | ||
| 859 | my ($self, $row, $descr, $match) = @_; | ||
| 860 | my $fit; | ||
| 861 | my $set1 = $self->_evalPath($row, $descr, $match->{+Q_PATH}->[0]); | ||
| 862 | if (exists $match->{+Q_PATH}->[1]) { | ||
| 863 | my $set2 = $self->_evalPath($row, $descr, $match->{+Q_PATH}->[1]); | ||
| 864 | foreach my $val1 (@$set1) { | ||
| 865 | foreach my $val2 (@$set2) { | ||
| 866 | if (_relation($val1, $val2,$match->{+Q_RELATION}->[0] )) { | ||
| 867 | $fit = 1; | ||
| 868 | last; #in $set2 | ||
| 869 | last; #in $set1 | ||
| 870 | } | ||
| 871 | } | ||
| 872 | } | ||
| 873 | } else { | ||
| 874 | $fit = scalar @$set1; | ||
| 875 | } | ||
| 876 | return $fit || 0; | ||
| 877 | } | ||
| 878 | |||
| 879 | sub _evalPath { | ||
| 880 | my ($self, $row, $descr, $path) = @_; | ||
| 881 | my @values; | ||
| 882 | |||
| 883 | #If the path is a literal expression, return it's value | ||
| 884 | if (exists $path->{+Q_EXPRESSION}) { | ||
| 885 | my $value = $self->_evalExpression($row, $descr, | ||
| 886 | $path->{+Q_EXPRESSION}->[0]); | ||
| 887 | return [$value]; | ||
| 888 | } | ||
| 889 | |||
| 890 | #Otherwise evaluate resources ($roots) | ||
| 891 | my $roots; | ||
| 892 | my $pathAtom; | ||
| 893 | if (exists $path->{+Q_ELEMENTS}) { | ||
| 894 | $pathAtom = Q_ELEMENTS; | ||
| 895 | } elsif (exists $path->{+Q_ELEMENT}) { | ||
| 896 | $pathAtom = Q_ELEMENT; | ||
| 897 | } | ||
| 898 | $roots = $self->_extractElement($path->{$pathAtom}[0]); | ||
| 899 | my $newRoots = []; | ||
| 900 | foreach my $inst (@$roots) { | ||
| 901 | if ($inst->{type} eq Q_VARIABLE) { | ||
| 902 | $inst->{object} = $self->_evalVar($row, $descr, | ||
| 903 | $inst->{name}); | ||
| 904 | $inst->{type} = Q_NODE; | ||
| 905 | push @$newRoots, $inst; | ||
| 906 | } elsif ($inst->{type} eq Q_FUNCTION) { | ||
| 907 | my ($val) = $self->_evalFun($row, $descr,$inst->{name}, | ||
| 908 | undef, $inst->{elementpath}); | ||
| 909 | foreach (@$val) { | ||
| 910 | push @$newRoots, {object=>$_}; | ||
| 911 | } | ||
| 912 | } else { | ||
| 913 | push @$newRoots, $inst; | ||
| 914 | } | ||
| 915 | $roots = $newRoots; | ||
| 916 | } | ||
| 917 | if (exists $path->{+Q_CLASS}) { | ||
| 918 | # Check class (rdf:type) | ||
| 919 | my $passed = 0; | ||
| 920 | my $class = $path->{+Q_CLASS}->[0]; | ||
| 921 | my $type = $self->getOptions->{Factory}-> | ||
| 922 | newResource(RDF_NS, 'type'); | ||
| 923 | foreach my $inst (@$roots) { | ||
| 924 | my $classes = $self->_extractElement($class->{$pathAtom}->[0]); | ||
| 925 | foreach my $class (@$classes) { | ||
| 926 | if ($inst->{type} eq Q_VARIABLE) { | ||
| 927 | $inst->{object} = $self->_evalVar($row, $descr, | ||
| 928 | $inst->{name}); | ||
| 929 | $inst->{type} = Q_NODE; | ||
| 930 | } | ||
| 931 | my $found = $self->_getStmts($row, $descr, | ||
| 932 | $inst,{object=>$type}, $class); | ||
| 933 | foreach my $enum (@$found) { | ||
| 934 | while ($enum->getNext) { | ||
| 935 | $passed = 1; | ||
| 936 | last; | ||
| 937 | } | ||
| 938 | $enum->close; | ||
| 939 | } | ||
| 940 | } | ||
| 941 | } | ||
| 942 | return [] unless $passed; | ||
| 943 | } | ||
| 944 | my $node = $path->{$pathAtom}; | ||
| 945 | my $skipFirstElement = 1; | ||
| 946 | foreach (@$node) { | ||
| 947 | if ($skipFirstElement) { | ||
| 948 | #skip first element (it's processed already) | ||
| 949 | $skipFirstElement = 0; | ||
| 950 | next; | ||
| 951 | } | ||
| 952 | my $newRoots = []; | ||
| 953 | my $element = $self->_extractElement($_); | ||
| 954 | foreach my $subj (@$roots) { | ||
| 955 | foreach my $pred (@$element) { | ||
| 956 | my $found = $self->_getStmts($row, $descr, | ||
| 957 | $subj,$pred,undef); | ||
| 958 | foreach my $enum (@{$found}) { | ||
| 959 | while (my $st = $enum->getNext) { | ||
| 960 | my $root = {}; | ||
| 961 | $root->{object} = $st->getObject; | ||
| 962 | push (@$newRoots, $root); | ||
| 963 | } | ||
| 964 | $enum->close; | ||
| 965 | } | ||
| 966 | } | ||
| 967 | } | ||
| 968 | $roots = $newRoots; | ||
| 969 | } | ||
| 970 | |||
| 971 | my %lit; | ||
| 972 | my %res; | ||
| 973 | foreach (@$roots) { | ||
| 974 | if ($_->{object}->isLiteral) { | ||
| 975 | next if $lit{$_->{object}->getValue}; | ||
| 976 | $lit{$_->{object}->getValue} = 1; | ||
| 977 | } else { | ||
| 978 | next if $res{$_->{object}->getURI}; | ||
| 979 | $res{$_->{object}->getURI} = 1; | ||
| 980 | } | ||
| 981 | push @values, $_->{object}; | ||
| 982 | } | ||
| 983 | return \@values; | ||
| 984 | } | ||
| 985 | |||
| 986 | sub _getStmts { | ||
| 987 | my ($self, $row, $descr, $s, $p, $o) = @_; | ||
| 988 | #Returns an array of enumerators. | ||
| 989 | #Example: [$enum1,$enum2,...] | ||
| 990 | my @subjects; | ||
| 991 | my @predicates; | ||
| 992 | my @objects; | ||
| 993 | my @retValEnum; | ||
| 994 | #Evaluate variables if you can | ||
| 995 | foreach ($s,$p,$o) { | ||
| 996 | $_ = {object=>undef, type=>Q_NODE} unless defined $_; | ||
| 997 | $_->{object} = $self->_evalVar($row,$descr,$_->{name},'RELAX') | ||
| 998 | if $_->{type} eq Q_VARIABLE; | ||
| 999 | } | ||
| 1000 | if ($s->{type} eq Q_FUNCTION) { | ||
| 1001 | my ($val) = $self->_evalFun($row, $descr, $s->{name}, undef, | ||
| 1002 | $s->{elementpath}); | ||
| 1003 | push @subjects, @{$val}; | ||
| 1004 | } else { | ||
| 1005 | push @subjects, $s->{object}; | ||
| 1006 | } | ||
| 1007 | if ($o->{type} eq Q_FUNCTION) { | ||
| 1008 | my ($val) = $self->_evalFun($row, $descr, $o->{name}, undef, | ||
| 1009 | $o->{elementpath}); | ||
| 1010 | push @objects, @{$val}; | ||
| 1011 | } else { | ||
| 1012 | push @objects, $o->{object}; | ||
| 1013 | } | ||
| 1014 | if ($p->{type} eq Q_FUNCTION && (defined $s->{object} || !($s->{type} eq Q_NODE))) { | ||
| 1015 | my $fakePredicate = new RDF::Core::Resource($p->{name}); | ||
| 1016 | my @retValArray; | ||
| 1017 | foreach my $subject (@subjects) { | ||
| 1018 | my ($val,$pred) = $self->_evalFun($row, $descr, $p->{name}, | ||
| 1019 | $subject, $p->{elementpath}); | ||
| 1020 | foreach my $object (@objects) { | ||
| 1021 | for (my $i = 0; $i < @$val; $i++) { | ||
| 1022 | if (!defined $object || | ||
| 1023 | ($val->[$i]->isLiteral == $object->isLiteral && | ||
| 1024 | $val->[$i]->getLabel eq $object->getLabel)) { | ||
| 1025 | push @retValArray , new RDF::Core::Statement | ||
| 1026 | ($subject,$pred->[$i]||$fakePredicate,$val->[$i]); | ||
| 1027 | } | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | } | ||
| 1031 | push @retValEnum, new RDF::Core::Enumerator::Memory(\@retValArray) | ||
| 1032 | } else { | ||
| 1033 | if ($p->{type} eq Q_FUNCTION) { | ||
| 1034 | my ($val) = $self->_evalFun($row, $descr, $p->{name}, undef, | ||
| 1035 | $p->{elementpath}); | ||
| 1036 | push @predicates, @{$val}; | ||
| 1037 | } else { | ||
| 1038 | push @predicates, $p->{object}; | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | foreach my $subject (@subjects) { | ||
| 1042 | next if defined $subject && $subject->isLiteral; | ||
| 1043 | foreach my $predicate (@predicates) { | ||
| 1044 | next if defined $predicate && $predicate->isLiteral; | ||
| 1045 | foreach my $object (@objects) { | ||
| 1046 | push @retValEnum, $self->getOptions->{Model}-> | ||
| 1047 | getStmts($subject, $predicate, $object); | ||
| 1048 | warn "Getting \n\t", | ||
| 1049 | $subject ? $subject->getLabel : 'undef',"\n\t", | ||
| 1050 | $predicate ? $predicate->getLabel : 'undef',"\n\t", | ||
| 1051 | $object ? $object->getLabel : 'undef',"\n" | ||
| 1052 | if $self->getOptions->{Debug}; | ||
| 1053 | # warn "Got ",$self->getOptions->{Model}-> | ||
| 1054 | # countStmts($subject, $predicate, $object)," statements\n" | ||
| 1055 | # if $self->getOptions->{Debug}; | ||
| 1056 | } | ||
| 1057 | } | ||
| 1058 | } | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | return \@retValEnum; | ||
| 1062 | } | ||
| 1063 | sub _evalVar { | ||
| 1064 | my ($self, $row, $descr, $varName, $relax) = @_; | ||
| 1065 | |||
| 1066 | my $retVal = $row->[$descr->{$varName}] if defined $descr->{$varName}; | ||
| 1067 | croak "Variable not found: $varName\n" | ||
| 1068 | unless defined $retVal || $relax; | ||
| 1069 | return $retVal; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | sub _evalFun { | ||
| 1073 | my ($self, $row, $descr, $name, $subject, $elementPath) = @_; | ||
| 1074 | my @retVal; | ||
| 1075 | my @predicates; | ||
| 1076 | my $params; | ||
| 1077 | my $functions = $self->getOptions->{Functions}; | ||
| 1078 | $params = $self->_evalRow($row, $descr, $elementPath); | ||
| 1079 | foreach (@$params) { | ||
| 1080 | my $fun = $functions->getFunctions->{$name}; | ||
| 1081 | croak "Unknown function: $name" unless $fun; | ||
| 1082 | my @ret = &{$fun}($functions,$subject,$_); | ||
| 1083 | push @retVal, @{$ret[0]}; | ||
| 1084 | push @predicates, @{$ret[1]}; | ||
| 1085 | } | ||
| 1086 | |||
| 1087 | |||
| 1088 | return (\@retVal, \@predicates); | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | sub _evalExpression { | ||
| 1092 | my ($self, $row, $descr, $expression) = @_; | ||
| 1093 | my $retVal; | ||
| 1094 | if (exists $expression->{+Q_LITERAL}) { | ||
| 1095 | $retVal = $self->getOptions->{Factory}-> | ||
| 1096 | newLiteral($expression->{+Q_LITERAL}->[0]); | ||
| 1097 | } elsif (exists $expression->{+Q_SUBSTITUTION}) { | ||
| 1098 | my $substName = $expression->{+Q_SUBSTITUTION}->[0]->{+Q_NAME}->[0]; | ||
| 1099 | $retVal = $self->{_subst}{$substName} || | ||
| 1100 | croak "Substitution not defined for $substName.\n"; | ||
| 1101 | } else { | ||
| 1102 | $retVal = $self->_evalExpression($row, $descr, | ||
| 1103 | $expression->{+Q_EXPRESSION}->[0]); | ||
| 1104 | for (my $i=1; $i < @{$expression->{+Q_EXPRESSION}}; $i++) { | ||
| 1105 | #TODO: Operator precedence may be needed if more operators | ||
| 1106 | # are implemented | ||
| 1107 | my $newVal = $self->_evalExpression($row, $descr, $expression-> | ||
| 1108 | {+Q_EXPRESSION}->[$i]); | ||
| 1109 | $retVal = $self->_operation ($retVal,$newVal,$expression-> | ||
| 1110 | {+Q_OPERATION}->[$i-1]); | ||
| 1111 | |||
| 1112 | } | ||
| 1113 | |||
| 1114 | } | ||
| 1115 | return $retVal; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | ############################################################ | ||
| 1119 | # Utils | ||
| 1120 | |||
| 1121 | sub _namespaces { | ||
| 1122 | my ($self, $query) = @_; | ||
| 1123 | my $namespaces = {}; | ||
| 1124 | $self->getOptions()->{_localNamespaces} = $namespaces; | ||
| 1125 | return unless $query->{+Q_NAMESPACE} && $query->{+Q_NAMESPACE}[0]{+Q_NAME}; | ||
| 1126 | |||
| 1127 | my $query_ns = $query->{+Q_NAMESPACE}[0]; | ||
| 1128 | for (my $i = 0; $i < @{$query_ns->{+Q_NAME}}; $i++) { | ||
| 1129 | $namespaces->{$query_ns->{+Q_NAME}[$i]} = $query_ns->{+Q_URI}[$i]; | ||
| 1130 | } | ||
| 1131 | $self->getOptions()->{_localNamespaces} = $namespaces; | ||
| 1132 | } | ||
| 1133 | sub _relation { | ||
| 1134 | #Compare two nodes in graph | ||
| 1135 | my ($lval, $rval, $rel) = @_; | ||
| 1136 | my $retVal = 0; | ||
| 1137 | |||
| 1138 | return undef | ||
| 1139 | unless defined $lval && defined $rval; | ||
| 1140 | |||
| 1141 | if ($rel eq '=') { | ||
| 1142 | $retVal = 1 if $lval->isLiteral == $rval->isLiteral && | ||
| 1143 | $lval->getLabel eq $rval->getLabel; | ||
| 1144 | } elsif ($rel eq '!=') { | ||
| 1145 | $retVal = 1 if $lval->isLiteral != $rval->isLiteral || | ||
| 1146 | !($lval->getLabel eq $rval->getLabel); | ||
| 1147 | } elsif ($rel eq '<=') { | ||
| 1148 | $retVal = 1 if $lval->isLiteral && $rval->isLiteral && | ||
| 1149 | $lval->getLabel le $rval->getLabel; | ||
| 1150 | } elsif ($rel eq '<') { | ||
| 1151 | $retVal = 1 if $lval->isLiteral && $rval->isLiteral && | ||
| 1152 | $lval->getLabel lt $rval->getLabel; | ||
| 1153 | } elsif ($rel eq '>=') { | ||
| 1154 | $retVal = 1 if $lval->isLiteral && $rval->isLiteral && | ||
| 1155 | $lval->getLabel ge $rval->getLabel; | ||
| 1156 | } elsif ($rel eq '>') { | ||
| 1157 | $retVal = 1 if $lval->isLiteral && $rval->isLiteral && | ||
| 1158 | $lval->getLabel gt $rval->getLabel; | ||
| 1159 | } else { | ||
| 1160 | croak "Relation not defined: $rel\n"; | ||
| 1161 | } | ||
| 1162 | return $retVal; | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | sub _operation { | ||
| 1166 | my ($self, $lval, $rval, $op) = @_; | ||
| 1167 | my $retVal; | ||
| 1168 | |||
| 1169 | return undef | ||
| 1170 | unless defined $lval && defined $rval; | ||
| 1171 | if ($op eq '|') { | ||
| 1172 | if ($lval->isLiteral && $rval->isLiteral) { | ||
| 1173 | $retVal = $self->getOptions->{Factory}-> | ||
| 1174 | newLiteral($lval->getValue.$rval->getValue); | ||
| 1175 | } | ||
| 1176 | } else { | ||
| 1177 | croak "Operation not defined: $op\n"; | ||
| 1178 | } | ||
| 1179 | return $retVal | ||
| 1180 | } | ||
| 1181 | sub _findVars { | ||
| 1182 | # Return variable elements found in given subtree of query | ||
| 1183 | my ($self, $node, $vars) = @_; | ||
| 1184 | |||
| 1185 | if (ref $node eq 'ARRAY') { | ||
| 1186 | foreach (@$node) { | ||
| 1187 | $self->_findVars($_, $vars) if ref $_; | ||
| 1188 | } | ||
| 1189 | } elsif (ref $node eq 'HASH') { | ||
| 1190 | if (exists $node->{+Q_VARIABLE} | ||
| 1191 | && $node->{+Q_VARIABLE}[0]{+Q_NAME}[0]) { | ||
| 1192 | push @$vars, $node->{+Q_VARIABLE}[0]{+Q_NAME}[0]; | ||
| 1193 | } else { | ||
| 1194 | foreach (values %$node) { | ||
| 1195 | $self->_findVars($_, $vars) if ref $_; | ||
| 1196 | } | ||
| 1197 | } | ||
| 1198 | # if ($node->{+Q_ELEMENT}) { | ||
| 1199 | # for (my $i = 0; $i < @{$node->{+Q_ELEMENT}}; $i++) { | ||
| 1200 | # my $binding; | ||
| 1201 | # $binding = $_->{+Q_VARIABLE}->[$i]->{+Q_NAME}->[0] | ||
| 1202 | # if $_->{+Q_VARIABLE}; | ||
| 1203 | # my $element = $self->_extractElement($_->{+Q_ELEMENT}->[$i], | ||
| 1204 | # $binding); | ||
| 1205 | # foreach (@$element) { | ||
| 1206 | # #add variables to result | ||
| 1207 | # push @$vars, $_ | ||
| 1208 | # if $_->{type} eq Q_VARIABLE; | ||
| 1209 | # #add variable binding, if found | ||
| 1210 | # push @$vars, {name=>$_->{binding}, type=>Q_VARIABLE} | ||
| 1211 | # if $_->{binding}; | ||
| 1212 | # } | ||
| 1213 | # } | ||
| 1214 | # } else { | ||
| 1215 | # foreach (values %$node) { | ||
| 1216 | # $self->_findVars($_, $vars) if ref $_; | ||
| 1217 | # } | ||
| 1218 | # } | ||
| 1219 | } elsif (!defined $node) { | ||
| 1220 | croak "Can't find vars in subtree - node is not defined\n"; | ||
| 1221 | } else { | ||
| 1222 | croak "Can't find vars in subtree - $node is not a tree node\n"; | ||
| 1223 | } | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | sub _prepareHandler { | ||
| 1227 | my ($self, $handler, $default) = @_; | ||
| 1228 | my $defaulting = 0; | ||
| 1229 | if (defined $handler) { | ||
| 1230 | $self->{_handler} = $handler; | ||
| 1231 | } else { | ||
| 1232 | $self->{_handler} = $default; | ||
| 1233 | $defaulting = 1; | ||
| 1234 | } | ||
| 1235 | return $defaulting; | ||
| 1236 | } | ||
| 1237 | 1; | ||
| 1238 | __END__ |