File Coverage

blib/lib/ODO/Statement/Group.pm
Criterion Covered Total %
statement 18 69 26.0
branch 0 20 0.0
condition n/a
subroutine 6 13 46.1
pod 7 7 100.0
total 31 109 28.4


line stmt bran cond sub pod time code
1             #
2             # Copyright (c) 2005-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/Group.pm,v $
10             # Created by: Stephen Evanchik( evanchik@us.ibm.com )
11             # Created on: 01/18/2005
12             # Revision: $Id: Group.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::Group;
18              
19 3     3   17 use strict;
  3         7  
  3         111  
20 3     3   25 use warnings;
  3         7  
  3         110  
21              
22 3     3   46 use ODO::Exception;
  3         5  
  3         114  
23 3     3   16 use ODO::Node;
  3         5  
  3         102  
24              
25 3     3   15 use base qw/ODO/;
  3         5  
  3         254  
26              
27 3     3   18 use vars qw /$VERSION/;
  3         5  
  3         3495  
28             $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
29              
30             __PACKAGE__->mk_accessors(qw/subject predicates properties/);
31              
32             =head1 NAME
33              
34             ODO::Statement::Group - Group of statements with a common subject.
35              
36             =head1 SYNOPSIS
37              
38             Synopsis.
39              
40             =head1 DESCRIPTION
41              
42             Description.
43              
44             =head1 METHODS
45              
46             =over
47              
48             =item new( [ s=> $subject ] | $subject )
49              
50             Create a new group of statements with the same subject
51              
52             =item add( $subject, $predicate, $object )
53              
54             =cut
55              
56             sub add {
57 0     0 1   my ($self, $subject, $predicate, $object) = @_;
58            
59 0 0         unless($self->subject()->equal($subject)) {
60 0           my $s1 = $self->subject()->value();
61 0           my $s2 = $subject->value();
62            
63 0           throw ODO::Exception::Parameter::Invalid(error=> "Cannot add statements to ODO::Statement::Group with differing subjects: $s1 != $s2");
64             }
65            
66 0 0         throw ODO::Exception::Parameter::Invalid(error=> 'Predicate node must be an ODO::Node::Resource')
67             unless($predicate->isa('ODO::Node::Resource'));
68            
69 0 0         throw ODO::Exception::Parameter::Invalid(error=> 'Object node must be an ODO::Node')
70             unless($object->isa('ODO::Node'));
71            
72             # Properties are keys in the hash, that hold arrays of objects for that property
73 0 0         unless(exists($self->properties()->{ $predicate->hash() })) {
74 0           $self->properties()->{ $predicate->hash() } = [];
75 0           $self->predicates()->{ $predicate->hash() } = $predicate;
76             }
77            
78             # Finally add the property's object to the list
79 0           push @{ $self->properties()->{ $predicate->hash() }}, $object;
  0            
80             }
81              
82             =item delete( )
83              
84             =cut
85              
86             sub delete {
87 0     0 1   my ($self, $subject, $predicate, $object) = @_;
88            
89 0 0         unless($self->subject()->equal($subject)) {
90 0           my $s1 = $self->subject()->value();
91 0           my $s2 = $subject->value();
92              
93 0           throw ODO::Exception::Parameter::Invalid(error=> "Cannot delete statements from ODO::Statement::Group with differing subjects: $s1 != $s2");
94             }
95            
96 0 0         throw ODO::Exception::Parameter::Invalid(error=> 'Predicate node must be an ODO::Node::Resource')
97             unless($predicate->isa('ODO::Node::Resource'));
98            
99 0 0         throw ODO::Exception::Parameter::Invalid(error=> 'Object node must be an ODO::Node')
100             unless($object->isa('ODO::Node'));
101            
102             # FIXME: Finish delete method
103             }
104              
105             =item add_property( $predicate, $object )
106              
107             =cut
108              
109             sub add_property {
110 0     0 1   my $self = shift;
111 0           $self->add($self->subject(), @_);
112             }
113              
114             =item remove_property( $predicate, $object )
115              
116             =cut
117              
118             sub remove_property {
119 0     0 1   my $self = shift;
120 0           $self->delete($self->subject(), @_);
121             }
122              
123             =item merge( $stmt_group )
124              
125             =cut
126              
127             sub merge {
128 0     0 1   my ($self, $stmt_group) = @_;
129            
130 0 0         unless($stmt_group->subject()->equal($self->subject())) {
131 0           my $s1 = $stmt_group->subject()->value();
132 0           my $s2 = $self->subject()->value();
133 0           throw ODO::Exception::Statement(error=> "Subjects of statement groups must match in order to be merged: $s1 != $s2");
134             }
135            
136 0           foreach my $predicateURI (keys( %{ $stmt_group->properties() })) {
  0            
137 0           foreach my $object ($stmt_group->properties()->{ $predicateURI }) {
138 0           $self->add($self->subject(), $stmt_group->predicates()->{ $predicateURI }, $object);
139             }
140             }
141            
142 0           return $self;
143             }
144              
145             =item statements()
146              
147             Convert this group in to an array of statements.
148              
149             =cut
150              
151             sub statements {
152 0     0 1   my $self = shift;
153            
154 0           my $statements = [];
155            
156 0           foreach my $predicate (keys(%{ $self->properties() })) {
  0            
157 0           foreach my $object (@{ $self->properties()->{ $predicate }}) {
  0            
158 0           push @{ $statements }, ODO::Statement->new($self->subject(), $self->predicate()->{ $predicate }, $object);
  0            
159             }
160             }
161            
162 0 0         return (wantarray) ? @{ $statements } : $statements;
  0            
163             }
164              
165              
166             sub init {
167 0     0 1   my ($self, $config) = @_;
168            
169 0           my $subject = $config->{'s'};
170            
171 0           $subject = ODO::Node::Resource->new($subject);
172            
173 0 0         throw ODO::Exception::Parameter::Invalid(error=> 'Subject for a ODO::Statement::Group must be an ODO::Node::Resource')
174             unless($subject->isa('ODO::Node::Resource'));
175            
176 0           $self->subject($subject);
177 0           $self->properties( {} );
178 0           $self->predicates( {} );
179            
180 0           return $self;
181             }
182              
183              
184             =back
185              
186             =head1 AUTHOR
187              
188             IBM Corporation
189              
190             =head1 COPYRIGHT
191              
192             Copyright (c) 2005-2006 IBM Corporation.
193              
194             All rights reserved. This program and the accompanying materials
195             are made available under the terms of the Eclipse Public License v1.0
196             which accompanies this distribution, and is available at
197             http://www.eclipse.org/legal/epl-v10.html
198              
199             =cut
200              
201             1;
202              
203             __END__