LDF.pm

filter: Statementstmt 96.6% 284 / 294 Branchbran 73.4% 94 / 128 Conditioncond 55.1% 32 / 58 MC/DCmcdc 30.3% 10 / 33 Subroutinesub 97.8% 46 / 47 Podpod 33.3% 2 / 6 totaltotal 82.6% 468 / 566 CC 94 SCAR 20.4
CC
94
Cov
85%
get_statements
22.8
_find_best_pattern
11.2
get_query_pattern
10.5
CRAP 7.7
↑ Summary Trine/Store/LDF.pm »
1 package RDF::LDF;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use feature qw(state);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use RDF::NS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use RDF::Trine;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use RDF::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Log::Any ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use URI::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use RDF::LDF::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 our $VERSION = '0.251';
24  
25 5 has url => (
26 is => 'ro' ,
27 required => 1
28 );
29  
30 has sn => (
31 is => 'ro' ,
32 lazy => 1,
33 2 ▶ builder => sub {
Subroutine _build_sn: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
34 2 RDF::NS->new->REVERSE;
35 }
36 5 );
37  
38 has lru => (
39 is => 'ro' ,
40 lazy => 1,
41 3 ▶ builder => sub {
Subroutine _build_lru: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 Cache::LRU->new( size => 100 );
43 }
44 5 );
45  
46 has log => (
47 is => 'ro',
48 lazy => 1,
49 3 ▶ builder => sub {
Subroutine _build_log: called CC=1 SCAR=0.0
50 3 Log::Any->get_logger(category => ref(shift));
51 }
52 5 );
53  
54 # Public method
55 3 ▶sub is_fragment_server {
Subroutine is_fragment_server: called CC=4 SCAR=13.9
Pod undocumented
56 3 my $self = shift;
57 3 ▶ my $federated = ref($self->url) ? $self->url : [ $self->url ];
Branch: 1 branch
BranchTrueFalseTotal
ref $self->url ? : 1 2 3
58 3 for my $part (@$federated) {
59 5 ▶ return 0 unless $self->get_query_pattern($part);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_query_pattern($part) 0 5 5
60 }
61 3 return 1;
62 }
63  
64 # Public method
65 # Optimized method to find all bindings matching a pattern
66 # See:
67 # Verborgh, Ruben, et al. Querying Datasets on the Web with High Availability. ISWC2014
68 # http://linkeddatafragments.org/publications/iswc2014.pdf
69 7 ▶sub get_pattern {
Subroutine get_pattern: called CC=5 SCAR=16.1
Pod documented
70 7 my ($self,$bgp,$context,%args) = @_;
71  
72 7 ▶ unless (defined $bgp) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bgp) 1 6 7
73 1 RDF::LDF::Error->throw(text => "can't execute get_pattern for an empty pattern");
74 }
75  
76 6 ▶ my (@triples) = ($bgp->isa('RDF::Trine::Statement') or $bgp->isa('RDF::Query::Algebra::Filter'))
Branch: 1 branch
BranchTrueFalseTotal
$bgp->isa('RDF::Trine::Statement') || $bgp->isa('RDF::Query::Algebra::Filter') ? : 4 2 6
Condition66%2/3
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
l!l&&r!l&&!r
402
MC/DC50%1/2
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
$bgp->isa("RDF::Trine::Statement") $bgp->isa("RDF::Query::Algebra::Filter")
Truth table66%2/3
A or B
A= $bgp->isa("RDF::Trine::Statement")B= $bgp->isa("RDF::Query::Algebra::Filter")
ABresult
1X1
011
000
77 ? $bgp
78 : $bgp->triples;
79  
80 6 ▶ unless (@triples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@triples) 0 6 6
81 0 RDF::LDF::Error->throw(text => "can't execute get_pattern for an empty pattern");
82 }
83  
84 6 my @vars = $bgp->referenced_variables;
85  
86 6 my @bgps = map { $self->_parse_triple_pattern($_)} @triples;
87  
88 12 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
89 12 state $it = $self->_find_variable_bindings(\@bgps);
90 12 my $b = $it->();
91  
92 12 ▶ return undef unless $b;
Branch: 1 branch
BranchTrueFalseTotal
unless $b 2 10 12
93  
94 10 my $binding = RDF::Trine::VariableBindings->new({});
95  
96 10 for my $key (keys %$b) {
97 26 my $val = $b->{$key};
98 26 $key =~ s{^\?}{};
99 26 $binding->set($key => $val);
100 }
101  
102 10 $binding;
103 6 };
104  
105 6 RDF::Trine::Iterator::Bindings->new($sub,\@vars);
106 }
107  
108 14 ▶sub _find_variable_bindings {
Subroutine _find_variable_bindings: called CC=2 SCAR=6.9
109 14 my $self = shift;
110 14 my $bgps = shift;
111 14 ▶ my $bindings = shift // {};
Condition100%2/2
shift() // {}
l!l
86
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
112  
113 26 ▶ my $iterator = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
114 26 state $it;
115 26 ▶ state $results = sub {};
Subroutine __ANON__: called CC=1 SCAR=0.0
116  
117 26 my $ret;
118  
119 # Loop over all variabe bindings with multiple matches
120 26 ▶ while (!defined($ret = $results->())) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($ret = &$results())) 28 6 34
121 28 ▶ unless (defined $it) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $it) 14 14 28
122 # Find the an binding iterator for the best pattern from $bgpgs
123 14 ($it,$bgps) = $self->_find_variable_bindings_($bgps);
124  
125 14 ▶ return undef unless $it;
Branch: 1 branch
BranchTrueFalseTotal
unless $it 0 14 14
126 }
127  
128 # Update all the other bgps with the current binding..
129 28 my $this_binding = $it->();
130  
131 28 ▶ return undef unless $this_binding;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_binding 10 18 28
132  
133 18 $bindings = { %$bindings , %$this_binding };
134  
135 18 ▶ return $bindings unless @$bgps;
Branch: 1 branch
BranchTrueFalseTotal
unless @$bgps 10 8 18
136  
137 # Apply all the bindings to the rest of the bgps;
138 8 my $bgps_prime = $self->_apply_binding($this_binding,$bgps);
139  
140 8 $results = $self->_find_variable_bindings($bgps_prime,$bindings);
141 }
142  
143 6 $ret;
144 14 };
145  
146 14 $iterator;
147 }
148  
149 # Given an array ref of patterns return the variable bindings for the
150 # pattern with the least number of triples.
151 #
152 # my ($iterator, $rest) = $self->_find_variable_bindings([ {pattern} , {pattern} , ... ]);
153 #
154 # where:
155 #
156 # $iterator - Iterator for variable bindings for the winnnig pattern, or undef when no
157 # patterns are provided or we get zero results
158 #
159 # $rest - An array ref of patterns not containing the best pattern
160 14 ▶sub _find_variable_bindings_ {
Subroutine _find_variable_bindings_: called CC=5 SCAR=16.2
161 14 my ($self,$bgps) = @_;
162  
163 14 ▶ return (undef, undef) unless _is_array_ref($bgps) && @$bgps > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless _is_array_ref($bgps) and @$bgps > 0 0 14 14
Condition33%1/3
_is_array_ref($bgps) and @$bgps > 0
!ll&&!rl&&r
0014
MC/DC0%0/2
_is_array_ref($bgps) and @$bgps > 0
_is_array_ref($bgps) @$bgps > 0
Truth table33%1/3
A and B
A= _is_array_ref($bgps)B= @$bgps > 0
ABresult
0X0
100
111
164  
165 14 my ($pattern,$rest) = $self->_find_best_pattern($bgps);
166  
167 14 ▶ return (undef,undef) unless defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 14 14
168  
169 14 my $it = $self->get_statements($pattern);
170  
171 # Build a mapping of variable bindings to Triple nodes. E.g.
172 # {
173 # '?s' => 'subject' ,
174 # '?p' => 'predicate' ,
175 # '?o' => 'object' ,
176 #}
177 14 ▶ my %pattern_var_map = map { $pattern->{$_} =~ /^\?/ ? ($pattern->{$_} , $_) : () } keys %$pattern;
Branch: 1 branch
BranchTrueFalseTotal
$pattern->{$_} =~ /^\?/ ? : 20 22 42
178 14 my $num_of_bindings = keys %pattern_var_map;
179  
180 28 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
181 28 my $triple = $it->();
182  
183 28 ▶ return undef unless defined $triple;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $triple 10 18 28
184  
185 18 my %var_map = %pattern_var_map;
186  
187 18 for (keys %var_map) {
188 30 my $method = $var_map{$_};
189 30 $var_map{$_} = $triple->$method;
190 }
191  
192 18 return {%var_map};
193 14 };
194  
195 14 return ($sub,$rest);
196 }
197  
198 8 ▶sub _apply_binding {
Subroutine _apply_binding: called CC=7 SCAR=20.2
199 8 my ($self,$binding,$bgps) = @_;
200  
201 8 ▶ return unless _is_array_ref($bgps) && @$bgps > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless _is_array_ref($bgps) and @$bgps > 0 0 8 8
Condition33%1/3
_is_array_ref($bgps) and @$bgps > 0
!ll&&!rl&&r
008
MC/DC0%0/2
_is_array_ref($bgps) and @$bgps > 0
_is_array_ref($bgps) @$bgps > 0
Truth table33%1/3
A and B
A= _is_array_ref($bgps)B= @$bgps > 0
ABresult
0X0
100
111
202  
203 8 my $copy = clone $bgps;
204 8 my @new = ();
205  
206 8 for my $pattern (@$copy) {
207 8 for (qw(subject predicate object)) {
208 24 my $val = $pattern->{$_};
209 24 ▶ if (defined($val) && $binding->{$val}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $binding->{$val}) 8 16 24
Condition33%1/3
defined $val and $binding->{$val}
!ll&&!rl&&r
0240
MC/DC0%0/2
defined $val and $binding->{$val}
defined $val $binding->{$val}
Truth table33%1/3
A and B
A= defined $valB= $binding->{$val}
ABresult
0X0
100
111
210 8 my $str_val = $self->_node_as_string($binding->{$val});
211 8 $pattern->{$_} = $str_val
212 }
213 }
214 8 push @new, $pattern;
215 }
216  
217 8 return \@new;
218 }
219  
220 # Create a pattern which binds to the graph pattern
221 #
222 # Usage:
223 #
224 # my $triples = [
225 # { subject => ... , predicate => ... , object => ... } , #tp1
226 # { subject => ... , predicate => ... , object => ... } , #tp2
227 # ...
228 # { subject => ... , predicate => ... , object => ... } , #tpN
229 # ];
230 #
231 # my ($pattern, $rest) = $self->_find_best_pattern($triples);
232 #
233 # $pattern => Pattern in $triples which least amount of results
234 # $rest => All patterns in $triples except $pattern
235 #
236 14 ▶sub _find_best_pattern {
Subroutine _find_best_pattern: called CC=10 SCAR=24.2
237 14 my ($self,$triples) = @_;
238  
239 14 ▶ return undef unless @$triples > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @$triples > 0 0 14 14
240  
241 # If we only have one tripple pattern, the use it to create the bind
242 14 ▶ if (@$triples == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$triples == 1) 12 2 14
243 12 return $triples->[0] , [];
244 }
245  
246 2 my $best_pattern = undef;
247 2 my $best_count = undef;
248  
249 2 for my $pattern (@$triples) {
250 4 ▶ my $count = $self->_total_triples($pattern) // 0;
Condition50%1/2
$self->_total_triples($pattern) // 0
l!l
40
MC/DC0%0/1
$self->_total_triples($pattern) // 0
$self->_total_triples($pattern)
Truth table50%1/2
A
A= $self->_total_triples($pattern)
Aresult
11
00
251  
252 4 ▶ if ($count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count == 0) { } 0 4 4
elsif (not defined $best_count or $count < $best_count) { } 2 2 4
253 0 $best_pattern = undef;
254 0 $best_count = 0;
255 0 last;
256 }
257 ▶ elsif (!defined $best_count || $count < $best_count) {
Condition66%2/3
not defined $best_count or $count < $best_count
l!l&&r!l&&!r
202
MC/DC50%1/2
not defined $best_count or $count < $best_count
not defined $best_count $count < $best_count
Truth table66%2/3
not(A) or B
A= not defined $best_countB= $count < $best_count
ABresult
1X1
011
000
258 2 $best_count = $count;
259 2 $best_pattern = $pattern;
260 }
261 }
262  
263 2 ▶ return (undef,$triples) unless defined $best_pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $best_pattern 0 2 2
264  
265 2 ▶ my @rest_triples = map { Data::Compare::Compare($_,$best_pattern) ? () : ($_) } @$triples;
Branch: 1 branch
BranchTrueFalseTotal
Data::Compare::Compare($_, $best_pattern) ? : 2 2 4
266  
267 2 return ($best_pattern, \@rest_triples);
268 }
269  
270 # Find the total number of triples available for a pattern
271 #
272 # Usage:
273 #
274 # my $count = $self->_total_triples(
275 # { subject => ... , predicate => ... , object => ...}
276 # );
277 # Where
278 # $count is a number
279 4 ▶sub _total_triples {
Subroutine _total_triples: called CC=2 SCAR=7.0
280 4 my ($self,$pattern) = @_;
281  
282 # Retrieve one...
283 4 my $iterator = $self->get_statements($pattern);
284  
285 4 ▶ return 0 unless $iterator;
Branch: 1 branch
BranchTrueFalseTotal
unless $iterator 0 4 4
286  
287 4 my ($model,$info) = $iterator->();
288  
289 4 $info->{hydra_totalItems};
290 }
291  
292 32 ▶sub _node_as_string {
Subroutine _node_as_string: called CC=5 SCAR=18.1
293 32 my $self = shift;
294 32 my $node = shift;
295 32 ▶ if (_is_blessed($node) && $node->isa('RDF::Trine::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_blessed($node) and $node->isa("RDF::Trine::Node")) 32 0 32
Condition33%1/3
_is_blessed($node) and $node->isa("RDF::Trine::Node")
!ll&&!rl&&r
0032
MC/DC0%0/2
_is_blessed($node) and $node->isa("RDF::Trine::Node")
_is_blessed($node) $node->isa("RDF::Trine::Node")
Truth table33%1/3
A and B
A= _is_blessed($node)B= $node->isa("RDF::Trine::Node")
ABresult
0X0
100
111
296 32 ▶ if ($node->isa('RDF::Trine::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->isa('RDF::Trine::Node::Variable')) { } 16 16 32
elsif ($node->isa('RDF::Trine::Node::Literal')) { } 0 16 16
297 16 return $node->as_string; # ?foo
298 } elsif ($node->isa('RDF::Trine::Node::Literal')) {
299 0 return $node->as_string; # includes quotes and any language or datatype
300 } else {
301 16 return $node->value; # the raw IRI or blank node identifier value, without other syntax
302 }
303 }
304 0 return '';
305 }
306  
307 # For an BGP triple create a fragment pattern
308 8 ▶sub _parse_triple_pattern {
Subroutine _parse_triple_pattern: called CC=1 SCAR=0.0
309 8 my ($self,$triple) = @_;
310 8 my $subject = $self->_node_as_string($triple->subject);
311 8 my $predicate = $self->_node_as_string($triple->predicate);
312 8 my $object = $self->_node_as_string($triple->object);
313 8 my $hash = {
314 subject => $subject ,
315 predicate => $predicate,
316 object => $object
317 };
318 8 return $hash;
319 }
320  
321 # Dynamic find out which triple patterns need to be used to query the fragment server
322 # Returns a hash:
323 # {
324 # rdf_subject => <name_of_subject_variable> ,
325 # rdf_predicate => <name_of_predicate_variable> ,
326 # rdf_object => <name_of_object_variable>
327 # hydra_template => <endpoint_for_tripple_pattern>
328 # }
329 42 ▶sub get_query_pattern {
Subroutine get_query_pattern: called CC=10 SCAR=23.6
Pod undocumented
330 42 my ($self,$url) = @_;
331  
332 42 my $fragment = $self->get_model_and_info($url);
333  
334 42 ▶ return undef unless defined $fragment;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment 0 42 42
335  
336 42 my $info = $fragment->{info};
337  
338 42 my $pattern;
339  
340 42 ▶ return undef unless _is_hash_ref($info);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hash_ref($info) 0 42 42
341  
342 42 ▶ return undef unless $info->{hydra_template};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"hydra_template"} 0 42 42
343  
344 42 for (keys %$info) {
345 716 ▶ next unless _is_hash_ref($info->{$_}) && $info->{$_}->{hydra_property};
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hash_ref($info->{$_}) and $info->{$_}{"hydra_property"} 590 126 716
Condition100%3/3
_is_hash_ref($info->{$_}) and $info->{$_}{"hydra_property"}
!ll&&!rl&&r
54842126
MC/DC100%2/2
_is_hash_ref($info->{$_}) and $info->{$_}{"hydra_property"}
_is_hash_ref($info->{$_}) $info->{$_}{"hydra_property"}
Truth table100%3/3
A and B
A= _is_hash_ref($info->{$_})B= $info->{$_}{"hydra_property"}
ABresult
0X0
100
111
346 126 my $property = join "_" , $self->sn->qname($info->{$_}->{hydra_property});
347 126 my $variable = $info->{$_}->{hydra_variable};
348  
349 126 $pattern->{$property} = $variable;
350 }
351  
352 42 ▶ return undef unless $pattern->{rdf_subject};
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->{"rdf_subject"} 0 42 42
353 42 ▶ return undef unless $pattern->{rdf_predicate};
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->{"rdf_predicate"} 0 42 42
354 42 ▶ return undef unless $pattern->{rdf_object};
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->{"rdf_object"} 0 42 42
355  
356 42 $pattern->{hydra_template} = $info->{hydra_template};
357  
358 42 $pattern;
359 }
360  
361 #----------------------------------------------------------------------------------
362  
363 # Public method
364 19 ▶sub get_statements {
Subroutine get_statements: called CC=19 SCAR=31.3
Pod documented
365 19 my ($self,@triple) = @_;
366 19 my ($subject,$predicate,$object);
367  
368 19 ▶ if (@triple == 3) {
Branch: 2 branches
BranchTrueFalseTotal
if (@triple == 3) { } 0 19 19
elsif (_is_hash_ref($triple[0])) { } 18 1 19
369 0 ($subject,$predicate,$object) = @triple;
370 }
371 elsif (_is_hash_ref($triple[0])) {
372 18 $subject = $triple[0]->{subject};
373 18 $predicate = $triple[0]->{predicate};
374 18 $object = $triple[0]->{object};
375 }
376  
377 19 ▶ $subject = $subject->value if (_is_blessed($subject) && $subject->isa('RDF::Trine::Node') and not $subject->is_variable);
Branch: 1 branch
BranchTrueFalseTotal
if _is_blessed($subject) and $subject->isa("RDF::Trine::Node") and not $subject->is_variable 0 19 19
Condition33%2/6
_is_blessed($subject) and $subject->isa("RDF::Trine::Node") and not $subject->is_variable
!ll&&!rl&&r
1900
_is_blessed($subject) and $subject->isa("RDF::Trine::Node")
!ll&&!rl&&r
1900
MC/DC0%0/3
_is_blessed($subject) and $subject->isa("RDF::Trine::Node") and not $subject->is_variable
_is_blessed($subject) $subject->isa("RDF::Trine::Node") not $subject->is_variable
Truth table25%1/4
A and B and not(C)
A= _is_blessed($subject)B= $subject->isa("RDF::Trine::Node")C= not $subject->is_variable
ABCresult
0XX0
10X0
1100
1111
378 19 ▶ $predicate = $predicate->value if (_is_blessed($predicate) && $predicate->isa('RDF::Trine::Node') and not $predicate->is_variable);
Branch: 1 branch
BranchTrueFalseTotal
if _is_blessed($predicate) and $predicate->isa("RDF::Trine::Node") and not $predicate->is_variable 0 19 19
Condition33%2/6
_is_blessed($predicate) and $predicate->isa("RDF::Trine::Node") and not $predicate->is_variable
!ll&&!rl&&r
1900
_is_blessed($predicate) and $predicate->isa("RDF::Trine::Node")
!ll&&!rl&&r
1900
MC/DC0%0/3
_is_blessed($predicate) and $predicate->isa("RDF::Trine::Node") and not $predicate->is_variable
_is_blessed($predicate) $predicate->isa("RDF::Trine::Node") not $predicate->is_variable
Truth table25%1/4
A and B and not(C)
A= _is_blessed($predicate)B= $predicate->isa("RDF::Trine::Node")C= not $predicate->is_variable
ABCresult
0XX0
10X0
1100
1111
379 19 ▶ if (_is_blessed($object) && $object->isa('RDF::Trine::Node') and not $object->is_variable) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_blessed($object) and $object->isa("RDF::Trine::Node") and not $object->is_variable) 0 19 19
Condition33%2/6
_is_blessed($object) and $object->isa("RDF::Trine::Node") and not $object->is_variable
!ll&&!rl&&r
1900
_is_blessed($object) and $object->isa("RDF::Trine::Node")
!ll&&!rl&&r
1900
MC/DC0%0/3
_is_blessed($object) and $object->isa("RDF::Trine::Node") and not $object->is_variable
_is_blessed($object) $object->isa("RDF::Trine::Node") not $object->is_variable
Truth table25%1/4
A and B and not(C)
A= _is_blessed($object)B= $object->isa("RDF::Trine::Node")C= not $object->is_variable
ABCresult
0XX0
10X0
1100
1111
380 0 ▶ $object = ($object->isa('RDF::Trine::Node::Literal')) ? $object->as_string : $object->value;
Branch: 1 branch
BranchTrueFalseTotal
$object->isa('RDF::Trine::Node::Literal') ? : 0 0 0
381 }
382  
383 # Do a federated search over all the URLs provided
384 19 ▶ my $parts = ref($self->url) ? $self->url : [ $self->url ];
Branch: 1 branch
BranchTrueFalseTotal
ref $self->url ? : 9 10 19
385 19 my @federated;
386  
387 19 for my $part (@$parts) {
388 37 my $pattern = $self->get_query_pattern($part);
389 37 ▶ return undef unless defined $pattern;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 37 37
390  
391 37 my %params;
392 37 ▶ $params{ $pattern->{rdf_subject} } = $subject if _is_string($subject);
Branch: 1 branch
BranchTrueFalseTotal
if _is_string($subject) 36 1 37
393 37 ▶ $params{ $pattern->{rdf_predicate} } = $predicate if _is_string($predicate);
Branch: 1 branch
BranchTrueFalseTotal
if _is_string($predicate) 36 1 37
394 37 ▶ $params{ $pattern->{rdf_object} } = $object if _is_string($object);
Branch: 1 branch
BranchTrueFalseTotal
if _is_string($object) 36 1 37
395  
396 37 my $template = URI::Template->new($pattern->{hydra_template});
397 37 push @federated , $template->process(%params)->as_string;
398 }
399  
400 34 ▶ my $sub = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
401 34 state $model;
402 34 state $info;
403 34 state $iterator;
404 34 state $url = shift(@federated);
405  
406 34 my $triple;
407  
408 34 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not defined $triple and defined($url = pop @federated) and do { do { unless (defined $model) { return unless defined $url or defined($url = pop @federated); my $fragment = $self->get_model_and_info($url); return unless defined $fragment->{"model"}; $model = $fragment->{"model"}; $info = $fragment->{"info"}; $url = $info->{"hydra_next"}; $iterator = $model->get_statements; } ; $triple = $iterator->next; unless ($iterator->peek) { $model = undef; } } } 10 28 38
Condition100%3/3
not defined $triple and defined($url = pop @federated)
!ll&&!rl&&r
24410
MC/DC100%2/2
not defined $triple and defined($url = pop @federated)
not defined $triple defined($url = pop @federated)
Truth table100%3/3
not(A) and B
A= not defined $tripleB= defined($url = pop @federated)
ABresult
0X0
100
111
409 44 ▶ unless (defined $model) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $model) 37 7 44
410 # When no more result pages are available switch
411 # to the next federated url...
412 37 ▶ return unless defined($url) || defined($url = pop(@federated));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url or defined($url = pop @federated) 6 31 37
Condition100%3/3
defined $url or defined($url = pop @federated)
l!l&&r!l&&!r
2926
MC/DC100%2/2
defined $url or defined($url = pop @federated)
defined $url defined($url = pop @federated)
Truth table100%3/3
A or B
A= defined $urlB= defined($url = pop @federated)
ABresult
1X1
011
000
413  
414 31 my $fragment = $self->get_model_and_info($url);
415  
416 31 ▶ return unless defined $fragment->{model};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fragment->{"model"} 0 31 31
417  
418 31 $model = $fragment->{model};
419 31 $info = $fragment->{info};
420  
421 31 $url = $info->{hydra_next};
422 31 $iterator = $model->get_statements;
423 }
424  
425 38 $triple = $iterator->next;
426  
427 38 ▶ unless ($iterator->peek) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iterator->peek) 23 15 38
428 23 $model = undef;
429 }
430 }
431 while (!defined $triple && defined($url = pop(@federated)));
432  
433 28 ▶ wantarray ? ($triple,$info) : $triple;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 23 28
434 19 };
435  
436 19 $sub;
437 }
438  
439 # Fetch a fragment page and extract the metadata
440 73 ▶sub get_model_and_info {
Subroutine get_model_and_info: called CC=3 SCAR=11.0
Pod undocumented
441 73 my ($self,$url) = @_;
442  
443 73 ▶ if (my $cache = $self->lru->get($url)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $self->lru->get($url)) 49 24 73
444 49 return $cache;
445 }
446  
447 24 my $model = $self->get_fragment($url);
448 24 my $info = {};
449  
450 24 ▶ if (defined $model) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $model) 24 0 24
451 24 $info = $self->_model_metadata($model,$url, clean => 1);
452 }
453  
454 24 my $fragment = { model => $model , info => $info };
455  
456 24 $self->lru->set($url => $fragment);
457  
458 24 $fragment;
459 }
460  
461 # Fetch a result page from fragment server
462 24 ▶sub get_fragment {
Subroutine get_fragment: called CC=3 SCAR=11.2
Pod undocumented
463 24 my ($self,$url) = @_;
464  
465 24 ▶ return undef unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 24 24
466  
467 24 $self->log->info("fetching: $url");
468  
469 24 my $model = RDF::Trine::Model->temporary_model;
470  
471 # JSON support in RDF::Trine isn't JSON-LD
472 # Set the accept header quality parameter at a minimum for this format
473 24 my $ua = clone(RDF::Trine->default_useragent);
474 24 $ua->agent("RDF:::LDF/$RDF::LDF::VERSION " . $ua->_agent);
475 24 $ua->default_header('Accept','text/turtle;q=1.0,application/turtle;q=1.0,application/x-turtle;q=1.0,application/rdf+xml;q=0.9,text/x-nquads;q=0.9,application/json;q=0.1,application/x-rdf+json;q=0.1');
476  
477 24 eval {
478 # Need to explicitly set the useragent to keep the accept headers
479 24 RDF::Trine::Parser->parse_url_into_model($url, $model, useragent => $ua);
480 };
481  
482 24 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
483 0 $self->log->error("failed to parse input");
484 }
485  
486 24 return $model;
487 }
488  
489 # Create a hash with fragment metadata from a RDF::Trine::Model
490 # parameters:
491 # $model - RDF::Trine::Model
492 # $this_uri - result page URL
493 # %opts
494 # clean => 1 - remove the metadata from the model
495 24 ▶sub _model_metadata {
Subroutine _model_metadata: called CC=7 SCAR=19.9
496 24 my ($self,$model,$this_uri,%opts) = @_;
497  
498 24 my $info = {};
499  
500 24 $self->_build_metadata($model, {
501 subject => RDF::Trine::Node::Resource->new($this_uri)
502 } , $info);
503  
504 24 ▶ if ($opts{clean}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"clean"}) 24 0 24
505 24 $model->remove_statements(
506 RDF::Trine::Node::Resource->new($this_uri),
507 undef,
508 undef
509 );
510 24 $model->remove_statements(
511 undef,
512 undef,
513 RDF::Trine::Node::Resource->new($this_uri)
514 );
515 }
516  
517 24 for my $predicate (
518 'http://www.w3.org/ns/hydra/core#variable' ,
519 'http://www.w3.org/ns/hydra/core#property' ,
520 'http://www.w3.org/ns/hydra/core#mapping' ,
521 'http://www.w3.org/ns/hydra/core#template' ,
522 'http://www.w3.org/ns/hydra/core#member' ,
523 'http://www.w3.org/ns/hydra/core#variableRepresentation' ,
524 ) {
525 144 $self->_build_metadata($model, {
526 predicate => RDF::Trine::Node::Resource->new($predicate)
527 }, $info);
528  
529 144 ▶ if ($opts{clean}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"clean"}) 144 0 144
530 144 $model->remove_statements(
531 undef,
532 RDF::Trine::Node::Resource->new($predicate) ,
533 undef);
534 }
535 }
536  
537 24 ▶ my $source = $info->{dct_source}->[0] if _is_array_ref($info->{dct_source});
Branch: 1 branch
BranchTrueFalseTotal
if _is_array_ref($info->{"dct_source"}) 24 0 24
538  
539 24 ▶ if ($source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source) 24 0 24
540 24 $self->_build_metadata($model, {
541 subject => RDF::Trine::Node::Resource->new($source)
542 }, $info);
543  
544 24 ▶ if ($opts{clean}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"clean"}) 24 0 24
545 24 $model->remove_statements(
546 RDF::Trine::Node::Resource->new($source),
547 undef,
548 undef
549 );
550 24 $model->remove_statements(
551 undef,
552 undef,
553 RDF::Trine::Node::Resource->new($source)
554 );
555 }
556 }
557  
558 24 $info;
559 }
560  
561 # Helper method for _parse_model
562 192 ▶sub _build_metadata {
Subroutine _build_metadata: called CC=5 SCAR=16.1
563 192 my ($self, $model, $triple, $info) = @_;
564  
565 my $iterator = $model->get_statements(
566 $triple->{subject},
567 $triple->{predicate},
568 $triple->{object}
569 192 );
570  
571 192 ▶ while (my $triple = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $triple = $iterator->next) 621 192 813
572 621 my $subject = $triple->subject->as_string;
573 621 my $predicate = $triple->predicate->uri_value;
574 621 my $object = $triple->object->value;
575  
576 621 my $qname = join "_" , $self->sn->qname($predicate);
577  
578 621 ▶ if ($qname =~ /^(hydra_variable|hydra_property)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($qname =~ /^(hydra_variable|hydra_property)$/) { } 144 477 621
elsif ($qname eq 'hydra_mapping') { } 72 405 477
elsif ($qname =~ /^(void|hydra)_/) { } 237 168 405
579 144 my $id= $triple->subject->value;
580  
581 144 $info->{"_$id"}->{$qname} = $object;
582 }
583 elsif ($qname eq 'hydra_mapping') {
584 72 my $id= $triple->subject->value;
585  
586 72 push @{$info->{"_$id"}->{$qname}} , $object;
587 }
588 elsif ($qname =~ /^(void|hydra)_/) {
589 237 $info->{$qname} = $object;
590 }
591 else {
592 168 push @{$info->{$qname}} , $object;
593 }
594 }
595  
596 192 $info;
597 }
598  
599 46 ▶sub _is_array_ref {
Subroutine _is_array_ref: called CC=1 SCAR=0.0
600 46 return ref($_[0]) eq 'ARRAY';
601 }
602  
603 777 ▶sub _is_hash_ref {
Subroutine _is_hash_ref: called CC=1 SCAR=0.0
604 777 return ref($_[0]) eq 'HASH';
605 }
606  
607 89 ▶sub _is_blessed {
Subroutine _is_blessed: called CC=1 SCAR=0.0
608 89 return ref($_[0]) =~ /\S/;
609 }
610  
611 111 ▶sub _is_string {
Subroutine _is_string: called CC=4 SCAR=14.9
612 111 ▶ return defined($_[0]) && !ref($_[0]) && ref(\$_[0]) ne 'GLOB' && length($_[0]) > 0;
Condition66%6/9
defined $_[0] && !ref($_[0]) && ref \$_[0] ne 'GLOB' && length $_[0] > 0
!ll&&!rl&&r
30108
defined $_[0] and not ref $_[0] and ref \$_[0] ne "GLOB"
!ll&&!rl&&r
30108
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
30108
MC/DC25%1/4
defined $_[0] && !ref($_[0]) && ref \$_[0] ne 'GLOB' && length $_[0] > 0
defined $_[0] not ref $_[0] ref \$_[0] ne "GLOB" length $_[0] > 0
Truth table40%2/5
A and not(B) and C && D
A= defined $_[0]B= not ref $_[0]C= ref \$_[0] ne "GLOB"D= length $_[0] > 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
613 }
614  
615 5 1;
616  
617 __END__
↑ Summary Trine/Store/LDF.pm »