ORE/N3.pm

filter: Statementstmt 82.8% 116 / 140 Branchbran 56.5% 26 / 46 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 91.3% 21 / 23 Podpod 100% 3 / 3 totaltotal 74.1% 172 / 232 CC 32 SCAR 18.1
CC
32
Cov
74%
_n3_escape
12.3
serialize
12.0
_make_n3_line
6.2
CRAP 6.1
« ORE/ReM.pm ↑ Summary ORE/Agent.pm »
1 package SemanticWeb::OAI::ORE::N3;
2 #$Id: N3.pm,v 1.14 2010-12-06 14:44:15 simeon Exp $
3  
4 =head1 NAME
5  
6 SemanticWeb::OAI::ORE::N3 - Parse/serialize OAI-ORE Resource Maps in N3 format
7  
8 =head1 SYNPOSIS
9  
10 Class to parse and serialize OAI-ORE ReMs in N3 format.
11  
12 =head1 DESCRIPTION
13  
14 Follows N3 specification defined by L<http://www.w3.org/DesignIssues/Notation3.html>.
15  
16 =cut
17  
18 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use SemanticWeb::OAI::ORE::ReM;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use SemanticWeb::OAI::ORE::Constant qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 8 ▶use RDF::Notation3::RDFCore;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use RDF::Notation3::Triples;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use RDF::Core::Storage::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use RDF::Core::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use RDF::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 8 ▶use RDF::Core::Enumerator;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 8 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 =head1 METHODS
34  
35 =head2 new()
36  
37 =cut
38  
39 5 ▶sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
40 5 my $class=shift;
41 5 my $self={'strict'=>'warn',
42 @_};
43 5 ▶ bless $self, (ref($class) || $class);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
44 5 return($self);
45 }
46  
47  
48 =head2 parse($src,$uri_rem)
49  
50 Parse $src which is either a string containing the N3 serialization of
51 the ReM with URI $uri_rem, or a filehandle.
52  
53 =cut
54  
55 3 ▶sub parse {
Subroutine parse: called CC=3 SCAR=12.3
Pod documented
56 3 my $self=shift;
57 3 my ($src,$uri_rem)=@_;
58  
59 3 my $rdf = RDF::Notation3::RDFCore->new();
60 3 $rdf->set_storage(RDF::Core::Storage::Memory->new());
61 3 my $model;
62 3 ▶ if (ref($src) eq 'IO::File') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $src eq 'IO::File') { } 3 0 3
elsif (ref $src) { } 0 0 0
63 3 $model = $rdf->parse_file($src); #takes IO::File or path
64 } elsif (ref($src)) {
65 0 croak("Attempt to parse N3 with a ".ref($src)." object supplied");
66 } else {
67 0 $model = $rdf->parse_string($src);
68 }
69  
70 # $model is an RDF::Core::Model
71 # Now we look for statements that are special to OAI-ORE
72 #print "got $model ".ref($model)."\n";
73 #print Dumper($model);
74 3 return($model);
75 }
76  
77  
78 =head3 serialize()
79  
80 We could do this by simply converting all data to triples
81 and then dumping them as N3. However, we attempt to do a
82 nice "pretty print" specific to ORE, with a few comments for
83 the parts.
84  
85 =cut
86  
87 2 ▶sub serialize {
Subroutine serialize: called CC=11 SCAR=24.8
Pod documented
88 2 my $self=shift;
89 2 my $out='';
90 2 my $rem=$self->{rem};
91 2 ▶ if (ref($rem) and $rem->isa('SemanticWeb::OAI::ORE::ReM')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rem and $rem->isa('SemanticWeb::OAI::ORE::ReM')) { } 2 0 2
Condition33%1/3
ref $rem and $rem->isa("SemanticWeb::OAI::ORE::ReM")
!ll&&!rl&&r
002
MC/DC0%0/2
ref $rem and $rem->isa("SemanticWeb::OAI::ORE::ReM")
ref $rem $rem->isa("SemanticWeb::OAI::ORE::ReM")
Truth table33%1/3
A and B
A= ref $remB= $rem->isa("SemanticWeb::OAI::ORE::ReM")
ABresult
0X0
100
111
92 # Get the info from the ReM
93 2 my $uri_r=$rem->uri;
94 2 my $uri_a=$rem->aggregation;
95 2 my @rem_and_agg=();
96 2 my @agg_res=();
97 2 my @lines=();
98 2 $self->_prefixify_setup;
99 2 foreach my $statement (@{$rem->model->as_array()}) {
100 3 my ($subject,$predicate,$object,$is_literal)=@$statement;
101 3 my $line=$self->_make_n3_line(@$statement);
102 3 ▶ if ($subject eq $uri_r and $predicate eq DESCRIBES) {
Branch: 2 branches
BranchTrueFalseTotal
if ($subject eq $uri_r and $predicate eq 'http://www.openarchives.org/ore/terms/describes') { } 1 2 3
elsif ($subject eq $uri_a and $predicate eq 'http://www.openarchives.org/ore/terms/aggregates') { } 2 0 2
Condition66%2/3
$subject eq $uri_r and $predicate eq "http://www.openarchives.org/ore/terms/describes"
!ll&&!rl&&r
201
MC/DC50%1/2
$subject eq $uri_r and $predicate eq "http://www.openarchives.org/ore/terms/describes"
$subject eq $uri_r $predicate eq "http://www.openarchives.org/ore/terms/describes"
Truth table66%2/3
A and B
A= $subject eq $uri_rB= $predicate eq "http://www.openarchives.org/ore/terms/describes"
ABresult
0X0
100
111
103 1 push(@rem_and_agg,$line);
104 ▶ } elsif ($subject eq $uri_a and $predicate eq AGGREGATES) {
Condition33%1/3
$subject eq $uri_a and $predicate eq "http://www.openarchives.org/ore/terms/aggregates"
!ll&&!rl&&r
002
MC/DC0%0/2
$subject eq $uri_a and $predicate eq "http://www.openarchives.org/ore/terms/aggregates"
$subject eq $uri_a $predicate eq "http://www.openarchives.org/ore/terms/aggregates"
Truth table33%1/3
A and B
A= $subject eq $uri_aB= $predicate eq "http://www.openarchives.org/ore/terms/aggregates"
ABresult
0X0
100
111
105 2 push(@agg_res,$line);
106 } else {
107 0 push(@lines,$line);
108 }
109 #FIXME
110 }
111 # Now print it
112 2 $out.="### OAI-ORE Resource Map ($uri_r)\n";
113 2 $out.=$self->_prefixes_section;
114 2 $out.="\n# Resource Map and Aggregation\n";
115 2 ▶ $out.=join('',(sort @rem_and_agg)) if (@rem_and_agg);
Branch: 1 branch
BranchTrueFalseTotal
if @rem_and_agg 1 1 2
116 2 $out.="\n# Aggregated resources\n";
117 2 ▶ $out.=join('',(sort @agg_res)) if (@agg_res);
Branch: 1 branch
BranchTrueFalseTotal
if @agg_res 1 1 2
118 2 $out.="\n# Relations\n";
119 2 ▶ $out.=join('',(sort @lines)) if (@lines);
Branch: 1 branch
BranchTrueFalseTotal
if @lines 0 2 2
120 2 $out.="\n### End Resource Map\n";
121 } else {
122 0 carp "Can't serialize something that isn't a rem: $rem, ".ref($rem)."\n";
123 }
124 2 return($out);
125 }
126  
127  
128 # Create a single N3 line from subject,predicate,object labels with
129 # appropriate escaping.
130 #
131 3 ▶sub _make_n3_line {
Subroutine _make_n3_line: called CC=5 SCAR=18.2
132 3 my $self=shift;
133 3 my ($subject,$predicate,$object,$object_is_literal)=@_;
134 3 my $line='';
135 3 ▶ if (my $psubject=$self->_prefixify($subject)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $psubject = $self->_prefixify($subject)) { } 0 3 3
136 0 $line.=$psubject.' ';
137 } else {
138 3 $line.='<'.$subject.'> ';
139 }
140 3 ▶ if (my $ppredicate=$self->_prefixify_predicate($predicate)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ppredicate = $self->_prefixify_predicate($predicate)) { } 3 0 3
141 3 $line.=$ppredicate.' ';
142 } else {
143 0 $line.='<'.$predicate.'> ';
144 }
145 3 ▶ if ($object_is_literal) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object_is_literal) { } 0 3 3
elsif (my $pobject = $self->_prefixify($object)) { } 0 3 3
146 0 $line.='"'._n3_escape($object).'"';
147 } elsif (my $pobject=$self->_prefixify($object)) {
148 0 $line.=$pobject;
149 } else {
150 3 $line.='<'.$object.'>';
151 }
152 3 return($line.".\n");
153 }
154  
155  
156 # Escape a string for use in N3 quoted string.
157 # See: http://www.w3.org/DesignIssues/Notation3
158 #
159 5 ▶sub _n3_escape {
Subroutine _n3_escape: called CC=7 SCAR=25.1
160 5 my ($sstr)=@_;
161 5 my $rstr='';
162 5 foreach my $c (split(//,$sstr)) {
163 100 ▶ if ($c eq '\\') {
Branch: 5 branches
BranchTrueFalseTotal
if ($c eq '\\') { } 0 100 100
elsif ($c eq '"') { } 0 100 100
elsif ($c eq "\n") { } 0 100 100
elsif ($c eq "\r") { } 0 100 100
elsif ($c eq "\t") { } 0 100 100
164 0 $c='\\\\';
165 } elsif ($c eq '"') {
166 0 $c='\\"';
167 } elsif ($c eq "\n") {
168 0 $c='\\n';
169 } elsif ($c eq "\r") {
170 0 $c='\\r';
171 } elsif ($c eq "\t") {
172 0 $c='\\t';
173 }
174 100 $rstr.=$c;
175 }
176 5 return($rstr);
177 }
178  
179  
180 0 ▶sub _parse_warning {
Subroutine _parse_warning: not called CC=1 SCAR=6.9
181 0 my $self=shift;
182 0 my $msg=shift;
183 0 print "_parse_warning: $msg\n";
184 }
185  
186  
187 0 ▶sub _parse_error {
Subroutine _parse_error: not called CC=2 SCAR=17.9
188 0 my $self=shift;
189 0 my $msg=shift;
190 0 ▶ if ($self->{strict} eq 'warn') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"strict"} eq "warn") 0 0 0
191 0 return($self->_parse_warning($msg));
192 }
193 0 croak "_parse_error: $msg\n";
194 }
195  
196  
197 # Setup ready to use _prefixify.
198 #
199 # WARNING - have to be carefull that constants do not get quoted
200 # in the hash. See Caveats in http://perldoc.perl.org/constant.html
201 #
202 2 ▶sub _prefixify_setup {
Subroutine _prefixify_setup: called CC=1 SCAR=0.0
203 2 my $self=shift;
204 $self->{prefixes_known}={
205 2 ORE_NS() => ORE_PREFIX(),
206 DC_NS() => DC_PREFIX(),
207 DCT_NS() => DCT_PREFIX(),
208 RDF_NS() => RDF_PREFIX() };
209 2 $self->{prefixes_used}={};
210 }
211  
212  
213 # Return prefixes section of N3 output.
214 #
215 2 ▶sub _prefixes_section {
Subroutine _prefixes_section: called CC=3 SCAR=11.0
216 2 my $self=shift;
217 2 my $out='';
218 2 ▶ if (scalar(keys %{$self->{prefixes_used}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$self{"prefixes_used"};}) 1 1 2
219 1 $out.="\n# Namespace prefixes\n";
220 1 foreach my $prefix (sort keys %{$self->{prefixes_used}}) {
221 1 $out.="\@prefix $prefix: <".$self->{prefixes_used}{$prefix}.">.\n";
222 }
223 }
224 2 return($out);
225 }
226  
227  
228 # Takes input $uri, return possible prefixed $uri having added
229 # prefix to list of known used prefixes.
230 #
231 9 ▶sub _prefixify {
Subroutine _prefixify: called CC=3 SCAR=11.0
232 9 my $self=shift;
233 9 my ($uri)=@_;
234 9 foreach my $prefix (keys %{$self->{prefixes_known}}) {
235 30 ▶ if ($uri=~s%^$prefix%%) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ s/^$prefix//) 3 27 30
236 3 $self->{prefixes_used}{$self->{prefixes_known}{$prefix}}=$prefix;
237 3 return($self->{prefixes_known}{$prefix}.':'.$uri);
238 }
239 }
240 # Nothing found, return nothing
241 6 return();
242 }
243  
244  
245 # Special support for predicates where N3 has certain shorthands
246 #
247 3 ▶sub _prefixify_predicate {
Subroutine _prefixify_predicate: called CC=4 SCAR=17.2
248 3 my $self=shift;
249 3 my ($uri)=@_;
250 3 ▶ if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
Branch: 3 branches
BranchTrueFalseTotal
if ($uri eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') { } 0 3 3
elsif ($uri eq 'http://www.w3.org/2002/07/owl#sameAs') { } 0 3 3
elsif ($uri eq 'http://www.w3.org/2000/10/swap/log#implies') { } 0 3 3
251 0 return('a');
252 } elsif ($uri eq 'http://www.w3.org/2002/07/owl#sameAs') {
253 0 return('=');
254 } elsif ($uri eq 'http://www.w3.org/2000/10/swap/log#implies') {
255 0 return('=>');
256 }
257 # No specials for predicate found, try normal _prefixify
258 3 return($self->_prefixify($uri));
259 }
260  
261  
262 =head1 SEE ALSO
263  
264 L<SemanticWeb::OAI::ORE> and associated modules.
265  
266 =head1 AUTHORS
267  
268 Simeon Warner
269  
270 =head1 LICENSE AND COPYRIGHT
271  
272 Copyright 2007-2010 Simeon Warner.
273  
274 This module is free software; you can redistribute it and/or
275 modify it under the same terms as Perl itself. See L<perlartistic>.
276  
277 =cut
278  
279 8 1;
« ORE/ReM.pm ↑ Summary ORE/Agent.pm »