/usr/share/perl5/Attean/TripleModel.pm is in libattean-perl 0.019-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 | use v5.14;
use warnings;
=head1 NAME
Attean::TripleModel - RDF model backed by a set of triple-stores
=head1 VERSION
This document describes Attean::TripleModel version 0.019
=head1 SYNOPSIS
use v5.14;
use Attean;
my $model = Attean::TripleModel->new( stores => {
'http://example.org/graph1' => $store1,
'http://example.org/graph2' => $store2,
} );
=head1 DESCRIPTION
The Attean::TripleModel class represents a model that is backed by a set of
L<Attean::API::TripleStore|Attean::API::Store> objects, identified by an IRI
string. It conforms to the L<Attean::API::Model> role.
The Attean::TripleModel constructor requires one named argument:
=over 4
=item stores
A hash mapping graph IRI values to L<Attean::API::TripleStore|Attean::API::Store>
objects representing the backing triple-store for that graph.
=back
=head1 METHODS
=over 4
=cut
package Attean::TripleModel 0.019 {
use Moo;
use Types::Standard qw(ArrayRef ConsumerOf HashRef);
use Scalar::Util qw(reftype blessed);
use namespace::clean;
with 'MooX::Log::Any';
with 'Attean::API::Model';
with 'Attean::API::CostPlanner';
has 'stores' => (
is => 'ro',
isa => HashRef[ConsumerOf['Attean::API::TripleStore']],
required => 1,
default => sub { +{} },
);
=item C<< size >>
=cut
sub size {
my $self = shift;
my $count = 0;
foreach my $store (values %{ $self->stores }) {
$count += $store->size;
}
return $count;
}
=item C<< count_quads >>
=cut
sub count_quads {
my $self = shift;
# TODO: don't materialize results here just to count them
my $iter = $self->get_quads( @_ );
my $count = 0;
while (my $r = $iter->next) {
$count++;
}
return $count;
}
=item C<< get_graphs >>
=cut
sub get_graphs {
my $self = shift;
my @graphs = map { Attean::IRI->new($_) } keys %{ $self->stores };
return Attean::ListIterator->new( values => \@graphs, item_type => 'Attean::API::Term' );
}
=item C<< get_quads ( $subject, $predicate, $object, $graph ) >>
Returns an L<Attean::API::Iterator> for quads in the model that match the
supplied C<< $subject >>, C<< $predicate >>, C<< $object >>, and C<< $graph >>.
Any of these terms may be undefined or a L<Attean::API::Variable> object, in
which case that term will be considered as a wildcard for the purposes of
matching.
The returned iterator conforms to both L<Attean::API::Iterator> and
L<Attean::API::QuadIterator>.
=cut
sub get_quads {
my $self = shift;
my @nodes = @_[0..3];
foreach my $i (0..3) {
my $t = $nodes[$i] // Attean::Variable->new();
if (not(ref($t)) or reftype($t) ne 'ARRAY') {
$nodes[$i] = [$t];
}
}
my @iters;
foreach my $s (@{ $nodes[0] }) {
foreach my $p (@{ $nodes[1] }) {
foreach my $o (@{ $nodes[2] }) {
foreach my $g (@{ $nodes[3] }) {
my $iter = $self->_get_quads($s, $p, $o, $g);
push(@iters, $iter);
}
}
}
}
if (scalar(@iters) <= 1) {
return shift(@iters);
} else {
return Attean::IteratorSequence->new( iterators => \@iters, item_type => $iters[0]->item_type );
}
}
sub _get_quads {
my $self = shift;
my $s = shift;
my $p = shift;
my $o = shift;
my $g = shift;
if (blessed($g) and $g->does('Attean::API::IRI')) {
if (my $store = $self->stores->{ $g->value }) {
my $iter = $store->get_triples($s, $p, $o);
return $iter->as_quads($g);
}
} elsif (blessed($g) and $g->does('Attean::API::Variable')) {
my @iters;
while (my ($g, $store) = each %{ $self->stores }) {
my $iter = $store->get_triples($s, $p, $o);
my $graph = Attean::IRI->new($g);
my $quads = $iter->map(sub { $_->as_quad($graph) }, 'Attean::API::Quad');
push(@iters, $quads);
}
my $iter = Attean::IteratorSequence->new( iterators => \@iters, item_type => $iters[0]->item_type );
return $iter;
} else {
my $name = (blessed($g) and $g->can('as_string')) ? $g->as_string : "$g";
$self->log->warn("TripleModel cannot produce quads for non-IRI graph: $name");
}
return Attean::ListIterator->new( values => [], item_type => 'Attean::API::Quad' );
}
=item C<< plans_for_algebra( $algebra, $planner, $active_graphs, $default_graphs ) >>
Delegates to an underlying store if the active graph is bound to the store,
and the store consumes Attean::API::CostPlanner.
=cut
sub plans_for_algebra {
my $self = shift;
my $algebra = shift;
my $planner = shift;
my $active_graphs = shift;
my $default_graphs = shift;
my @plans;
if (scalar(@$active_graphs) == 1) {
my $graph = $active_graphs->[0];
if (my $store = $self->stores->{ $graph->value }) {
if ($store->does('Attean::API::CostPlanner')) {
push(@plans, $store->plans_for_algebra($algebra, $planner, $active_graphs, $default_graphs));
}
}
}
return @plans;
}
=item C<< cost_for_plan( $plan ) >>
Attempts to delegate to all the underlying stores if that store consumes Attean::API::CostPlanner.
=cut
sub cost_for_plan {
my $self = shift;
my $plan = shift;
foreach my $store (values %{ $self->stores }) {
if ($store->does('Attean::API::CostPlanner')) {
if (defined(my $cost = $store->cost_for_plan($plan, @_))) {
return $cost;
}
}
}
return;
}
}
package Attean::AddativeTripleModelRole 0.019 {
use Scalar::Util qw(blessed);
use Types::Standard qw(CodeRef);
use Moo::Role;
with 'Attean::API::Model';
has 'store_constructor' => (is => 'ro', isa => CodeRef, required => 1);
=item C<< add_store( $graph => $store ) >>
Add the L<Attean::TripleStore> C<< $store >> object to the model using the
IRI string value C<< $graph >> as the graph name.
=cut
sub add_store {
my $self = shift;
my $graph = shift;
my $iri = blessed($graph) ? $graph->value : $graph;
my $store = shift;
die if exists $self->stores->{ $iri };
$self->stores->{ $iri } = $store;
}
=item C<< create_graph( $graph ) >>
Create a new L<Attean::TripleStore> and add it to the model using the
L<Attean::API::IRI> C<< $graph >> as the graph name.
The store is constructed by using this object's C<< store_constructor >>
attribute:
my $store = $self->store_constructor->($graph);
=cut
sub create_graph {
my $self = shift;
my $graph = shift;
my $iri = $graph->value;
return if exists $self->stores->{ $iri };
my $store = $self->store_constructor->($graph);
$self->stores->{ $iri } = $store;
};
=item C<< drop_graph( $graph ) >>
Removes the store associated with the given C<< $graph >>.
=cut
sub drop_graph {
my $self = shift;
my $g = shift;
if ($g->does('Attean::API::IRI')) {
delete $self->stores->{ $g->value };
}
}
}
package Attean::MutableTripleModel 0.019 {
use Moo;
use Types::Standard qw(ArrayRef ConsumerOf HashRef);
use Scalar::Util qw(reftype);
use namespace::clean;
extends 'Attean::TripleModel';
with 'Attean::API::MutableModel';
has 'stores' => (
is => 'ro',
isa => HashRef[ConsumerOf['Attean::API::MutableTripleStore']],
required => 1,
default => sub { +{} },
);
=item C<< add_quad ( $quad ) >>
Adds the specified C<$quad> to the underlying model.
=cut
sub add_quad {
my $self = shift;
my $q = shift;
my $g = $q->graph;
die "Cannot add a quad whose graph is not an IRI" unless ($g->does('Attean::API::IRI'));
my $v = $g->value;
if (my $store = $self->stores->{ $v }) {
$store->add_triple( $q->as_triple );
} else {
Carp::confess "No such graph: $v";
}
}
=item C<< remove_quad ( $quad ) >>
Removes the specified C<< $quad >> from the underlying store.
=cut
sub remove_quad {
my $self = shift;
my $q = shift;
my $g = $q->graph;
if ($g->does('Attean::API::IRI')) {
my $v = $g->value;
if (my $store = $self->stores->{ $v }) {
$store->remove_triple( $q->as_triple );
}
}
}
sub create_graph { die; }
=item C<< drop_graph( $graph ) >>
Removes the store associated with the given C<< $graph >>.
=cut
sub drop_graph {
my $self = shift;
my $g = shift;
if ($g->does('Attean::API::IRI')) {
delete $self->stores->{ $g->value };
}
}
=item C<< clear_graph( $graph ) >>
Removes all quads with the given C<< $graph >>.
=cut
sub clear_graph {
my $self = shift;
my $g = shift;
$self->drop_graph($g);
$self->create_graph($g);
}
}
package Attean::AddativeTripleModel 0.019 {
use Moo;
use Scalar::Util qw(blessed);
use Types::Standard qw(CodeRef);
use namespace::clean;
extends 'Attean::TripleModel';
with 'Attean::AddativeTripleModelRole';
}
package Attean::AddativeMutableTripleModel 0.019 {
use Moo;
use Scalar::Util qw(blessed);
use Types::Standard qw(CodeRef);
use namespace::clean;
extends 'Attean::MutableTripleModel';
with 'Attean::AddativeTripleModelRole';
}
1;
__END__
=back
=head1 BUGS
Please report any bugs or feature requests to through the GitHub web interface
at L<https://github.com/kasei/attean/issues>.
=head1 SEE ALSO
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2018 Gregory Todd Williams.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|