RDF.pm

filter: Statementstmt 32.8% 135 / 411 Branchbran 0.6% 1 / 156 Conditioncond 0.0% 0 / 48 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 46.3% 38 / 82 Podpod 62.5% 5 / 8 totaltotal 24.3% 179 / 736 CC 114 SCAR 39.0
CC
114
Cov
22%
new
272.0
set
90.0
remove
90.0
CRAP 49.2
↑ Summary RDF/Cache.pm »
1 package Class::RDF::Store;
2  
3 1 ▶use base "Class::DBI";
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our @Create_SQL = (<<'', <<'', <<'', <<'');
7 create table ns (
8 prefix char(16),
9 uri char(255)
10 );
11  
12 create table node (
13 id integer primary key,
14 created timestamp,
15 value text,
16 is_resource integer(1)
17 );
18  
19 create table statement (
20 id integer primary key,
21 created timestamp,
22 subject integer,
23 predicate integer,
24 object integer,
25 context integer
26 );
27  
28 create table metastatement (
29 id integer primary key,
30 created timestamp,
31 subject integer,
32 predicate integer,
33 object integer
34 );
35  
36  
37 1 ▶sub is_transient {
Subroutine is_transient: called CC=2 SCAR=6.9
38 1 my $class = shift;
39 1 my %args = ( TEMPLATE => "crdfXXXX", SUFFIX => ".db", UNLINK => 1, @_ );
40 1 my $tmp = File::Temp->new( %args );
41  
42 1 $class->set_db( Main => "dbi:SQLite:".$tmp->filename, "", "" );
43  
44 1 for my $st (@Create_SQL) {
45 4 $class->db_Main->do($st);
46 }
47 }
48  
49 package Class::RDF::NS;
50  
51 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use base 'Class::RDF::Store';
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 __PACKAGE__->table( "ns" );
59 1 __PACKAGE__->columns( All => qw( prefix uri ) );
60  
61 1 our (%Cache, $Prefix_RE);
62  
63 1 ▶sub define {
Subroutine define: called CC=2 SCAR=9.9
64 1 my ($class, %uri) = @_;
65 1 ▶ while (my ($prefix, $uri) = each %uri) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %uri) 1 0 1
66 1 my $ns = $class->find_or_create({ prefix => $prefix });
67 0 $Cache{$prefix} = $ns;
68 0 $ns->uri( $uri );
69 0 $ns->update;
70 }
71 0 $class->_build_prefix_re;
72 }
73  
74 0 ▶sub export {
Subroutine export: not called CC=3 SCAR=21.3
75 0 my ($class, @prefixes) = @_;
76 0 for my $prefix (@prefixes) {
77 0 my $ns = $class->retrieve($prefix);
78 0 ▶ croak "Can't find prefix $prefix" unless $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless $ns 0 0 0
79 0 my $uri = $ns->uri;
80  
81 1 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 ▶ *{"$prefix\::AUTOLOAD"} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
83 0 my $object = shift;
84 0 (my $prop = $AUTOLOAD) =~ s/^.*:://o;
85 0 ▶ if (ref($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $object) { } 0 0 0
86 0 $object->get_or_set( "$uri$prop", @_ );
87 } else {
88 0 return "$uri$prop";
89 }
90 0 };
91 }
92 }
93  
94 0 ▶sub retrieve {
Subroutine retrieve: not called CC=2 SCAR=17.9
95 0 my ($class, $prefix) = @_;
96 0 ▶ return $Cache{$prefix} if $Cache{$prefix};
Branch: 1 branch
BranchTrueFalseTotal
if $Class::RDF::NS::Cache{$prefix} 0 0 0
97 0 my $ns = $class->SUPER::retrieve($prefix);
98 0 $Cache{$prefix} = $ns;
99 0 $class->_build_prefix_re;
100 }
101  
102 0 ▶sub load {
Subroutine load: not called CC=2 SCAR=17.9
103 0 my $class = shift;
104 0 my $iter = $class->retrieve_all;
105 0 ▶ while (my $ns = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $iter->next) 0 0 0
106 0 $Cache{$ns->prefix} = $ns
107 }
108 0 $class->_build_prefix_re;
109 }
110  
111 0 ▶sub expand {
Subroutine expand: not called CC=1 SCAR=6.9
112 0 my ($class, $uri) = @_;
113 0 $uri =~ s/$Prefix_RE/$Cache{$1}->uri/es;
114 0 return $uri;
115 }
116  
117 0 ▶sub _build_prefix_re {
Subroutine _build_prefix_re: not called CC=1 SCAR=6.9
118 0 my $class = shift;
119 0 my $list = join("|", keys %Cache);
120 0 $Prefix_RE = qr/^($list):/;
121 }
122  
123 package Class::RDF::Node;
124  
125 1 ▶use base "Class::RDF::Store";
Subroutine BEGIN: called CC=1 SCAR=0.0
126 1 ▶use overload '""' => \&as_string,
Subroutine BEGIN: called CC=1 SCAR=0.0
127 1 eq => \&_string_eq;
128
129 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 1 __PACKAGE__->table( "node" );
133 1 __PACKAGE__->columns( All => qw( id created value is_resource ) );
134 1 __PACKAGE__->autoupdate(1);
135 1 our %Cache;
136  
137 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
138 0 my $class = shift;
139 0 ▶ my $value = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
140 0 ▶ return $class if ref $class and $class->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if ref $class and $class->isa(__PACKAGE__) 0 0 0
Condition0%0/3
ref $class and $class->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $class and $class->isa(__PACKAGE__)
ref $class $class->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= ref $classB= $class->isa(__PACKAGE__)
ABresult
0X0
100
111
141  
142 0 my $cached = $class->cache($value);
143 0 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
144  
145 0 ▶ my $is_resource = ($value =~ /^\w+:\S+$/o ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$value =~ /^\w+:\S+$/o ? : 0 0 0
146 0 my $obj =$class->find_or_create({ value => $value, is_resource => $is_resource });
147 0 return $class->cache($value, $obj);
148 }
149  
150 0 ▶sub find {
Subroutine find: not called CC=4 SCAR=30.0
151 0 my ($class,$value) = @_;
152 0 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
153
154 0 my $cached = $class->cache($value);
155 0 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
156  
157 0 my ($found) = $class->search({ value => $value });
158 0 ▶ $class->cache($value, $found) if $found;
Branch: 1 branch
BranchTrueFalseTotal
if $found 0 0 0
159 0 return $found;
160 }
161  
162 0 ▶sub cache {
Subroutine cache: not called CC=3 SCAR=24.8
163 0 my ($class, $value, $node) = @_;
164 0 ▶ if ($node) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node) { } 0 0 0
elsif (exists $Class::RDF::Node::Cache{$value}) { } 0 0 0
165 0 return ($Cache{$value} = $node);
166 } elsif (exists $Cache{$value}) {
167 0 return $Cache{$value}
168 } else {
169 0 return undef;
170 }
171 }
172  
173 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
174 0 my $self = shift;
175 0 return $self->value;
176 }
177 0 ▶sub _string_eq {
Subroutine _string_eq: not called CC=1 SCAR=6.9
178 0 my ($self,$other) = @_;
179 0 $self->as_string eq $other;
180 }
181  
182 package Class::RDF::Statement;
183  
184 1 ▶use base "Class::RDF::Store";
Subroutine BEGIN: called CC=1 SCAR=0.0
185 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 1 ▶use constant Node => "Class::RDF::Node";
Subroutine BEGIN: called CC=1 SCAR=0.0
189 1 ▶use constant Object => "Class::RDF::Object";
Subroutine BEGIN: called CC=1 SCAR=0.0
190  
191 1 our @Quad = qw( subject predicate object context );
192  
193 1 __PACKAGE__->table( "statement" );
194 1 __PACKAGE__->columns( All => "id", "created", @Quad );
195 1 __PACKAGE__->has_a( $_ => Node ) for @Quad;
196 1 __PACKAGE__->autoupdate(1);
197  
198 1 __PACKAGE__->set_sql( RetrieveFull => <<"" );
199 SELECT st.*, n.*
200 FROM statement st, node n
201 WHERE %s
202 AND ( st.subject = n.id
203 OR st.predicate = n.id
204 OR st.object = n.id
205 OR st.context = n.id )
206  
207  
208 1 __PACKAGE__->set_sql( RetrieveOrdered => <<"" );
209 SELECT st.*, n.*
210 FROM statement st, node n, node m
211 WHERE %s
212 AND ( st.subject = n.id
213 OR st.predicate = n.id
214 OR st.object = n.id
215 OR st.context = n.id )
216 AND m.id = object
217 ORDER BY m.value %s
218  
219 1 __PACKAGE__->set_sql( RetrieveObjects => <<"" );
220 SELECT obj.*, n.*
221 FROM statement st, statement obj, node n
222 WHERE %s
223 AND obj.subject = st.subject
224 AND ( obj.subject = n.id
225 OR obj.predicate = n.id
226 OR obj.object = n.id
227 OR obj.context = n.id )
228  
229 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
230 0 my ($class, @nodes) = @_;
231  
232 0 my @triple;
233 0 for my $node (@nodes) {
234 0 ▶ $node = $class->Node->new($node) unless ref $node;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node 0 0 0
235 0 push @triple, $node;
236 }
237 0 $class->find_or_create({ subject => $triple[0],
238 predicate => $triple[1],
239 object => $triple[2],
240 context => $triple[3] });
241  
242 }
243  
244 0 ▶sub value {
Subroutine value: not called CC=2 SCAR=17.9
245 0 my $self = shift;
246 0 my $obj = $self->object;
247 0 ▶ return $obj->is_resource ?
Branch: 1 branch
BranchTrueFalseTotal
$obj->is_resource ? : 0 0 0
248 $self->Object->new($obj->value) : $obj->value;
249 }
250  
251 0 ▶sub triples {
Subroutine triples: not called CC=4 SCAR=30.0
252 0 my $self = shift;
253 0 my @t;
254 0 foreach (qw(subject predicate object)) {
255 0 my $node = $self->$_;
256 0 ▶ if ($node and $node->can('value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node and $node->can('value')) { } 0 0 0
Condition0%0/3
$node and $node->can("value")
!ll&&!rl&&r
000
MC/DC0%0/2
$node and $node->can("value")
$node $node->can("value")
Truth table0%0/3
A and B
A= $nodeB= $node->can("value")
ABresult
0X0
100
111
257 0 push @t, $node->value;
258 }
259 else {
260 # XXX: why are we returning undef here?
261 0 return undef;
262 }
263 }
264 0 return @t;
265 }
266  
267 0 ▶sub search {
Subroutine search: not called CC=8 SCAR=42.8
268 0 my $class = shift;
269 0 ▶ my %args = ref($_[0]) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
270  
271 0 ▶ $args{like} = delete $args{object} if $args{like};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"like"} 0 0 0
272  
273 0 my ($where, $vals) = $class->compose_query(%args);
274 0 ▶ return $class->no_match unless ref $where;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $where 0 0 0
275  
276 0 ▶ if ( $args{like} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"like"}) 0 0 0
277 0 my @nodes = $class->Node->search_like( value => '%'.$args{like}.'%' );
278 0 ▶ return $class->no_match unless @nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 0 0
279 0 push @$where, "st.object IN (" . join(",", map($_->id, @nodes)) . ")";
280 }
281  
282 0 my $sth;
283 0 ▶ if ( $args{objects} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'objects'}) { } 0 0 0
elsif ($args{'order'}) { } 0 0 0
284 0 $sth = $class->sql_RetrieveObjects( join(" AND ", @$where) );
285 } elsif ( $args{order} ) {
286 0 $sth = $class->sql_RetrieveOrdered( join(" AND ", @$where), $args{order} );
287 } else {
288 0 $sth = $class->sql_RetrieveFull( join(" AND ", @$where) );
289 }
290  
291 0 return $class->retrieve_from_sth($sth, @$vals);
292 }
293  
294 0 ▶sub no_match {
Subroutine no_match: not called CC=1 SCAR=6.9
295 0 my $class = shift;
296 0 $class->_ids_to_objects([]);
297 }
298  
299 0 ▶sub compose_query {
Subroutine compose_query: not called CC=6 SCAR=37.4
300 0 my ($class, %args) = @_;
301 0 my (@where, @vals);
302  
303 $args{predicate} = Class::RDF::NS->expand( $args{predicate} )
304 0 ▶ if exists $args{predicate} and not ref $args{predicate};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"predicate"} and not ref $args{"predicate"} 0 0 0
Condition0%0/3
exists $args{"predicate"} and not ref $args{"predicate"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"predicate"} and not ref $args{"predicate"}
exists $args{"predicate"} not ref $args{"predicate"}
Truth table0%0/3
A and not(B)
A= exists $args{"predicate"}B= not ref $args{"predicate"}
ABresult
0X0
100
111
305  
306 0 for my $position (grep exists $args{$_}, @Quad) {
307 0 push @where, "st.$position = ?";
308 0 ▶ if ( ref $args{$position} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{$position}) { } 0 0 0
309 0 push @vals, $args{$position};
310 } else {
311 0 ▶ my $node = $class->Node->find( $args{$position} ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $node = $class->Node->find($args{$position}) or return 0 0 0
312 0 push @vals, $node;
313 }
314 }
315  
316 0 return (\@where, \@vals);
317 }
318  
319 0 ▶sub retrieve_from_sth {
Subroutine retrieve_from_sth: not called CC=8 SCAR=42.8
320 0 my ($class, $sth, @bind) = @_;
321  
322 0 my (@results, %nodes, %triples, %t, %n);
323 0 eval {
324 0 $sth->execute( map($_->id, @bind) );
325 $sth->bind_columns(\(
326 @t{qw{ id created subject predicate object context }},
327 0 @n{qw{ id created value is_resource }} ));
328  
329 0 ▶ while ($sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) 0 0 0
330 0 ▶ unless ( exists $nodes{$n{id}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $nodes{$n{"id"}}) 0 0 0
331 0 $nodes{$n{id}} = $class->Node->construct(\%n);
332 }
333 0 ▶ unless ( exists $triples{$t{id}} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $triples{$t{"id"}}) 0 0 0
334 0 push @results, ( $triples{$t{id}} = {%t} );
335 }
336 }
337 };
338  
339 0 ▶ return $class->_croak("$class can't $sth->{Statement}: $@", err => $@)
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
340 if $@;
341  
342 0 for my $st (values %triples) {
343 0 for my $which (@Quad) {
344 0 ▶ $st->{$which} = $nodes{$st->{$which}} if $st->{$which};
Branch: 1 branch
BranchTrueFalseTotal
if $st->{$which} 0 0 0
345 }
346 }
347  
348 0 return $class->_ids_to_objects(\@results);
349 }
350  
351 # ... we need to figure out where this belongs ...
352 #
353 # use Time::Piece;
354 #
355 # sub ical_to_sql {
356 # my ($class,$ical) = @_;
357 # warn($ical);
358 # my $t = Time::Piece->strptime($ical,"%Y%m%dT%H%M%SZ");
359 # $t->strftime("%Y%m%d%H%M%S");
360 # }
361 #
362 # sub timeslice {
363 # my ($self,%p) = @_;
364 # my $start = $p{start};
365 # my $end = $p{end};
366 # my @where;
367 # # SQL for timestamp
368 # warn("time");
369 # push @where, "created > " . $self->ical_to_sql($start) if $start;
370 # push @where, "created < " . $self->ical_to_sql($end) if $end;
371 # my $sql = join(" and ", @where); warn($sql);
372 # my @o = $self->retrieve_from_sql($sql);
373 # }
374  
375 package Class::RDF::Object;
376  
377 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
378 1 ▶use overload '""' => \&as_string,
Subroutine BEGIN: called CC=1 SCAR=0.2
379 0 ▶ eq => sub { $_[0]->as_string eq $_[1] };
Subroutine __ANON__: not called CC=1 SCAR=0.2
380 1 ▶use vars '$AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
381 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
382 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
383  
384 1 ▶use constant Node => "Class::RDF::Node";
Subroutine BEGIN: called CC=1 SCAR=0.0
385 1 ▶use constant Statement => "Class::RDF::Statement";
Subroutine BEGIN: called CC=1 SCAR=0.0
386  
387 0 ▶sub new {
Subroutine new: not called CC=16 SCAR=56.1
388 0 my $class = shift;
389 0 my ($uri, $context, $data, $base);
390 0 ▶ $uri = shift unless ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
391 0 ▶ $context = shift unless ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
392 0 ▶ $data = shift if ref $_[0] eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
393 0 ▶ $base = shift if $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 0 0 0
394 0 ▶ $base ||= '_id:';
Condition0%0/2
$base ||= "_id:"
l!l
00
MC/DC0%0/1
$base ||= "_id:"
$base
Truth table0%0/2
A
A= $base
Aresult
11
00
395 0 ▶ $uri ||= $base.sprintf("%08x%04x", time, int rand(0xFFFF));
Condition0%0/3
$uri ||= $base . sprintf("%08x%04x", time, int rand 65535)
l!l&&r!l&&!r
000
MC/DC0%0/2
$uri ||= $base . sprintf("%08x%04x", time, int rand 65535)
$uri $base . sprintf("%08x%04x", time, int rand 65535)
Truth table0%0/3
A ||= B
A= $uriB= $base . sprintf("%08x%04x", time, int rand 65535)
ABresult
1X1
011
000
396 0 ▶ unless (ref $uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $uri) 0 0 0
397 0 $uri = $class->Node->new($uri);
398 }
399
400 0 ▶ $context = $class->Node->find($context)
Branch: 1 branch
BranchTrueFalseTotal
if $context and not ref $context 0 0 0
Condition0%0/3
$context and not ref $context
!ll&&!rl&&r
000
MC/DC0%0/2
$context and not ref $context
$context not ref $context
Truth table0%0/3
A and not(B)
A= $contextB= not ref $context
ABresult
0X0
100
111
401 if $context and not ref $context;
402  
403 0 ▶ my $self = bless {
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
404 context => $context,
405 uri => $uri,
406 triples => {},
407 stub => 1
408 }, ref($class) || $class;
409
410 0 ▶ while (my ($key, $vals) = each %$data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 0 0 0
411 0 ▶ for my $val (ref $vals eq 'ARRAY' ? @$vals : $vals) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 0 0 0
412 0 ▶ $val = $val->{uri}->value if ref($val) and $val->{'uri'};
Branch: 1 branch
BranchTrueFalseTotal
if ref $val and $val->{"uri"} 0 0 0
Condition0%0/3
ref $val and $val->{"uri"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $val and $val->{"uri"}
ref $val $val->{"uri"}
Truth table0%0/3
A and B
A= ref $valB= $val->{"uri"}
ABresult
0X0
100
111
413 0 my $st = $self->Statement->new( $uri, $key, $val );
414 0 $self->_add_statement($st);
415 }
416 }
417 0 return $self;
418 }
419  
420 0 ▶sub _fetch_statements {
Subroutine _fetch_statements: not called CC=2 SCAR=17.9
421 0 my $self = shift;
422 # warn "fetch_statements ", $self->uri->value, "\n";
423 0 my $iter = $self->Statement->search( subject => $self->uri );
424 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
425 0 $self->_add_statement($st);
426 }
427 0 delete $self->{stub};
428 }
429  
430 0 ▶sub _add_statement {
Subroutine _add_statement: not called CC=2 SCAR=17.9
431 0 my ($self, $statement) = @_;
432 0 ▶ push @{$self->{triples}{$statement->predicate->value} ||= []}, $statement;
Condition0%0/2
$self->{"triples"}{$statement->predicate->value} ||= []
l!l
00
MC/DC0%0/1
$self->{"triples"}{$statement->predicate->value} ||= []
$self->{"triples"}{$statement->predicate->value}
Truth table0%0/2
A
A= $self->{"triples"}{$statement->predicate->value}
Aresult
11
00
433 }
434  
435 0 ▶sub statements {
Subroutine statements: not called CC=2 SCAR=17.9
436 0 my $self = shift;
437 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
438 0 return map( @$_, values %{$self->{triples}} );
439 }
440  
441 0 ▶sub triples {
Subroutine triples: not called CC=2 SCAR=17.9
442 0 my $self = shift;
443 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
444 0 return map( [$_->triples], $self->statements );
445 }
446  
447 0 ▶sub uri {
Subroutine uri: not called CC=1 SCAR=6.9
448 0 my $self = shift;
449 # read only because Goddess help us if an object's URI
450 # changes in mid-flight
451 0 return $self->{uri};
452 }
453  
454 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
455 0 my $self = shift;
456 0 return $self->uri->as_string;
457 }
458 0 ▶sub _string_eq {
Subroutine _string_eq: not called CC=1 SCAR=6.9
459 0 my ($self,$other) = @_;
460 0 $self->as_string eq $other;
461 }
462  
463 0 ▶sub context {
Subroutine context: not called CC=3 SCAR=24.8
464 0 my $self = shift;
465 0 ▶ $self->{context} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
466 0 ▶ return $self->{context} if $self->{context};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"context"} 0 0 0
467 }
468  
469 0 ▶sub get {
Subroutine get: not called CC=4 SCAR=30.0
470 0 my ($self, $prop) = @_;
471 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
472 0 ▶ my $statements = $self->{triples}{$prop} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $statements = $self->{"triples"}{$prop} or return 0 0 0
473 0 my @vals = map( $_->value, @$statements );
474 0 ▶ return wantarray ? @vals : $vals[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
475 }
476  
477 0 ▶sub set {
Subroutine set: not called CC=9 SCAR=45.0
478 0 my ($self, %args) = @_;
479 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
480 0 ▶ while (my ($key, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
481 0 ▶ if (exists $self->{triples}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"triples"}{$key}) 0 0 0
482 0 $_->delete for @{$self->{triples}{$key}};
483 0 delete $self->{triples}{$key};
484 }
485  
486 0 ▶ for my $value (ref($val) eq "ARRAY" ? @$val : $val) {
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
487 0 ▶ $value = $value->uri if ref($value) and $value->can('uri');
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $value->can("uri") 0 0 0
Condition0%0/3
ref $value and $value->can("uri")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $value->can("uri")
ref $value $value->can("uri")
Truth table0%0/3
A and B
A= ref $valueB= $value->can("uri")
ABresult
0X0
100
111
488 0 my $triple = $self->Statement->new(
489 $self->uri->value, $key, $value, $self->context );
490 0 $self->_add_statement( $triple );
491 }
492 }
493 }
494  
495 # delete forward and backward references to me
496 #
497 0 ▶ sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
498 0 my $self = shift;
499 0 my $uri = $self->uri->value;
500 # triples that have my subject, and other predicate and object.
501 #
502 0 my @triples = $self->statements;
503 0 foreach (@triples) {
504 0 $_->delete;
505 }
506 # triples that have me as their object
507 0 my @pointers = Class::RDF::Statement->search(object => $uri);
508 0 foreach (@pointers) {
509 0 $_->delete;
510 }
511 # is that really it?
512 }
513  
514 0 ▶sub get_or_set {
Subroutine get_or_set: not called CC=2 SCAR=17.9
515 0 my ($self, $prop, @vals) = @_;
516 0 ▶ if (@vals) {
Branch: 1 branch
BranchTrueFalseTotal
if (@vals) { } 0 0 0
517 0 $self->set($prop => shift @vals);
518 } else {
519 0 return $self->get($prop);
520 }
521 }
522  
523 0 ▶sub add {
Subroutine add: not called CC=7 SCAR=40.3
524 0 my ($self, %args) = @_;
525 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
526 0 ▶ while (my ($key, $val) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
527 0 ▶ for my $value (ref($val) eq "ARRAY" ? @$val : $val) {
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 0 0
528 0 ▶ $value = $value->{uri} if ref($value) and $value->{uri};
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and $value->{"uri"} 0 0 0
Condition0%0/3
ref $value and $value->{"uri"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value and $value->{"uri"}
ref $value $value->{"uri"}
Truth table0%0/3
A and B
A= ref $valueB= $value->{"uri"}
ABresult
0X0
100
111
529
530 0 my $triple = $self->Statement->new(
531 $self->uri, $key, $value, $self->context );
532 0 $self->_add_statement( $triple );
533 }
534 }
535 }
536  
537 0 ▶sub remove {
Subroutine remove: not called CC=9 SCAR=45.0
538 0 my ($self, %args) = @_;
539 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
540 0 ▶ while (my ($key, $vals) = each %args) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %args) 0 0 0
541 0 my %remove;
542 0 ▶ my @v = ref($vals) eq 'ARRAY' ? @$vals : ($vals);
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 0 0 0
543 0 foreach my $o (@v) {
544 0 ▶ $o = $o->{uri}->value if ref($o) and $o->{uri};
Branch: 1 branch
BranchTrueFalseTotal
if ref $o and $o->{"uri"} 0 0 0
Condition0%0/3
ref $o and $o->{"uri"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $o and $o->{"uri"}
ref $o $o->{"uri"}
Truth table0%0/3
A and B
A= ref $oB= $o->{"uri"}
ABresult
0X0
100
111
545 0 $remove{$o} = 1;
546 }
547  
548 0 my $triples = $self->{triples}{$key};
549 0 ▶ for (my $st = 0; $st < scalar(@$triples); $st++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st < scalar @$triples) 0 0 0
550 0 ▶ if ($remove{$triples->[$st]->object->value}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remove{$triples->[$st]->object->value}) 0 0 0
551 0 $triples->[$st]->delete;
552 0 splice @$triples, $st--, 1;
553 }
554 }
555 }
556 }
557  
558 0 ▶sub contains {
Subroutine contains: not called CC=3 SCAR=24.8
559 0 my ($self, $prop, $val) = @_;
560 0 ▶ $self->_fetch_statements if $self->{stub};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stub"} 0 0 0
561 0 return scalar grep( $_ eq $val, @{$self->{triples}{$prop}} )
562 0 ▶ if exists $self->{triples}{$prop};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"triples"}{$prop} 0 0 0
563 0 return;
564 }
565  
566 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
567 0 my ($class, $uri) = @_;
568 0 my $node = $class->Node->find($uri);
569 0 ▶ return $node ? $class->new($node) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$node ? : 0 0 0
570 }
571  
572 0 ▶sub find_or_create {
Subroutine find_or_create: not called CC=3 SCAR=24.8
573 0 my $class = shift;
574 0 my ($args) = @_;
575 0 my $obj;
576  
577 0 ▶ if (ref $args eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 0 0 0
578 0 ($obj) = $class->search( %$args );
579 } else { # $args is really a uri
580 0 $obj = $class->new( $args );
581 }
582  
583 0 ▶ $obj ||= $class->new( @_ );
Condition0%0/3
$obj ||= $class->new(@_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj ||= $class->new(@_)
$obj $class->new(@_)
Truth table0%0/3
A ||= B
A= $objB= $class->new(@_)
ABresult
1X1
011
000
584 0 return $obj;
585 }
586  
587 0 ▶sub search {
Subroutine search: not called CC=8 SCAR=42.8
588 0 my ($class, $predicate, $object, $args) = @_;
589  
590 0 ▶ my %args = (ref($args) ? %$args : ());
Branch: 1 branch
BranchTrueFalseTotal
ref $args ? : 0 0 0
591 0 $args{predicate} = $predicate;
592 0 ▶ $args{object} = $object if $object;
Branch: 1 branch
BranchTrueFalseTotal
if $object 0 0 0
593 0 my $iter = $class->Statement->search( %args, objects => 1 );
594  
595 0 my (@results, %seen);
596 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
597 0 my $id = $st->subject->id;
598 0 ▶ unless ( $seen{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$id}) 0 0 0
599 0 $seen{$id} = $class->new( $st->subject, {} );
600 0 delete $seen{$id}{stub};
601 0 push @results, $seen{$id};
602 }
603 0 $seen{$id}->_add_statement($st);
604 }
605  
606 0 ▶ if (my $order = $args{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $order = $args{"order"}) 0 0 0
607 0 @results = map { $_->[1] }
608 0 ▶ sort { $order eq "asc" ? $a->[0] cmp $b->[0] : $b->[0] cmp $a->[0] }
Branch: 1 branch
BranchTrueFalseTotal
$order eq 'asc' ? : 0 0 0
609 0 map { [($_->get($predicate))[0], $_] } @results;
610 }
611  
612 0 ▶ return( wantarray ? @results : $results[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
613 }
614  
615 package Class::RDF;
616  
617 1 ▶use RDF::Simple::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
618 1 ▶use RDF::Simple::Serialiser;
Subroutine BEGIN: called CC=1 SCAR=0.0
619 1 ▶use LWP::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
620 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
621 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
622 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
623  
624 1 ▶use constant Node => "Class::RDF::Node";
Subroutine BEGIN: called CC=1 SCAR=0.0
625 1 ▶use constant Statement => "Class::RDF::Statement";
Subroutine BEGIN: called CC=1 SCAR=0.0
626 1 ▶use constant Object => "Class::RDF::Object";
Subroutine BEGIN: called CC=1 SCAR=0.0
627  
628 1 our ($Parser, $Serializer);
629 1 our $VERSION = '0.20';
630  
631 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
632 0 my $class = shift;
633 0 $class->Object->new( @_ );
634 }
635  
636 0 ▶sub set_db {
Subroutine set_db: not called CC=2 SCAR=17.9
Pod documented
637 0 my $class = shift;
638 0 Class::RDF::Store->set_db( Main => @_ );
639 0 Class::RDF::NS->load;
640 0 ▶ if ( $_[0] =~ /^dbi:Pg:/io ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^dbi:Pg:/io) 0 0 0
641 0 $class->Node->sequence( "node_id_seq" );
642 0 $class->Statement->sequence( "statement_id_seq" );
643 }
644 }
645  
646 1 ▶sub is_transient {
Subroutine is_transient: called CC=1 SCAR=0.0
Pod documented
647 1 my $class = shift;
648 1 Class::RDF::Store->is_transient;
649 }
650  
651 1 ▶sub define {
Subroutine define: called CC=1 SCAR=0.0
Pod documented
652 1 my $class = shift;
653 1 Class::RDF::NS->define(@_);
654 }
655  
656 0 ▶sub parser {
Subroutine parser: not called CC=2 SCAR=17.9
Pod undocumented
657 0 my $class = shift;
658 0 ▶ $Parser ||= RDF::Simple::Parser->new;
Condition0%0/3
$Class::RDF::Parser ||= "RDF::Simple::Parser"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Class::RDF::Parser ||= "RDF::Simple::Parser"->new
$Class::RDF::Parser "RDF::Simple::Parser"->new
Truth table0%0/3
A ||= B
A= $Class::RDF::ParserB= "RDF::Simple::Parser"->new
ABresult
1X1
011
000
659 0 return $Parser;
660 }
661  
662 0 ▶sub serializer {
Subroutine serializer: not called CC=2 SCAR=17.9
Pod undocumented
663 0 my $class = shift;
664 0 ▶ $Serializer ||= RDF::Simple::Serialiser->new;
Condition0%0/3
$Class::RDF::Serializer ||= "RDF::Simple::Serialiser"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$Class::RDF::Serializer ||= "RDF::Simple::Serialiser"->new
$Class::RDF::Serializer "RDF::Simple::Serialiser"->new
Truth table0%0/3
A ||= B
A= $Class::RDF::SerializerB= "RDF::Simple::Serialiser"->new
ABresult
1X1
011
000
665 0 return $Serializer;
666 }
667  
668 0 ▶sub parse {
Subroutine parse: not called CC=6 SCAR=37.4
Pod documented
669 0 my ($class, %args) = @_;
670 my @triples = $args{uri} ?
671 $class->parser->parse_uri($args{uri}) :
672 0 ▶ $class->parser->parse_rdf($args{xml});
Branch: 1 branch
BranchTrueFalseTotal
$args{'uri'} ? : 0 0 0
673 0 my %output;
674  
675 0 ▶ return unless @triples;
Branch: 1 branch
BranchTrueFalseTotal
unless @triples 0 0 0
676  
677 # we care about getting the root object back first
678 0 my $root = $triples[0][0];
679  
680 0 ▶ $args{context} ||= $args{uri};
Condition0%0/3
$args{"context"} ||= $args{"uri"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"context"} ||= $args{"uri"}
$args{"context"} $args{"uri"}
Truth table0%0/3
A ||= B
A= $args{"context"}B= $args{"uri"}
ABresult
1X1
011
000
681 0 for my $triple (@triples) {
682 0 $class->Statement->new(@$triple, $args{context});
683 0 $output{$triple->[0]}++;
684 }
685  
686 0 $output{$_} = $class->new($_) for keys %output;
687
688 0 my $first = delete $output{$root};
689 0 return ($first, values %output);
690 }
691  
692 0 ▶sub serialize {
Subroutine serialize: not called CC=3 SCAR=24.8
Pod undocumented
693 0 my ($class, @objects) = @_;
694 0 my @triples;
695 0 for (@objects) {
696 0 my @t = $_->triples;
697 0 push @triples, @t;
698 }
699 $class->serializer->addns( $_->prefix, $_->uri )
700 0 for Class::RDF::NS->retrieve_all;
701 0 return $class->serializer->serialise(@triples);
702 }
703  
704 1 *serialise = *serialise = \&serialize; # because I'm in love with her
705  
706 1 1;
707  
708 __END__
↑ Summary RDF/Cache.pm »