Query/Algebra/Triple.pm

filter: Statementstmt 82.5% 99 / 120 Branchbran 64.2% 18 / 28 Conditioncond 55.5% 5 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 91.6% 22 / 24 Podpod 100% 9 / 9 totaltotal 79.5% 156 / 196 CC 23 SCAR 15.9
CC
23
Cov
78%
as_spin
20.0
new
8.8
subsumes
5.0
CRAP 4.9
« Query/Expression/Unary.pm ↑ Summary Query/Algebra/Service.pm »
1 # RDF::Query::Algebra::Triple
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::Triple - Algebra class for Triple patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::Triple version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::Triple;
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);
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(refaddr);
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 36 ▶use Scalar::Util qw(blessed reftype refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 36 ▶use RDF::Trine::Iterator qw(smap sgrep swatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30  
31 36 our ($VERSION);
32 36 my %TRIPLE_LABELS;
33 36 my @node_methods = qw(subject predicate object);
34 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 36 $VERSION = '2.919';
36 }
37  
38 ######################################################################
39  
40 =head1 METHODS
41  
42 Beyond the methods documented below, this class inherits methods from the
43 L<RDF::Query::Algebra> class.
44  
45 =over 4
46  
47 =cut
48  
49 =item C<new ( $s, $p, $o )>
50  
51 Returns a new Triple structure.
52  
53 =cut
54  
55 529 ▶sub new {
Subroutine new: called CC=7 SCAR=21.8
Pod documented
56 529 my $class = shift;
57 529 my @nodes = @_;
58 529 foreach my $i (0 .. 2) {
59 1587 ▶ unless (defined($nodes[ $i ])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes[$i]) 0 1587 1587
60 0 $nodes[ $i ] = RDF::Query::Node::Variable->new($node_methods[ $i ]);
61 }
62 1587 ▶ if (blessed($nodes[ $i ]) and not($nodes[ $i ]->isa('RDF::Query::Node'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($nodes[$i]) and not $nodes[$i]->isa("RDF::Query::Node")) 6 1581 1587
Condition66%2/3
builtin::blessed($nodes[$i]) and not $nodes[$i]->isa("RDF::Query::Node")
!ll&&!rl&&r
015816
MC/DC50%1/2
builtin::blessed($nodes[$i]) and not $nodes[$i]->isa("RDF::Query::Node")
builtin::blessed($nodes[$i]) not $nodes[$i]->isa("RDF::Query::Node")
Truth table66%2/3
A and not(B)
A= builtin::blessed($nodes[$i])B= not $nodes[$i]->isa("RDF::Query::Node")
ABresult
0X0
100
111
63 6 ▶ if ($nodes[ $i ]->isa('RDF::Trine::Node')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nodes[$i]->isa('RDF::Trine::Node')) { } 6 0 6
elsif ($nodes[$i]->does('Attean::API::TermOrVariable')) { } 0 0 0
64 6 $nodes[ $i ] = RDF::Query::Node->from_trine( $nodes[ $i ] );
65 } elsif ($nodes[ $i ]->does('Attean::API::TermOrVariable')){
66 0 $nodes[ $i ] = RDF::Query::Node->from_attean( $nodes[ $i ] );
67 }
68 }
69 }
70 529 return $class->_new( @nodes );
71 }
72  
73 721 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
74 721 my $class = shift;
75 721 return $class->SUPER::new( @_ );
76 }
77  
78 =item C<< as_sparql >>
79  
80 Returns the SPARQL string for this algebra expression.
81  
82 =cut
83  
84 111 ▶sub as_sparql {
Subroutine as_sparql: called CC=3 SCAR=11.0
Pod documented
85 111 my $self = shift;
86 111 my $context = shift;
87 111 my $indent = shift;
88
89 111 my $pred = $self->predicate;
90 111 ▶ 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') { } 7 104 111
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
5997
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
91 7 $pred = 'a';
92 } else {
93 104 $pred = $pred->as_sparql( $context );
94 }
95
96 111 my $subj = $self->subject->as_sparql( $context );
97 111 my $obj = $self->object->as_sparql( $context );
98 111 my $string = sprintf(
99 "%s %s %s .",
100 $subj,
101 $pred,
102 $obj,
103 );
104 111 return $string;
105 }
106  
107 =item C<< as_hash >>
108  
109 Returns the query as a nested set of plain data structures (no objects).
110  
111 =cut
112  
113 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
114 0 my $self = shift;
115 0 my $context = shift;
116 return {
117 type => lc($self->type),
118 0 nodes => [ map { $_->as_hash } $self->nodes ],
119 };
120 }
121  
122 =item C<< as_spin ( $model ) >>
123  
124 Adds statements to the given model to represent this algebra object in the
125 SPARQL Inferencing Notation (L<http://www.spinrdf.org/>).
126  
127 =cut
128  
129 0 ▶sub as_spin {
Subroutine as_spin: not called CC=4 SCAR=30.0
Pod documented
130 0 my $self = shift;
131 0 my $model = shift;
132 0 my $spin = RDF::Trine::Namespace->new('http://spinrdf.org/spin#');
133 0 my $t = RDF::Query::Node::Blank->new();
134 0 my @nodes = $self->nodes;
135 0 foreach (@nodes) {
136 0 ▶ if (blessed($_) and $_->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_) and $_->isa("RDF::Trine::Node::Variable")) 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa("RDF::Trine::Node::Variable")
builtin::blessed($_) $_->isa("RDF::Trine::Node::Variable")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("RDF::Trine::Node::Variable")
ABresult
0X0
100
111
137 0 $_ = RDF::Query::Node::Blank->new( "variable_" . $_->name );
138 }
139 }
140
141 0 $model->add_statement( RDF::Trine::Statement->new($t, $spin->subject, $nodes[0]) );
142 0 $model->add_statement( RDF::Trine::Statement->new($t, $spin->predicate, $nodes[1]) );
143 0 $model->add_statement( RDF::Trine::Statement->new($t, $spin->object, $nodes[2]) );
144 0 return $t;
145 }
146  
147 =item C<< referenced_blanks >>
148  
149 Returns a list of the blank node names used in this algebra expression.
150  
151 =cut
152  
153 338 ▶sub referenced_blanks {
Subroutine referenced_blanks: called CC=2 SCAR=7.0
Pod documented
154 338 my $self = shift;
155 338 my @nodes = $self->nodes;
156 338 ▶ my @blanks = grep { Carp::confess Dumper($_) unless blessed($_); $_->isa('RDF::Trine::Node::Blank') } @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_) 0 1014 1014
157 338 return map { $_->blank_identifier } @blanks;
158 }
159  
160 =item C<< subsumes ( $pattern ) >>
161  
162 Returns true if the triple subsumes the pattern, false otherwise.
163  
164 =cut
165  
166 15 ▶sub subsumes {
Subroutine subsumes: called CC=5 SCAR=16.1
Pod documented
167 15 my $self = shift;
168 15 my $pattern = shift;
169 15 ▶ return 0 unless ($pattern->isa('RDF::Trine::Statement'));
Branch: 1 branch
BranchTrueFalseTotal
unless $pattern->isa("RDF::Trine::Statement") 0 15 15
170 15 foreach my $method (@node_methods) {
171 38 my $snode = $self->$method();
172 38 ▶ next if ($snode->isa('RDF::Trine::Node::Variable'));
Branch: 1 branch
BranchTrueFalseTotal
if $snode->isa("RDF::Trine::Node::Variable") 11 27 38
173 27 my $pnode = $pattern->$method();
174 27 ▶ next if ($snode->equal( $pnode ));
Branch: 1 branch
BranchTrueFalseTotal
if $snode->equal($pnode) 21 6 27
175 6 return 0;
176 }
177 9 return 1;
178 }
179  
180 =item C<< bf () >>
181  
182 Returns a string representing the state of the nodes of the triple (bound or free).
183  
184 =cut
185  
186 24 ▶sub bf {
Subroutine bf: called CC=3 SCAR=11.0
Pod documented
187 24 my $self = shift;
188 24 my $bf = '';
189 24 foreach my $n ($self->nodes) {
190 72 ▶ $bf .= ($n->isa('RDF::Query::Node::Variable'))
Branch: 1 branch
BranchTrueFalseTotal
$n->isa('RDF::Query::Node::Variable') ? : 31 41 72
191 ? 'f'
192 : 'b';
193 }
194 24 return $bf;
195 }
196  
197 =item C<< distinguish_bnode_variables >>
198  
199 Returns a new Quad object with blank nodes replaced by distinguished variables.
200  
201 =cut
202  
203 284 ▶sub distinguish_bnode_variables {
Subroutine distinguish_bnode_variables: called CC=3 SCAR=11.0
Pod documented
204 284 my $self = shift;
205 284 my $class = ref($self);
206 284 my @nodes = $self->nodes;
207 284 foreach my $i (0 .. $#nodes) {
208 852 ▶ if ($nodes[$i]->isa('RDF::Query::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$i]->isa("RDF::Query::Node::Blank")) 38 814 852
209 38 $nodes[$i] = $nodes[$i]->make_distinguished_variable;
210 }
211 }
212 284 return $class->new( @nodes );
213 }
214  
215 2 ▶sub _from_sse {
Subroutine _from_sse: called CC=1 SCAR=0.0
216 2 my $class = shift;
217 2 return RDF::Trine::Statement->from_sse( @_ );
218 }
219  
220 =item C<< label ( $label => $value ) >>
221  
222 Sets the named C<< $label >> to C<< $value >> for this triple object.
223 If no C<< $value >> is given, returns the current label value, or undef if none
224 exists.
225  
226 =cut
227  
228 3 ▶sub label {
Subroutine label: called CC=3 SCAR=12.6
Pod documented
229 3 my $self = shift;
230 3 my $addr = refaddr($self);
231 3 my $label = shift;
232 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
233 0 my $value = shift;
234 0 $TRIPLE_LABELS{ $addr }{ $label } = $value;
235 }
236 3 ▶ if (exists $TRIPLE_LABELS{ $addr }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $TRIPLE_LABELS{$addr}) { } 0 3 3
237 0 return $TRIPLE_LABELS{ $addr }{ $label };
238 } else {
239 3 return;
240 }
241 }
242  
243 746 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
244 746 my $self = shift;
245 746 my $addr = refaddr( $self );
246 746 delete $TRIPLE_LABELS{ $addr };
247 }
248  
249 36 1;
250  
251 __END__
« Query/Expression/Unary.pm ↑ Summary Query/Algebra/Service.pm »