/usr/share/perl5/RDF/Redland/Model.pm is in librdf-perl 1.0.13.1-2build1.
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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | # -*- Mode: Perl -*-
#
# Model.pm - Redland Perl RDF Model module
#
# Copyright (C) 2000-2005 David Beckett - http://www.dajobe.org/
# Copyright (C) 2000-2005 University of Bristol - http://www.bristol.ac.uk/
#
# This package is Free Software and part of Redland http://librdf.org/
#
# It is licensed under the following three licenses as alternatives:
# 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
# 2. GNU General Public License (GPL) V2 or any newer version
# 3. Apache License, V2.0 or any newer version
#
# You may not use this file except in compliance with at least one of
# the above three licenses.
#
# See LICENSE.html or LICENSE.txt at the top of this package for the
# full license terms.
#
#
#
package RDF::Redland::Model;
use strict;
use RDF::Redland::Iterator;
use RDF::Redland::Stream;
=pod
=head1 NAME
RDF::Redland::Model - Redland RDF Model Class
=head1 SYNOPSIS
use RDF::Redland;
my $storage=new RDF::Redland::Storage("hashes", "test", "new='yes',hash-type='memory'");
my $model=new RDF::Redland::Model($storage, "");
...
my(@sources)=$model->targets($predicate_node, $object_node);
...
=head1 DESCRIPTION
Manipulate the RDF model.
=cut
######################################################################
=pod
=head1 CONSTRUCTORS
=over
=item new STORAGE OPTIONS_STRING
=item new_with_options STORAGE OPTIONS_HASH
Create a new RDF::Redland::Model object using RDF::Redland::Storage object I<STORAGE>
with a options. The options can be given either as a string in
the first form as I<OPTIONS_STRING>. The options take the form
key1='value1',key2='value2'. The quotes are required. In the
second case I<OPTIONS_HASH> is a reference to a Perl hash of options.
=cut
sub new ($$$) {
my($proto,$storage,$options_string)=@_;
my $class = ref($proto) || $proto;
my $self = {};
warn qq{RDF::Redland::Model->new(storage, "$options_string")\n} if $RDF::Redland::Debug;
$self->{MODEL}=&RDF::Redland::CORE::librdf_new_model($RDF::Redland::World->{WORLD},$storage->{STORAGE},$options_string);
return undef if !$self->{MODEL};
# keep a reference to storage so model is always destroyed before storage
$self->{STORAGE}=$storage;
bless ($self, $class);
return $self;
}
sub new_with_options ($$$) {
my($proto,$storage,$options)=@_;
my $class = ref($proto) || $proto;
my $self = {};
my $options_hash=RDF::Redland::Hash->new_from_perl_hash($options);
$self->{MODEL}=&RDF::Redland::CORE::librdf_new_model_with_options($storage->{STORAGE},$options_hash->{HASH});
return undef if !$self->{MODEL};
# keep a reference to storage so model is always destroyed before storage
$self->{STORAGE}=$storage;
bless ($self, $class);
return $self;
}
=item new_from_model MODEL
Create a new model from an existing RDF::Redland::Model I<MODEL> (copy constructor).
=cut
sub new_from_model ($$) {
my($proto,$model)=@_;
my $class = ref($proto) || $proto;
my $self = {};
$self->{MODEL}=&RDF::Redland::CORE::librdf_new_model_from_model($model->{MODEL});
return undef if !$self->{MODEL};
bless ($self, $class);
return $self;
}
=pod
=back
=cut
# DESTRUCTOR
sub DESTROY ($) {
my $self=shift;
warn "RDF::Redland::Model DESTROY $self" if $RDF::Redland::Debug;
if(!$self->{MODEL}) {
warn "RDF::Redland::Model DESTROY - librdf model object gone\n" if $RDF::Redland::Debug;
} else {
&RDF::Redland::CORE::librdf_free_model($self->{MODEL});
}
warn "RDF::Redland::Model DESTROY done\n" if $RDF::Redland::Debug;
}
=head1 METHODS
=over
=item size
Return the size of the model (number of statements).
=cut
sub size ($) {
&RDF::Redland::CORE::librdf_model_size(shift->{MODEL});
}
=item sync
Synchronise the model to the underlying storage.
=cut
sub sync ($) {
&RDF::Redland::CORE::librdf_model_sync(shift->{MODEL});
}
=item add SUBJECT PREDICATE OBJECT
Add a new statement to the model with I<SUBJECT>,
I<PREDICATE> and I<OBJECT>. These can be
RDF::Redland::Node, RDF::Redland::URI or perl URI objects.
=cut
sub add ($$$$) {
my($self,$subject,$predicate,$object)=@_;
return $self->add_statement($subject,$predicate,$object);
}
=item add_typed_literal_statement SUBJECT PREDICATE STRING [XML_LANGUAGE [DATATYPE]]
Add a new statement to the model containing a typed literal string object
I<STRING> with (optional) XML language (xml:lang attribute) I<XML_LANGUAGE>
and (optional) datatype URI I<DATATYPE>. I<XML_LANGUAGE>
or I<DATATYPE> can either or both be set to undef.
=cut
sub add_typed_literal_statement ($$$$$;$$) {
my($self,$subject,$predicate,$string,$xml_language,$datatype)=@_;
$subject=&RDF::Redland::CORE::librdf_new_node_from_node($subject->{NODE});
$predicate=&RDF::Redland::CORE::librdf_new_node_from_node($predicate->{NODE});
$xml_language ||= "";
my $uri=$datatype ? $datatype->{URI} : undef;
return &RDF::Redland::CORE::librdf_model_add_typed_literal_statement($self->{MODEL},$subject,$predicate,$string,$xml_language,$uri);
}
=item add_statement STATEMENT [CONTEXT] | NODE NODE NODE [CONTEXT]
Add RDF::Redland::Statement I<STATEMENT>
or the statement formed by I<NODE NODE NODE> to the model.
If the optional I<CONTEXT> is given, associate it with that context.
Any of I<NODE> or I<CONTEXT> can be a RDF::Redland::Node,
RDF::Redland::URI or perl URI object.
=cut
sub add_statement ($;$$$$) {
my($self,@rest)=@_;
my $statement=undef;
if(scalar(@rest)>2) {
$statement=new RDF::Redland::Statement(splice(@rest, 0, 3));
} else {
$statement=shift(@rest);
}
my $node=shift(@rest);
if($node) {
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
return &RDF::Redland::CORE::librdf_model_context_add_statement($self->{MODEL},$node,$statement->{STATEMENT});
} else {
return &RDF::Redland::CORE::librdf_model_add_statement($self->{MODEL},$statement->{STATEMENT});
}
}
=item add_statements STREAM [CONTEXT]
Add the statements from the RDF::Redland::Stream I<STREAM> to the
model. If the optional I<CONTEXT> is given,
associate it with that context.
I<CONTEXT> can be a RDF::Redland::Node, RDF::Redland::URI or perl URI object.
=cut
sub add_statements ($$;$) {
my($self,$statement_stream,$node)=@_;
if($node) {
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
return &RDF::Redland::CORE::librdf_model_context_add_statements($self->{MODEL},$node,$statement_stream->{STREAM});
} else {
return &RDF::Redland::CORE::librdf_model_add_statements($self->{MODEL},$statement_stream->{STREAM});
}
}
=item remove_statement STATEMENT [CONTEXT] | NODE NODE NODE [CONTEXT]
Remove RDF::Redland::Statement I<STATEMENT>
or the statement formed by I<NODE NODE NODE> from the model.
If the optional I<CONTEXT> is given,
remove only the statement stored with that context.
Any of I<NODE> or I<CONTEXT> can be a RDF::Redland::Node,
RDF::Redland::URI or perl URI object.
=cut
sub remove_statement ($;$$$$) {
my($self,@rest)=@_;
my $statement=undef;
if(scalar(@rest)>2) {
$statement=new RDF::Redland::Statement(splice(@rest, 0, 3));
} else {
$statement=shift(@rest);
}
my $node=shift(@rest);
if($node) {
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
return &RDF::Redland::CORE::librdf_model_context_remove_statement($self->{MODEL},$node,$statement->{STATEMENT});
} else {
return &RDF::Redland::CORE::librdf_model_remove_statement($self->{MODEL},$statement->{STATEMENT});
}
}
=item remove_context_statements CONTEXT
Remove all RDF::Redland::Statement I<STATEMENT>s from the model
with the given I<CONTEXT> context.
I<CONTEXT> can be a RDF::Redland::Node, RDF::Redland::URI or perl URI object.
=cut
sub remove_context_statements ($$) {
my($self,$node)=@_;
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
return &RDF::Redland::CORE::librdf_model_context_remove_statements($self->{MODEL},$node);
}
=item contains_statement STATEMENT
Return non 0 if the model contains RDF::Redland::Statement I<STATEMENT>.
=cut
sub contains_statement ($$) {
my($self,$statement)=@_;
return &RDF::Redland::CORE::librdf_model_contains_statement($self->{MODEL},$statement->{STATEMENT});
}
=item as_stream [CONTEXT]
Return a new RDF::Redland::Stream object seralising the entire model,
or just those statements with I<CONTEXT>, as RDF::Redland::Statement
objects. If given, I<CONTEXT> can be a RDF::Redland::Node,
RDF::Redland::URI or perl URI object.
=cut
sub as_stream ($;$) {
my($self,$node)=@_;
my $stream;
if($node) {
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
$stream=&RDF::Redland::CORE::librdf_model_context_as_stream($self->{MODEL},
$node);
} else {
$stream=&RDF::Redland::CORE::librdf_model_as_stream($self->{MODEL});
}
return new RDF::Redland::Stream($stream,$self);
}
# Older names
sub serialise ($) { shift->as_stream; }
sub serialize ($) { shift->as_stream; }
=item find_statements STATEMENT [CONTEXT]
Find all matching statements in the model matching partial RDF::Redland::Statement
I<STATEMENT> (any of the subject, predicate, object RDF::Redland::Node can be undef).
If I<CONTEXT> is given, finds statements only in that context.
In an array context, returns an array of the matching RDF::Redland::Statement
objects. In a scalar context, returns the RDF::Redland::Stream object
representing the results.
=cut
sub find_statements ($$;$) {
my($self,$statement,$node)=@_;
my $stream;
if($node) {
my $class=ref($node);
$node=&RDF::Redland::Node::_ensure($node);
die "Cannot make a Node from an object of class $class\n"
unless $node;
$stream=&RDF::Redland::CORE::librdf_model_find_statements_in_context($self->{MODEL},$statement->{STATEMENT}, $node);
} else {
$stream=&RDF::Redland::CORE::librdf_model_find_statements($self->{MODEL},$statement->{STATEMENT});
}
my $user_stream=new RDF::Redland::Stream($stream,$self);
return $user_stream if !wantarray;
my(@results)=();
while(!$user_stream->end) {
push(@results, $user_stream->current);
$user_stream->next;
}
$user_stream=undef;
@results;
}
=item sources ARC TARGET
Get all source RDF::Redland::Node objects for a given arc I<ARC>, target I<TARGET>>
RDF::Redland::Node objects as a list of RDF::Redland::Node objects.
=cut
sub sources ($$$) {
my($self,$arc,$target)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_sources($self->{MODEL},$arc->{NODE},$target->{NODE});
return () if !$iterator;
my $user_iterator=new RDF::Redland::Iterator($iterator,$self,$arc,$target);
return () if !$user_iterator;
my(@results)=();
while(!$user_iterator->end) {
push(@results, $user_iterator->current);
$user_iterator->next;
}
$user_iterator=undef;
@results;
}
=item arcs SOURCE TARGET
Get all arc RDF::Redland::Node objects for a given source I<SOURCE>, target I<TARGET>
RDF::Redland::Node objects as a list of RDF::Redland::Node objects.
=cut
sub arcs ($$$) {
my($self,$source,$target)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_arcs($self->{MODEL},$source->{NODE},$target->{NODE});
return () if !$iterator;
my $user_iterator=new RDF::Redland::Iterator($iterator,$self,$source,$target);
return () if !$user_iterator;
my(@results)=();
while(!$user_iterator->end) {
push(@results, $user_iterator->current);
$user_iterator->next;
}
$user_iterator=undef;
@results;
}
=item targets SOURCE ARC
Get all target RDF::Redland::Node objects for a given source I<SOURCE>, arc I<ARC>
RDF::Redland::Node objects as a list of RDF::Redland::Node objects.
=cut
sub targets ($$$) {
my($self,$source,$arc)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_targets($self->{MODEL},$source->{NODE},$arc->{NODE});
return () if !$iterator;
my $user_iterator=new RDF::Redland::Iterator($iterator,$self,$source,$arc);
return () if !$user_iterator;
my(@results)=();
while(!$user_iterator->end) {
push(@results, $user_iterator->current);
$user_iterator->next;
}
$user_iterator=undef;
@results;
}
=item sources_iterator ARC TARGET
Get all source RDF::Redland::Node objects for a given arc I<ARC>, target
I<TARGET> RDF::Redland::Node objects as an RDF::Redland::Iterator or undef on failure.
=cut
sub sources_iterator ($$$) {
my($self,$arc,$target)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_sources($self->{MODEL},$arc->{NODE},$target->{NODE});
my $user_iterator=new RDF::Redland::Iterator($iterator,$self,$arc,$target);
$user_iterator;
}
=item arcs_iterator SOURCE TARGET
Get all arc RDF::Redland::Node objects for a given source I<SOURCE>, target
I<TARGET> RDF::Redland::Node objects as an RDF::Redland::Iterator or undef on failure.
=cut
sub arcs_iterator ($$$) {
my($self,$source,$target)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_arcs($self->{MODEL},$source->{NODE},$target->{NODE});
return new RDF::Redland::Iterator($iterator,$self,$source,$target);
}
=item targets_iterator SOURCE ARC
Get all target RDF::Redland::Node objects for a given source I<SOURCE>, arc I<ARC>
RDF::Redland::Node objects as an RDF::Redland::Iterator or undef on failure.
=cut
sub targets_iterator ($$$) {
my($self,$source,$arc)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_targets($self->{MODEL},$source->{NODE},$arc->{NODE});
return new RDF::Redland::Iterator($iterator,$self,$source,$arc);
}
=item source ARC TARGET
Get one source RDF::Redland::Node object that matches a given arc I<ARC>,
target I<TARGET> RDF::Redland::Node objects or undef if there is no match.
=cut
sub source ($$$) {
my($self,$arc,$target)=@_;
my $node=&RDF::Redland::CORE::librdf_model_get_source($self->{MODEL},$arc->{NODE},$target->{NODE});
return $node ? RDF::Redland::Node->_new_from_object($node,1) : undef;
}
=item arc SOURCE TARGET
Get one arc RDF::Redland::Node object that matches a given source I<SOURCE>,
target I<TARGET> RDF::Redland::Node objects or undef if there is no match.
=cut
sub arc ($$$) {
my($self,$source,$target)=@_;
my $node=&RDF::Redland::CORE::librdf_model_get_arc($self->{MODEL},$source->{NODE},$target->{NODE});
return $node ? RDF::Redland::Node->_new_from_object($node,1) : undef;
}
=item target SOURCE ARC
Get one target RDF::Redland::Node object that matches a given source
I<SOURCE>, arc I<ARC> RDF::Redland::Node objects or undef if there is no
match.
=cut
sub target ($$$) {
my($self,$source,$arc)=@_;
my $node=&RDF::Redland::CORE::librdf_model_get_target($self->{MODEL},$source->{NODE},$arc->{NODE});
return $node ? RDF::Redland::Node->_new_from_object($node,1) : undef;
}
=item contexts
Get all context RDF::Redland::Node objects in the model
=cut
sub contexts ($) {
my($self)=@_;
my $iterator=&RDF::Redland::CORE::librdf_model_get_contexts($self->{MODEL});
return () if !$iterator;
my $user_iterator=new RDF::Redland::Iterator($iterator,$self);
return () if !$user_iterator;
my(@results)=();
while(!$user_iterator->end) {
push(@results, $user_iterator->current);
$user_iterator->next;
}
$user_iterator=undef;
@results;
}
=item feature URI [VALUE]
Get/set a model feature. The feature is named via RDF::Redland::URI
I<URI> and the value is a RDF::Redland::Node. If I<VALUE> is given,
the feature is set to that value, otherwise the current value is
returned.
=cut
sub feature ($$;$) {
my($self,$uri,$value)=@_;
warn "RDF::Redland::Model->feature('$uri', '$value')\n" if $RDF::Redland::Debug;
$uri=RDF::Redland::URI->new($uri)
unless ref $uri;
return &RDF::Redland::CORE::librdf_model_set_feature($self->{MODEL},$uri->{URI},$value->{NODE})
if $value;
$value=&RDF::Redland::CORE::librdf_model_get_feature($self->{MODEL},$uri->{URI});
return $value ? RDF::Redland::Node->_new_from_object($value,1) : undef;
}
=item query_execute QUERY
Execute the I<QUERY> RDF::Redland::Query against the model returning
a result set RDF::Redland::QueryResults or undef on failure.
=cut
sub query_execute ($$) {
my($self,$query)=@_;
my $results = &RDF::Redland::CORE::librdf_model_query_execute($self->{MODEL}, $query->{QUERY});
return $results ? RDF::Redland::QueryResults->new($results) : undef;
}
=item load URI [SYNTAX-NAME [ MIME-TYPE [SYNTAX-URI [HANDLER ]]]
Load content from I<URI> into the model, guessing the parser.
=cut
sub load ($$;$$$$) {
my($self,$uri,$name,$type,$syntax_uri,$handler)=@_;
my $ruri=$uri ? $uri->{URI} : undef;
my $rsyntax_uri=$syntax_uri ? $syntax_uri->{URI} : undef;
if($handler) {
&RDF::Redland::set_log_handler($handler);
}
my $rc=&RDF::Redland::CORE::librdf_model_load($self->{MODEL},$ruri, $name, $type, $rsyntax_uri);
if($handler) {
&RDF::Redland::reset_log_handler();
}
return $rc;
}
=item to_string [BASE-URI [SYNTAX-NAME [ MIME-TYPE [SYNTAX-URI]]]
Serialize the model to a syntax. If no serializer name is given,
the default serializer RDF/XML is used.
=cut
sub to_string($;$$$$) {
my($self,$uri,$name,$type,$syntax_uri)=@_;
my $ruri=$uri ? $uri->{URI} : undef;
my $rsyntax_uri=$syntax_uri ? $syntax_uri->{URI} : undef;
return &RDF::Redland::CORE::librdf_model_to_string($self->{MODEL}, $ruri, $name, $type, $rsyntax_uri);
}
=pod
=back
=head1 OLDER METHODS
=over
=item serialise
=item serialize
Return a new RDF::Redland::Stream object seralising the model as
RDF::Redland::Statement objects. Replaced by as_stream to
reduce confusion with the RDF::Redland::Serializer class.
=back
=head1 SEE ALSO
L<RDF::Redland::Storage>, L<RDF::Redland::Node> and L<RDF::Redland::Statement>
=head1 AUTHOR
Dave Beckett - http://www.dajobe.org/
=cut
1;
|