LS/RDF/ComplexDocument.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 157 CC 9 SCAR 22.4
CC
9
Cov
0%
new
30.0
createBag
12.0
new
6.0
CRAP 9.4
1 # =====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::RDF::Complex;
10  
11  
12 package LS::RDF::ComplexDocument;
13  
14 use strict;
15 use warnings;
16  
17  
18 use LS::RDF::Constants;
19 use LS::RDF::SimpleDocument;
20  
21  
22 use base 'LS::RDF::SimpleDocument';
23  
24  
25 #
26 # new( %options ) -
27 #
28 sub new {
29  
30 my $self = shift;
31  
32 unless (ref $self) {
33  
34 my $class = $self->SUPER::new(@_);
35
36 $self = bless $class, $self;
37 }
38
39 return $self;
40 }
41  
42  
43 #
44 # addBlankNode( $subject, $predicate ) -
45 #
46 sub addBlankNode {
47  
48 my $self = shift;
49  
50 my ($_sub, $_pred) = @_;
51  
52 my $subject = $self->{'_factory'}->newResource($_sub);
53 my $predicate = $subject->new($_pred);
54 my $object = $self->{'_factory'}->newResource();
55  
56 $self->_create_statement($subject, $predicate, $object);
57  
58 return $object->getURI();
59 }
60  
61  
62 #
63 # createBag( ) - Creates an RDF Bag
64 #
65 sub createBag {
66  
67 my $self = shift;
68  
69 my $elementFactory = ( $self->{'_bagElementFactory'} ||
70 RDF::Core::NodeFactory->new(BaseURI=> ${ LS::RDF::Constants::BASE_URI },
71 GenPrefix=> '_:element')
72 );
73  
74 $self->{'_bagElementFactory'} = $elementFactory unless($self->{'_bagElementFactory'});
75  
76 my $predicateFactory = ( $self->{'_bagPredicateFactory'} ||
77 RDF::Core::NodeFactory->new(BaseURI=> ${ LS::RDF::Constants::BASE_URI },
78 GenCounter=> 1,
79 GenPrefix=> 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_')
80 );
81  
82 $self->{'_bagPredicateFactory'} = $predicateFactory unless($self->{'_bagPredicateFactory'});
83  
84 my $bag = LS::RDF::Bag->new(factory=> $self->{'_factory'},
85 elementFactory=> $elementFactory,
86 predicateFactory=> $predicateFactory);
87  
88 return $bag;
89  
90 }
91  
92  
93 #
94 # addBag( $subject, $predicate, $bag ) -
95 #
96 sub addBag {
97  
98 my $self = shift;
99 my $subject = shift;
100 my $predicate = shift;
101  
102 my $bag = shift;
103  
104 unless(UNIVERSAL::isa($bag, 'LS::RDF::Bag')) {
105  
106 # FIXME: Carp
107 return undef;
108 }
109  
110 $self->addTripleResource($subject, $predicate, $bag->bagID());
111  
112 foreach my $stmt ( @{ $bag->statements() } ) {
113  
114 push @{ $self->{'_statements'} }, $stmt;
115 }
116 }
117  
118  
119 package LS::RDF::Bag;
120  
121 use strict;
122 use warnings;
123  
124 use vars qw( @ISA );
125  
126 @ISA = ( 'LS::RDF::ComplexDocument' );
127  
128  
129 #
130 # new( %options ) -
131 #
132 sub new {
133  
134 my $self = shift;
135 my (%options) = @_;
136  
137 my $elementFactory;
138 unless( ($elementFactory = $options{'elementFactory'}) ) {
139  
140 $elementFactory = RDF::Core::NodeFactory->new(BaseURI=> ${ LS::RDF::Constants::BASE_URI },
141 GenPrefix=> '_:element');
142 }
143  
144 my $predicateFactory;
145 unless( ($predicateFactory = $options{'predicateFactory'}) ) {
146  
147 $predicateFactory = RDF::Core::NodeFactory->new(BaseURI=> ${ LS::RDF::Constants::BASE_URI },
148 GenCounter=> 1,
149 GenPrefix=> 'http://www.w3.org/1999/02/22-rdf-syntax-ns#_');
150 }
151  
152 delete($options{'elementFactory'});
153 delete($options{'predicateFactory'});
154  
155 my $factory;
156 unless( ($factory = $options{'factory'}) ) {
157  
158 $factory = RDF::Core::NodeFactory->new(BaseURI=> ${ LS::RDF::Constants::BASE_URI },
159 GenPrefix=> '_:lsid');
160 }
161  
162 unless(ref $self eq 'LS::RDF::Bag') {
163  
164 my $class = $self->SUPER::new(@_);
165  
166 $self = bless $class, $self;
167  
168  
169 # Record the node factories
170 $self->{'_factory'} = $factory;
171 $self->{'_elementFactory'} = $elementFactory;
172 $self->{'_predicateFactory'} = $predicateFactory;
173  
174 # The main bag node is a blank node
175 $self->{'_bagResource'} = $elementFactory->newResource();
176 $self->{'_bagID'} = $self->{'_bagResource'}->getURI();
177  
178 # Add the rdf:type to the bag
179 $self->_create_statement($self->{'_bagResource'},
180 $self->{'_bagResource'}->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
181 $factory->newResource('http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag')
182 );
183 }
184  
185  
186
187 return $self;
188 }
189  
190  
191 #
192 # bagID( ) - Returns the bag's subject ID
193 #
194 sub bagID {
195  
196 my $self = shift;
197 return $self->{'_bagID'};
198 }
199  
200  
201 #
202 # addBlankElement( ) -
203 #
204 sub addBlankElement {
205  
206 my $self = shift;
207  
208 my $factory = $self->{'_elementFactory'};
209 my $predFactory = $self->{'_predicateFactory'};
210  
211 my $object = $factory->newResource;
212  
213 $self->_create_statement($self->{'_bagResource'},
214 $predFactory->newResource(),
215 $object
216 );
217  
218 return $object->getURI();
219 }
220  
221  
222 #
223 # addElementResource( $element ) - Adds an element to the bag as a resource
224 #
225 sub addElementResource {
226  
227 my $self = shift;
228 my $element = shift;
229  
230 my $factory = $self->{'_elementFactory'};
231 my $predFactory = $self->{'_predicateFactory'};
232  
233 $self->_create_statement($self->{'_bagResource'},
234 $predFactory->newResource,
235 $factory->newResource($element)
236 );
237 }
238  
239  
240 #
241 # addElementLiteral( $element ) -
242 #
243 sub addElementLiteral {
244  
245 my $self = shift;
246 my $element = shift;
247  
248 my $factory = $self->{'_elementFactory'};
249 my $predFactory = $self->{'_predicateFactory'};
250  
251 $self->_create_statement($self->{'_bagResource'},
252 $predFactory->newResource(),
253 $factory->newLiteral($element)
254 );
255 }
256  
257  
258  
259 1;
260  
261 __END__