Query/Expression/Binary.pm

filter: Statementstmt 90.4% 104 / 115 Branchbran 76.7% 43 / 56 Conditioncond 44.7% 17 / 38 MC/DCmcdc 14.2% 3 / 21 Subroutinesub 100% 14 / 14 Podpod 100% 4 / 4 totaltotal 74.6% 185 / 248 CC 43 SCAR 40.9
CC
43
Cov
78%
evaluate
81.0
promote_type
2.0
as_sparql
2.0
CRAP 59.6
« Query/BGPOptimizer.pm ↑ Summary Query/Plan/Service.pm »
1 # RDF::Query::Expression::Binary
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Expression::Binary - Algebra class for binary expressions
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Expression::Binary version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Expression::Binary;
15  
16 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use base qw(RDF::Query::Expression);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ######################################################################
27  
28 36 our ($VERSION);
29 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
30 36 $VERSION = '2.919';
31 }
32  
33 ######################################################################
34  
35 =head1 METHODS
36  
37 Beyond the methods documented below, this class inherits methods from the
38 L<RDF::Query::Expression> class.
39  
40 =over 4
41  
42 =cut
43  
44 =item C<< sse >>
45  
46 Returns the SSE string for this algebra expression.
47  
48 =cut
49  
50 60 ▶sub sse {
Subroutine sse: called CC=1 SCAR=0.0
Pod documented
51 60 my $self = shift;
52 60 my $context = shift;
53
54 return sprintf(
55 '(%s %s %s)',
56 $self->op,
57 60 map { $_->sse( $context ) } $self->operands,
58 );
59 }
60  
61 =item C<< as_sparql >>
62  
63 Returns the SPARQL string for this algebra expression.
64  
65 =cut
66  
67 3 ▶sub as_sparql {
Subroutine as_sparql: called CC=2 SCAR=6.9
Pod documented
68 3 my $self = shift;
69 3 my $context = shift;
70 3 my $indent = shift;
71 3 my $op = $self->op;
72 3 ▶ $op = '=' if ($op eq '==');
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "==" 1 2 3
73 3 return sprintf("(%s $op %s)", map { $_->as_sparql( $context, $indent ) } $self->operands);
74 }
75  
76 =item C<< evaluate ( $query, \%bound ) >>
77  
78 Evaluates the expression using the supplied bound variables.
79 Will return a RDF::Query::Node object.
80  
81 =cut
82  
83 91 ▶sub evaluate {
Subroutine evaluate: called CC=41 SCAR=43.9
Pod documented
84 91 my $self = shift;
85 91 my $query = shift;
86 91 my $bound = shift;
87 91 my $l = Log::Log4perl->get_logger("rdf.query.expression.binary");
88 91 my $op = $self->op;
89 91 my @operands = $self->operands;
90 my ($lhs, $rhs) = map {
91 91 ▶ throw RDF::Query::Error::ExecutionError ( -text => "error in evaluating operands to binary $op" )
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_) 1 181 182
92 unless (blessed($_));
93 $_->isa('RDF::Query::Algebra')
94 ? $_->evaluate( $query, $bound, @_ )
95 : ($_->isa('RDF::Trine::Node::Variable'))
96 181 ▶ ? $bound->{ $_->name }
Branch: 2 branches
BranchTrueFalseTotal
$_->isa('RDF::Query::Algebra') ? : 58 123 181
$_->isa('RDF::Trine::Node::Variable') ? : 12 111 123
97 : $_
98 } @operands;
99
100 90 $l->debug("Binary Operator '$op': " . Dumper($lhs, $rhs));
101
102 ### This does overloading of infix<+> on literal values to perform string concatenation
103 # if ($op eq '+') {
104 # if (blessed($lhs) and $lhs->isa('RDF::Query::Node::Literal') and blessed($rhs) and $rhs->isa('RDF::Query::Node::Literal')) {
105 # if (not($lhs->has_datatype) and not($rhs->has_datatype)) {
106 # my $value = $lhs->literal_value . $rhs->literal_value;
107 # return RDF::Query::Node::Literal->new( $value );
108 # }
109 # }
110 # }
111
112 90 ▶ if ($op =~ m#^[-+/*]$#) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ m[^[-+/*]$]) { } 29 61 90
elsif ($op =~ /^([<>]=?)|!?=$/) { } 61 0 61
113 29 ▶ if (blessed($lhs) and blessed($rhs) and $lhs->isa('RDF::Query::Node::Literal') and $rhs->isa('RDF::Query::Node::Literal') and $lhs->is_numeric_type and $rhs->is_numeric_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa('RDF::Query::Node::Literal') and $rhs->isa('RDF::Query::Node::Literal') and $lhs->is_numeric_type and $rhs->is_numeric_type) { } 29 0 29
Condition33%5/15
builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa("RDF::Query::Node::Literal") and $rhs->isa("RDF::Query::Node::Literal") and $lhs->is_numeric_type and $rhs->is_numeric_type
!ll&&!rl&&r
0029
builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa("RDF::Query::Node::Literal") and $rhs->isa("RDF::Query::Node::Literal") and $lhs->is_numeric_type
!ll&&!rl&&r
0029
builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa("RDF::Query::Node::Literal") and $rhs->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
0029
builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
0029
builtin::blessed($lhs) and builtin::blessed($rhs)
!ll&&!rl&&r
0029
MC/DC0%0/6
builtin::blessed($lhs) and builtin::blessed($rhs) and $lhs->isa("RDF::Query::Node::Literal") and $rhs->isa("RDF::Query::Node::Literal") and $lhs->is_numeric_type and $rhs->is_numeric_type
builtin::blessed($lhs) builtin::blessed($rhs) $lhs->isa("RDF::Query::Node::Literal") $rhs->isa("RDF::Query::Node::Literal") $lhs->is_numeric_type $rhs->is_numeric_type
Truth table14%1/7
A and B and C and D and E and F
A= builtin::blessed($lhs)B= builtin::blessed($rhs)C= $lhs->isa("RDF::Query::Node::Literal")D= $rhs->isa("RDF::Query::Node::Literal")E= $lhs->is_numeric_typeF= $rhs->is_numeric_type
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
114 29 my $type = $self->promote_type( $op, $lhs->literal_datatype, $rhs->literal_datatype );
115 29 my $value;
116 29 ▶ if ($op eq '+') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '+') { } 4 25 29
elsif ($op eq '-') { } 3 22 25
elsif ($op eq '*') { } 20 2 22
elsif ($op eq '/') { } 2 0 2
117 4 my $lhsv = $lhs->numeric_value;
118 4 my $rhsv = $rhs->numeric_value;
119 4 ▶ if (defined($lhsv) and defined($rhsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lhsv and defined $rhsv) { } 4 0 4
Condition33%1/3
defined $lhsv and defined $rhsv
!ll&&!rl&&r
004
MC/DC0%0/2
defined $lhsv and defined $rhsv
defined $lhsv defined $rhsv
Truth table33%1/3
A and B
A= defined $lhsvB= defined $rhsv
ABresult
0X0
100
111
120 4 $value = $lhsv + $rhsv;
121 } else {
122 0 throw RDF::Query::Error::ComparisonError -text => "Cannot evaluate infix:<+> on non-numeric types";
123 }
124 } elsif ($op eq '-') {
125 3 my $lhsv = $lhs->numeric_value;
126 3 my $rhsv = $rhs->numeric_value;
127 3 ▶ if (defined($lhsv) and defined($rhsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lhsv and defined $rhsv) { } 3 0 3
Condition33%1/3
defined $lhsv and defined $rhsv
!ll&&!rl&&r
003
MC/DC0%0/2
defined $lhsv and defined $rhsv
defined $lhsv defined $rhsv
Truth table33%1/3
A and B
A= defined $lhsvB= defined $rhsv
ABresult
0X0
100
111
128 3 $value = $lhsv - $rhsv;
129 } else {
130 0 throw RDF::Query::Error::ComparisonError -text => "Cannot evaluate infix:<-> on non-numeric types";
131 }
132 } elsif ($op eq '*') {
133 20 my $lhsv = $lhs->numeric_value;
134 20 my $rhsv = $rhs->numeric_value;
135 20 ▶ if (defined($lhsv) and defined($rhsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lhsv and defined $rhsv) { } 20 0 20
Condition33%1/3
defined $lhsv and defined $rhsv
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $lhsv and defined $rhsv
defined $lhsv defined $rhsv
Truth table33%1/3
A and B
A= defined $lhsvB= defined $rhsv
ABresult
0X0
100
111
136 20 $value = $lhsv * $rhsv;
137 } else {
138 0 throw RDF::Query::Error::ComparisonError -text => "Cannot evaluate infix:<*> on non-numeric types";
139 }
140 } elsif ($op eq '/') {
141 2 my $lhsv = $lhs->numeric_value;
142 2 my $rhsv = $rhs->numeric_value;
143
144 2 my ($lt, $rt) = ($lhs->literal_datatype, $rhs->literal_datatype);
145 2 ▶ if ($lt eq $rt and $lt eq 'http://www.w3.org/2001/XMLSchema#integer') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lt eq $rt and $lt eq "http://www.w3.org/2001/XMLSchema#integer") 1 1 2
Condition66%2/3
$lt eq $rt and $lt eq "http://www.w3.org/2001/XMLSchema#integer"
!ll&&!rl&&r
101
MC/DC50%1/2
$lt eq $rt and $lt eq "http://www.w3.org/2001/XMLSchema#integer"
$lt eq $rt $lt eq "http://www.w3.org/2001/XMLSchema#integer"
Truth table66%2/3
A and B
A= $lt eq $rtB= $lt eq "http://www.w3.org/2001/XMLSchema#integer"
ABresult
0X0
100
111
146 1 $type = 'http://www.w3.org/2001/XMLSchema#decimal';
147 }
148
149 2 ▶ if (defined($lhsv) and defined($rhsv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lhsv and defined $rhsv) { } 2 0 2
Condition33%1/3
defined $lhsv and defined $rhsv
!ll&&!rl&&r
002
MC/DC0%0/2
defined $lhsv and defined $rhsv
defined $lhsv defined $rhsv
Truth table33%1/3
A and B
A= defined $lhsvB= defined $rhsv
ABresult
0X0
100
111
150 2 ▶ if ($rhsv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rhsv == 0) 1 1 2
151 1 throw RDF::Query::Error::FilterEvaluationError -text => "Illegal division by zero";
152 }
153 1 $value = $lhsv / $rhsv;
154 } else {
155 0 throw RDF::Query::Error::ComparisonError -text => "Cannot evaluate infix:</> on non-numeric types";
156 }
157 } else {
158 0 throw RDF::Query::Error::ExecutionError -text => "Unrecognized binary operator '$op'";
159 }
160 28 return RDF::Query::Node::Literal->new( $value, undef, $type, 1 );
161 } else {
162 0 throw RDF::Query::Error::ExecutionError -text => "Numeric binary operator '$op' with non-numeric data";
163 }
164 } elsif ($op =~ m#^([<>]=?)|!?=$#) {
165 61 my @types = qw(RDF::Query::Node::Literal RDF::Query::Node::Resource RDF::Query::Node::Blank);
166
167 61 ▶ if ($op =~ /[<>]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op =~ /[<>]/) 25 36 61
168 # if it's a relational operation other than equality testing,
169 # the two nodes must be of the same type.
170 25 my $ok = 0;
171 25 foreach my $type (@types) {
172 75 ▶ $ok ||= 1 if ($lhs->isa($type) and $rhs->isa($type));
Branch: 1 branch
BranchTrueFalseTotal
if $lhs->isa($type) and $rhs->isa($type) 23 52 75
Condition80%4/5
$lhs->isa($type) and $rhs->isa($type)
!ll&&!rl&&r
50223
$ok ||= 1
l!l
023
MC/DC100%2/2
$lhs->isa($type) and $rhs->isa($type)
$lhs->isa($type) $rhs->isa($type)
MC/DC0%0/1
$ok ||= 1
$ok
Truth table100%3/3
A and B
A= $lhs->isa($type)B= $rhs->isa($type)
ABresult
0X0
100
111
Truth table50%1/2
A
A= $ok
Aresult
11
00
173 }
174 25 ▶ if (not($ok) and not($RDF::Query::Node::Literal::LAZY_COMPARISONS)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok or $RDF::Query::Node::Literal::LAZY_COMPARISONS) 0 25 25
Condition66%2/3
$ok or $RDF::Query::Node::Literal::LAZY_COMPARISONS
l!l&&r!l&&!r
2320
MC/DC0%0/2
$ok or $RDF::Query::Node::Literal::LAZY_COMPARISONS
$ok $RDF::Query::Node::Literal::LAZY_COMPARISONS
Truth table66%2/3
A or B
A= $okB= $RDF::Query::Node::Literal::LAZY_COMPARISONS
ABresult
1X1
011
000
175 0 throw RDF::Query::Error::TypeError -text => "Attempt to compare two nodes of different types.";
176 }
177 }
178
179 61 my $bool;
180 61 ▶ if ($op eq '<') {
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '<') { } 14 47 61
elsif ($op eq '<=') { } 1 46 47
elsif ($op eq '>') { } 10 36 46
elsif ($op eq '>=') { } 0 36 36
elsif ($op eq '==') { } 36 0 36
elsif ($op eq '!=') { } 0 0 0
181 14 $bool = ($lhs < $rhs);
182 } elsif ($op eq '<=') {
183 1 $bool = ($lhs <= $rhs);
184 } elsif ($op eq '>') {
185 10 $bool = ($lhs > $rhs);
186 } elsif ($op eq '>=') {
187 0 $bool = ($lhs >= $rhs);
188 } elsif ($op eq '==') {
189 36 $bool = ($lhs == $rhs);
190 } elsif ($op eq '!=') {
191 0 $bool = ($lhs != $rhs);
192 } else {
193 0 throw RDF::Query::Error::ExecutionError -text => "Unrecognized binary operator '$op'";
194 }
195
196 61 ▶ my $value = ($bool) ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 37 24 61
197 61 $l->debug("-> $value");
198 61 return RDF::Query::Node::Literal->new( $value, undef, 'http://www.w3.org/2001/XMLSchema#boolean' );
199 } else {
200 0 $l->logdie("Unknown operator: $op");
201 }
202 }
203  
204 36 my $xsd = 'http://www.w3.org/2001/XMLSchema#';
205 36 my %integer_types = map { join('', $xsd, $_) => 1 } qw(nonPositiveInteger nonNegativeInteger positiveInteger negativeInteger short unsignedShort byte unsignedByte long unsignedLong);
206 36 my %rel = (
207 "${xsd}integer" => 0,
208 "${xsd}int" => 1,
209 "${xsd}unsignedInt" => 2,
210 "${xsd}nonPositiveInteger" => 3,
211 "${xsd}nonNegativeInteger" => 4,
212 "${xsd}positiveInteger" => 5,
213 "${xsd}negativeInteger" => 6,
214 "${xsd}short" => 7,
215 "${xsd}unsignedShort" => 8,
216 "${xsd}byte" => 9,
217 "${xsd}unsignedByte" => 10,
218 "${xsd}long" => 11,
219 "${xsd}unsignedLong" => 12,
220 "${xsd}decimal" => 13,
221 "${xsd}float" => 14,
222 "${xsd}double" => 15,
223 );
224  
225 =item C<< promote_type ( $op, $lhs_datatype, $rhs_datatype ) >>
226  
227 Returns the XSD type URI (as a string) for the resulting value of performing the
228 supplied operation on arguments of the indicated XSD types.
229  
230 =cut
231  
232 36 ▶sub promote_type {
Subroutine promote_type: called CC=2 SCAR=6.9
Pod documented
233 36 my $self = shift;
234 36 my $op = shift;
235 36 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
236 36 my @types = sort { $rel{$b} <=> $rel{$a} } @_;
237
238 36 my $type = $types[0];
239 36 ▶ $type = "${xsd}integer" if ($integer_types{ $type });
Branch: 1 branch
BranchTrueFalseTotal
if $integer_types{$type} 0 36 36
240 36 return $type;
241 }
242  
243 36 1;
244  
245 __END__
« Query/BGPOptimizer.pm ↑ Summary Query/Plan/Service.pm »