Attean/TermMap.pm

filter: Statementstmt 98.2% 109 / 111 Branchbran 88.8% 32 / 36 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 95.4% 21 / 22 Podpod 100% 6 / 6 totaltotal 95.0% 171 / 180 CC 20 SCAR 12.0
CC
20
Cov
95%
_cdt_recursive_handler
7.0
__ANON__
5.0
__ANON__
4.0
CRAP 3.3
« Test/Attean/TestIDPQueryPlanner.pm ↑ Summary AtteanX/Serializer/SPARQLCSV.pm »
1 51 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Attean::TermMap - Mapping terms to new terms
7  
8 =head1 VERSION
9  
10 This document describes Attean::TermMap version 0.041
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $m = Attean::TermMap->short_blank_map;
17 my $new_blank = $m->map( Attean::Blank->new('abcdefg') );
18 say $new_blank->ntriples_string; # _:a
19  
20 =head1 DESCRIPTION
21  
22 The Attean::TermMap class represents a one-way mapping process from and to
23 L<Attean::API::Term> objects. This mapping may rename the blank identifiers,
24 skolemize nodes, or map the nodes in some other, custom way.
25  
26 It conforms to the L<Attean::API::Mapper> role.
27  
28 =head1 ATTRIBUTES
29  
30 =over 4
31  
32 =item C<< mapper >>
33  
34 A CODE reference that will map L<Attean::API::Term> objects to (possibly different)
35 term objects.
36  
37 =back
38  
39 =head1 CLASS METHODS
40  
41 =over 4
42  
43 =cut
44  
45 51 package Attean::TermMap 0.041 {
46 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 51 ▶ use Types::Standard qw(CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 51 ▶ use Attean::API::Binding;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 51 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
52
53 51 with 'Attean::Mapper';
54 0 ▶ has 'mapper' => (is => 'ro', isa => CodeRef, default => sub { shift }, required => 1);
Subroutine __ANON__: not called CC=1 SCAR=1.2
55
56 45 ▶ around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
57 45 my $orig = shift;
58 45 my $class = shift;
59 45 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 0 45 45
60 0 return $class->$orig(mapper => shift);
61 }
62 45 return $class->$orig(@_);
63 51 };
64  
65 =item C<< canonicalization_map >>
66  
67 Returns a new L<Attean::TermMap> that canonicalizes recognized typed
68 L<Attean::API::Literal> values.
69  
70 =cut
71  
72 18 ▶ sub canonicalization_map {
Subroutine canonicalization_map: called CC=1 SCAR=0.0
Pod documented
73 18 my $class = shift;
74 18 my %map;
75
76 my $code;
77 141 ▶ $code = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
78 141 my $term = shift;
79 141 ▶ if (my $mapped = $class->_cdt_recursive_handler($code, $term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $class->_cdt_recursive_handler($code, $term)) 1 140 141
80 1 return $mapped;
81 }
82 140 ▶ return $term unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 124 16 140
83 16 ▶ if ($term->does('Attean::API::CanonicalizingLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::CanonicalizingLiteral")) 2 14 16
84 2 my $c = eval { $term->canonicalized_term };
85 2 ▶ return ($@) ? undef : $c;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 2 2
86 }
87
88 14 return $term;
89 18 };
90 18 return $class->new(mapper => $code);
91 }
92  
93  
94 270 ▶ sub _cdt_recursive_handler {
Subroutine _cdt_recursive_handler: called CC=7 SCAR=19.5
95 270 my $class = shift;
96 270 my $code = shift;
97 270 my $term = shift;
98 270 ▶ if ($term->does('Attean::API::Literal') and my $dt = $term->datatype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Literal") and my $dt = $term->datatype) 42 228 270
Condition66%2/3
$term->does("Attean::API::Literal") and my $dt = $term->datatype
!ll&&!rl&&r
228042
MC/DC50%1/2
$term->does("Attean::API::Literal") and my $dt = $term->datatype
$term->does("Attean::API::Literal") my $dt = $term->datatype
Truth table66%2/3
A and B
A= $term->does("Attean::API::Literal")B= my $dt = $term->datatype
ABresult
0X0
100
111
99 42 my $dturi = $dt->value;
100 42 ▶ if ($dturi eq "$AtteanX::Functions::CompositeLists::LIST_TYPE_IRI") {
Branch: 2 branches
BranchTrueFalseTotal
if ($dturi eq "$AtteanX::Functions::CompositeLists::LIST_TYPE_IRI") { } 4 38 42
elsif ($dturi eq "$AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI") { } 3 35 38
101 4 my $mapper = $class->new( mapper => $code );
102 4 my $mapped = eval { AtteanX::Functions::CompositeLists->map_terms($mapper, $term) };
103 4 ▶ warn $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
104 4 return $mapped;
105 } elsif ($dturi eq "$AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI") {
106 3 my $mapper = $class->new( mapper => $code );
107 3 my $mapped = eval { AtteanX::Functions::CompositeMaps->map_terms($mapper, $term) };
108 3 ▶ warn $@ if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
109 3 return $mapped;
110 }
111 }
112 263 return;
113 }
114  
115 =item C<< uuid_blank_map >>
116  
117 Returns a new L<Attean::TermMap> that renames blank nodes with UUID values.
118  
119 =cut
120  
121 4 ▶ sub uuid_blank_map {
Subroutine uuid_blank_map: called CC=1 SCAR=0.0
Pod documented
122 4 my $class = shift;
123 4 my %map;
124
125 my $code;
126 31 ▶ $code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
127 31 my $term = shift;
128 31 ▶ if (my $mapped = $class->_cdt_recursive_handler($code, $term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $class->_cdt_recursive_handler($code, $term)) 2 29 31
129 2 return $mapped;
130 }
131 29 ▶ return $term unless ($term->does('Attean::API::Blank'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Blank") 16 13 29
132 13 my $id = $term->value;
133 13 ▶ return $map{$id} if (defined($map{$id}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $map{$id} 7 6 13
134
135 6 my $uuid = unpack('H*', create_uuid());
136 6 my $new = Attean::Blank->new( 'b' . $uuid );
137 6 $map{$id} = $new;
138 6 return $new;
139 4 };
140
141 4 return $class->new(mapper => $code);
142 }
143  
144 =item C<< short_blank_map >>
145  
146 Returns a new L<Attean::TermMap> that renames blank nodes with short
147 alphabetic names (e.g. _:a, _:b).
148  
149 =cut
150  
151 2 ▶ sub short_blank_map {
Subroutine short_blank_map: called CC=1 SCAR=0.0
Pod documented
152 2 my $class = shift;
153 2 my %map;
154 2 my $next = 'a';
155
156 2 my $code;
157 20 ▶ $code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
158 20 my $term = shift;
159 20 ▶ if (my $mapped = $class->_cdt_recursive_handler($code, $term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $class->_cdt_recursive_handler($code, $term)) 2 18 20
160 2 return $mapped;
161 }
162 18 ▶ return $term unless ($term->does('Attean::API::Blank'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Blank") 8 10 18
163 10 my $id = $term->value;
164 10 ▶ if (defined(my $t = $map{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $t = $map{$id})) { } 4 6 10
165 4 return $t;
166 } else {
167 6 my $new = Attean::Blank->new( $next++ );
168 6 $map{$id} = $new;
169 6 return $new;
170 }
171 2 };
172  
173 2 return $class->new(mapper => $code);
174 }
175  
176 =item C<< rewrite_map( \%map ) >>
177  
178 Given C<< %map >> whose keys are term C<< as_string >> serializations, and
179 objects are L<Attean::API::Term> objects, returns a new term map object that
180 maps terms matching entries in C<< %map >>, and all other terms to themselves.
181  
182 =cut
183  
184 14 ▶ sub rewrite_map {
Subroutine rewrite_map: called CC=1 SCAR=0.0
Pod documented
185 14 my $class = shift;
186 14 my $map = shift;
187
188 14 my $code;
189 78 ▶ $code = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
190 78 my $term = shift;
191 78 ▶ if (my $mapped = $class->_cdt_recursive_handler($code, $term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $class->_cdt_recursive_handler($code, $term)) 2 76 78
192 2 return $mapped;
193 }
194 76 ▶ return $map->{ $term->as_string } if (exists $map->{ $term->as_string });
Branch: 1 branch
BranchTrueFalseTotal
if exists $$map{$term->as_string} 32 44 76
195 44 return $term;
196 14 };
197
198 14 return $class->new(mapper => $code);
199 }
200  
201 =back
202  
203 =head1 METHODS
204  
205 =over 4
206  
207 =item C<< map( $term ) >>
208  
209 Returns the term that is mapped to by the supplied C<< $term >>.
210  
211 =cut
212  
213 270 ▶ sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
214 270 my $self = shift;
215 270 my $term = shift;
216 270 return $self->mapper->( $term );
217 }
218  
219 =item C<< binding_mapper >>
220  
221 Returns a mapping function reference that maps L<Attean::API::Binding>
222 objects by mapping their constituent mapped L<Attean::API::Term> objects.
223  
224 =cut
225  
226 2 ▶ sub binding_mapper {
Subroutine binding_mapper: called CC=1 SCAR=0.0
Pod documented
227 2 my $self = shift;
228 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
229 4 my $binding = shift;
230 4 return $binding->apply_map($self);
231 }
232 2 }
233 }
234  
235 51 1;
236  
237 __END__
« Test/Attean/TestIDPQueryPlanner.pm ↑ Summary AtteanX/Serializer/SPARQLCSV.pm »