Query/Expression/Function.pm

filter: Statementstmt 68.5% 96 / 140 Branchbran 63.4% 33 / 52 Conditioncond 56.0% 14 / 25 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 82.6% 19 / 23 Podpod 100% 8 / 8 totaltotal 66.9% 176 / 263 CC 37 SCAR 28.8
CC
37
Cov
66%
qualify_uris
72.0
evaluate
20.4
__ANON__
12.0
CRAP 17.7
« Query/Plan/ThresholdUnion.pm ↑ Summary Query/Plan/Update.pm »
1 # RDF::Query::Expression::Function
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Expression::Function - Class for Function expressions
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Expression::Function version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Expression::Function;
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 RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Scalar::Util qw(blessed reftype);
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 36 our %FUNCTION_MAP = (
36 str => "STR",
37 strdt => "STRDT",
38 strlang => "STRLANG",
39 lang => "LANG",
40 langmatches => "LANGMATCHES",
41 sameterm => "sameTerm",
42 datatype => "DATATYPE",
43 bound => "BOUND",
44 isuri => "isURI",
45 isiri => "isIRI",
46 isblank => "isBlank",
47 isliteral => "isLiteral",
48 regex => "REGEX",
49 iri => "IRI",
50 uri => "IRI",
51 bnode => "BNODE",
52 in => "IN",
53 notin => "NOT IN",
54 if => "IF",
55 'logical-or' => "||",
56 'logical-and' => "&&",
57 );
58  
59 =head1 METHODS
60  
61 Beyond the methods documented below, this class inherits methods from the
62 L<RDF::Query::Expression> class.
63  
64 =over 4
65  
66 =cut
67  
68 =item C<new ( $uri, @arguments )>
69  
70 Returns a new Expr structure.
71  
72 =cut
73  
74 223 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
75 223 my $class = shift;
76 223 my $uri = shift;
77 223 my @args = @_;
78 223 ▶ unless (blessed($uri) and $uri->isa('RDF::Trine::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")) 85 138 223
Condition66%2/3
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
850138
MC/DC50%1/2
builtin::blessed($uri) and $uri->isa("RDF::Trine::Node::Resource")
builtin::blessed($uri) $uri->isa("RDF::Trine::Node::Resource")
Truth table66%2/3
A and B
A= builtin::blessed($uri)B= $uri->isa("RDF::Trine::Node::Resource")
ABresult
0X0
100
111
79 85 $uri = RDF::Query::Node::Resource->new( $uri );
80 }
81 223 return $class->SUPER::new( $uri, @args );
82 }
83  
84 =item C<< uri >>
85  
86 Returns the URI of the function.
87  
88 =cut
89  
90 613 ▶sub uri {
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
91 613 my $self = shift;
92 613 return $self->op;
93 }
94  
95 =item C<< arguments >>
96  
97 Returns a list of the arguments to the function.
98  
99 =cut
100  
101 509 ▶sub arguments {
Subroutine arguments: called CC=1 SCAR=0.0
Pod documented
102 509 my $self = shift;
103 509 return $self->operands;
104 }
105  
106 =item C<< sse >>
107  
108 Returns the SSE string for this algebra expression.
109  
110 =cut
111  
112 148 ▶sub sse {
Subroutine sse: called CC=2 SCAR=6.9
Pod documented
113 148 my $self = shift;
114 148 my $context = shift;
115
116 148 my $uri = $self->uri->uri_value;
117 148 ▶ if ($uri =~ m/^(sop|sparql):(in|notin|str(dt|lang)?|if|iri|uri|bnode|lang(matches)?|sameTerm|datatype|regex|bound|is(URI|IRI|Blank|Literal))/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /^(sop|sparql):(in|notin|str(dt|lang)?|if|iri|uri|bnode|lang(matches)?|sameTerm|datatype|regex|bound|is(URI|IRI|Blank|Literal))/i) { } 77 71 148
118 77 my $func = $2;
119 return sprintf(
120 '(%s %s)',
121 $func,
122 77 join(' ', map { $_->sse( $context ) } $self->arguments),
123 );
124 } else {
125 return sprintf(
126 '(%s %s)',
127 $self->uri->sse( $context ),
128 71 join(' ', map { $_->sse( $context ) } $self->arguments),
129 );
130 }
131 }
132  
133 =item C<< as_sparql >>
134  
135 Returns the SPARQL string for this algebra expression.
136  
137 =cut
138  
139 7 ▶sub as_sparql {
Subroutine as_sparql: called CC=6 SCAR=23.1
Pod documented
140 7 my $self = shift;
141 7 my $context = shift;
142 7 my $indent = shift;
143 7 my @args = $self->arguments;
144 7 my $uri = $self->uri->uri_value;
145 my $func = ($uri =~ m/^(sop|sparql):(logical-and|logical-or|in|notin|str(dt|lang)?|if|iri|uri|bnode|lang(matches)?|sameTerm|datatype|regex|bound|is(URI|IRI|Blank|Literal))/i)
146 7 ▶ ? $FUNCTION_MAP{ lc($2) }
Branch: 1 branch
BranchTrueFalseTotal
$uri =~ /^(sop|sparql):(logical-and|logical-or|in|notin|str(dt|lang)?|if|iri|uri|bnode|lang(matches)?|sameTerm|datatype|regex|bound|is(URI|IRI|Blank|Literal))/i ? : 7 0 7
147 : $self->uri->as_sparql( $context, $indent );
148 7 ▶ if ($func eq 'IN' or $func eq 'NOT IN') {
Branch: 2 branches
BranchTrueFalseTotal
if ($func eq 'IN' or $func eq 'NOT IN') { } 0 7 7
elsif ($func eq '||' or $func eq '&&') { } 0 7 7
Condition33%1/3
$func eq "IN" or $func eq "NOT IN"
l!l&&r!l&&!r
007
MC/DC0%0/2
$func eq "IN" or $func eq "NOT IN"
$func eq "IN" $func eq "NOT IN"
Truth table33%1/3
A or B
A= $func eq "IN"B= $func eq "NOT IN"
ABresult
1X1
011
000
149 0 my $term = shift(@args);
150 my $string = sprintf(
151 "%s %s (%s)",
152 $term->as_sparql( $context, $indent ),
153 $func,
154 0 join(', ', map { $_->as_sparql( $context, $indent ) } @args),
155 );
156 0 return $string;
157 ▶ } elsif ($func eq '||' or $func eq '&&') {
Condition33%1/3
$func eq "||" or $func eq "&&"
l!l&&r!l&&!r
007
MC/DC0%0/2
$func eq "||" or $func eq "&&"
$func eq "||" $func eq "&&"
Truth table33%1/3
A or B
A= $func eq "||"B= $func eq "&&"
ABresult
1X1
011
000
158 my $string = sprintf(
159 "(%s) $func (%s)",
160 0 (map { $_->as_sparql( $context, $indent ) } @args),
161 );
162 0 return $string;
163 } else {
164 my $string = sprintf(
165 "%s(%s)",
166 $func,
167 7 join(', ', map { $_->as_sparql( $context, $indent ) } @args),
168 );
169 7 return $string;
170 }
171 }
172  
173 =item C<< type >>
174  
175 Returns the type of this algebra expression.
176  
177 =cut
178  
179 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
180 0 return 'FUNCTION';
181 }
182  
183 =item C<< qualify_uris ( \%namespaces, $base_uri ) >>
184  
185 Returns a new algebra pattern where all referenced Resource nodes representing
186 QNames (ns:local) are qualified using the supplied %namespaces.
187  
188 =cut
189  
190 0 ▶sub qualify_uris {
Subroutine qualify_uris: not called CC=8 SCAR=42.8
Pod documented
191 0 my $self = shift;
192 0 my $class = ref($self);
193 0 my $ns = shift;
194 0 my $base_uri = shift;
195 0 my @args;
196 0 foreach my $arg ($self->construct_args) {
197 0 ▶ if (blessed($arg) and $arg->isa('RDF::Query::Algebra')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($arg) and $arg->isa('RDF::Query::Algebra')) { } 0 0 0
elsif (builtin::blessed($arg) and $arg->isa('RDF::Query::Node::Resource')) { } 0 0 0
Condition0%0/3
builtin::blessed($arg) and $arg->isa("RDF::Query::Algebra")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arg) and $arg->isa("RDF::Query::Algebra")
builtin::blessed($arg) $arg->isa("RDF::Query::Algebra")
Truth table0%0/3
A and B
A= builtin::blessed($arg)B= $arg->isa("RDF::Query::Algebra")
ABresult
0X0
100
111
198 0 push(@args, $arg->qualify_uris( $ns, $base_uri ));
199 ▶ } elsif (blessed($arg) and $arg->isa('RDF::Query::Node::Resource')) {
Condition0%0/3
builtin::blessed($arg) and $arg->isa("RDF::Query::Node::Resource")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arg) and $arg->isa("RDF::Query::Node::Resource")
builtin::blessed($arg) $arg->isa("RDF::Query::Node::Resource")
Truth table0%0/3
A and B
A= builtin::blessed($arg)B= $arg->isa("RDF::Query::Node::Resource")
ABresult
0X0
100
111
200 0 my $uri = $arg->uri;
201 0 ▶ if (ref($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri) { } 0 0 0
202 0 my ($n,$l) = @$uri;
203 0 ▶ unless (exists($ns->{ $n })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ns->{$n}) 0 0 0
204 0 throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined";
205 }
206 0 my $resolved = RDF::Query::Node::Resource->new( join('', $ns->{ $n }, $l), $base_uri );
207 0 push(@args, $resolved);
208 } else {
209 0 push(@args, $arg);
210 }
211 } else {
212 0 push(@args, $arg);
213 }
214 }
215 0 return $class->new( @args );
216 }
217  
218 =item C<< evaluate ( $query, \%bound, $context ) >>
219  
220 Evaluates the expression using the supplied bound variables.
221 Will return a RDF::Query::Node object.
222  
223 =cut
224  
225 354 ▶sub evaluate {
Subroutine evaluate: called CC=16 SCAR=30.1
Pod documented
226 354 my $self = shift;
227 354 ▶ my $query = shift || 'RDF::Query';
Condition100%2/2
shift() || 'RDF::Query'
l!l
27480
MC/DC100%1/1
shift() || 'RDF::Query'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
228 354 my $bound = shift;
229 354 my $context = shift;
230 354 my $active_graph = shift;
231 354 my $uri = $self->uri;
232
233 36 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 354 my $uriv = $uri->uri_value;
235 354 ▶ if ($uriv =~ /^sparql:logical-(.+)$/ or $uriv =~ /^sparql:(not)?in$/ or $uriv eq 'sparql:coalesce') {
Branch: 3 branches
BranchTrueFalseTotal
if ($uriv =~ /^sparql:logical-(.+)$/ or $uriv =~ /^sparql:(not)?in$/ or $uriv eq 'sparql:coalesce') { } 28 326 354
elsif ($uriv =~ /^sparql:if$/) { } 0 326 326
elsif ($uriv eq 'sparql:exists') { } 4 322 326
Condition100%6/6
$uriv =~ /^sparql:logical-(.+)$/ or $uriv =~ /^sparql:(not)?in$/ or $uriv eq "sparql:coalesce"
l!l&&r!l&&!r
208326
$uriv =~ /^sparql:logical-(.+)$/ or $uriv =~ /^sparql:(not)?in$/
l!l&&r!l&&!r
164334
MC/DC100%3/3
$uriv =~ /^sparql:logical-(.+)$/ or $uriv =~ /^sparql:(not)?in$/ or $uriv eq "sparql:coalesce"
$uriv =~ /^sparql:logical-(.+)$/ $uriv =~ /^sparql:(not)?in$/ $uriv eq "sparql:coalesce"
Truth table100%4/4
A or B or C
A= $uriv =~ /^sparql:logical-(.+)$/B= $uriv =~ /^sparql:(not)?in$/C= $uriv eq "sparql:coalesce"
ABCresult
1XX1
01X1
0011
0000
236 # logical operators must have their arguments passed lazily, because
237 # some of them can still succeed even if some of their arguments throw
238 # TypeErrors (e.g. true || fail ==> true).
239 28 my @args = $self->arguments;
240 61 ▶ my $args = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
241 61 my $value = shift(@args);
242 61 ▶ return unless (blessed($value));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 11 50 61
243 50 my $val = 0;
244 50 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
245 $val = $value->isa('RDF::Query::Expression')
246 ? $value->evaluate( $query, $bound, $context, $active_graph )
247 : ($value->isa('RDF::Trine::Node::Variable'))
248 50 ▶ ? $bound->{ $value->name }
Branch: 2 branches
BranchTrueFalseTotal
$value->isa('RDF::Query::Expression') ? : 12 38 50
$value->isa('RDF::Trine::Node::Variable') ? : 8 30 38
249 : $value;
250 50 ▶ } otherwise {};
Subroutine __ANON__: called CC=1 SCAR=0.0
251 50 ▶ return $val || 0;
Condition100%2/2
$val || 0
l!l
437
MC/DC100%1/1
$val || 0
$val
Truth table100%2/2
A
A= $val
Aresult
11
00
252 28 };
253 28 my $func = $query->get_function( $uri );
254 28 my $value = $func->( $query, $args );
255 28 return $value;
256 } elsif ($uriv =~ /^sparql:if$/) {
257 0 my @args = $self->arguments;
258 0 my $ebv = RDF::Query::Node::Resource->new( "sparql:ebv" );
259 0 my $expr = shift(@args);
260 0 my $index = 1;
261 0 my $ok = 1;
262 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
263 0 my $exprval = $query->var_or_expr_value( $bound, $expr, $context );
264 0 my $func = RDF::Query::Expression::Function->new( $ebv, $exprval );
265 0 my $value = $func->evaluate( $query, {}, $context, $active_graph );
266 0 ▶ my $bool = ($value->literal_value eq 'true') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value->literal_value eq 'true' ? : 0 0 0
267 0 ▶ if ($bool) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bool) 0 0 0
268 0 $index = 0;
269 }
270 0 ▶ } catch RDF::Query::Error::TypeError with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
271 0 $ok = 0;
272 0 };
273 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 0 0 0
274 0 my $expr2 = $args[$index];
275 0 return $query->var_or_expr_value( $bound, $expr2, $context );
276 } else {
277 0 return;
278 }
279 } elsif ($uriv eq 'sparql:exists') {
280 4 my $func = $query->get_function($uri);
281 4 my ($ggp) = $self->arguments;
282 4 return $func->( $query, $context, $bound, $ggp, $active_graph );
283 } else {
284 322 ▶ my $model = ref($query) ? $query->{model} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $query ? : 233 89 322
285 322 ▶ if (blessed($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($context)) 217 105 322
286 217 $model = $context->model;
287 }
288
289 322 my @args;
290 322 ▶ if (ref($query)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query) { } 233 89 322
291 # localize the model in the query object (legacy code wants the model accessible from the query object)
292 233 local($query->{model}) = $model;
293 @args = map {
294 233 $_->isa('RDF::Query::Algebra')
295 ? $_->evaluate( $query, $bound, $context, $active_graph )
296 : ($_->isa('RDF::Trine::Node::Variable'))
297 309 ▶ ? $bound->{ $_->name }
Branch: 2 branches
BranchTrueFalseTotal
$_->isa('RDF::Query::Algebra') ? : 116 193 309
$_->isa('RDF::Trine::Node::Variable') ? : 114 79 193
298 : $_
299 } $self->arguments;
300 } else {
301 @args = map {
302 89 $_->isa('RDF::Query::Algebra')
303 ? $_->evaluate( $query, $bound, $context, $active_graph )
304 : ($_->isa('RDF::Trine::Node::Variable'))
305 103 ▶ ? $bound->{ $_->name }
Branch: 2 branches
BranchTrueFalseTotal
$_->isa('RDF::Query::Algebra') ? : 1 102 103
$_->isa('RDF::Trine::Node::Variable') ? : 2 100 102
306 : $_
307 } $self->arguments;
308 }
309
310 322 my $func = $query->get_function($uri);
311 322 ▶ unless ($func) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func) 0 322 322
312 0 throw RDF::Query::Error::ExecutionError -text => "Failed to get function for IRI $uri";
313 }
314 322 my $value = $func->( $query, @args );
315 311 return $value;
316 }
317 }
318  
319 36 1;
320  
321 __END__
« Query/Plan/ThresholdUnion.pm ↑ Summary Query/Plan/Update.pm »