RDF/Trine/Graph.pm

filter: Statementstmt 93.2% 165 / 177 Branchbran 82.1% 46 / 56 Conditioncond 66.6% 10 / 15 MC/DCmcdc 50.0% 5 / 10 Subroutinesub 92.8% 26 / 28 Podpod 100% 8 / 8 totaltotal 88.4% 260 / 294 CC 43 SCAR 18.4
CC
43
Cov
89%
_find_mapping
15.1
_check_equality
8.0
_check_subgraph
7.4
CRAP 6.3
« RDF/Trine/Serializer/NQuads.pm ↑ Summary RDF/Trine/Serializer.pm »
1 # RDF::Trine::Graph
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Graph - Materialized RDF Graphs for testing isomorphism
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Graph version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Graph;
15 my $a = RDF::Trine::Graph->new( $model_a );
16 my $b = RDF::Trine::Graph->new( $model_b );
17 print "graphs are " . ($a->equals( $b ) ? "the same" : "different");
18  
19 =head1 DESCRIPTION
20  
21 RDF::Trine::Graph provdes a mechanism for testing graph isomorphism based on
22 graph triples from either a RDF::Trine::Model or a RDF::Trine::Iterator.
23 Isomorphism testing requires materializing all of a graph's triples in memory,
24 and so should be used carefully in situations with large graphs.
25  
26 =head1 METHODS
27  
28 =over 4
29  
30 =cut
31  
32 package RDF::Trine::Graph;
33  
34 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 68 ▶use Algorithm::Combinatorics qw(permutations);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 68 our ($VERSION, $debug, $AUTOLOAD);
41 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 $debug = 0;
43 68 $VERSION = '1.019';
44 }
45  
46 use overload
47 68 ▶ '==' => \&RDF::Trine::Graph::_eq,
Subroutine BEGIN: called CC=1 SCAR=0.0
48 'eq' => \&RDF::Trine::Graph::_eq,
49 'le' => \&RDF::Trine::Graph::_le,
50 'ge' => \&RDF::Trine::Graph::_ge,
51 'lt' => \&RDF::Trine::Graph::_lt,
52 'gt' => \&RDF::Trine::Graph::_gt,
53 68 ;
54  
55 2 ▶sub _eq {
Subroutine _eq: called CC=1 SCAR=0.0
56 2 my ($x, $y) = @_;
57 2 return $x->equals($y);
58 }
59  
60 4 ▶sub _le {
Subroutine _le: called CC=1 SCAR=0.0
61 4 my ($x, $y) = @_;
62 4 return $x->is_subgraph_of($y);
63 }
64  
65 2 ▶sub _ge {
Subroutine _ge: called CC=1 SCAR=0.0
66 2 return _le(@_[1,0]);
67 }
68  
69 4 ▶sub _lt {
Subroutine _lt: called CC=2 SCAR=7.1
70 4 my ($x, $y) = @_;
71 # Test::More::diag(sprintf('%s // %s', ref($x), ref($y)));
72 4 ▶ return ($x->size < $y->size) && ($x->is_subgraph_of($y));
Condition66%2/3
$x->size < $y->size && $x->is_subgraph_of($y)
!ll&&!rl&&r
202
MC/DC50%1/2
$x->size < $y->size && $x->is_subgraph_of($y)
$x->size < $y->size $x->is_subgraph_of($y)
Truth table66%2/3
A && B
A= $x->size < $y->sizeB= $x->is_subgraph_of($y)
ABresult
0X0
100
111
73 }
74  
75 2 ▶sub _gt {
Subroutine _gt: called CC=1 SCAR=0.0
76 2 return _lt(@_[1,0]);
77 }
78  
79 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
82 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 68 ▶use RDF::Trine::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 =item C<< new ( $model ) >>
86  
87 =item C<< new ( $iterator ) >>
88  
89 Returns a new graph from the given RDF::Trine::Model or RDF::Trine::Iterator::Graph object.
90  
91 =cut
92  
93 33 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
94 33 my $class = shift;
95 33 ▶ unless (blessed($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_[0])) 2 31 33
96 2 throw RDF::Trine::Error::MethodInvocationError -text => "RDF::Trine::Graph::new must be called with a Model or Iterator argument";
97 }
98
99 31 my %data;
100 31 ▶ if ($_[0]->isa('RDF::Trine::Iterator::Graph')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]->isa('RDF::Trine::Iterator::Graph')) { } 2 29 31
elsif ($_[0]->isa('RDF::Trine::Model')) { } 28 1 29
101 2 my $iter = shift;
102 2 my $model = RDF::Trine::Model->new( RDF::Trine::Store->temporary_store() );
103 2 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 4 2 6
104 4 $model->add_statement( $st );
105 }
106 2 $data{ model } = $model;
107 } elsif ($_[0]->isa('RDF::Trine::Model')) {
108 28 $data{ model } = shift;
109 } else {
110 1 throw RDF::Trine::Error::MethodInvocationError -text => "RDF::Trine::Graph::new must be called with a Model or Iterator argument";
111 }
112
113 30 my $self = bless(\%data, $class);
114 }
115  
116 =item C<< equals ( $graph ) >>
117  
118 Returns true if the invocant and $graph represent two equal RDF graphs (e.g.
119 there exists a bijection between the RDF statements of the invocant and $graph).
120  
121 =cut
122  
123 11 ▶sub equals {
Subroutine equals: called CC=2 SCAR=6.9
Pod documented
124 11 my $self = shift;
125 11 my $graph = shift;
126 11 undef($self->{error});
127 11 ▶ return $self->_check_equality($graph) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_equality($graph) ? : 5 4 9
128 }
129  
130 11 ▶sub _check_equality {
Subroutine _check_equality: called CC=8 SCAR=20.8
131 11 my $self = shift;
132 11 my $graph = shift;
133 11 ▶ unless (blessed($graph) and $graph->isa('RDF::Trine::Graph')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")) 2 9 11
Condition100%3/3
builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")
!ll&&!rl&&r
119
MC/DC100%2/2
builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")
builtin::blessed($graph) $graph->isa("RDF::Trine::Graph")
Truth table100%3/3
A and B
A= builtin::blessed($graph)B= $graph->isa("RDF::Trine::Graph")
ABresult
0X0
100
111
134 2 $self->{error} = "RDF::Trine::Graph::equals must be called with a Graph argument";
135 2 throw RDF::Trine::Error::MethodInvocationError -text => $self->{error};
136 }
137
138 9 my @graphs = ($self, $graph);
139 9 my ($ba, $nba) = $self->split_blank_statements;
140 9 my ($bb, $nbb) = $graph->split_blank_statements;
141 9 ▶ if (scalar(@$nba) != scalar(@$nbb)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$nba != scalar @$nbb) 1 8 9
142 1 my $nbac = scalar(@$nba);
143 1 my $nbbc = scalar(@$nbb);
144 1 $self->{error} = "count of non-blank statements didn't match ($nbac != $nbbc)";
145 1 return 0;
146 }
147 8 my $bac = scalar(@$ba);
148 8 my $bbc = scalar(@$bb);
149 8 ▶ if ($bac != $bbc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bac != $bbc) 1 7 8
150 1 $self->{error} = "count of blank statements didn't match ($bac != $bbc)";
151 1 return 0;
152 }
153
154 7 for ($nba, $nbb) {
155 14 @$_ = sort map { $_->as_string } @$_;
156 }
157
158 7 foreach my $i (0 .. $#{ $nba }) {
159 6 ▶ unless ($nba->[$i] eq $nbb->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nba->[$i] eq $nbb->[$i]) 1 5 6
160 1 $self->{error} = "non-blank triples don't match: " . Dumper($nba->[$i], $nbb->[$i]);
161 1 return 0;
162 }
163 }
164
165 6 return _find_mapping($self, $ba, $bb);
166 }
167  
168 =item C<< is_subgraph_of ( $graph ) >>
169  
170 Returns true if the invocant is a subgraph of $graph. (i.e. there exists an
171 injection of RDF statements from the invocant to $graph.)
172  
173 =cut
174  
175 15 ▶sub is_subgraph_of {
Subroutine is_subgraph_of: called CC=2 SCAR=6.9
Pod documented
176 15 my $self = shift;
177 15 my $graph = shift;
178 15 undef($self->{error});
179 15 ▶ return $self->_check_subgraph($graph) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_subgraph($graph) ? : 9 4 13
180 }
181  
182 =item C<< injection_map ( $graph ) >>
183  
184 If the invocant is a subgraph of $graph, returns a mapping of blank node
185 identifiers from the invocant graph to $graph as a hashref. Otherwise
186 returns false. The solution is not always unique; where there exist multiple
187 solutions, the solution returned is arbitrary.
188  
189 =cut
190  
191 0 ▶sub injection_map {
Subroutine injection_map: not called CC=2 SCAR=17.9
Pod documented
192 0 my $self = shift;
193 0 my $graph = shift;
194 0 undef($self->{error});
195 0 my $map = $self->_check_subgraph($graph);
196 0 ▶ return $map if $map;
Branch: 1 branch
BranchTrueFalseTotal
if $map 0 0 0
197 0 return;
198 }
199  
200 15 ▶sub _check_subgraph {
Subroutine _check_subgraph: called CC=7 SCAR=20.1
201 15 my $self = shift;
202 15 my $graph = shift;
203 15 ▶ unless (blessed($graph) and $graph->isa('RDF::Trine::Graph')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")) 2 13 15
Condition100%3/3
builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")
!ll&&!rl&&r
1113
MC/DC100%2/2
builtin::blessed($graph) and $graph->isa("RDF::Trine::Graph")
builtin::blessed($graph) $graph->isa("RDF::Trine::Graph")
Truth table100%3/3
A and B
A= builtin::blessed($graph)B= $graph->isa("RDF::Trine::Graph")
ABresult
0X0
100
111
204 2 throw RDF::Trine::Error::MethodInvocationError -text => "RDF::Trine::Graph::equals must be called with a Graph argument";
205 }
206
207 13 my @graphs = ($self, $graph);
208 13 my ($ba, $nba) = $self->split_blank_statements;
209 13 my ($bb, $nbb) = $graph->split_blank_statements;
210
211 13 ▶ if (scalar(@$nba) > scalar(@$nbb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @$nba > scalar @$nbb) { } 0 13 13
elsif (scalar @$ba > scalar @$bb) { } 0 13 13
212 0 $self->{error} = "invocant had too many blank node statements to be a subgraph of argument";
213 0 return 0;
214 } elsif (scalar(@$ba) > scalar(@$bb)) {
215 0 $self->{error} = "invocant had too many non-blank node statements to be a subgraph of argument";
216 0 return 0;
217 }
218  
219 13 my %NBB = map { $_->as_string => 1 } @$nbb;
220
221 13 foreach my $st (@$nba) {
222 11 ▶ unless ($NBB{ $st->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($NBB{$st->as_string}) 1 10 11
223 1 return 0;
224 }
225 }
226
227 12 return _find_mapping($self, $ba, $bb);
228 }
229  
230 18 ▶sub _find_mapping {
Subroutine _find_mapping: called CC=15 SCAR=27.1
231 18 my ($self, $ba, $bb) = @_;
232  
233 18 ▶ if (scalar(@$ba) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$ba == 0) 2 16 18
234 2 return {};
235 }
236
237 16 my %blank_ids_a;
238 16 foreach my $st (@$ba) {
239 26 foreach my $n (grep { $_->isa('RDF::Trine::Node::Blank') } $st->nodes) {
240 30 $blank_ids_a{ $n->blank_identifier }++;
241 }
242 }
243  
244 16 my %blank_ids_b;
245 16 foreach my $st (@$bb) {
246 40 foreach my $n (grep { $_->isa('RDF::Trine::Node::Blank') } $st->nodes) {
247 44 $blank_ids_b{ $n->blank_identifier }++;
248 }
249 }
250
251 16 my %bb_master = map { $_->as_string => 1 } @$bb;
252
253 16 my @ka = keys %blank_ids_a;
254 16 my @kb = keys %blank_ids_b;
255 16 my $kbp = permutations( \@kb );
256 16 my $count = 0;
257 16 ▶ MAPPING: while (my $mapping = $kbp->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapping = $kbp->next) 29 4 33
258 29 my %mapping;
259 29 @mapping{ @ka } = @$mapping;
260 29 ▶ warn "trying mapping: " . Dumper(\%mapping) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Graph::debug 0 29 29
261
262 29 my %bb = %bb_master;
263 29 foreach my $st (@$ba) {
264 39 my @nodes;
265 39 foreach my $method ($st->node_names) {
266 117 my $n = $st->$method();
267 117 ▶ if ($n->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa('RDF::Trine::Node::Blank')) { } 49 68 117
268 49 my $id = $mapping{ $n->blank_identifier };
269 49 ▶ warn "mapping " . $n->blank_identifier . " to $id\n" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Graph::debug 0 49 49
270 49 push(@nodes, RDF::Trine::Node::Blank->new( $id ));
271 } else {
272 68 push(@nodes, $n);
273 }
274 }
275 39 my $class = ref($st);
276 39 my $mapped_st = $class->new( @nodes )->as_string;
277 39 ▶ warn "checking for '$mapped_st' in " . Dumper(\%bb) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Graph::debug 0 39 39
278 39 ▶ if ($bb{ $mapped_st }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bb{$mapped_st}) { } 22 17 39
279 22 delete $bb{ $mapped_st };
280 } else {
281 17 next MAPPING;
282 }
283 }
284 12 ▶ $self->{error} = "found mapping: " . Dumper(\%mapping) if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Graph::debug 0 12 12
285 12 return \%mapping;
286 }
287
288 4 $self->{error} = "didn't find blank node mapping\n";
289 4 return 0;
290 }
291  
292 =item C<< split_blank_statements >>
293  
294 Returns two array refs, containing triples with blank nodes and triples without
295 any blank nodes, respectively.
296  
297 =cut
298  
299 44 ▶sub split_blank_statements {
Subroutine split_blank_statements: called CC=3 SCAR=11.0
Pod documented
300 44 my $self = shift;
301 44 my $iter = $self->get_statements;
302 44 my (@blanks, @nonblanks);
303 44 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 135 44 179
304 135 ▶ if ($st->has_blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->has_blanks) { } 75 60 135
305 75 push(@blanks, $st);
306 } else {
307 60 push(@nonblanks, $st);
308 }
309 }
310 44 return (\@blanks, \@nonblanks);
311 }
312  
313 =item C<< get_statements >>
314  
315 Returns a RDF::Trine::Iterator::Graph object for the statements in this graph.
316  
317 =cut
318  
319 # The code below actually goes further now and makes RDF::Trine::Graph
320 # into a subclass of RDF::Trine::Model via object delegation. This feature
321 # is undocumented as it's not clear whether this is desirable or not.
322  
323 =begin private
324  
325 =item C<< isa >>
326  
327 =cut
328  
329 46 ▶sub isa {
Subroutine isa: called CC=3 SCAR=12.5
Pod documented
330 46 my ($proto, $queried) = @_;
331 46 ▶ $proto = ref($proto) if ref($proto);
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 46 0 46
332 46 ▶ return UNIVERSAL::isa($proto, $queried) || RDF::Trine::Model->isa($queried);
Condition33%1/3
UNIVERSAL::isa($proto, $queried) || 'RDF::Trine::Model'->isa($queried)
l!l&&r!l&&!r
4600
MC/DC0%0/2
UNIVERSAL::isa($proto, $queried) || 'RDF::Trine::Model'->isa($queried)
UNIVERSAL::isa($proto, $queried) 'RDF::Trine::Model'->isa($queried)
Truth table33%1/3
A || B
A= UNIVERSAL::isa($proto, $queried)B= 'RDF::Trine::Model'->isa($queried)
ABresult
1X1
011
000
333 }
334  
335 =item C<< can >>
336  
337 =cut
338  
339 1 ▶sub can {
Subroutine can: called CC=3 SCAR=12.5
Pod documented
340 1 my ($proto, $queried) = @_;
341 1 ▶ $proto = ref($proto) if ref($proto);
Branch: 1 branch
BranchTrueFalseTotal
if ref $proto 0 1 1
342 1 ▶ return UNIVERSAL::can($proto, $queried) || RDF::Trine::Model->can($queried);
Condition33%1/3
UNIVERSAL::can($proto, $queried) || 'RDF::Trine::Model'->can($queried)
l!l&&r!l&&!r
100
MC/DC0%0/2
UNIVERSAL::can($proto, $queried) || 'RDF::Trine::Model'->can($queried)
UNIVERSAL::can($proto, $queried) 'RDF::Trine::Model'->can($queried)
Truth table33%1/3
A || B
A= UNIVERSAL::can($proto, $queried)B= 'RDF::Trine::Model'->can($queried)
ABresult
1X1
011
000
343 }
344  
345 82 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=6.9
346 82 my $self = shift;
347 82 ▶ return if $AUTOLOAD =~ /::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Graph::AUTOLOAD =~ /::DESTROY$/ 30 52 82
348 52 $AUTOLOAD =~ s/^(.+)::([^:]+)$/$2/;
349 52 return $self->{model}->$AUTOLOAD(@_);
350 }
351  
352 =end private
353  
354 =item C<< error >>
355  
356 Returns an error string explaining the last failed C<< equal >> call.
357  
358 =cut
359  
360 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
361 0 my $self = shift;
362 0 return $self->{error};
363 }
364  
365 68 1;
366  
367 __END__
« RDF/Trine/Serializer/NQuads.pm ↑ Summary RDF/Trine/Serializer.pm »