Attean/TermMap.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 88.8% 16 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.0% 19 / 20 Podpod 100% 6 / 6 totaltotal 95.8% 116 / 121 CC 10 SCAR 6.8
CC
10
Cov
96%
__ANON__
4.0
__ANON__
3.0
__ANON__
3.0
CRAP 2.0
« Test/Attean/MutableTripleStore.pm ↑ Summary AtteanX/Serializer/SPARQLTSV.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶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.040
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 50 package Attean::TermMap 0.040 {
46 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 50 ▶ use Types::Standard qw(CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 50 ▶ use Attean::API::Binding;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 50 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
50 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
51
52 50 with 'Attean::Mapper';
53 0 ▶ has 'mapper' => (is => 'ro', isa => CodeRef, default => sub { shift }, required => 1);
Subroutine __ANON__: not called CC=1 SCAR=1.2
54
55 35 ▶ around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
56 35 my $orig = shift;
57 35 my $class = shift;
58 35 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 0 35 35
59 0 return $class->$orig(mapper => shift);
60 }
61 35 return $class->$orig(@_);
62 50 };
63  
64 =item C<< canonicalization_map >>
65  
66 Returns a new L<Attean::TermMap> that canonicalizes recognized typed
67 L<Attean::API::Literal> values.
68  
69 =cut
70  
71 17 ▶ sub canonicalization_map {
Subroutine canonicalization_map: called CC=1 SCAR=0.0
Pod documented
72 17 my $class = shift;
73 17 my %map;
74 137 ▶ return $class->new(mapper => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
75 137 my $term = shift;
76 137 ▶ return $term unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 122 15 137
77
78 15 ▶ if ($term->does('Attean::API::CanonicalizingLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::CanonicalizingLiteral")) 1 14 15
79 1 my $c = eval { $term->canonicalized_term };
80 1 ▶ return ($@) ? undef : $c;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 1 1
81 }
82
83 14 return $term;
84 17 });
85 }
86  
87 =item C<< uuid_blank_map >>
88  
89 Returns a new L<Attean::TermMap> that renames blank nodes with UUID values.
90  
91 =cut
92  
93 3 ▶ sub uuid_blank_map {
Subroutine uuid_blank_map: called CC=1 SCAR=0.0
Pod documented
94 3 my $class = shift;
95 3 my %map;
96 25 ▶ return $class->new(mapper => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
97 25 my $term = shift;
98 25 ▶ return $term unless ($term->does('Attean::API::Blank'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Blank") 16 9 25
99 9 my $id = $term->value;
100 9 ▶ return $map{$id} if (defined($map{$id}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $map{$id} 5 4 9
101
102 4 my $uuid = unpack('H*', create_uuid());
103 4 my $new = Attean::Blank->new( 'b' . $uuid );
104 4 $map{$id} = $new;
105 4 return $new;
106 3 });
107 }
108  
109 =item C<< short_blank_map >>
110  
111 Returns a new L<Attean::TermMap> that renames blank nodes with short
112 alphabetic names (e.g. _:a, _:b).
113  
114 =cut
115  
116 1 ▶ sub short_blank_map {
Subroutine short_blank_map: called CC=1 SCAR=0.0
Pod documented
117 1 my $class = shift;
118 1 my %map;
119 1 my $next = 'a';
120 14 ▶ return $class->new(mapper => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
121 14 my $term = shift;
122 14 ▶ return $term unless ($term->does('Attean::API::Blank'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Blank") 8 6 14
123 6 my $id = $term->value;
124 6 ▶ if (defined(my $t = $map{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $t = $map{$id})) { } 2 4 6
125 2 return $t;
126 } else {
127 4 my $new = Attean::Blank->new( $next++ );
128 4 $map{$id} = $new;
129 4 return $new;
130 }
131 1 });
132 }
133  
134 =item C<< rewrite_map( \%map ) >>
135  
136 Given C<< %map >> whose keys are term C<< as_string >> serializations, and
137 objects are L<Attean::API::Term> objects, returns a new term map object that
138 maps terms matching entries in C<< %map >>, and all other terms to themselves.
139  
140 =cut
141  
142 14 ▶ sub rewrite_map {
Subroutine rewrite_map: called CC=1 SCAR=0.0
Pod documented
143 14 my $class = shift;
144 14 my $map = shift;
145 81 ▶ return $class->new(mapper => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
146 81 my $term = shift;
147 81 ▶ return $map->{ $term->as_string } if (exists $map->{ $term->as_string });
Branch: 1 branch
BranchTrueFalseTotal
if exists $$map{$term->as_string} 31 50 81
148 50 return $term;
149 14 });
150 }
151  
152 =back
153  
154 =head1 METHODS
155  
156 =over 4
157  
158 =item C<< map( $term ) >>
159  
160 Returns the term that is mapped to by the supplied C<< $term >>.
161  
162 =cut
163  
164 257 ▶ sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
165 257 my $self = shift;
166 257 my $term = shift;
167 257 return $self->mapper->( $term );
168 }
169  
170 =item C<< binding_mapper >>
171  
172 Returns a mapping function reference that maps L<Attean::API::Binding>
173 objects by mapping their constituent mapped L<Attean::API::Term> objects.
174  
175 =cut
176  
177 2 ▶ sub binding_mapper {
Subroutine binding_mapper: called CC=1 SCAR=0.0
Pod documented
178 2 my $self = shift;
179 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
180 4 my $binding = shift;
181 4 return $binding->apply_map($self);
182 }
183 2 }
184 }
185  
186 50 1;
187  
188 __END__
« Test/Attean/MutableTripleStore.pm ↑ Summary AtteanX/Serializer/SPARQLTSV.pm »