AtteanX-Store-LDF
view release on metacpan or search on metacpan
lib/AtteanX/Store/LDF.pm view on Meta::CPAN
use Attean;
use Attean::RDF qw(iri blank literal);
use AtteanX::Store::LDF;
my $uri = 'http://fragments.dbpedia.org/2014/en';
my $store = Attean->get_store('LDF')->new(start_url => $uri);
my $iter = $store->get_triples(undef,undef,literal("Albert Einstein"));
while (my $triple = $iter->next) {
say $triple->subject->ntriples_string .
" " .
$triple->predicate->ntriples_string .
" " .
$triple->object->ntriples_string .
" .";
}
=head1 DESCRIPTION
AtteanX::Store::LDF provides a triple-store connected to a Linked Data Fragment server.
For more information on Triple Pattern Fragments consult L<http://linkeddatafragments.org/>
=cut
use v5.14;
use warnings;
package AtteanX::Store::LDF;
our $AUTHORITY = 'cpan:KJETILK';
our $VERSION = '0.04';
use Moo;
use Attean;
use Type::Tiny::Role;
use Types::URI -all;
use RDF::LDF;
use namespace::clean;
use Carp;
with 'Attean::API::TripleStore', 'Attean::API::CostPlanner', 'MooX::Log::Any';
=head1 METHODS
Beyond the methods documented below, this class inherits methods from the
L<Attean::API::TripleStore> class.
=over 4
=item new( start_url => $start_url )
Returns a new LDF-backed storage object. The required C<start_url>
argument is a URL pointing at any Linked Data Fragment. The attribure
will be coerced, so it can be a string, a URI object, etc.
=cut
has start_url => (is => 'ro', isa => Uri, coerce => 1, required => 1);
has endpoint_url => (is => 'ro', lazy => 1, builder => '_croak_on_endpoint');
has ldf => (is => 'ro', lazy => 1, builder => '_ldf');
sub _croak_on_endpoint {
Carp::croak "endpoint_url has been deprecated, use start_url instead";
}
sub _ldf {
my $self = shift;
RDF::LDF->new(url => $self->start_url->as_string);
}
sub _term_as_string {
my ($self,$term) = @_;
if (!defined $term) {
return undef
}
elsif ($term->is_variable) {
return undef;
}
elsif ($term->is_literal) {
return $term->as_string; # includes quotes and any language or datatype
}
else {
return $term->value; # the raw IRI or blank node identifier value, without other syntax
}
}
=item count_triples_estimate( $subject, $predicate, $object )
Return the count of triples matching the specified subject, predicate and
objects.
=cut
sub count_triples_estimate {
my $self = shift;
my ($s_pattern,$p_pattern,$o_pattern) = @_;
my $ldf_iter = $self->ldf->get_statements(
$self->_term_as_string($s_pattern),
$self->_term_as_string($p_pattern),
$self->_term_as_string($o_pattern)
);
return 0 unless defined $ldf_iter;
my ($statement,$info) = $ldf_iter->();
return $info->{'void_triples'};
}
=item get_triples( $subject, $predicate, $object)
Returns a stream object of all statements matching the specified subject,
predicate and objects. Any of the arguments may be undef to match any value.
=cut
sub get_triples {
my $self = shift;
my ($s_pattern,$p_pattern,$o_pattern) = @_;
my $ldf_iter = $self->ldf->get_statements(
$self->_term_as_string($s_pattern),
$self->_term_as_string($p_pattern),
$self->_term_as_string($o_pattern)
( run in 0.670 second using v1.01-cache-2.11-cpan-d8267643d1d )