RDF/Trine/Serializer/NTriples/Canonical.pm

filter: Statementstmt 90.7% 98 / 108 Branchbran 78.2% 36 / 46 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 87.2% 150 / 172 CC 31 SCAR 32.7
CC
31
Cov
87%
serialize_model_to_string
34.7
new
2.0
BEGIN
1.0
CRAP 26.3
« RDF/Trine/Parser/TriG.pm ↑ Summary RDF/Trine/Store/Redland.pm »
1 =head1 NAME
2  
3 RDF::Trine::Serializer::NTriples::Canonical - Canonical representation of an RDF model
4  
5 =head1 VERSION
6  
7 This document describes RDF::Trine::Serializer::NTriples::Canonical version 1.019
8  
9 =head1 SYNOPSIS
10  
11 use RDF::Trine::Serializer::NTriples::Canonical;
12 my $serializer = RDF::Trine::Serializer::NTriples->new( onfail=>'truncate' );
13 $serializer->serialize_model_to_file(FH, $model);
14  
15 =head1 DESCRIPTION
16  
17 This module produces a canonical string representation of an RDF graph.
18 If the graph contains blank nodes, sometimes there is no canonical
19 representation that can be produced. The 'onfail' option allows you to
20 decide what is done in those circumstances:
21  
22 =over 8
23  
24 =item * truncate - drop problematic triples and only serialize a subgraph.
25  
26 =item * append - append problematic triples to the end of graph. The result will be non-canonical. This is the default behaviour.
27  
28 =item * space - As with 'append', but leave a blank line separating the canonical and non-canonical portions of the graph.
29  
30 =item * die - cause a fatal error.
31  
32 =back
33  
34 Other than the 'onfail' option, this package has exactly the same
35 interface as L<RDF::Trine::Serializer::NTriples>, providing
36 C<serialize_model_to_file> and C<serialize_model_to_string> methods.
37  
38 This package will be considerably slower than the non-canonicalising
39 serializer though, so should only be used for small to medium-sized
40 graphs, and only when you need canonicalisation (e.g. for side-by-side
41 comparison of two graphs to check they're isomorphic; or creating a
42 canonical representation for digital signing).
43  
44 =head1 METHODS
45  
46 Beyond the methods documented below, this class inherits methods from the
47 L<RDF::Trine::Serializer::NTriples> class.
48  
49 =over 4
50  
51 =cut
52  
53 package RDF::Trine::Serializer::NTriples::Canonical;
54  
55 68 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 68 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 68 ▶use RDF::Trine;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 68 ▶use base qw(RDF::Trine::Serializer::NTriples);
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 ######################################################################
64  
65 68 our ($VERSION);
66 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
67 68 $VERSION = '1.019';
68 68 $RDF::Trine::Serializer::serializer_names{ 'ntriples-canonical' } = __PACKAGE__;
69 # foreach my $type (qw(text/plain)) {
70 # $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
71 # }
72 }
73  
74 ######################################################################
75  
76 =item C<< new ( [ onfail => $rule ] ) >>
77  
78 Returns a new Canonical N-Triples serializer object. If specified, the value of
79 the 'onfail' argument dictates the handling of blank nodes with no canonical
80 representation. The allowable rule values are 'truncate', 'append', 'space',
81 and 'die', and their respective behaviour is described in L</DESCRIPTION> above.
82  
83 =cut
84  
85 779 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
86 779 my $class = shift;
87 779 my %opts = (onfail => '');
88
89 779 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 779 781
90 2 my $field = lc shift;
91 2 my $value = shift;
92 2 $opts{$field} = $value;
93 }
94
95 779 return bless \%opts, $class;
96 }
97  
98 =item C<< serialize_model_to_file ( $fh, $model ) >>
99  
100 Serializes the C<$model> to canonical NTriples, printing the results to the
101 supplied filehandle C<<$fh>>.
102  
103 =cut
104  
105 1 ▶sub serialize_model_to_file {
Subroutine serialize_model_to_file: called CC=1 SCAR=0.0
Pod documented
106 1 my $self = shift;
107 1 my $file = shift;
108 1 my $model = shift;
109
110 1 my $string = $self->serialize_model_to_string($model);
111 1 print {$file} $string;
112 }
113  
114 =item C<< serialize_model_to_string ( $model ) >>
115  
116 Serializes the C<$model> to canonical NTriples, returning the result as a string.
117  
118 =cut
119  
120 778 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: called CC=30 SCAR=35.5
Pod documented
121 778 my $self = shift;
122 778 my $model = shift;
123
124 778 my $blankNodes = {};
125 778 my @statements;
126
127 778 my $stream = $model->as_stream;
128 778 ▶ while (my $ST = $stream->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ST = $stream->next) 1402 778 2180
129 1402 push @statements, { 'trine' => $ST };
130
131 1402 ▶ if ($ST->subject->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ST->subject->isa("RDF::Trine::Node::Blank")) 330 1072 1402
132 330 $blankNodes->{ $ST->subject->blank_identifier }->{'trine'} = $ST->subject;
133 }
134
135 1402 ▶ if ($ST->object->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ST->object->isa("RDF::Trine::Node::Blank")) 182 1220 1402
136 182 $blankNodes->{ $ST->object->blank_identifier }->{'trine'} = $ST->object;
137 }
138 }
139
140 778 my %lexCounts;
141
142 778 foreach my $st (@statements) {
143 # Really need to canonicalise typed literals as per XSD.
144
145 $st->{'lex'} = sprintf('%s %s %s',
146 ($st->{'trine'}->subject->isa('RDF::Trine::Node::Blank') ? '~' : $st->{'trine'}->subject->as_ntriples),
147 $st->{'trine'}->predicate->as_ntriples,
148 1402 ▶ ($st->{'trine'}->object->isa('RDF::Trine::Node::Blank') ? '~' : $st->{'trine'}->object->as_ntriples)
Branch: 2 branches
BranchTrueFalseTotal
$st->{'trine'}->subject->isa('RDF::Trine::Node::Blank') ? : 330 1072 1402
$st->{'trine'}->object->isa('RDF::Trine::Node::Blank') ? : 182 1220 1402
149 );
150 1402 $lexCounts{ $st->{'lex'} }++;
151 }
152  
153 778 my $blankNodeCount = scalar keys %$blankNodes;
154 778 my $blankNodeLength = length "$blankNodeCount";
155 778 my $blankNodePattern = '_:g%0'.$blankNodeLength.'d';
156 778 my $hardNodePattern = '_:h%0'.$blankNodeLength.'d';
157
158 778 @statements = sort { $a->{'lex'} cmp $b->{'lex'} } @statements;
159
160 778 my $genSymCounter = 1;
161
162 778 foreach my $st (@statements) {
163 1402 ▶ next unless $lexCounts{ $st->{'lex'} } == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $lexCounts{$st->{"lex"}} == 1 32 1370 1402
164
165 1370 ▶ if ($st->{'trine'}->object->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"trine"}->object->isa("RDF::Trine::Node::Blank")) 182 1188 1370
166 182 ▶ unless (defined $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$blankNodes{$st->{"trine"}->object->blank_identifier}{"lex"}) 128 54 182
167 128 $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'} =
168 sprintf($blankNodePattern, $genSymCounter);
169 128 $genSymCounter++;
170 }
171 182 my $b = $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'};
172 182 $st->{'lex'} =~ s/\~$/$b/;
173 }
174
175 1370 ▶ if ($st->{'trine'}->subject->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"trine"}->subject->isa("RDF::Trine::Node::Blank")) 298 1072 1370
176 298 ▶ unless (defined $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$blankNodes{$st->{"trine"}->subject->blank_identifier}{"lex"}) 118 180 298
177 118 $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'} =
178 sprintf($blankNodePattern, $genSymCounter);
179 118 $genSymCounter++;
180 }
181 298 my $b = $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'};
182 298 $st->{'lex'} =~ s/^\~/$b/;
183 }
184 }
185
186 778 foreach my $st (@statements) {
187 1402 ▶ if ($st->{'lex'} =~ /\~$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"lex"} =~ /\~$/) 0 1402 1402
188 0 ▶ if (defined $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$blankNodes{$st->{"trine"}->object->blank_identifier}{"lex"}) 0 0 0
189 0 my $b = $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'};
190 0 $st->{'lex'} =~ s/\~$/$b/;
191 }
192 }
193
194 1402 ▶ if ($st->{'lex'} =~ /^\~/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"lex"} =~ /^\~/) 32 1370 1402
195 32 ▶ if (defined $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$blankNodes{$st->{"trine"}->subject->blank_identifier}{"lex"}) 30 2 32
196 30 my $b = $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'};
197 30 $st->{'lex'} =~ s/^\~/$b/;
198 }
199 }
200 }
201
202 778 @statements = sort { $a->{'lex'} cmp $b->{'lex'} } @statements;
203
204 778 my @canonicalStatements;
205 my @otherStatements;
206 778 foreach my $st (@statements) {
207 1402 ▶ if ($st->{'lex'} =~ /(^\~)|(\~$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{'lex'} =~ /(^\~)|(\~$)/) { } 2 1400 1402
208 2 ▶ if (lc $self->{'onfail'} eq 'die') {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $self->{'onfail'} eq 'die') { } 0 2 2
elsif (lc $self->{'onfail'} eq 'truncate') { } 0 2 2
209 0 croak "Model could not be canonicalised";
210 } elsif (lc $self->{'onfail'} eq 'truncate') {
211 0 next;
212 }
213
214 2 ▶ if ($st->{'lex'} =~ /\~$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"lex"} =~ /\~$/) 0 2 2
215 0 ▶ unless (defined $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$blankNodes{$st->{"trine"}->object->blank_identifier}{"lex"}) 0 0 0
216 0 $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'} =
217 sprintf($hardNodePattern, $genSymCounter);
218 0 $genSymCounter++;
219 }
220 0 my $b = $blankNodes->{ $st->{'trine'}->object->blank_identifier }->{'lex'};
221 0 $st->{'lex'} =~ s/\~$/$b/;
222 }
223  
224 2 ▶ if ($st->{'lex'} =~ /^\~/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->{"lex"} =~ /^\~/) 2 0 2
225 2 ▶ unless (defined $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$blankNodes{$st->{"trine"}->subject->blank_identifier}{"lex"}) 2 0 2
226 2 $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'} =
227 sprintf($hardNodePattern, $genSymCounter);
228 2 $genSymCounter++;
229 }
230 2 my $b = $blankNodes->{ $st->{'trine'}->subject->blank_identifier }->{'lex'};
231 2 $st->{'lex'} =~ s/^\~/$b/;
232 }
233  
234 2 push @otherStatements, $st;
235 } else {
236 1400 push @canonicalStatements, $st;
237 }
238 }
239
240 778 my $rv = '';
241 778 foreach my $st (@canonicalStatements) {
242 1400 $rv .= $st->{'lex'} . " .\r\n";
243 }
244  
245 $rv .= "\r\n"
246 778 ▶ if (defined($self->{'onfail'}) && (lc $self->{'onfail'} eq 'space'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"onfail"} and lc $self->{"onfail"} eq "space" 2 776 778
Condition66%2/3
defined $self->{"onfail"} and lc $self->{"onfail"} eq "space"
!ll&&!rl&&r
07762
MC/DC50%1/2
defined $self->{"onfail"} and lc $self->{"onfail"} eq "space"
defined $self->{"onfail"} lc $self->{"onfail"} eq "space"
Truth table66%2/3
A and B
A= defined $self->{"onfail"}B= lc $self->{"onfail"} eq "space"
ABresult
0X0
100
111
247
248 778 foreach my $st (@otherStatements) {
249 2 $rv .= $st->{'lex'} . " .\r\n";
250 }
251  
252 778 return $rv;
253 }
254  
255 68 1;
256 __END__
« RDF/Trine/Parser/TriG.pm ↑ Summary RDF/Trine/Store/Redland.pm »