File Coverage

blib/lib/AtteanX/Store/LDF.pm
Criterion Covered Total %
statement 26 50 52.0
branch 0 12 0.0
condition n/a
subroutine 9 14 64.2
pod 2 2 100.0
total 37 78 47.4


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   13611 use v5.14;
  1         3  
43 1     1   3 use warnings;
  1         1  
  1         58  
44              
45             package AtteanX::Store::LDF;
46              
47             our $AUTHORITY = 'cpan:KJETILK';
48             our $VERSION = '0.02';
49              
50 1     1   475 use Moo;
  1         9461  
  1         4  
51 1     1   1450 use Attean::API::Store;
  1         42712  
  1         31  
52 1     1   5 use Type::Tiny::Role;
  1         1  
  1         19  
53 1     1   371 use Types::URI -all;
  1         121524  
  1         10  
54 1     1   1926 use RDF::LDF;
  1         1368885  
  1         41  
55 1     1   7 use namespace::clean;
  1         1  
  1         8  
56 1     1   239 use Carp;
  1         1  
  1         559  
57              
58             with 'Attean::API::TripleStore', '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 0     0 1   my $self = shift;
198 0           my $plan = shift;
199 0 0         if ($plan->isa('AtteanX::Plan::LDF::Triple')) {
200 0           my $totals = $self->count_triples_estimate();
201 0 0         if ($totals < 1) {
202 0           $self->log->error("Total number of triples in model were $totals, probably an error");
203 0           return 10000; # Probably a plan we don't want
204             }
205 0           return 10 + int(990 * $self->count_triples_estimate($plan->values) / $totals)
206             }
207 0           return;
208             }
209              
210             1;
211              
212             __END__
213              
214             =back
215              
216             =head1 SEE ALSO
217              
218             L<Attean> , L<Attean::API::TripleStore>
219              
220             =head1 BUGS
221              
222             Please report any bugs or feature requests to through the GitHub web interface
223             at L<https://github.com/phochste/AtteanX-Store-LDF>.
224              
225             =head1 AUTHOR
226              
227             Patrick Hochstenbach C<< <patrick.hochstenbach@ugent.be> >>
228             Kjetil Kjernsmo E<lt>kjetilk@cpan.orgE<gt>.
229              
230             =head1 COPYRIGHT
231              
232             This software is copyright (c) 2015 by Patrick Hochstenbach.
233             This software is copyright (c) 2016 by Patrick Hochstenbach and Kjetil Kjernsmo.
234             This is free software; you can redistribute it and/or modify it under
235             the same terms as the Perl 5 programming language system itself.
236              
237             =cut