Importer/RDF.pm

filter: Statementstmt 91.4% 182 / 199 Branchbran 78.4% 69 / 88 Conditioncond 46.8% 15 / 32 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 36 / 36 Podpod 0.0% 0 / 4 totaltotal 80.6% 304 / 377 CC 62 SCAR 19.0
CC
62
Cov
83%
__ANON__
13.6
__ANON__
12.0
_hashref_stream
10.2
CRAP 6.7
↑ Summary Exporter/RDF.pm »
1 package Catmandu::Importer::RDF;
2  
3 15 ▶use open ':std', ':encoding(utf8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use RDF::Trine::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use RDF::Trine::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use RDF::Trine::Store::SPARQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use RDF::Trine::Store::LDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use RDF::Trine::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use RDF::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use RDF::LDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use RDF::aREF;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use RDF::aREF::Encoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use RDF::NS;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use LWP::UserAgent::CHICaching;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 15 our $VERSION = '0.32';
22  
23 15 with 'Catmandu::RDF';
24 15 with 'Catmandu::Importer';
25  
26 15 has url => (
27 is => 'ro'
28 );
29  
30 has base => (
31 is => 'ro',
32 lazy => 1,
33 2 ▶ builder => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
Subroutine _build_base: called CC=2 SCAR=7.6
34 2 ▶ defined $_[0]->file ? "file://".$_[0]->file : "http://example.org/";
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0]->file ? : 2 0 2
defined $_[0]->file ? : 6 0 6
35 }
36 15 );
37  
38 has encoder => (
39 is => 'ro',
40 lazy => 1,
41 13 ▶ builder => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
Subroutine _build_encoder: called CC=3 SCAR=11.1
42 13 my $ns = $_[0]->ns;
43 13 ▶ RDF::aREF::Encoder->new(
Branch: 2 branches
BranchTrueFalseTotal
$ns // 1 ? : 10 3 13
$ns // 1 ? : 13 4 17
Condition50%2/4
$ns // 1
l!l
130
$ns // 1
l!l
170
MC/DC0%0/1
$ns // 1
$ns
MC/DC0%0/1
$ns // 1
$ns
Truth table50%1/2
A
A= $ns
Aresult
11
00
Truth table50%1/2
A
A= $ns
Aresult
11
00
44 ns => (($ns // 1) ? $ns : { }),
45 subject_map => !$_[0]->predicate_map,
46 );
47 }
48 15 );
49  
50 has sparql => (
51 is => 'ro',
52 lazy => 1,
53 4 ▶ trigger => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
54 4 my ($sparql, $ns) = ($_[1], $_[0]->ns);
55 4 ▶ $sparql = do { local (@ARGV,$/) = $sparql; <> } if $sparql =~ /^\S+$/ && -r $sparql;
Branch: 1 branch
BranchTrueFalseTotal
if $sparql =~ /^\S+$/ and -r $sparql 1 3 4
Condition66%2/3
$sparql =~ /^\S+$/ and -r $sparql
!ll&&!rl&&r
301
MC/DC50%1/2
$sparql =~ /^\S+$/ and -r $sparql
$sparql =~ /^\S+$/ -r $sparql
Truth table66%2/3
A and B
A= $sparql =~ /^\S+$/B= -r $sparql
ABresult
0X0
100
111
56 4 my %prefix;
57 # guess requires prefixes (don't override existing). Don't mind false positives
58 4 $prefix{$_} = 1 for ($sparql =~ /\s([a-z][a-z0-0_-]*):/mig);
59 4 delete $prefix{$_} for ($sparql =~ /PREFIX\s+([^:]+):/mg);
60 4 $_[0]->{sparql} = join "\n", (map { $ns->SPARQL($_) } keys %prefix), $sparql;
61 }
62 15 );
63  
64 has sparql_result => (
65 is => 'ro',
66 52 ▶ default => sub { 'simple' }
Subroutine __ANON__: called CC=1 SCAR=0.0
67 15 );
68  
69 15 has predicate_map => (
70 is => 'ro',
71 );
72  
73 15 has triples => (
74 is => 'ro',
75 );
76  
77 has cache => (
78 is => 'ro',
79 52 ▶ default => sub { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
80 15 );
81  
82 has cache_options => (
83 is => 'ro',
84 52 ▶ default => sub { +{
Subroutine __ANON__: called CC=1 SCAR=0.0
85 52 driver => 'Memory',
86 global => 1 ,
87 max_size => 1024*1024
88 } }
89 15 );
90  
91 15 has speed => (
92 is => 'ro',
93 );
94  
95 52 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=18.1
Pod undocumented
96 52 my ($self) = @_;
97  
98 52 ▶ if ($self->cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->cache) 0 52 52
99 0 ▶ my $options = $self->cache_options // {};
Condition0%0/2
$self->cache_options // {}
l!l
00
MC/DC0%0/1
$self->cache_options // {}
$self->cache_options
Truth table0%0/2
A
A= $self->cache_options
Aresult
11
00
100 0 my $cache = CHI->new( %$options );
101 0 my $ua = LWP::UserAgent::CHICaching->new(cache => $cache);
102 0 RDF::Trine->default_useragent($ua);
103 }
104 }
105  
106 44 ▶sub generator {
Subroutine generator: called CC=2 SCAR=6.9
Pod undocumented
107 44 my ($self) = @_;
108  
109 44 ▶ if ($self->sparql) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sparql) { } 3 41 44
110 3 return $self->sparql_generator;
111 } else {
112 41 return $self->rdf_generator;
113 }
114 }
115  
116 3 ▶sub sparql_generator {
Subroutine sparql_generator: called CC=3 SCAR=11.8
Pod undocumented
117 3 my ($self) = @_;
118  
119 3 ▶ warn "--triples not active for sparql queries" if ($self->triples);
Branch: 1 branch
BranchTrueFalseTotal
if $self->triples 0 3 3
120 3 ▶ warn "--predicate_map not active for sparql queries" if ($self->predicate_map);
Branch: 1 branch
BranchTrueFalseTotal
if $self->predicate_map 0 3 3
121  
122 3 my $encoder = RDF::aREF::Encoder->new( ns => {} ); # never return qnames
123  
124 3 ▶ sub {
Subroutine __ANON__: called CC=9 SCAR=24.8
125 3 state $stream = $self->_sparql_stream;
126 3 ▶ if (defined($stream) && defined(my $row = $stream->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stream and defined(my $row = $stream->next)) { } 3 0 3
Condition33%1/3
defined $stream and defined(my $row = $stream->next)
!ll&&!rl&&r
003
MC/DC0%0/2
defined $stream and defined(my $row = $stream->next)
defined $stream defined(my $row = $stream->next)
Truth table33%1/3
A and B
A= defined $streamB= defined(my $row = $stream->next)
ABresult
0X0
100
111
127 3 ▶ if (ref $row eq 'RDF::Query::VariableBindings' || ref $row eq 'RDF::Trine::VariableBindings') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row eq 'RDF::Query::VariableBindings' or ref $row eq 'RDF::Trine::VariableBindings') { } 3 0 3
Condition66%2/3
ref $row eq "RDF::Query::VariableBindings" or ref $row eq "RDF::Trine::VariableBindings"
l!l&&r!l&&!r
210
MC/DC0%0/2
ref $row eq "RDF::Query::VariableBindings" or ref $row eq "RDF::Trine::VariableBindings"
ref $row eq "RDF::Query::VariableBindings" ref $row eq "RDF::Trine::VariableBindings"
Truth table66%2/3
A or B
A= ref $row eq "RDF::Query::VariableBindings"B= ref $row eq "RDF::Trine::VariableBindings"
ABresult
1X1
011
000
128 3 my $ref = {};
129 3 for (keys %$row) {
130 4 my $val = $row->{$_};
131 $ref->{$_} = $self->sparql_result eq 'aref'
132 4 ▶ ? $encoder->object($val) : do { # TODO: clean up
Branch: 1 branch
BranchTrueFalseTotal
$self->sparql_result eq 'aref' ? : 0 4 4
133 4 ▶ if ( $val->is_resource ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val->is_resource) { } 2 2 4
elsif ($val->is_literal) { } 2 0 2
134 2 $val->uri_value;
135 } elsif ( $val->is_literal) {
136 2 $val->literal_value;
137 } else {
138 0 $val->as_string
139 }
140 };
141 }
142 3 return $ref;
143 } else {
144 0 die "Expected a RDF::Query::VariableBindings or RDF::Trine::VariableBindings but got a " . ref($row);
145 }
146 } else {
147 0 return ($stream = undef);
148 }
149 3 };
150 }
151  
152 41 ▶sub rdf_generator {
Subroutine rdf_generator: called CC=1 SCAR=0.0
Pod undocumented
153 41 my ($self) = @_;
154 49 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
155 49 state $stream = $self->_hashref_stream;
156 38 ▶ return unless $stream;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream 0 38 38
157  
158 38 my $aref = { };
159  
160 38 ▶ if ($self->triples) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->triples) { } 10 28 38
161 10 ▶ if (my $hashref = $stream->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hashref = &$stream()) { } 8 2 10
162 8 $self->encoder->add_hashref($hashref, $aref);
163 }
164 else {
165 2 return ($stream = undef);
166 }
167 }
168 else {
169 # TODO: include namespace mappings if requested
170 28 ▶ while (my $hashref = $stream->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hashref = &$stream()) 103 28 131
171 103 $self->encoder->add_hashref(
172 $hashref,
173 $aref
174 );
175 }
176  
177 28 ▶ if ($self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url) 6 22 28
178 6 $aref->{_url} = $self->url;
179 }
180  
181 28 $stream = undef;
182 }
183  
184 36 ▶ if ($self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url) 6 30 36
185 # RDF::Trine::Parser parses data from URL to UTF-8
186 # but we want internal character sequences
187 6 _utf8_decode($aref);
188 }
189  
190 36 return $aref;
191 41 };
192 }
193  
194 18 ▶sub _utf8_decode {
Subroutine _utf8_decode: called CC=6 SCAR=22.6
195 18 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 18 0 18
196 # FIXME: UTF-8 in property values
197 18 foreach (values %{$_[0]}) {
198 42 ▶ ref($_) ? _utf8_decode($_) : utf8::decode($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 12 30 42
199 }
200 } else {
201 0 foreach (@{$_[0]}) {
202 0 ▶ ref($_) ? _utf8_decode($_) : utf8::decode($_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
203 }
204 }
205 }
206  
207 3 ▶sub _sparql_stream {
Subroutine _sparql_stream: called CC=6 SCAR=19.9
208 3 my ($self) = @_;
209  
210 3 ▶ die "need an url" unless $self->url;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->url 0 3 3
211  
212 3 $self->log->info("parsing: " . $self->sparql);
213  
214 3 my $store;
215  
216 # Check if this server is an LDF server
217 3 my $ldf_client = RDF::LDF->new(url => $self->url);
218  
219 3 ▶ if ($ldf_client->is_fragment_server) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ldf_client->is_fragment_server) { } 2 1 3
220 2 $store = RDF::Trine::Store->new_with_config({
221 storetype => 'LDF',
222 url => $self->url
223 });
224 }
225 else {
226 1 $store = RDF::Trine::Store->new_with_config({
227 storetype => 'SPARQL',
228 url => $self->url
229 });
230 }
231  
232 3 ▶ unless ($store) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($store) 0 3 3
233 0 $self->log->error("failed to connect to " . $self->url);
234 0 return;
235 }
236  
237 3 my $model = RDF::Trine::Model->new($store);
238  
239 3 my $rdf_query = RDF::Query->new($self->sparql);
240  
241 3 ▶ unless ($rdf_query) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rdf_query) 0 3 3
242 0 $self->log->error("failed to parse " . $self->sparql);
243 0 return;
244 }
245  
246 3 my $iterator = $rdf_query->execute($model);
247  
248 3 ▶ unless ($iterator) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($iterator) 0 3 3
249 0 $self->log->error("failed to execute " . $self->sparql . " at " . $self->url);
250 0 return;
251 }
252 }
253  
254 41 ▶sub _hashref_stream {
Subroutine _hashref_stream: called CC=10 SCAR=23.2
255 41 my ($self) = @_;
256  
257 # Create a pipe stream to convert a callback handler into an iterator
258 41 my $pipe = IO::Pipe->new();
259  
260 41 ▶ if (my $pid = fork()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pid = fork) { } 30 11 41
261 # parent
262 30 $pipe->reader();
263  
264 30 binmode($pipe,':encoding(UTF-8)');
265  
266 141 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
267 141 state $line = <$pipe>;
268  
269 141 ▶ return decode_json($line) if defined($line);
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 111 30 141
270  
271 30 waitpid($pid,0);
272  
273 30 return undef;
274 30 };
275 }
276 else {
277 # child
278 11 $pipe->writer();
279  
280 11 binmode($pipe,':encoding(UTF-8)');
281  
282 11 ▶ my $parser = $self->type
Branch: 1 branch
BranchTrueFalseTotal
$self->type ? : 5 6 11
283 ? RDF::Trine::Parser->new( $self->type ) : 'RDF::Trine::Parser';
284  
285 41 ▶ my $handler = sub {
Subroutine __ANON__: called CC=13 SCAR=26.1
286 41 my $triple = shift;
287 41 state $start = time;
288 41 state $count = 0;
289  
290 41 ▶ my $subject = $triple->subject->is_blank ?
Branch: 1 branch
BranchTrueFalseTotal
$triple->subject->is_blank ? : 6 35 41
291 '_:' . $triple->subject->blank_identifier :
292 $triple->subject->uri_value;
293 41 ▶ my $predicate = $triple->predicate->is_blank ?
Branch: 1 branch
BranchTrueFalseTotal
$triple->predicate->is_blank ? : 0 41 41
294 '_:' . $triple->predicate->blank_identifier :
295 $triple->predicate->value;
296 41 ▶ my $value = $triple->object->is_literal ?
Branch: 2 branches
BranchTrueFalseTotal
$triple->object->is_literal ? : 28 13 41
$triple->object->is_blank ? : 3 10 13
297 $triple->object->literal_value :
298 $triple->object->is_blank ?
299 '_:' . $triple->object->blank_identifier :
300 $triple->object->uri_value;
301 41 my $type = lc $triple->object->type;
302 41 ▶ $type = 'bnode' if $type eq 'blank';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "blank" 3 38 41
303 41 ▶ my $lang = $triple->object->is_literal ? $triple->object->literal_value_language : undef;
Branch: 1 branch
BranchTrueFalseTotal
$triple->object->is_literal ? : 28 13 41
304 41 ▶ my $datatype = $triple->object->is_literal ? $triple->object->literal_datatype : undef;
Branch: 1 branch
BranchTrueFalseTotal
$triple->object->is_literal ? : 28 13 41
305  
306 # Create the RDF::Trine type RDF/JSON RDF::aREF can parse
307 41 my $hashref = {};
308  
309 41 $hashref->{$subject}->{$predicate}->[0]->{type} = $type;
310 41 ▶ $hashref->{$subject}->{$predicate}->[0]->{datatype} = $datatype if $datatype;
Branch: 1 branch
BranchTrueFalseTotal
if $datatype 7 34 41
311 41 ▶ $hashref->{$subject}->{$predicate}->[0]->{lang} = $lang if $lang;
Branch: 1 branch
BranchTrueFalseTotal
if $lang 8 33 41
312 41 $hashref->{$subject}->{$predicate}->[0]->{value} = $value;
313  
314 41 print $pipe encode_json($hashref) , "\n";
315  
316 41 $count++;
317  
318 41 ▶ if ($self->speed && ($count % 100 == 0) && (my $elapsed = time - $start) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->speed and $count % 100 == 0 and my $elapsed = time - $start) 0 41 41
Condition33%2/6
$self->speed and $count % 100 == 0 and my $elapsed = time - $start
!ll&&!rl&&r
4100
$self->speed and $count % 100 == 0
!ll&&!rl&&r
4100
MC/DC0%0/3
$self->speed and $count % 100 == 0 and my $elapsed = time - $start
$self->speed $count % 100 == 0 my $elapsed = time - $start
Truth table25%1/4
A and B and C
A= $self->speedB= $count % 100 == 0C= my $elapsed = time - $start
ABCresult
0XX0
10X0
1100
1111
319 0 printf STDERR "triples %9d (%d/sec)\n" , $count , $count/$elapsed;
320 }
321 11 };
322  
323 11 ▶ if ($self->url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->url) { } 3 8 11
324 3 $parser->parse_url( $self->url, $handler);
325 }
326 else {
327 8 ▶ my $from_scalar = (ref $self->file // '') eq 'SCALAR';
Condition50%1/2
ref $self->file // ""
l!l
80
MC/DC0%0/1
ref $self->file // ""
ref $self->file
Truth table50%1/2
A
A= ref $self->file
Aresult
11
00
328  
329 8 ▶ if (!$self->type and $self->file and !$from_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->type and $self->file and not $from_scalar) 6 2 8
Condition66%4/6
not $self->type and $self->file and not $from_scalar
!ll&&!rl&&r
206
not $self->type and $self->file
!ll&&!rl&&r
206
MC/DC33%1/3
not $self->type and $self->file and not $from_scalar
not $self->type $self->file not $from_scalar
Truth table50%2/4
not(A) and B and not(C)
A= not $self->typeB= $self->fileC= not $from_scalar
ABCresult
0XX0
10X0
1100
1111
330 6 $parser = $parser->guess_parser_by_filename($self->file)->new;
331 }
332  
333 8 ▶ if ($from_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_scalar) { } 2 6 8
334 2 $parser->parse( $self->base, ${$self->file}, $handler );
335 }
336 else {
337 6 ▶ $parser->parse_file( $self->base, $self->file // $self->fh, $handler );
Condition33%1/3
$self->file // $self->fh
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->file // $self->fh
$self->file $self->fh
Truth table33%1/3
A // B
A= $self->fileB= $self->fh
ABresult
1X1
011
000
338 }
339 }
340  
341 11 exit(0);
342 }
343 }
344  
345  
346 15 1;
347  
348 __END__
↑ Summary Exporter/RDF.pm »