File Coverage

blib/lib/ODO/Statement.pm
Criterion Covered Total %
statement 45 56 80.3
branch 3 14 21.4
condition 2 21 9.5
subroutine 14 16 87.5
pod 5 5 100.0
total 69 112 61.6


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2004-2006 IBM Corporation.
3             #
4             # All rights reserved. This program and the accompanying materials
5             # are made available under the terms of the Eclipse Public License v1.0
6             # which accompanies this distribution, and is available at
7             # http://www.eclipse.org/legal/epl-v10.html
8             #
9             # File: $Source: /var/lib/cvs/ODO/lib/ODO/Statement.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 10/05/2004
12             # Revision: $Id: Statement.pm,v 1.3 2010-02-17 17:17:09 ubuntu Exp $
13             #
14             # Contributors:
15             # IBM Corporation - initial API and implementation
16             #
17             package ODO::Statement;
18              
19 12     12   2271 use strict;
  12         29  
  12         499  
20 12     12   61 use warnings;
  12         32  
  12         326  
21              
22 12     12   3351 use ODO::Exception;
  12         33  
  12         693  
23 12     12   3370 use ODO::Node;
  12         31  
  12         552  
24              
25 12     12   64 use base qw/ODO/;
  12         21  
  12         1232  
26              
27 12     12   63 use vars qw /$VERSION/;
  12         24  
  12         1260  
28             $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
29              
30 12     12   75 use Digest::MD5 qw/md5_hex/;
  12         24  
  12         1942  
31              
32             __PACKAGE__->mk_accessors(qw/s p o/);
33              
34             =head1 NAME
35              
36             ODO::Statement - Encapsulation of an RDF triple for graphs
37              
38             =head1 SYNOPSIS
39              
40             use ODO::Node;
41             use ODO::Statement;
42              
43             my $s = ODO::Node::Resource->new('urn:lsid:testuri.org:ns:object:');
44             my $p = ODO::Node::Resource->new('http://testuri.org/predicate');
45             my $o = ODO::Node::Literal->new('literal');
46              
47             my $statement_1 = ODO::Statement->new($s, $p, $o);
48              
49             # or
50              
51             my $statement_2 = ODO::Statement->new(s=> $s, p=> $p, o=> $o);
52              
53             # and then..
54              
55             if($statement_1->equals($statement_2)) {
56             print "\$statement_1 == \$statement_2\n";
57             }
58             else {
59             print "The statements are not equal\n";
60             }
61              
62             =head1 DESCRIPTION
63              
64             A simple container that encapsulates a single RDF statement. This object also provides tests
65             for equality.
66              
67             =head1 METHODS
68              
69             =over
70              
71             =item new( s=> $subject, p=> $predicate, o=> $object)
72              
73             =item new( $s, $p, $o )
74              
75             Creates a new ODO::Statement object with the specified subject ($s), predicate ($p), object ($o)
76             The subject, predicate, object must be any combination of L,
77             L, L, L
78             (more generically, anything that conforms to L).
79              
80             =cut
81              
82             sub new {
83 261     261 1 34383 my $self = shift;
84 261         2297 my $params = $self->params_to_hash(\@_, 0, [qw/s p o/], { 'subject'=> 's', 'predicate'=> 'p', 'object'=> 'o' } );
85 261         10597 return $self->SUPER::new(%{ $params });
  261         1767  
86             }
87              
88 12     12   66 no warnings;
  12         25  
  12         1113  
89              
90             *subject = \&s;
91             *predicate = \&p;
92             *object = \&o;
93              
94 12     12   80 use warnings;
  12         22  
  12         15610  
95              
96             =item s( [ $subject ] )
97              
98             Get or set the subject of this statement.
99              
100             =item p( [ $predicate ] )
101              
102             Get or set the predicate of this statement.
103              
104             =item o( [ $object ] )
105              
106             Get or set the object of this statement
107              
108             =item equal( $statement )
109              
110             Determines whether or not the statement is the same as the statement passed as the parameter.
111              
112             =cut
113              
114             sub equal {
115 0     0 1 0 my ($self, $statement) = @_;
116            
117 0 0       0 throw ODO::Exception::Parameter::Invalid(error=> 'Parameter must be an ODO::Statement')
118             unless($statement->isa('ODO::Statement'));
119            
120 0 0 0     0 return 1
      0        
121             if( $self->s()->equal($statement->s())
122             && $self->p()->equal($statement->p())
123             && $self->o()->equal($statement->o())
124             );
125            
126 0         0 return 0;
127             }
128              
129             =item strict_equal( $statement )
130              
131             Tests whether or not $self and $statement are the same statement, L nodes
132             _MUST_ match in this method.
133              
134             =cut
135              
136             sub strict_equal {
137 0     0 1 0 my ($self, $statement) = @_;
138            
139 0 0       0 throw ODO::Exception::Parameter::Invalid(error=> 'Parameter must be an ODO::Statement')
140             unless($statement->isa('ODO::Statement'));
141              
142             # TripleMatches are equal iff each component is the same..
143             # 'Any' nodes must be 'Any' nodes
144 0         0 foreach my $comp ('s', 'p', 'o') {
145            
146 0 0 0     0 return 0
147             if( $self->$comp()->isa('ODO::Node::Any')
148             && !$statement->$comp()->isa('ODO::Node::Any'));
149             }
150            
151 0 0 0     0 return 1
      0        
152             if( $self->s()->equal($statement->s())
153             && $self->p()->equal($statement->p())
154             && $self->o()->equal($statement->o())
155             );
156            
157 0         0 return 0;
158             }
159              
160              
161             =item hash( )
162              
163             Generates a hash signature of this statement for comparison purposes internally and wherever
164             the application developer feels appropriate.
165              
166             =cut
167              
168             sub hash {
169 47     47 1 56 my $self = shift;
170              
171 47 100       139 if(!defined($self->{'_hash_value'})) {
172 22         64 $self->{'_hash_value'} = $self->s()->hash() . '-' . $self->p()->hash() . '-' . $self->o()->hash();
173             }
174            
175 47         375 return $self->{'_hash_value'};
176             }
177              
178              
179             sub init {
180 108     108 1 3209 my ($self, $config) = @_;
181            
182 108 50 33     1478 unless(
      33        
183             $config->{'s'}->isa('ODO::Node')
184             && $config->{'p'}->isa('ODO::Node')
185             && $config->{'o'}->isa('ODO::Node')
186             ) {
187              
188 0         0 throw ODO::Exception::Parameter::Invalid(error=> 'All three parameters to constructor must be ODO::Node');
189             }
190            
191 108         387 $self->params($config, qw/s p o/);
192            
193 108         4804 return $self;
194             }
195              
196             =back
197              
198             =cut
199              
200             package ODO::Statement::Virtual;
201              
202 12     12   86 use strict;
  12         25  
  12         420  
203 12     12   73 use warnings;
  12         19  
  12         2026  
204              
205             our @ISA = ( 'ODO::Statement' );
206              
207             =head1 NAME
208              
209             ODO::Statement::Virtual - Encapsulation of an virtual RDF triple pattern.
210              
211             =head1 SYNOPSIS
212             use ODO::Node;
213             use ODO::Statement::Virtual;
214              
215             my $s = ODO::Node::Resource->new('urn:lsid:testuri.org:ns:object:');
216             my $p = ODO::Node::Resource->new('http://testuri.org/predicate');
217             my $o = ODO::Node::Literal->new('literal');
218              
219             my $virtual_1 = ODO::Statement::Virtual->new($s, $p, $o);
220              
221             my $virtual_2 = ODO::Statement::Virtual->new(s=> $s, p=> $p, o=> $o);
222              
223             if($statement_1->equals($statement_2)) {
224             print "\$statement_1 == \$statement_2\n";
225             }
226             else {
227             print "The statements are not equal\n";
228             }
229              
230             =head1 DESCRIPTION
231              
232             These objects are used in the presences of a reasoner to create statements that have
233             been inferred in order to differentiate them from statements that are actually 'in'
234             the graph.
235              
236             =head1 SEE ALSO
237              
238             L, L, L, L
239              
240             =head1 COPYRIGHT
241              
242             Copyright (c) 2004-2006 IBM Corporation.
243              
244             All rights reserved. This program and the accompanying materials
245             are made available under the terms of the Eclipse Public License v1.0
246             which accompanies this distribution, and is available at
247             http://www.eclipse.org/legal/epl-v10.html
248              
249              
250             =cut
251              
252             1;
253              
254             __END__