ORE/Model.pm

filter: Statementstmt 74.1% 207 / 279 Branchbran 54.0% 54 / 100 Conditioncond 58.3% 7 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 84.8% 28 / 33 Podpod 100% 18 / 18 totaltotal 70.4% 317 / 450 CC 56 SCAR 25.1
CC
56
Cov
69%
check_model
31.6
add
30.0
objects_matching
12.3
CRAP 12.3
« ORE/TriX.pm ↑ Summary ORE/ReM.pm »
1 package SemanticWeb::OAI::ORE::Model;
2 #$Id: Model.pm,v 1.16 2010-12-06 14:44:15 simeon Exp $
3  
4 =head1 NAME
5  
6 SemanticWeb::OAI::ORE::Model - Module for model component of an OAI-ORE Resource Map
7  
8 =head1 SYNOPSIS
9  
10 In essence, the model is simply a set of triples and we thus store them in
11 a triple store provided by L<RDF::Core::Model>, L<RDF::Core::Storage> etc..
12  
13 =cut
14  
15 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 8 ▶use SemanticWeb::OAI::ORE::Constant qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use SemanticWeb::OAI::ORE::N3;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 8 ▶use RDF::Core::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶use RDF::Core::Model::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶use RDF::Core::Storage;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 8 ▶use RDF::Core::Storage::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 8 ▶use RDF::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 8 ▶use RDF::Core::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 8 ▶use RDF::Core::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 8 ▶use Class::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 8 ▶use base qw(Class::Accessor RDF::Core::Model);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 8 SemanticWeb::OAI::ORE::Model->mk_accessors(qw(die_level));
33  
34 =head1 METHODS
35  
36 =head2 CREATION AND MODIFICATION
37  
38 =head3 SemanticWeb::OAI::ORE::Model->new(%args) or SemanticWeb::OAI::ORE::Model->new($rdf_model)
39  
40 Create new relationships object as part of a resource map.
41  
42 If supplied with a single argument that is a L<RDF::Core::Model> then that
43 object is blessed into this class an returned. Otherwise a new
44 L<RDF::Core::Model> object is created and any %args are passed to the
45 creator.
46  
47 =cut
48  
49 7 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
50 7 my $class=shift;
51 7 my $self;
52 7 ▶ if (ref($_[0]) and $_[0]->isa('RDF::Core::Model')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and $_[0]->isa('RDF::Core::Model')) { } 3 4 7
Condition66%2/3
ref $_[0] and $_[0]->isa("RDF::Core::Model")
!ll&&!rl&&r
403
MC/DC50%1/2
ref $_[0] and $_[0]->isa("RDF::Core::Model")
ref $_[0] $_[0]->isa("RDF::Core::Model")
Truth table66%2/3
A and B
A= ref $_[0]B= $_[0]->isa("RDF::Core::Model")
ABresult
0X0
100
111
53 3 $self=$_[0];
54 } else {
55 4 $self=RDF::Core::Model->new(Storage=>RDF::Core::Storage::Memory->new(),@_);
56 }
57 7 bless $self, $class;
58 7 $self->die_level(FATAL);
59 7 return($self);
60 }
61  
62  
63 =head3 $model->add($model_or_statement)
64  
65 Add either another model object or a single statement to this $model.
66 Returns the number of statements added.
67  
68 =cut
69  
70 0 ▶sub add {
Subroutine add: not called CC=5 SCAR=34.0
Pod documented
71 0 my $self=shift;
72 0 my $count=0;
73 0 foreach my $to_add (@_) {
74 0 ▶ if ($to_add->isa('RDF::Core::Model')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($to_add->isa('RDF::Core::Model')) { } 0 0 0
elsif ($to_add->isa('RDF::Core::Statement')) { } 0 0 0
75 0 my $enum=$to_add->getStmts(undef,undef,undef);
76 0 my $statement=$enum->getFirst();
77 0 ▶ while ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 0 0 0
78 0 $self->addStmt($statement);
79 0 $count++;
80 0 $statement=$enum->getNext();
81 }
82 0 $enum->close();
83 } elsif ($to_add->isa('RDF::Core::Statement')) {
84 0 $self->addStmt($to_add);
85 0 $count++;
86 } else {
87 0 die "Don't know how to add a ".ref($to_add)." to the Model";
88 }
89 }
90 0 return($count);
91 }
92  
93  
94 =head3 $model->add_rel_to_resource($subject,$predicate,$object)
95  
96 Add relationship where the object is a resource (URI).
97  
98 =cut
99  
100 5 ▶sub add_rel_to_resource {
Subroutine add_rel_to_resource: called CC=1 SCAR=0.0
Pod documented
101 5 my $self=shift;
102 5 my ($subject,$predicate,$object)=@_;
103 5 $subject=RDF::Core::Resource->new($subject);
104 5 $predicate=RDF::Core::Resource->new($predicate);
105 5 $object=RDF::Core::Resource->new($object);
106 5 $self->addStmt(RDF::Core::Statement->new($subject,$predicate,$object));
107 }
108  
109  
110 =head3 $model->add_rel_to_literal($subject,$predicate,$object)
111  
112 Add relationship where the object is a literal.
113  
114 =cut
115  
116 0 ▶sub add_rel_to_literal {
Subroutine add_rel_to_literal: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self=shift;
118 0 my ($subject,$predicate,$object)=@_;
119 0 $subject=RDF::Core::Resource->new($subject);
120 0 $predicate=RDF::Core::Resource->new($predicate);
121 0 $object=RDF::Core::Literal->new($object);
122 0 $self->addStmt(RDF::Core::Statement->new($subject,$predicate,$object));
123 }
124  
125  
126 =head3 $model->count()
127  
128 Returns the number of statements or relationships.
129  
130 =cut
131  
132 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self=shift;
134 0 return($self->countStmts(undef,undef,undef));
135 }
136  
137  
138 =head3 $model->as_array()
139  
140 Return an array reference to all triples each as a four element array with
141 [subject, predicate, object, object_is_literal] for each statement.
142  
143 FIXME - should perhaps implement iterator or similar...
144  
145 =cut
146  
147 2 ▶sub as_array {
Subroutine as_array: called CC=2 SCAR=6.9
Pod documented
148 2 my $self=shift;
149 2 my $enum=$self->getStmts(undef,undef,undef);
150 2 my $statement=$enum->getFirst();
151 2 my @triples=();
152 2 ▶ while ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 3 2 5
153 3 push(@triples,[$statement->getSubject()->getLabel(),
154 $statement->getPredicate()->getLabel(),
155 $statement->getObject()->getLabel(),
156 $statement->getObject()->isLiteral()]);
157 3 $statement=$enum->getNext();
158 }
159 2 return(\@triples);
160 }
161  
162  
163 =head3 $model->objects_matching($subject,$predicate,$only)
164  
165 Return an array of objects from triples where the subject and predicate
166 are as specified. Will return an empty array if there are no matches.
167  
168 If $only is not specified then the objects matching will be returned.
169  
170 If $only is RESOURCE then only resource labels will be included, if
171 LITERAL then only literal labels will be returned.
172  
173 =cut
174  
175 7 ▶sub objects_matching {
Subroutine objects_matching: called CC=11 SCAR=25.1
Pod documented
176 7 my $self=shift;
177 7 my ($subject,$predicate,$only)=@_;
178  
179 7 ▶ if (not defined($subject)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $subject) { } 1 6 7
elsif (not ref $subject) { } 1 5 6
180 #empty list
181 1 return([]);
182 } elsif (not ref($subject)) {
183 1 $subject=RDF::Core::Resource->new($subject);
184 }
185 6 ▶ if (not defined($predicate)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $predicate) { } 0 6 6
elsif (not ref $predicate) { } 6 0 6
186 #leave undef so we match any
187 } elsif (not ref($predicate)) {
188 6 $predicate=expand_qname($predicate);
189 6 $predicate=RDF::Core::Resource->new($predicate);
190 }
191  
192 6 my $enum=$self->getStmts($subject,$predicate,undef);
193 6 my $statement=$enum->getFirst();
194 6 my @matching=();
195 6 ▶ while ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 6 6 12
196 6 my $obj=$statement->getObject();
197 6 ▶ if ($only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only) { } 2 4 6
198 2 ▶ if ($only==RESOURCE) {
Branch: 2 branches
BranchTrueFalseTotal
if ($only == 1) { } 2 0 2
elsif ($only == 2) { } 0 0 0
199 2 ▶ next if ($obj->isLiteral());
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isLiteral 0 2 2
200 } elsif ($only==LITERAL) {
201 0 ▶ next if (not $obj->isLiteral());
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->isLiteral 0 0 0
202 }
203 2 push(@matching,$obj->getLabel());
204 } else {
205 4 push(@matching,$obj);
206 }
207 6 $statement=$enum->getNext();
208 }
209 6 return(@matching);
210 }
211  
212  
213  
214 =head3 $model->literal_matching($subject,$predicate)
215  
216 Wrapper around objects_matching to get the first literal matching the
217 specified condition, else retursn undef. Ignores any other matches.
218  
219 =cut
220  
221 0 ▶sub literal_matching {
Subroutine literal_matching: not called CC=2 SCAR=17.9
Pod documented
222 0 my $self=shift;
223 0 my ($subject,$predicate)=@_;
224 0 my @objects=$self->objects_matching($subject,$predicate,LITERAL);
225 0 ▶ return(@objects ? $objects[0] : undef );
Branch: 1 branch
BranchTrueFalseTotal
@objects ? : 0 0 0
226 }
227  
228  
229  
230 # Return URI or literal based on whether string looks like a URI
231 #
232 0 ▶sub __uri_or_literal {
Subroutine __uri_or_literal: not called CC=2 SCAR=17.9
233 0 my ($str)=@_;
234 0 my $ul;
235 0 ▶ if ($str=~/^[a-z]+:\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^[a-z]+:\S+$/) { } 0 0 0
236 0 $ul=RDF::Core::Resource->new($str);
237 } else {
238 0 $ul=RDF::Core::Literal->new($str);
239 }
240 0 return($ul);
241 }
242  
243  
244 =head2 VALIDATION
245  
246 =head3 $model->check_model($uri_rem,$rem)
247  
248 Take an RDF model of type RDF::Core::Model in $self and a Resource
249 Map URI $uri_rem. Attempt to parse/interpret it as a resource map. Will
250 croak if parsing fails so usual call would be to wrap in an eval:
251  
252 eval {
253 $model->check_model($uri_rem,$rem);
254 };
255 if ($@) {
256 # oops
257 }
258  
259 If $rem is supplied then this is expected to be a SemanticWeb::OAI::ORE::ReM object
260 with methods uri(), aggregation(), creator() and
261 timestamp_as_iso8601() which are used to set these values for easy reference.
262  
263 The requirements are based mainly on the table given in
264 L<http://www.openarchives.org/ore/1.0/datamodel#Constraints>.
265  
266 =cut
267  
268 3 ▶sub check_model {
Subroutine check_model: called CC=19 SCAR=34.5
Pod documented
269 3 my $self=shift;
270 3 my ($uri_rem,$rem)=@_;
271  
272 3 my $resource_map=RDF::Core::Resource->new(RESOURCE_MAP);
273 3 my $aggregation=RDF::Core::Resource->new(AGGREGATION);
274 3 my $has_type=RDF::Core::Resource->new(HAS_TYPE);
275 3 my $describes=RDF::Core::Resource->new(DESCRIBES);
276 3 my $aggregates=RDF::Core::Resource->new(AGGREGATES);
277  
278 # First, work out what the Resource Map URI (URI-R) is
279 {
280 3 my $statement=undef;
281 3 my $uri=undef;
282 3 my $cnt=$self->countStmts(undef,$has_type,$resource_map);
283 3 ▶ if ($cnt==0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 0) { } 1 2 3
elsif ($cnt == 1) { } 2 0 2
284 1 ▶ $self->err(FATAL,"No resource map node defined as such and not URI-R supplied") if (not defined $uri_rem);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri_rem 0 1 1
285 #if FATAL turned off or $uri_rem supplied then just assume $uri_rem as given
286 1 ▶ $self->err(WARN,"Using supplied URI-R ($uri_rem) as resource map URI") if (defined $uri_rem);
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri_rem 1 0 1
287 1 $uri=$uri_rem;
288 } elsif ($cnt==1) {
289 2 my $enum=$self->getStmts(undef,$has_type,$resource_map);
290 2 $statement=$enum->getFirst;
291 2 $enum->close();
292 2 $uri=$statement->getSubject->getURI;
293 } else {
294 # more than one match, can't handle that yet so barf.
295 # can probably work it out by looking for an AGGREGATES arc from the same Subject
296 0 $self->err(FATAL,"Got $cnt candidates for resourceMap node");
297 0 return(0); #if FATAL turned off
298 }
299 # Only get here if we found $statement and extracted $uri
300 3 ▶ if (defined $rem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rem) 3 0 3
301 3 $rem->uri($uri);
302 }
303 3 ▶ if (defined $uri_rem and $uri_rem ne $uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri_rem and $uri_rem ne $uri) 0 3 3
Condition33%1/3
defined $uri_rem and $uri_rem ne $uri
!ll&&!rl&&r
030
MC/DC0%0/2
defined $uri_rem and $uri_rem ne $uri
defined $uri_rem $uri_rem ne $uri
Truth table33%1/3
A and B
A= defined $uri_remB= $uri_rem ne $uri
ABresult
0X0
100
111
304 0 $self->err(WARN,"URI for ReM supplied ($uri_rem) but does not match that inside object ($uri)");
305 }
306 3 $uri_rem=$uri;
307 }
308  
309 # Second, work out what the Aggregation URI (URI-A) is. First look for a DESCRIBES
310 # predicate, look for a node typed as an aggregation if that fails.
311 3 my $uri_agg=undef;
312 {
313 3 my $statement=undef;
314 3 my $rem_resource=RDF::Core::Resource->new($uri_rem);
315 3 my $cnt=$self->countStmts($rem_resource,$describes,undef);
316 3 ▶ if ($cnt==1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 1) { } 2 1 3
elsif ($cnt == 0) { } 1 0 1
317 2 my $enum=$self->getStmts($rem_resource,$describes,undef);
318 2 $statement=$enum->getFirst();
319 2 $enum->close();
320 2 $uri_agg=$statement->getObject()->getURI();
321 } elsif ($cnt==0) {
322 # Any describes statement..
323 1 my $cnt=$self->countStmts(undef,$describes,undef);
324 1 ▶ if ($cnt==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt == 1) 0 1 1
325 0 my $enum=$self->getStmts(undef,$describes,undef);
326 0 $statement=$enum->getFirst();
327 0 $enum->close();
328 0 $uri_agg=$statement->getObject()->getURI();
329 }
330 }
331 # If that did not work, try typed node
332 3 ▶ if (not defined $uri_agg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uri_agg) 1 2 3
333 1 my $cnt=$self->countStmts(undef,$has_type,$aggregation);
334 1 ▶ if ($cnt==0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 0) { } 1 0 1
elsif ($cnt == 1) { } 0 0 0
335 1 $self->err(FATAL,"Failed to find an Aggregation node!");
336 0 return(0);
337 } elsif ($cnt==1) {
338 0 my $enum=$self->getStmts(undef,$has_type,$aggregation);
339 0 $statement=$enum->getFirst();
340 0 $enum->close();
341 0 $uri_agg=$statement->getSubject()->getURI();
342 } else {
343 # more than one match, can't handle that yet so barf.
344 0 $self->err(FATAL,"Got $cnt candidates for Aggregation node");
345 0 return(0);
346 }
347 }
348 # Only get here if we found $statement and extracted $uri_agg, record
349 # in model.
350 2 ▶ if (defined $rem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rem) 2 0 2
351 2 $rem->aggregation($uri_agg);
352 }
353 }
354  
355 # Now look for $uri_agg AGGREGATES <blah> statements and add to
356 # the list of aggregated resources
357 2 my $uri_agg_resource=RDF::Core::Resource->new($uri_agg);
358 {
359 2 my $cnt=$self->countStmts($uri_agg_resource,$aggregates,undef);
360 2 ▶ if ($cnt==0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt == 0) { } 0 2 2
361 0 $self->err(WARN,"No resources aggregated by Aggregation $uri_agg. This is legal but perhaps not what is intended.");
362 } else {
363 2 ▶ carp "Found $cnt aggregated resources" if ($self->{debug});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 2 2
364 }
365 }
366  
367 # Now look for essential metadata: creator and modified
368 {
369 2 ▶ if (scalar($self->creators($uri_rem))==0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $self->creators($uri_rem) == 0) 0 2 2
370 0 $self->err(FATAL,"Resource map must have at least one ".CREATOR);
371 0 return(0);
372 }
373 }
374  
375 2 my $uri_rem_resource=RDF::Core::Resource->new($uri_rem);
376 2 ▶ if (my $timestamp=$self->get_timestamp($uri_rem_resource,1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timestamp = $self->get_timestamp($uri_rem_resource, 1)) { } 2 0 2
377 2 $rem->timestamp_as_iso8601(MODIFIED,$timestamp);
378 } else {
379 # Will have already thrown error
380 0 return(0);
381 }
382
383 2 return(1);
384 }
385  
386  
387 =head3 $model->creators($uri)
388  
389 Find all the CREATOR objects (resources or literals) for $uri.
390  
391 =cut
392  
393 3 ▶sub creators {
Subroutine creators: called CC=1 SCAR=0.0
Pod documented
394 3 my $self=shift;
395 3 my ($uri_rem)=@_;
396 3 my $uri_rem_resource=RDF::Core::Resource->new($uri_rem);
397 3 return($self->objects_matching($uri_rem_resource,CREATOR));
398 }
399  
400  
401 =head3 $model->get_timestamp($uri_rem,$throw_error)
402  
403 Return timestamp literal associated with $uri_rem. There must be
404 just one otherwise nothing (error) will be returned.
405  
406 =cut
407  
408 2 ▶sub get_timestamp {
Subroutine get_timestamp: called CC=5 SCAR=22.7
Pod documented
409 2 my $self=shift;
410 2 my ($uri_rem,$throw_error)=@_;
411 2 my @timestamps=$self->objects_matching($uri_rem,MODIFIED);
412 2 ▶ if (scalar(@timestamps)!=1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @timestamps != 1) 0 2 2
413 0 ▶ if ($throw_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throw_error) 0 0 0
414 0 $self->err(FATAL,"Resource map must have one and only one ".MODIFIED);
415 }
416 0 return();
417 }
418 2 my $timestamp=$timestamps[0];
419 2 ▶ if (not $timestamp->isLiteral()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timestamp->isLiteral) 0 2 2
420 0 ▶ if ($throw_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($throw_error) 0 0 0
421 0 $self->err(FATAL,"Resource map timestamp must be a literal value");
422 }
423 0 return();
424 }
425 2 return($timestamp->getLabel());
426 }
427  
428  
429 =head3 $model->err($level,$msg)
430  
431 Error handling. Will use similar error method of $self->{errobj} if
432 that is set. Otherwise handles here.
433  
434 =cut
435  
436 2 ▶sub err {
Subroutine err: called CC=3 SCAR=16.7
Pod documented
437 2 my $self=shift;
438 2 ▶ if ($self->{errobj}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"errobj"}) 2 0 2
439 2 return($self->{errobj}->err(@_));
440 }
441 0 my ($level,$msg)=@_;
442 0 ▶ if ($level>=$self->die_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level >= $self->die_level) 0 0 0
443 0 croak "ERROR: $msg";
444 }
445 0 $self->add_errstr($msg);
446 }
447  
448  
449 =head2 INTROSPECTION
450  
451 These routines support examination of the model to pull out key reference
452 points and information such as the Resource Map URI or the Aggregation URI.
453  
454 =head3 $model->find_rem
455  
456 Attempt to find the Resource Map. Returns the appropriate Resource object
457 if successful, nothing otherwise.
458  
459 =cut
460  
461 4 ▶sub find_rem {
Subroutine find_rem: called CC=4 SCAR=14.3
Pod documented
462 4 my $self=shift;
463  
464 4 my $rem=undef;
465  
466 4 my $resource_map=RDF::Core::Resource->new(RESOURCE_MAP);
467 4 my $has_type=RDF::Core::Resource->new(HAS_TYPE);
468 4 my $enum=$self->getStmts(undef,$has_type,$resource_map);
469 4 ▶ if (my $statement=$enum->getFirst) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $statement = $enum->getFirst) { } 3 1 4
470 # If more than one match, recklessly pick the 'first'
471 # FIXME - could look for one with describes link
472 3 $rem=$statement->getSubject;
473 3 $enum->close;
474 } else {
475 # None found from that test, try looking for something
476 # that DESCRIBES
477 1 my $describes=RDF::Core::Resource->new(DESCRIBES);
478 1 my $cnt=$self->countStmts(undef,$describes,undef);
479 1 ▶ if ($cnt==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt == 1) { } 0 1 1
480 # Just one so we take it
481 0 $enum=$self->getStmts(undef,$describes,undef);
482 0 $rem=$enum->getFirst->getSubject;
483 0 $enum->close;
484 } else {
485 # FIXME - look for one with other matches
486 }
487 }
488  
489 # Now have Resource in $rem if we found it
490 4 ▶ return( $rem || () );
Condition66%2/3
$rem || ()
l!l&&r!l&&!r
301
MC/DC50%1/2
$rem || ()
$rem ()
Truth table66%2/3
A || B
A= $remB= ()
ABresult
1X1
011
000
491 }
492  
493  
494 =head3 $model->find_rem_uri(%opts)
495  
496 Wrapper around $model->find_rem that returns a URI on
497 success, nothing otherwise.
498  
499 =cut
500  
501 4 ▶sub find_rem_uri {
Subroutine find_rem_uri: called CC=2 SCAR=6.9
Pod documented
502 4 my $self=shift;
503 4 my $agg=$self->find_rem(@_);
504 4 ▶ return($agg ? $agg->getURI : () );
Branch: 1 branch
BranchTrueFalseTotal
$agg ? : 3 1 4
505 }
506  
507  
508 =head3 $model->find_aggregation(%opts)
509  
510 Find the Aggregation in this Resource Map. Returns the appropriate
511 Resource object if successful, nothing otherwise.
512  
513 Valid options are:
514  
515 uri_rem -> Resurce Map URI,
516  
517 =cut
518  
519 6 ▶sub find_aggregation {
Subroutine find_aggregation: called CC=5 SCAR=17.6
Pod documented
520 6 my $self=shift;
521  
522 6 my $agg=undef;
523  
524 6 my $aggregation=RDF::Core::Resource->new(AGGREGATION);
525 6 my $has_type=RDF::Core::Resource->new(HAS_TYPE);
526 6 my $cnt=$self->countStmts(undef,$has_type,$aggregation);
527 6 ▶ if ($cnt==1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cnt == 1) { } 3 3 6
elsif ($cnt > 1) { } 0 3 3
528 3 my $enum=$self->getStmts(undef,$has_type,$aggregation);
529 3 $agg=$enum->getFirst->getSubject;
530 3 $enum->close;
531 } elsif ($cnt>1) {
532 # FIXME - do something smarter than taking the first
533 0 my $enum=$self->getStmts(undef,$has_type,$aggregation);
534 0 $agg=$enum->getFirst->getSubject;
535 0 $enum->close;
536 } else { # ($cnt==0)
537 # None found from that test, try looking for something
538 # that the rem DESCRIBES
539 3 my $describes=RDF::Core::Resource->new(DESCRIBES);
540 3 my $cnt=$self->countStmts(undef,$describes,undef);
541 3 ▶ if ($cnt==1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt == 1) { } 0 3 3
542 # Just one so we take it
543 0 my $enum=$self->getStmts(undef,$describes,undef);
544 0 $agg=$enum->getFirst->getObject;
545 0 $enum->close;
546 } else {
547 # FIXME - look for one with other matches
548 }
549 # ???
550 }
551  
552 6 ▶ return( $agg || () );
Condition66%2/3
$agg || ()
l!l&&r!l&&!r
303
MC/DC50%1/2
$agg || ()
$agg ()
Truth table66%2/3
A || B
A= $aggB= ()
ABresult
1X1
011
000
553 }
554  
555  
556 =head3 $model->find_aggregation_uri(%opts)
557  
558 Wrapper around $model->find_aggregation that returns a URI on
559 success, nothing otherwise.
560  
561 =cut
562  
563 6 ▶sub find_aggregation_uri {
Subroutine find_aggregation_uri: called CC=2 SCAR=6.9
Pod documented
564 6 my $self=shift;
565 6 my $agg=$self->find_aggregation(@_);
566 6 ▶ return($agg ? $agg->getURI : () );
Branch: 1 branch
BranchTrueFalseTotal
$agg ? : 3 3 6
567 }
568  
569  
570 =head2 DATA DUMP
571  
572 These are low-level data dump methods. It is expected that normally
573 the methods provided via L<SemanticWeb::OAI::ORE::ReM>::serialize will be used.
574  
575 =head3 $model->as_n3($unsorted)
576  
577 Very simple dump of this object as N3. No prefixes are used and the triples
578 are sorted alphabetically by line unless $unsorted is set true (in which case
579 the output will be essentially random).
580  
581 See L<SemanticWeb::OAI::ORE::N3> for "pretty printing" methods.
582  
583 =cut
584  
585 3 ▶sub as_n3 {
Subroutine as_n3: called CC=4 SCAR=13.9
Pod documented
586 3 my $self=shift;
587 3 my ($unsorted)=@_;
588  
589 3 my @triples=();
590 3 my $enum=$self->getStmts(undef,undef,undef);
591 3 my $statement=$enum->getFirst();
592 3 ▶ while ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 32 3 35
593 32 my $subject='<'.$statement->getSubject()->getLabel().'>';
594 32 my $predicate='<'.$statement->getPredicate()->getLabel().'>';
595 32 my $obj=$statement->getObject();
596 32 my $object=$obj->getLabel();
597 32 ▶ if ($obj->isa('RDF::Core::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->isa('RDF::Core::Resource')) { } 27 5 32
598 27 $object='<'.$object.'>';
599 } else {
600 5 $object='"'.SemanticWeb::OAI::ORE::N3::_n3_escape($object).'"';
601 }
602 32 push(@triples,"$subject $predicate $object.\n");
603 32 $statement=$enum->getNext();
604 }
605  
606 3 my $str="# Dump of OAI-ORE Resource Map model as N3\n";
607 3 ▶ if ($unsorted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unsorted) { } 0 3 3
608 0 $str.=join('',@triples);
609 } else {
610 3 $str.=join('',sort(@triples));
611 }
612 3 return($str);
613 }
614  
615  
616 =head3 $model->as_rdfxml
617  
618 Simple RDF XML dump, returns string. For more sophisticated output
619 see L<SemanticWeb::OAI::ORE::RDFXML>.
620  
621 =cut
622  
623 1 ▶sub as_rdfxml {
Subroutine as_rdfxml: called CC=1 SCAR=0.0
Pod documented
624 1 my $self=shift;
625 1 my $xml = '';
626 1 my $serializer = new RDF::Core::Model::Serializer(Model=>$self,
627 Output=>\$xml,
628 BaseURI => 'http://example.com/',
629 );
630 1 $serializer->serialize;
631 1 return($xml);
632 }
633  
634  
635 =head1 SEE ALSO
636  
637 L<SemanticWeb::OAI::ORE::ReM>
638  
639 =head1 AUTHOR
640  
641 Simeon Warner
642  
643 =head1 LICENSE AND COPYRIGHT
644  
645 Copyright 2007-2010 Simeon Warner.
646  
647 This module is free software; you can redistribute it and/or
648 modify it under the same terms as Perl itself. See L<perlartistic>.
649  
650 =cut
651  
652 8 1;
« ORE/TriX.pm ↑ Summary ORE/ReM.pm »