Query/Algebra/Quad.pm

filter: Statementstmt 54.5% 60 / 110 Branchbran 25.0% 7 / 28 Conditioncond 42.8% 6 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 77.7% 14 / 18 Podpod 100% 8 / 8 totaltotal 52.4% 98 / 187 CC 24 SCAR 34.2
CC
24
Cov
48%
qualify_uris
110.0
bf
12.0
label
12.0
CRAP 30.5
« Query/Plan/Triple.pm ↑ Summary Query/Plan/Path.pm »
1 # RDF::Query::Algebra::Quad
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::Quad - Algebra class for Quad patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::Quad version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::Quad;
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::Algebra RDF::Trine::Statement::Quad);
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 Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use RDF::Trine::Iterator qw(smap sgrep swatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ######################################################################
27  
28 36 my %QUAD_LABELS;
29 36 our ($VERSION);
30 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 36 $VERSION = '2.919';
32 }
33  
34 ######################################################################
35  
36 =head1 METHODS
37  
38 Beyond the methods documented below, this class inherits methods from the
39 L<RDF::Query::Algebra> class.
40  
41 =over 4
42  
43 =cut
44  
45 =item C<new ( $s, $p, $o, $g )>
46  
47 Returns a new Quad structure.
48  
49 =cut
50  
51 426 ▶sub new {
Subroutine new: called CC=6 SCAR=19.4
Pod documented
52 426 my $class = shift;
53 426 my @nodes = @_;
54 426 ▶ unless (scalar(@nodes) == 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @nodes == 4) 0 426 426
55 0 throw RDF::Query::Error::MethodInvocationError -text => "Quad constructor must have four node arguments";
56 }
57 426 my @names = qw(subject predicate object context);
58 426 foreach my $i (0 .. 3) {
59 1704 ▶ unless (defined($nodes[ $i ]) and blessed($nodes[ $i ])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes[$i] and builtin::blessed($nodes[$i])) 0 1704 1704
Condition33%1/3
defined $nodes[$i] and builtin::blessed($nodes[$i])
!ll&&!rl&&r
001704
MC/DC0%0/2
defined $nodes[$i] and builtin::blessed($nodes[$i])
defined $nodes[$i] builtin::blessed($nodes[$i])
Truth table33%1/3
A and B
A= defined $nodes[$i]B= builtin::blessed($nodes[$i])
ABresult
0X0
100
111
60 0 $nodes[ $i ] = RDF::Query::Node::Variable->new($names[ $i ]);
61 }
62 1704 ▶ unless ($nodes[ $i ]->isa('RDF::Query::Node')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nodes[$i]->isa("RDF::Query::Node")) 358 1346 1704
63 358 $nodes[ $i ] = RDF::Query::Node->from_trine( $nodes[ $i ] );
64 }
65 }
66
67 426 return $class->SUPER::new( @nodes );
68 }
69  
70 =item C<< as_sparql >>
71  
72 Returns the SPARQL string for this algebra expression.
73  
74 =cut
75  
76 110 ▶sub as_sparql {
Subroutine as_sparql: called CC=4 SCAR=13.9
Pod documented
77 110 my $self = shift;
78 110 ▶ my $context = shift || {};
Condition100%2/2
shift() || {}
l!l
2108
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
79 110 my $indent = shift;
80
81 110 my $pred = $self->predicate;
82 110 ▶ if ($pred->isa('RDF::Trine::Node::Resource') and $pred->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->isa('RDF::Trine::Node::Resource') and $pred->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') { } 8 102 110
Condition100%3/3
$pred->isa("RDF::Trine::Node::Resource") and $pred->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
!ll&&!rl&&r
21008
MC/DC100%2/2
$pred->isa("RDF::Trine::Node::Resource") and $pred->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
$pred->isa("RDF::Trine::Node::Resource") $pred->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
Truth table100%3/3
A and B
A= $pred->isa("RDF::Trine::Node::Resource")B= $pred->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
ABresult
0X0
100
111
83 8 $pred = 'a';
84 } else {
85 102 $pred = $pred->as_sparql( $context );
86 }
87
88 110 my $string = sprintf(
89 "%s %s %s .",
90 $self->subject->as_sparql( $context ),
91 $pred,
92 $self->object->as_sparql( $context ),
93 );
94 110 return $string;
95 }
96  
97 =item C<< as_hash >>
98  
99 Returns the query as a nested set of plain data structures (no objects).
100  
101 =cut
102  
103 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
104 0 my $self = shift;
105 0 my $context = shift;
106 return {
107 type => lc($self->type),
108 0 nodes => [ map { $_->as_hash } $self->nodes ],
109 };
110 }
111  
112 =item C<< referenced_blanks >>
113  
114 Returns a list of the blank node names used in this algebra expression.
115  
116 =cut
117  
118 10 ▶sub referenced_blanks {
Subroutine referenced_blanks: called CC=1 SCAR=0.0
Pod documented
119 10 my $self = shift;
120 10 my @nodes = $self->nodes;
121 10 my @blanks = grep { $_->isa('RDF::Trine::Node::Blank') } @nodes;
122 0 return map { $_->blank_identifier } @blanks;
123 }
124  
125 =item C<< qualify_uris ( \%namespaces, $base_uri ) >>
126  
127 Returns a new algebra pattern where all referenced Resource nodes representing
128 QNames (ns:local) are qualified using the supplied %namespaces.
129  
130 =cut
131  
132 0 ▶sub qualify_uris {
Subroutine qualify_uris: not called CC=10 SCAR=47.0
Pod documented
133 0 my $self = shift;
134 0 my $class = ref($self);
135 0 my $ns = shift;
136 0 my $base_uri = shift;
137 0 my @nodes;
138 0 foreach my $n ($self->nodes) {
139 0 my $blessed = blessed($n);
140 0 ▶ if ($blessed and $n->isa('RDF::Query::Node::Resource')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($blessed and $n->isa('RDF::Query::Node::Resource')) { } 0 0 0
elsif ($blessed and $n->isa('RDF::Query::Node::Literal')) { } 0 0 0
Condition0%0/3
$blessed and $n->isa("RDF::Query::Node::Resource")
!ll&&!rl&&r
000
MC/DC0%0/2
$blessed and $n->isa("RDF::Query::Node::Resource")
$blessed $n->isa("RDF::Query::Node::Resource")
Truth table0%0/3
A and B
A= $blessedB= $n->isa("RDF::Query::Node::Resource")
ABresult
0X0
100
111
141 0 my $uri = $n->uri;
142 0 ▶ if (ref($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri) { } 0 0 0
143 0 my ($n,$l) = @$uri;
144 0 ▶ unless (exists($ns->{ $n })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ns->{$n}) 0 0 0
145 0 throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined";
146 }
147 0 my $resolved = RDF::Query::Node::Resource->new( join('', $ns->{ $n }, $l), $base_uri );
148 0 push(@nodes, $resolved);
149 } else {
150 0 push(@nodes, $n);
151 }
152 ▶ } elsif ($blessed and $n->isa('RDF::Query::Node::Literal')) {
Condition0%0/3
$blessed and $n->isa("RDF::Query::Node::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
$blessed and $n->isa("RDF::Query::Node::Literal")
$blessed $n->isa("RDF::Query::Node::Literal")
Truth table0%0/3
A and B
A= $blessedB= $n->isa("RDF::Query::Node::Literal")
ABresult
0X0
100
111
153 0 my $node = $n;
154 0 my $dt = $node->literal_datatype;
155 0 ▶ if (ref($dt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dt) { } 0 0 0
156 0 my ($n,$l) = @$dt;
157 0 ▶ unless (exists($ns->{ $n })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ns->{$n}) 0 0 0
158 0 throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined";
159 }
160 0 my $resolved = RDF::Query::Node::Resource->new( join('', $ns->{ $n }, $l), $base_uri );
161 0 my $lit = RDF::Query::Node::Literal->new( $node->literal_value, undef, $resolved->uri_value );
162 0 push(@nodes, $lit);
163 } else {
164 0 push(@nodes, $node);
165 }
166 } else {
167 0 push(@nodes, $n);
168 }
169 }
170 0 return $class->new( @nodes );
171 }
172  
173 =item C<< bf () >>
174  
175 Returns a string representing the state of the nodes of the triple (bound or free).
176  
177 =cut
178  
179 0 ▶sub bf {
Subroutine bf: not called CC=3 SCAR=24.8
Pod documented
180 0 my $self = shift;
181 0 my $bf = '';
182 0 foreach my $n ($self->nodes) {
183 0 ▶ $bf .= ($n->isa('RDF::Query::Node::Variable'))
Branch: 1 branch
BranchTrueFalseTotal
$n->isa('RDF::Query::Node::Variable') ? : 0 0 0
184 ? 'f'
185 : 'b';
186 }
187 0 return $bf;
188 }
189  
190 =item C<< distinguish_bnode_variables >>
191  
192 Returns a new Quad object with blank nodes replaced by distinguished variables.
193  
194 =cut
195  
196 80 ▶sub distinguish_bnode_variables {
Subroutine distinguish_bnode_variables: called CC=3 SCAR=11.3
Pod documented
197 80 my $self = shift;
198 80 my $class = ref($self);
199 80 my @nodes = $self->nodes;
200 80 foreach my $i (0 .. $#nodes) {
201 320 ▶ if ($nodes[$i]->isa('RDF::Query::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$i]->isa("RDF::Query::Node::Blank")) 0 320 320
202 0 $nodes[$i] = $nodes[$i]->make_distinguished_variable;
203 }
204 }
205 80 return $class->new( @nodes );
206 }
207  
208 =item C<< label ( $label => $value ) >>
209  
210 Sets the named C<< $label >> to C<< $value >> for this quad object.
211 If no C<< $value >> is given, returns the current label value, or undef if none
212 exists.
213  
214 =cut
215  
216 0 ▶sub label {
Subroutine label: not called CC=3 SCAR=24.8
Pod documented
217 0 my $self = shift;
218 0 my $addr = refaddr($self);
219 0 my $label = shift;
220 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
221 0 my $value = shift;
222 0 $QUAD_LABELS{ $addr }{ $label } = $value;
223 }
224 0 ▶ if (exists $QUAD_LABELS{ $addr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $QUAD_LABELS{$addr}) { } 0 0 0
225 0 return $QUAD_LABELS{ $addr }{ $label };
226 } else {
227 0 return;
228 }
229 }
230  
231 426 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
232 426 my $self = shift;
233 426 my $addr = refaddr( $self );
234 426 delete $QUAD_LABELS{ $addr };
235 }
236  
237  
238 36 1;
239  
240 __END__
« Query/Plan/Triple.pm ↑ Summary Query/Plan/Path.pm »