File Coverage

blib/lib/AtteanX/Store/LDF.pm
Criterion Covered Total %
statement 26 42 61.9
branch 0 8 0.0
condition n/a
subroutine 9 14 64.2
pod 2 2 100.0
total 37 66 56.0


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             AtteanX::Store::LDF - Linked Data Fragment RDF store
4              
5             =begin markdown
6              
7             # STATUS
8             [![Build Status](https://travis-ci.org/phochste/AtteanX-Store-LDF.svg)](https://travis-ci.org/phochste/AtteanX-Store-LDF)
9             [![Coverage Status](https://coveralls.io/repos/phochste/AtteanX-Store-LDF/badge.svg)](https://coveralls.io/r/phochste/AtteanX-Store-LDF)
10             [![Kwalitee Score](http://cpants.cpanauthors.org/dist/AtteanX-Store-LDF.png)](http://cpants.cpanauthors.org/dist/AtteanX-Store-LDF)
11              
12             =end markdown
13              
14             =head1 SYNOPSIS
15              
16             use v5.14;
17             use Attean;
18             use Attean::RDF qw(iri blank literal);
19             use AtteanX::Store::LDF;
20              
21             my $uri = 'http://fragments.dbpedia.org/2014/en';
22             my $store = Attean->get_store('LDF')->new(start_url => $uri);
23              
24             my $iter = $store->get_triples(undef,undef,literal("Albert Einstein"));
25              
26             while (my $triple = $iter->next) {
27             say $triple->subject->ntriples_string .
28             " " .
29             $triple->predicate->ntriples_string .
30             " " .
31             $triple->object->ntriples_string .
32             " .";
33             }
34              
35             =head1 DESCRIPTION
36              
37             AtteanX::Store::LDF provides a triple-store connected to a Linked Data Fragment server.
38             For more information on Triple Pattern Fragments consult L<http://linkeddatafragments.org/>
39              
40             =cut
41              
42 1     1   13709 use v5.14;
  1         2  
43 1     1   3 use warnings;
  1         1  
  1         53  
44              
45             package AtteanX::Store::LDF;
46              
47             our $AUTHORITY = 'cpan:KJETILK';
48             our $VERSION = '0.04';
49              
50 1     1   464 use Moo;
  1         10195  
  1         5  
51 1     1   1612 use Attean;
  1         900593  
  1         5  
52 1     1   33 use Type::Tiny::Role;
  1         1  
  1         22  
53 1     1   4 use Types::URI -all;
  1         1  
  1         16  
54 1     1   2155 use RDF::LDF;
  1         1558541  
  1         33  
55 1     1   7 use namespace::clean;
  1         1  
  1         8  
56 1     1   260 use Carp;
  1         1  
  1         576  
57              
58             with 'Attean::API::TripleStore', 'Attean::API::CostPlanner', 'MooX::Log::Any';
59              
60              
61             =head1 METHODS
62              
63             Beyond the methods documented below, this class inherits methods from the
64             L<Attean::API::TripleStore> class.
65              
66             =over 4
67              
68             =item new( start_url => $start_url )
69              
70             Returns a new LDF-backed storage object. The required C<start_url>
71             argument is a URL pointing at any Linked Data Fragment. The attribure
72             will be coerced, so it can be a string, a URI object, etc.
73              
74             =cut
75              
76             has start_url => (is => 'ro', isa => Uri, coerce => 1, required => 1);
77             has endpoint_url => (is => 'ro', lazy => 1, builder => '_croak_on_endpoint');
78             has ldf => (is => 'ro', lazy => 1, builder => '_ldf');
79              
80             sub _croak_on_endpoint {
81 0     0     Carp::croak "endpoint_url has been deprecated, use start_url instead";
82             }
83              
84             sub _ldf {
85 0     0     my $self = shift;
86 0           RDF::LDF->new(url => $self->start_url->as_string);
87             }
88              
89             sub _term_as_string {
90 0     0     my ($self,$term) = @_;
91 0 0         if (!defined $term) {
    0          
    0          
92             return undef
93 0           }
94             elsif ($term->is_variable) {
95 0           return undef;
96             }
97             elsif ($term->is_literal) {
98 0           return $term->as_string; # includes quotes and any language or datatype
99             }
100             else {
101 0           return $term->value; # the raw IRI or blank node identifier value, without other syntax
102             }
103             }
104              
105             =item count_triples_estimate( $subject, $predicate, $object )
106              
107             Return the count of triples matching the specified subject, predicate and
108             objects.
109              
110             =cut
111             sub count_triples_estimate {
112 0     0 1   my $self = shift;
113 0           my ($s_pattern,$p_pattern,$o_pattern) = @_;
114            
115 0           my $ldf_iter = $self->ldf->get_statements(
116             $self->_term_as_string($s_pattern),
117             $self->_term_as_string($p_pattern),
118             $self->_term_as_string($o_pattern)
119             );
120              
121 0 0         return 0 unless defined $ldf_iter;
122              
123 0           my ($statement,$info) = $ldf_iter->();
124              
125 0           return $info->{'void_triples'};
126             }
127              
128             =item get_triples( $subject, $predicate, $object)
129              
130             Returns a stream object of all statements matching the specified subject,
131             predicate and objects. Any of the arguments may be undef to match any value.
132              
133             =cut
134             sub get_triples {
135             my $self = shift;
136             my ($s_pattern,$p_pattern,$o_pattern) = @_;
137              
138             my $ldf_iter = $self->ldf->get_statements(
139             $self->_term_as_string($s_pattern),
140             $self->_term_as_string($p_pattern),
141             $self->_term_as_string($o_pattern)
142             );
143              
144             return Attean::ListIterator->new(values => [] , item_type => 'Attean::API::Triple')
145             unless $ldf_iter;
146              
147             my $iter = Attean::CodeIterator->new(
148             generator => sub {
149             my $statement = $ldf_iter->();
150             return () unless defined($statement);
151             my ($subject,$predicate,$object);
152              
153             if ($statement->subject->is_resource) {
154             $subject = Attean::IRI->new($statement->subject->value);
155             }
156             else {
157             $subject = Attean::Blank->new($statement->subject->value);
158             }
159              
160             $predicate = Attean::IRI->new($statement->predicate->value);
161              
162             if ($statement->object->is_resource) {
163             $object = Attean::IRI->new($statement->object->value);
164             }
165             elsif ($statement->object->is_literal) {
166             $object = Attean::Literal->new($statement->object->value);
167             }
168             else {
169             $object = Attean::Blank->new($statement->object->value);
170             }
171              
172             my @res = (
173             Attean::Triple->new(
174             subject => $subject ,
175             predicate => $predicate ,
176             object => $object
177             )
178             );
179              
180             @res;
181             },
182             item_type => 'Attean::API::Triple',
183             );
184              
185             return $iter;
186             }
187              
188             =item cost_for_plan($plan)
189              
190             Returns an cost estimation for a single LDF triple based on
191             estimates. The cost will be in the interval 10-1000 if the supplied
192             argument is a L<AtteanX::Plan::LDF::Triple>, undef otherwise.
193              
194             =cut
195              
196             sub cost_for_plan {
197             my $self = shift;
198             my $plan = shift;
199             if ($plan->isa('AtteanX::Plan::LDF::Triple')) {
200             my $totals = $self->count_triples_estimate();
201             if ($totals < 1) {
202             $self->log->error("Total number of triples in model were $totals, probably an error");
203             return 10000; # Probably a plan we don't want
204             }
205             return 10 + int(990 * $self->count_triples_estimate($plan->values) / $totals)
206             }
207             return;
208             }
209              
210             =item plans_for_algebra($algebra)
211              
212             Returns an empty plan since access_plans generates the needed plans.
213              
214             =cut
215              
216             sub plans_for_algebra {
217 0     0 1   return ();
218             }
219              
220             1;
221              
222             __END__
223              
224             =back
225              
226             =head1 SEE ALSO
227              
228             L<Attean> , L<Attean::API::TripleStore>
229              
230             =head1 BUGS
231              
232             Please report any bugs or feature requests to through the GitHub web interface
233             at L<https://github.com/phochste/AtteanX-Store-LDF>.
234              
235             =head1 AUTHOR
236              
237             Patrick Hochstenbach C<< <patrick.hochstenbach@ugent.be> >>
238             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
239              
240             =head1 COPYRIGHT
241              
242             This software is copyright (c) 2015 by Patrick Hochstenbach.
243             This software is copyright (c) 2016 by Patrick Hochstenbach and Kjetil Kjernsmo.
244             This is free software; you can redistribute it and/or modify it under
245             the same terms as the Perl 5 programming language system itself.
246              
247             =cut