RDF/RDB2RDF/DirectMapping/Store/Mixin/SuperGetPattern.pm

filter: Statementstmt 13.7% 17 / 124 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 50.0% 5 / 10 Podpod n/a totaltotal 12.0% 22 / 182 CC 26 SCAR 55.1
CC
26
Cov
10%
_SUPER_get_pattern
420.0
_sort_mapped_data
42.0
__ANON__
6.0
CRAP 247.8
« RDF/RDB2RDF/DirectMapping/Store.pm ↑ Summary RDF/RDB2RDF/DirectMapping.pm »
1 package RDF::RDB2RDF::DirectMapping::Store::Mixin::SuperGetPattern;
2  
3 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Scalar::Util qw[blessed reftype];
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 our $AUTHORITY = 'cpan:TOBYINK';
12 5 our $VERSION = '0.008';
13  
14 # This was removed from RDF::Trine::Store. :-(
15 sub _SUPER_get_pattern
16 0 ▶{
Subroutine _SUPER_get_pattern: not called CC=20 SCAR=60.4
17 0 my $self = shift;
18 0 my $bgp = shift;
19 0 my $context = shift;
20 0 my @args = @_;
21 0 my %args = @args;
22
23 0 ▶ if ($bgp->isa('RDF::Trine::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bgp->isa("RDF::Trine::Statement")) 0 0 0
24 0 $bgp = RDF::Trine::Pattern->new($bgp);
25 }
26
27 0 my %iter_args;
28 0 my @triples = $bgp->triples;
29
30 0 my ($iter);
31 0 ▶ if (1 == @triples)
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @triples) { } 0 0 0
32 {
33 0 my $t = shift @triples;
34 0 my @nodes = $t->nodes;
35 0 my $size = scalar @nodes;
36 0 my %vars;
37 0 my @names = qw(subject predicate object context);
38 0 foreach my $n (0 .. $#nodes)
39 {
40 0 ▶ if ($nodes[$n]->isa('RDF::Trine::Node::Variable'))
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$n]->isa("RDF::Trine::Node::Variable")) 0 0 0
41 {
42 0 $vars{ $names[ $n ] } = $nodes[$n]->name;
43 }
44 }
45
46 0 my $_iter = $self->get_statements(@nodes);
47 0 ▶ if ($_iter->finished)
Branch: 1 branch
BranchTrueFalseTotal
if ($_iter->finished) 0 0 0
48 {
49 0 return RDF::Trine::Iterator::Bindings->new( [], [] );
50 }
51
52 0 my @vars = values %vars;
53 my $sub = sub
54 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
55 0 ▶ my $row = $_iter->next or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $row = $_iter->next or return undef 0 0 0
56 0 my %data = map { $vars{$_} => eval { $row->$_ } } keys %vars;
57 0 return RDF::Trine::VariableBindings->new(\%data);
58 0 };
59
60 0 $iter = RDF::Trine::Iterator::Bindings->new($sub, \@vars);
61 }
62 else
63 {
64 0 my $t = shift(@triples);
65 0 my $rhs = $self->get_pattern( RDF::Trine::Pattern->new($t), $context, @args );
66 0 my $lhs = $self->get_pattern( RDF::Trine::Pattern->new(@triples), $context, @args );
67 0 my @inner;
68 0 ▶ while (my $row = $rhs->next)
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $rhs->next) 0 0 0
69 {
70 0 push @inner, $row;
71 }
72 0 my @results;
73 0 ▶ while (my $row = $lhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $lhs->next) 0 0 0
74 0 RESULT: foreach my $irow (@inner) {
75 0 my %keysa;
76 0 my @keysa = keys %$irow;
77 0 @keysa{ @keysa } = (1) x scalar(@keysa);
78 0 my @shared = grep { exists $keysa{ $_ } } keys %$row;
79 0 KEY: foreach my $key (@shared) {
80 0 my $val_a = $irow->{ $key };
81 0 my $val_b = $row->{ $key };
82 0 ▶ defined $val_a && defined $val_b
Branch: 1 branch
BranchTrueFalseTotal
defined $val_a and defined $val_b or next KEY 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
83 or next KEY;
84 0 ▶ $val_a->equal($val_b)
Branch: 1 branch
BranchTrueFalseTotal
$val_a->equal($val_b) or next RESULT 0 0 0
85 or next RESULT;
86 }
87
88 0 my $jrow = { (map { $_ => $irow->{$_} } grep { defined($irow->{$_}) } keys %$irow), (map { $_ => $row->{$_} } grep { defined($row->{$_}) } keys %$row) };
89 0 push @results, RDF::Trine::VariableBindings->new($jrow);
90 }
91 }
92 0 $iter = RDF::Trine::Iterator::Bindings->new(
93 \@results,
94 [ $bgp->referenced_variables ]
95 );
96 }
97
98 0 ▶ my $o = $args{orderby} or return $iter;
Branch: 1 branch
BranchTrueFalseTotal
my $o = $args{"orderby"} or return $iter 0 0 0
99
100 0 ▶ unless (reftype($o) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($o) eq "ARRAY") 0 0 0
101 {
102 0 throw RDF::Trine::Error::MethodInvocationError
103 -text => "The orderby argument to get_pattern must be an ARRAY reference";
104 }
105
106 0 my @order;
107 my %order;
108 0 my @o = @$o;
109 0 my @sorted_by;
110 0 my %vars = map { $_ => 1 } $bgp->referenced_variables;
111 0 ▶ if (scalar(@o) % 2 != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@o) % 2 != 0) 0 0 0
112 {
113 0 throw RDF::Trine::Error::MethodInvocationError
114 -text => "The orderby argument ARRAY to get_pattern must contain an even number of elements";
115 }
116 0 ▶ while (@o)
Branch: 1 branch
BranchTrueFalseTotal
if (@o) 0 0 0
117 {
118 0 my ($k,$dir) = splice @o, 0, 2, qw();
119 0 ▶ next unless ($vars{ $k });
Branch: 1 branch
BranchTrueFalseTotal
unless $vars{$k} 0 0 0
120 0 ▶ unless ($dir =~ m/^ASC|DESC$/i)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir =~ /^ASC|DESC$/i) 0 0 0
121 {
122 0 throw RDF::Trine::Error::MethodInvocationError
123 -text => "The sort direction for key $k must be either 'ASC' or 'DESC' in get_pattern call";
124 }
125 0 ▶ my $asc = ($dir eq 'ASC') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'ASC' ? : 0 0 0
126 0 push @order, $k;
127 0 $order{ $k } = $asc;
128 0 push @sorted_by, $k, $dir;
129 }
130
131 0 my @results = $iter->get_all;
132 0 @results = _sort_bindings( \@results, \@order, \%order );
133 0 $iter_args{ sorted_by } = \@sorted_by;
134 0 return RDF::Trine::Iterator::Bindings->new(
135 \@results,
136 [ $bgp->referenced_variables ],
137 %iter_args,
138 );
139 }
140  
141 sub _sort_bindings
142 0 ▶{
Subroutine _sort_bindings: not called CC=1 SCAR=6.9
143 0 my $res = shift;
144 0 my $o = shift;
145 0 my $dir = shift;
146 my @sorted =
147 0 map { $_->[0] }
148 0 sort { _sort_mapped_data($a,$b,$o,$dir) }
149 0 map { _map_sort_data( $_, $o ) }
150 @$res;
151 0 return @sorted;
152 }
153  
154 sub _sort_mapped_data
155 0 ▶{
Subroutine _sort_mapped_data: not called CC=6 SCAR=37.4
156 0 my $a = shift;
157 0 my $b = shift;
158 0 my $o = shift;
159 0 my $dir = shift;
160 0 foreach my $i (1 .. $#{ $a }){
161 0 my $av = $a->[ $i ];
162 0 my $bv = $b->[ $i ];
163 0 my $key = $o->[ $i-1 ];
164 0 ▶ next unless defined $av || defined $bv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $av or defined $bv 0 0 0
Condition0%0/3
defined $av or defined $bv
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $av or defined $bv
defined $av defined $bv
Truth table0%0/3
A or B
A= defined $avB= defined $bv
ABresult
1X1
011
000
165 0 my $cmp = RDF::Trine::Node::compare($av, $bv);
166 0 ▶ unless ($dir->{ $key })
Branch: 1 branch
BranchTrueFalseTotal
unless ($dir->{$key}) 0 0 0
167 {
168 0 $cmp *= -1;
169 }
170 0 ▶ return $cmp if ($cmp);
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 0 0 0
171 }
172 0 return 0;
173 }
174  
175 sub _map_sort_data
176 0 ▶{
Subroutine _map_sort_data: not called CC=1 SCAR=6.9
177 0 my $res = shift;
178 0 my $o = shift;
179 0 my @data = ($res, map { $res->{ $_ } } @$o);
180 0 return \@data;
181 }
182  
183 5 1;
184  
185 __END__
« RDF/RDB2RDF/DirectMapping/Store.pm ↑ Summary RDF/RDB2RDF/DirectMapping.pm »