RDF/Trine/VariableBindings.pm

filter: Statementstmt 28.4% 31 / 109 Branchbran 10.0% 2 / 20 Conditioncond 4.3% 1 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 56.2% 9 / 16 Podpod 100% 8 / 8 totaltotal 26.9% 51 / 189 CC 23 SCAR 37.3
CC
23
Cov
22%
copy_labels_from
110.0
join
56.0
label
12.0
CRAP 41.7
« RDF/Trine/Node/Literal.pm ↑ Summary RDF/Trine/Parser/LineProtocol.pm »
1 # RDF::Trine::VariableBindings
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::VariableBindings - Variable bindings
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::VariableBindings version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine qw(literal);
15 use RDF::Trine::VariableBindings;
16 my $vb = RDF::Trine::VariableBindings->new( {} );
17 $vb->set( foo => literal("bar") );
18 $vb->set( baz => literal("blee") );
19 $vb->variables; # qw(foo baz)
20
21 my $x = RDF::Trine::VariableBindings->new( { foo => literal("bar") } );
22 $x->set( greeting => literal("hello") );
23  
24 my $j = $vb->join( $x ); # { foo => "bar", baz => "blee", greeting => "hello" }
25  
26 my @keys = qw(baz greeting);
27 my $p = $j->project( @keys ); # { baz => "blee", greeting => "hello" }
28 print $p->{greeting}->literal_value; # "hello"
29  
30 =head1 DESCRIPTION
31  
32 RDF::Trine::VariableBindings objects provide a mapping from variable names to
33 RDF::Trine::Node objects. The objects may be used as a hash reference, with
34 variable names used as hash keys.
35  
36 =head1 METHODS
37  
38 =over 4
39  
40 =cut
41  
42 package RDF::Trine::VariableBindings;
43  
44 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 68 ▶use overload '""' => sub { $_[0]->as_string };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
47  
48 68 my %VB_LABELS;
49  
50 68 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 ######################################################################
53  
54 68 our ($VERSION);
55 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
56 68 $VERSION = '1.019';
57 }
58  
59 ######################################################################
60  
61 =item C<< new ( \%bindings ) >>
62  
63 =cut
64  
65 1856 ▶sub new {
Subroutine new: called CC=4 SCAR=19.2
Pod documented
66 1856 my $class = shift;
67 1856 my $bindings = shift;
68 1856 my $self = bless( { %$bindings }, $class );
69
70 1856 ▶ if (blessed($bindings) and $bindings->isa('RDF::Trine::VariableBindings')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($bindings) and $bindings->isa("RDF::Trine::VariableBindings")) 0 1856 1856
Condition33%1/3
builtin::blessed($bindings) and $bindings->isa("RDF::Trine::VariableBindings")
!ll&&!rl&&r
185600
MC/DC0%0/2
builtin::blessed($bindings) and $bindings->isa("RDF::Trine::VariableBindings")
builtin::blessed($bindings) $bindings->isa("RDF::Trine::VariableBindings")
Truth table33%1/3
A and B
A= builtin::blessed($bindings)B= $bindings->isa("RDF::Trine::VariableBindings")
ABresult
0X0
100
111
71 0 my $addr = refaddr($bindings);
72 0 ▶ if (ref($VB_LABELS{ $addr })) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $VB_LABELS{$addr}) 0 0 0
73 0 $VB_LABELS{ refaddr($self) } = { %{ $VB_LABELS{ $addr } } };
74 }
75 }
76
77 1856 return $self;
78 }
79  
80 =item C<< set ( $variable_name => $node ) >>
81  
82 =cut
83  
84 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 0 my $name = shift;
87 0 my $node = shift;
88 0 $self->{ $name } = $node;
89 }
90  
91 =item C<< join ( $row ) >>
92  
93 Returns a new VariableBindings object based on the join of this object and C<< $row >>.
94 If the two variable binding objects cannot be joined, returns undef.
95  
96 =cut
97  
98 0 ▶sub join {
Subroutine join: not called CC=7 SCAR=40.3
Pod documented
99 0 my $self = shift;
100 0 my $class = ref($self);
101 0 my $rowb = shift;
102
103 0 my %keysa;
104 0 my @keysa = keys %$self;
105 0 @keysa{ @keysa } = (1) x scalar(@keysa);
106 0 my @shared = grep { exists $keysa{ $_ } } (keys %$rowb);
107 0 foreach my $key (@shared) {
108 0 my $val_a = $self->{ $key };
109 0 my $val_b = $rowb->{ $key };
110 0 ▶ next unless (defined($val_a) and defined($val_b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val_a and defined $val_b 0 0 0
Condition0%0/3
defined $val_a and defined $val_b
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val_a and defined $val_b
defined $val_a defined $val_b
Truth table0%0/3
A and B
A= defined $val_aB= defined $val_b
ABresult
0X0
100
111
111 0 ▶ my $equal = (refaddr($val_a) == refaddr($val_b)) || ($val_a == $val_b) || $val_a->equal( $val_b );
Condition0%0/6
builtin::refaddr($val_a) == builtin::refaddr($val_b) || $val_a == $val_b || $val_a->equal($val_b)
l!l&&r!l&&!r
000
builtin::refaddr($val_a) == builtin::refaddr($val_b) or $val_a == $val_b
l!l&&r!l&&!r
000
MC/DC0%0/3
builtin::refaddr($val_a) == builtin::refaddr($val_b) || $val_a == $val_b || $val_a->equal($val_b)
builtin::refaddr($val_a) == builtin::refaddr($val_b) $val_a == $val_b $val_a->equal($val_b)
Truth table0%0/4
A or B || C
A= builtin::refaddr($val_a) == builtin::refaddr($val_b)B= $val_a == $val_bC= $val_a->equal($val_b)
ABCresult
1XX1
01X1
0011
0000
112 0 ▶ unless ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 0 0 0
113 0 return;
114 }
115 }
116
117 0 my $row = { (map { $_ => $self->{$_} } grep { defined($self->{$_}) } keys %$self), (map { $_ => $rowb->{$_} } grep { defined($rowb->{$_}) } keys %$rowb) };
118 0 my $joined = $class->new( $row );
119 0 $joined->copy_labels_from( $self );
120 0 $joined->copy_labels_from( $rowb );
121
122 0 return $joined;
123 }
124  
125 =item C<< variables >>
126  
127 =cut
128  
129 0 ▶sub variables {
Subroutine variables: not called CC=1 SCAR=6.9
Pod documented
130 0 my $self = shift;
131 0 return (keys %$self);
132 }
133  
134 =item C<< project ( @keys ) >>
135  
136 Returns a new binding with values for only the keys listed.
137  
138 =cut
139  
140 0 ▶sub project {
Subroutine project: not called CC=2 SCAR=17.9
Pod documented
141 0 my $self = shift;
142 0 my $class = ref($self);
143 0 my @keys = @_;
144 0 my %data = map { $_ => $self->{ $_ } } @keys;
145 0 my $p = $class->new( \%data );
146
147 0 my $addr = refaddr($self);
148 0 ▶ if (ref($VB_LABELS{ $addr })) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $VB_LABELS{$addr}) 0 0 0
149 0 $VB_LABELS{ refaddr($p) } = { %{ $VB_LABELS{ $addr } } };
150 }
151
152 0 return $p;
153 }
154  
155 =item C<< as_string >>
156  
157 Returns a string representation of the variable bindings.
158  
159 =cut
160  
161 1895 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.0
Pod documented
162 1895 my $self = shift;
163 1895 my @keys = sort keys %$self;
164 1895 ▶ my $string = sprintf('{ %s }', CORE::join(', ', map { CORE::join('=', $_, ($self->{$_}) ? $self->{$_}->as_string : '()') } (@keys)));
Branch: 1 branch
BranchTrueFalseTotal
$self->{$_} ? : 6733 0 6733
165 1895 return $string;
166 }
167  
168 =item C<< label ( $label => $value ) >>
169  
170 Sets the named C<< $label >> to C<< $value >> for this variable bindings object.
171 If no C<< $value >> is given, returns the current label value, or undef if none
172 exists.
173  
174 =cut
175  
176 0 ▶sub label {
Subroutine label: not called CC=3 SCAR=24.8
Pod documented
177 0 my $self = shift;
178 0 my $addr = refaddr($self);
179 0 my $label_name = shift;
180 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
181 0 my $value = shift;
182 0 $VB_LABELS{ $addr }{ $label_name } = $value;
183 }
184
185 0 my $labels = $VB_LABELS{ $addr };
186 0 ▶ if (ref($labels)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $labels) { } 0 0 0
187 0 my $value = $labels->{ $label_name };
188 0 return $value;
189 } else {
190 0 return;
191 }
192 }
193  
194 =item C<< copy_labels_from ( $vb ) >>
195  
196 Copies the labels from C<< $vb >>, adding them to the labels for this object.
197  
198 =cut
199  
200 0 ▶sub copy_labels_from {
Subroutine copy_labels_from: not called CC=10 SCAR=47.0
Pod documented
201 0 my $self = shift;
202 0 my $rowa = shift;
203 0 my $self_labels = $VB_LABELS{ refaddr($self) };
204 0 my $a_labels = $VB_LABELS{ refaddr($rowa) };
205 0 ▶ if ($self_labels or $a_labels) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_labels or $a_labels) 0 0 0
Condition0%0/3
$self_labels or $a_labels
l!l&&r!l&&!r
000
MC/DC0%0/2
$self_labels or $a_labels
$self_labels $a_labels
Truth table0%0/3
A or B
A= $self_labelsB= $a_labels
ABresult
1X1
011
000
206 0 ▶ $self_labels ||= {};
Condition0%0/2
$self_labels ||= {}
l!l
00
MC/DC0%0/1
$self_labels ||= {}
$self_labels
Truth table0%0/2
A
A= $self_labels
Aresult
11
00
207 0 ▶ $a_labels ||= {};
Condition0%0/2
$a_labels ||= {}
l!l
00
MC/DC0%0/1
$a_labels ||= {}
$a_labels
Truth table0%0/2
A
A= $a_labels
Aresult
11
00
208 0 my %new_labels = ( %$self_labels, %$a_labels );
209
210 0 ▶ if (exists $new_labels{'origin'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $new_labels{"origin"}) 0 0 0
211 0 my %origins;
212 0 ▶ foreach my $o (@{ $self_labels->{'origin'} || [] }) {
Condition0%0/2
$self_labels->{'origin'} || []
l!l
00
MC/DC0%0/1
$self_labels->{'origin'} || []
$self_labels->{'origin'}
Truth table0%0/2
A
A= $self_labels->{'origin'}
Aresult
11
00
213 0 $origins{ $o }++;
214 }
215 0 ▶ foreach my $o (@{ $a_labels->{'origin'} || [] }) {
Condition0%0/2
$a_labels->{'origin'} || []
l!l
00
MC/DC0%0/1
$a_labels->{'origin'} || []
$a_labels->{'origin'}
Truth table0%0/2
A
A= $a_labels->{'origin'}
Aresult
11
00
216 0 $origins{ $o }++;
217 }
218 0 $new_labels{'origin'} = [ keys %origins ];
219 }
220
221 0 $VB_LABELS{ refaddr($self) } = \%new_labels;
222 }
223 }
224  
225 0 ▶sub _labels {
Subroutine _labels: not called CC=1 SCAR=6.9
226 0 my $self = shift;
227 0 my $addr = refaddr($self);
228 0 my $labels = $VB_LABELS{ $addr };
229 0 return $labels;
230 }
231  
232 1856 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
233 1856 my $self = shift;
234 1856 my $addr = refaddr( $self );
235 1856 delete $VB_LABELS{ $addr };
236 1856 return;
237 }
238  
239 68 1;
240  
241 __END__
« RDF/Trine/Node/Literal.pm ↑ Summary RDF/Trine/Parser/LineProtocol.pm »