Store/AllegroGraph.pm

filter: Statementstmt 28.6% 37 / 129 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 39.2% 11 / 28 Podpod 100% 11 / 11 totaltotal 27.7% 59 / 213 CC 21 SCAR 21.9
CC
21
Cov
22%
_new_with_config
30.0
get_statements
13.6
count_statements
12.0
CRAP 8.9
↑ Summary AllegroGraph.pm »
1 package RDF::Trine::Store::AllegroGraph;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use base qw(RDF::Trine::Store);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our $VERSION;
10 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 $VERSION = "0.1";
12 5 my $class = __PACKAGE__;
13 5 $RDF::Trine::Store::STORE_CLASSES{ $class } = $VERSION;
14 }
15  
16 =head1 NAME
17  
18 RDF::Trine::Store::AllegroGraph - Triple store implementation based on AGv4
19  
20 =head1 SYNOPSIS
21  
22 use RDF::Trine::Store::AllegroGraph;
23 my $trine = RDF::Trine::Store->new_with_string( "AllegroGraph;http://ag_server:10035/scratch/catlitter" );
24  
25 # use the RDF::Trine::Store API
26  
27 =head1 DESCRIPTION
28  
29 This package implements the RDF::Trine::Store API on the basis of the AllegroGraph v4 Perl client.
30  
31 You will almost never invoke any of the functions/methods here directly, but will peruse the Trine
32 invocation stack. The only exception for that are those things which an AGv4 repository offers, but
33 are not covered by the Trine API. These are, among other things:
34  
35 =over
36  
37 =item
38  
39 I<Prolog> rule based querying
40  
41 =item
42  
43 I<sessions> and I<transactions>
44  
45 =item
46  
47 I<geo-spatial> support
48  
49 =item
50  
51 I<social network analysis>
52  
53 =back
54  
55 For these I suggest that you pull out the AGv4 repository from the trine store like this:
56  
57 my $repo = $store->{model};
58 my $result = $repo->prolog ("....");
59  
60 B<NOTE>: This will probably change as L<RDF::Trine> evolves.
61  
62 =head2 Restrictions/Shortcomings
63  
64 =over
65  
66 =item For the time being this will not be fast, and that for a number of reasons.
67  
68 =item This store has native support for SPQARL 1.0. You should be able to use SPARQL 1.1 as that
69 will be intercepted by the Trine framework. It will be slow, though, as it will be pulling
70 statements individually. To handle this properly will be future work.
71  
72 =back
73  
74  
75 =head1 INTERFACE
76  
77 =head2 Constructor(s)
78  
79 Are essentially those from L<RDF::Trine::Store> with the following flavor:
80  
81 =over
82  
83 =item C<_new_with_object> accepts one RDF::AllegroGraph::Repository4 object.
84  
85 That will be wrapped into the store and used from then on.
86  
87 =item C<_new_with_string> accepts a string of the following form:
88  
89 AllegroGraph;<HTTP-address-of-server>/<catalog-name>/<repository-name>
90  
91 such as
92  
93 AllegroGraph;http://super:super@127.0.0.1:10035/test/experiment
94  
95 B<NOTE>: If the repository does not exist, it will be generated (that usually takes a bit of time
96 with AG).
97  
98 =item C<_new_with_config> accepts a hash with the following fields:
99  
100 =over
101  
102 =item C<storetype>
103  
104 must be C<AllegroGraph>
105  
106 =item C<server>
107  
108 must be an HTTP URL of the server (excluding trailing slash), defaults to C<http://127.0.0.1:10035>
109  
110 =item C<catalog>
111  
112 must be an identifier of the form /something
113  
114 =item C<repository>
115  
116 must be an identifier of the form /somethingelse
117  
118 =item C<username>
119  
120 is used for authentication
121  
122 =item C<password>
123  
124 ditto
125  
126 =back
127  
128 =back
129  
130 =cut
131  
132 0 ▶sub _new_with_object {
Subroutine _new_with_object: not called CC=2 SCAR=17.9
133 0 my $class = shift;
134 0 my $obj = shift;
135 0 ▶ die "no valid AllegroGraph repository" unless $obj->isa ('RDF::AllegroGraph::Repository4');
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isa("RDF::AllegroGraph::Repository4") 0 0 0
136 0 return bless { model => $obj }, $class;
137 }
138  
139 0 ▶sub _new_with_config {
Subroutine _new_with_config: not called CC=5 SCAR=34.0
140 0 my $class = shift;
141 0 my $config = shift;
142 0 ▶ $config->{server} ||= 'http://127.0.0.1:10035';
Condition0%0/2
$config->{"server"} ||= "http://127.0.0.1:10035"
l!l
00
MC/DC0%0/1
$config->{"server"} ||= "http://127.0.0.1:10035"
$config->{"server"}
Truth table0%0/2
A
A= $config->{"server"}
Aresult
11
00
143 0 ▶ $config->{catalog} =~ m{^/\w+} or die "catalog identifier should be of the form /something";
Branch: 1 branch
BranchTrueFalseTotal
$config->{"catalog"} =~ m[^/\w+] or die "catalog identifier should be of the form /something" 0 0 0
144 0 ▶ $config->{repository} =~ m{^/\w+} or die "repository identifier should be of the form /something";
Branch: 1 branch
BranchTrueFalseTotal
$config->{"repository"} =~ m[^/\w+] or die "repository identifier should be of the form /something" 0 0 0
145  
146 0 my $server = $config->{server};
147 0 ▶ $server =~ s{http://}{http://$config->{username}:$config->{password}\@} if $config->{username};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"username"} 0 0 0
148 0 my $url = $server . $config->{catalog} . $config->{repository};
149 0 return $class->_new_with_string ($url);
150 }
151  
152 0 ▶sub _new_with_string {
Subroutine _new_with_string: not called CC=1 SCAR=0.8
153 0 my $class = shift;
154 0 my $config = shift;
155 5 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 0 my $uri = new URI ($config);
157 0 my $server = sprintf "%s://%s", $uri->scheme, $uri->authority;
158  
159 5 ▶ use RDF::AllegroGraph::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
160 0 my $storage = new RDF::AllegroGraph::Easy ($server); # we get hold of the backend server
161 5 ▶ use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
162 0 my $model = $storage->model ($uri->path, mode => O_CREAT); # and break out one model
163 0 return bless { model => $model }, $class;
164 }
165  
166 =pod
167  
168 =head2 Methods
169  
170 =over
171  
172 =item B<temporary_store>
173  
174 This is not implemented as we need serious information to actually create a repository (and to get
175 rid of it eventually).
176  
177 TODO: As additional parameters could provide this information, one could use that.
178  
179 =cut
180  
181 0 ▶sub temporary_store {
Subroutine temporary_store: not called CC=1 SCAR=6.9
Pod documented
182 0 my $class = shift;
183 0 die "cannot create a temporary store without information about the AG server";
184 }
185  
186 =pod
187  
188 =item B<supports>
189  
190 Currently this store supports the following features:
191  
192 =over
193  
194 =item http://www.w3.org/ns/sparql-service-description#SPARQL10Query
195  
196 I.e. SPARQL 1.0 queries can be directly funneled into here.
197  
198 =back
199  
200 =cut
201  
202 5 my %features = map { $_ => 1 } (
203 'http://www.w3.org/ns/sparql-service-description#SPARQL10Query',
204 );
205 0 ▶sub supports {
Subroutine supports: not called CC=2 SCAR=17.9
Pod documented
206 0 my $self = shift;
207 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
208 0 my $f = shift;
209 0 return $features{ $f };
210 } else {
211 0 return keys %features;
212 }
213 }
214  
215 =pod
216  
217 =item B<get_sparql>
218  
219 If you pass in a string holding a SPARQL query, then you will get back an
220 L<RDF::Trine::Iterator::Bindings> iterator.
221  
222 =cut
223  
224 0 ▶sub get_sparql {
Subroutine get_sparql: not called CC=3 SCAR=18.7
Pod documented
225 0 my $self = shift;
226 0 my $sparql = shift;
227  
228 0 my $results = $self->{model}->sparql ($sparql, RETURN => 'NAMED_TUPLE_LIST');
229 #warn "my sparql ".Dumper $results;
230 0 my @bs;
231 0 foreach my $vs (@{ $results->{values} }) { # reformatting results for the iterator
232 0 my @vs = map { RDF::Trine::Node::Resource->new ($_) }
233 0 ▶ map { /<(.+)>/ and $1}
Condition0%0/3
/<(.+)>/ && $1
!ll&&!rl&&r
000
MC/DC0%0/2
/<(.+)>/ && $1
/<(.+)>/ $1
Truth table0%0/3
A && B
A= /<(.+)>/B= $1
ABresult
0X0
100
111
234 @$vs;
235 5 ▶ use List::MoreUtils qw(zip);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 0 my @vss = zip @{ $results->{names} }, @vs;
237 0 push @bs, { @vss };
238 }
239  
240 5 ▶ use RDF::Trine::Iterator::Bindings;
Subroutine BEGIN: called CC=1 SCAR=0.0
241 0 return RDF::Trine::Iterator::Bindings->new(\@bs, $results->{names} );
242 }
243  
244 =pod
245  
246 =item B<count_statements>
247  
248 As the mother class commands.
249  
250 =cut
251  
252 0 ▶sub count_statements {
Subroutine count_statements: not called CC=3 SCAR=24.8
Pod documented
253 0 my $self = shift;
254 0 ▶ die "count: not in bulk mode please" if $self->{bulky};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulky"} 0 0 0
255 0 ▶ my @stms = $self->{model}->match ([ map { defined $_ ? $_->as_string : undef } @_ ]);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
256 # warn Dumper \@stms;
257 0 return scalar @stms;
258 }
259  
260 =pod
261  
262 =item B<add_statement>
263  
264 As the mother class, but:
265  
266 =over
267  
268 =item
269  
270 We do not check for duplicates here. I.e. you can add any number of identical triples. They will not
271 show up in your SPARQL results, but with C<get_statements> they will.
272  
273 =item
274  
275 Quads are not (yet) supported.
276  
277 =back
278  
279 =cut
280  
281 0 ▶sub add_statement {
Subroutine add_statement: not called CC=3 SCAR=24.8
Pod documented
282 0 my $self = shift;
283 0 my $stm = shift;
284  
285 0 my @stms = $self->{model}->match ( [ map { $_->as_string } $stm->nodes ] );
286 0 ▶ return if @stms; # do not add a statement already existing
Branch: 1 branch
BranchTrueFalseTotal
if @stms 0 0 0
287  
288 0 ▶ if ($self->{bulky}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'bulky'}) { } 0 0 0
289 0 push @{ $self->{'+updates'} }, $stm;
290 } else { # do it immediately
291 0 $self->{model}->add ( [ map { $_->as_string } $stm->nodes ] );
292 }
293 }
294  
295 =pod
296  
297 =item B<remove_statement>
298  
299 Same as mother class.
300  
301 B<NOTE>: No quads yet. And this does not work within bulk mode (yet).
302  
303 =cut
304  
305 0 ▶sub remove_statement {
Subroutine remove_statement: not called CC=2 SCAR=17.9
Pod documented
306 0 my $self = shift;
307 0 my $stm = shift;
308  
309 0 ▶ die "remove statement: not in bulk mode please" if $self->{bulky};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulky"} 0 0 0
310 0 $self->{model}->delete ( [ map { $_->as_string } $stm->nodes ] );
311 }
312  
313 =pod
314  
315 =item B<remove_statements>
316  
317 See C<remove_statement>
318  
319 =cut
320  
321  
322 0 ▶sub remove_statements {
Subroutine remove_statements: not called CC=3 SCAR=24.8
Pod documented
323 0 my $self = shift;
324 0 ▶ die "remove statements: not in bulk mode please" if $self->{bulky};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulky"} 0 0 0
325 0 ▶ $self->{model}->delete ( [ map { $_->is_variable ? undef : $_->as_string } @_ ] );
Branch: 1 branch
BranchTrueFalseTotal
$_->is_variable ? : 0 0 0
326 }
327  
328 =pod
329  
330 =item B<get_statements>
331  
332 As the mother class.
333  
334 =cut
335  
336 0 ▶sub get_statements {
Subroutine get_statements: not called CC=4 SCAR=26.1
Pod documented
337 0 my $self = shift;
338 0 my @nodes = @_;
339 0 ▶ die "get statements: not in bulk mode please" if $self->{bulky};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bulky"} 0 0 0
340 0 ▶ my @stms = $self->{model}->match ( [ map { $_->is_variable ? undef : $_->as_string } @nodes ] );
Branch: 1 branch
BranchTrueFalseTotal
$_->is_variable ? : 0 0 0
341  
342 5 ▶ use RDF::Trine::Iterator::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
343 return RDF::Trine::Iterator::Graph->new( [
344 0 map { RDF::Trine::Statement->new ( map { RDF::Trine::Node::Resource->new ($_) }
345 0 ▶ map { /<(.+)>/ and $1}
Condition0%0/3
/<(.+)>/ && $1
!ll&&!rl&&r
000
MC/DC0%0/2
/<(.+)>/ && $1
/<(.+)>/ $1
Truth table0%0/3
A && B
A= /<(.+)>/B= $1
ABresult
0X0
100
111
346 @$_) }
347 @stms
348 ] );
349 }
350  
351 =pod
352  
353 =item B<get_contexts>
354  
355 For now, this always will return an empty list.
356  
357 TODO: When Perl AG supports more context features this will change.
358  
359 =cut
360  
361 0 ▶sub get_contexts {
Subroutine get_contexts: not called CC=1 SCAR=6.9
Pod documented
362 0 return RDF::Trine::Iterator->new( [] );
363 }
364  
365 =pod
366  
367 =item B<size>
368  
369 As for the mother class, but remember that AG allows you to hold duplicates.
370  
371 =cut
372  
373 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
374 0 my $self = shift;
375 0 return $self->{model}->size;
376 }
377  
378 =pod
379  
380 =item B<etag>
381  
382 Not implemented. Will die.
383  
384 =cut
385  
386 # TODO: what is the intention MD5 over everything?
387 0 ▶sub etag {
Subroutine etag: not called CC=1 SCAR=6.9
Pod documented
388 0 my $self = shift;
389 0 die "not yet implemented as I am unsure what it exactly means";
390 }
391  
392  
393 =pod
394  
395 =item B<_begin_bulk_ops>, B<_end_bulk_ops>
396  
397 Currently bulk operation is only supporting I<adding> statements. Not I<deleting> them.
398  
399 =cut
400  
401 0 ▶sub _begin_bulk_ops {
Subroutine _begin_bulk_ops: not called CC=1 SCAR=6.9
402 0 my $self = shift;
403 # warn "start bulk";
404 0 $self->{bulky}++;
405 0 $self->{'+updates'} = []; # we will collect statements to add (and later remove)
406 }
407  
408  
409 # at the moment only for adding
410 0 ▶sub _end_bulk_ops {
Subroutine _end_bulk_ops: not called CC=3 SCAR=24.8
411 0 my $self = shift;
412 # warn "end bulk";
413 0 ▶ return unless $self->{bulky}; # if we never have been touched, forget it
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bulky"} 0 0 0
414 0 $self->{bulky}--; # reduce bulking
415 0 ▶ if ($self->{bulky} == 0) { # if we are out of bulk mode, then
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bulky"} == 0) 0 0 0
416 $self->{model}->add ( map { # we all add it in one go
417 0 [ map { $_->as_string } $_->nodes ]
418 0 } @{ $self->{'+updates'} } );
419 0 $self->{'+updates'} = [];
420 }
421 }
422  
423  
424 0 ▶sub _nuke {
Subroutine _nuke: not called CC=1 SCAR=6.9
425 0 my $self = shift;
426 0 $self->{model}->disband;
427 }
428  
429 =pod
430  
431 =back
432  
433 =head1 AUTHOR
434  
435 Robert Barta, C<< <drrho at cpan.org> >>
436  
437 =head1 BUGS
438  
439 Please report any bugs or feature requests to C<bug-rdf-trine-allegrograph at rt.cpan.org>, or through
440 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RDF-Trine-AllegroGraph>. I will be notified, and then you'll
441 automatically be notified of progress on your bug as I make changes.
442  
443 =head1 SEE ALSO
444  
445 L<RDF::Trine::AllegroGraph>
446  
447 =head1 ACKNOWLEDGEMENTS
448  
449 The development of this package was supported by Franz Inc.
450  
451 =head1 COPYRIGHT & LICENSE
452  
453 Copyright 2011 Robert Barta, all rights reserved.
454  
455 This program is free software; you can redistribute it and/or modify it under the same terms as Perl
456 itself.
457  
458 =cut
459  
460  
461  
462 5 1;
463  
464 __END__
↑ Summary AllegroGraph.pm »