/usr/share/perl5/Attean/API/Model.pm is in libattean-perl 0.017-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 | use v5.14;
use warnings;
=head1 NAME
Attean::API::Model - RDF Model
=head1 VERSION
This document describes Attean::API::Model version 0.017
=head1 DESCRIPTION
The Attean::API::Model role defines a common API for all RDF models to conform
to. It is provides a consistent interface for probing, counting, and retrieving
L<Attean::API::Quad|Attean::API::Binding>s matching a pattern, as well as
related functionality such as enumerating the graph names, and extracting
structured data from the models' quads.
=head1 REQUIRED METHODS
The following methods are required by the L<Attean::API::Model> role:
=over 4
=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:
* An L<Attean::API::Term> object, in which case matching is equality-based
* A L<Attean::API::Variable> object or C<< undef >>, in which case that term
will be considered as a wildcard for the purposes of matching
* An ARRAY reference of L<Attean::API::Term> objects, in which case the
matching will be equality-based on the disjunction of the supplied terms
The returned iterator conforms to both L<Attean::API::Iterator> and
L<Attean::API::QuadIterator>.
=item C<< count_quads( $subject, $predicate, $object, $graph ) >>
Returns the number of quads in the model matching the supplied pattern
(using the same matching semantics as C<< get_quads >>).
=item C<< get_graphs >>
Returns an L<Attean::API::Iterator> of distinct L<Attean::API::Term> objects
that are used in the graph position of quads in the model.
=back
=head1 METHODS
The L<Attean::API::Model> role provides default implementations of the
following methods:
=over 4
=item C<< get_bindings( $subject, $predicate, $object, $graph ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Result> objects
corresponding to quads in the model matching the supplied pattern. For each
L<Attean::API::Variable> in the pattern list, a mapping will be present in the
corresponding result object. For example,
C<< $model->get_bindings( variable('s') ) >> will return an iterator of results
containing just a mapping from C<< 's' >> to subjects of all quads in the
model.
=item C<< get_list( $graph, $head ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects that are
members of the rdf:List with the specified C<< $head >> (and matching
restricted to only the specified C<< $graph >>).
=item C<< get_sequence( $graph, $head ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects that are
members of the rdf:Sequence with the specified C<< $head >> (and matching
restricted to only the specified C<< $graph >>).
=item C<< subjects( $predicate, $object, $graph ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
subjects of quads matching the supplied pattern (using the same matching
semantics as C<< get_quads >>).
=item C<< predicates( $subject, $object, $graph ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
predicates of quads matching the supplied pattern (using the same matching
semantics as C<< get_quads >> with an C<< undef >> predicate).
=item C<< objects( $subject, $predicate, $graph ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
objects of quads matching the supplied pattern (using the same matching
semantics as C<< get_quads >> with an C<< undef >> object).
=item C<< graphs( $subject, $predicate, $object ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
graphs of quads matching the supplied pattern (using the same matching
semantics as C<< get_quads >> with an C<< undef >> graph).
=item C<< graph_nodes( $graph ) >>
Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of unique
subjects and objects present in the specified C<< $graph >>.
=cut
use Attean::API::Binding;
package Attean::API::Model 0.017 {
use Moo::Role;
use Sub::Install;
use Sub::Util qw(set_subname);
use URI::Namespace;
use Scalar::Util qw(blessed);
use List::MoreUtils qw(uniq);
use namespace::clean;
# get_quads($s, $p, $o, $g)
# or:
# get_quads([$s1, $s2, ...], \@p, \@o, \@g)
requires 'get_quads';
sub get_bindings {
my $self = shift;
my @nodes = @_;
my @pos = Attean::API::Quad->variables;
my %vars;
foreach my $i (0 .. $#nodes) {
my $n = $nodes[$i];
if (blessed($n) and $n->isa('Attean::Variable')) {
my $name = $n->value;
$vars{ $pos[ $i ] } = $name;
}
}
my $quads = $self->get_quads(@nodes);
return $quads->map(sub {
my $q = shift;
return unless blessed($q);
my %bindings = map { $vars{$_} => $q->$_() } (keys %vars);
return Attean::Result->new( bindings => \%bindings );
}, 'Attean::API::Result', variables => [values %vars]);
}
requires 'count_quads';
requires 'get_graphs';
sub get_list {
my $self = shift;
die "get_list called without a graph name" unless (scalar(@_));
my $graph = shift;
die "get_list called without a list head" unless (scalar(@_));
my $head = shift;
my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
my @elements;
my %seen;
while (blessed($head) and not($head->does('Attean::API::IRI') and $head->value eq $rdf_nil->value)) {
if ($seen{ $head->as_string }++) {
die "Loop found during rdf:List traversal";
}
my @n = $self->objects( $head, $rdf_first )->elements;
if (scalar(@n) != 1) {
die "Invalid structure found during rdf:List traversal";
}
push(@elements, @n);
($head) = $self->objects( $head, $rdf_rest )->elements;
}
return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
}
sub get_sequence {
my $self = shift;
my $graph = shift;
my $head = shift;
my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
my @elements;
my $i = 1;
while (1) {
my $term = Attean::IRI->new("${rdf}_$i");
my @elem = $self->objects( $head, $term, $graph )->elements;
last unless (scalar(@elem));
if (scalar(@elem) > 1) {
my $count = scalar(@elem);
die "Invalid structure found during rdf:Seq access: $count elements found for element $i";
}
my $elem = $elem[0];
last unless (blessed($elem));
push(@elements, $elem);
$i++;
}
return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
}
{
my @pos = Attean::API::Quad->variables;
my %pos = map { $pos[$_] => $_ } (0 .. $#pos);
for my $method (@pos) {
my $code = sub {
my $self = shift;
my @nodes = @_;
$#nodes = 3;
splice(@nodes, $pos{$method}, 0, undef);
my $iter = $self->get_quads(@nodes);
my $nodes = $iter->map(
sub { $_->$method() },
'Attean::API::Term',
);
return $nodes;
};
Sub::Install::install_sub({
code => set_subname("${method}s", $code),
as => "${method}s"
});
}
}
sub graph_nodes {
my $self = shift;
my $graph = shift;
my $s = $self->subjects(undef, undef, $graph);
my $o = $self->objects(undef, undef, $graph);
my $union = Attean::IteratorSequence->new( iterators => [$s, $o], item_type => 'Attean::API::Term' );
my %seen;
return $union->grep(sub {not($seen{shift->as_string}++)});
}
}
package Attean::API::MutableModel 0.017 {
use Moo::Role;
use Attean::RDF;
use LWP::UserAgent;
use Encode qw(encode);
use Scalar::Util qw(blessed);
use namespace::clean;
requires 'add_quad';
requires 'remove_quad';
requires 'create_graph';
requires 'drop_graph';
requires 'clear_graph';
requires 'add_iter';
with 'Attean::API::Model';
sub load_urls_into_graph {
my $self = shift;
my $graph = shift;
my @urls = @_;
my $ua = LWP::UserAgent->new();
my $accept = Attean->acceptable_parsers( handles => 'Attean::API::Triple' );
$ua->default_headers->push_header( 'Accept' => $accept );
foreach my $u (@urls) {
my $url = blessed($u) ? $u->value : $u;
my $resp = $ua->get($url);
if ($resp->is_success) {
my $ct = $resp->header('Content-Type');
my $pclass = Attean->get_parser( media_type => $ct ) // Attean->get_parser('ntriples');
if ($pclass) {
my $p = $pclass->new(base => iri($url));
my $str = $resp->decoded_content;
my $bytes = encode('UTF-8', $str, Encode::FB_CROAK);
my $iter = $p->parse_iter_from_bytes( $bytes );
$self->add_iter($iter->as_quads($graph));
} else {
die "No parser found for content type $ct: $url";
}
} else {
die $resp->status_line;
}
}
}
# $model->load_triples( 'turtle', iri('http://example.org/graph1') => "@prefix foaf: ...", iri('http://example.org/graph2') => "@prefix foaf: ..." );
sub load_triples {
my $self = shift;
my $format = shift;
my $class = Attean->get_parser($format) || die "Failed to load parser for '$format'";
my $parser = $class->new() || die "Failed to construct parser for '$format'";
while (scalar(@_)) {
my ($graph, $string) = splice(@_, 0, 2);
my $iter = $parser->parse_iter_from_bytes(encode('UTF-8', $string, Encode::FB_CROAK));
my $quads = $iter->as_quads($graph);
$self->add_iter($quads);
}
}
sub add_iter {
my $self = shift;
my $iter = shift;
my $type = $iter->item_type;
die "Iterator type $type isn't quads" unless (Role::Tiny::does_role($type, 'Attean::API::Quad'));
while (my $q = $iter->next) {
$self->add_quad($q);
}
}
sub add_list {
my $self = shift;
die "add_list called without a graph name" unless (scalar(@_));
my $graph = shift;
my @elements = @_;
my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
if (scalar(@elements) == 0) {
return $rdf_nil;
} else {
my $head = Attean::Blank->new();
my $node = shift(@elements);
my $rest = $self->add_list($graph, @elements);
$self->add_quad( Attean::Quad->new($head, $rdf_first, $node, $graph) );
$self->add_quad( Attean::Quad->new($head, $rdf_rest, $rest, $graph) );
return $head;
}
}
}
package Attean::API::ETagCacheableModel 0.017 {
use Moo::Role;
requires 'etag_value_for_quads';
}
package Attean::API::TimeCacheableModel 0.017 {
use Moo::Role;
requires 'mtime_for_quads';
}
package Attean::API::BulkUpdatableModel 0.017 {
use Moo::Role;
with 'Attean::API::MutableModel';
requires 'begin_bulk_updates';
requires 'end_bulk_updates';
around [qw(load_triples add_iter add_list)] => sub {
my $orig = shift;
my $self = shift;
$self->begin_bulk_updates();
$self->$orig(@_);
$self->end_bulk_updates();
};
# End bulk updates the moment a read operation is performed...
before [qw(get_quads get_bindings count_quads get_graphs subject predicate object graph)] => sub {
my $self = shift;
$self->end_bulk_updates();
};
}
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
L<http://www.perlrdf.org/>
=head1 AUTHOR
Gregory Todd Williams C<< <gwilliams@cpan.org> >>
=head1 COPYRIGHT
Copyright (c) 2014--2016 Gregory Todd Williams.
This program is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.
=cut
|