Core/Schema.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 275 CC 20 SCAR 29.4
CC
20
Cov
0%
doGetAncestors
42.0
doGetAncestorProperties
42.0
doGetSubClasses
20.0
CRAP 19.0
1 #
2 # The contents of this file are subject to the Mozilla Public
3 # License Version 1.1 (the "License"); you may not use this file
4 # except in compliance with the License. You may obtain a copy of
5 # the License at http://www.mozilla.org/MPL/
6 #
7 # Software distributed under the License is distributed on an "AS
8 # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 # implied. See the License for the specific language governing
10 # rights and limitations under the License.
11 #
12 # The Original Code is the RDF::Core module
13 #
14 # The Initial Developer of the Original Code is Ginger Alliance Ltd.
15 # Portions created by Ginger Alliance are
16 # Copyright (C) 2001 Ginger Alliance Ltd.
17 # All Rights Reserved.
18 #
19 # Contributor(s):
20 #
21 # Alternatively, the contents of this file may be used under the
22 # terms of the GNU General Public License Version 2 or later (the
23 # "GPL"), in which case the provisions of the GPL are applicable
24 # instead of those above. If you wish to allow use of your
25 # version of this file only under the terms of the GPL and not to
26 # allow others to use your version of this file under the MPL,
27 # indicate your decision by deleting the provisions above and
28 # replace them with the notice and other provisions required by
29 # the GPL. If you do not delete the provisions above, a recipient
30 # may use your version of this file under either the MPL or the
31 # GPL.
32 #
33  
34 package RDF::Core::Schema;
35  
36 use strict;
37  
38 use RDF::Core::Constants qw(:rdf :rdfs);
39 require RDF::Core::NodeFactory;
40 require RDF::Core::Model;
41  
42 use vars qw(@ISA);
43 @ISA = qw(RDF::Core::Model);
44  
45 =pod
46  
47 =head1 NAME
48  
49 RDF::Core::Schema - The RDF Schema access
50  
51 =head1 SYNOPSIS
52  
53 require RDF::Core::Schema;
54  
55 my $schema = new RDF::Core::Schema();
56 $schema->getClasses;
57  
58 =head1 DESCRIPTION
59  
60 This module provides the basic interface (OO) for RDF Schema manipulation.
61  
62 B<Interface>
63  
64 =over
65  
66 =item * new(Storage=>$storage, Factory => $factory)
67  
68 Construct the object. $storage is the RDF::Core::Storage object that contains schema data. $factory may specify the RDF::Core::Factory to be used.
69  
70 =cut
71  
72 sub new {
73 my ($class, %params) = @_;
74 #die "The Model must be an instance of RDF::Core::Model"
75 # unless $params{Model}->isa("RDF::Core::Model");
76 $class = ref $class || $class;
77 my $self = $class->SUPER::new(%params);
78 $self->{_options}{_factory} =
79 $params{Factory} || new RDF::Core::NodeFactory;
80 return $self;
81 }
82  
83 sub factory {
84 my $self = shift;
85 return $self->{_options}{_factory};
86 }
87  
88 # sub model {
89 # my $self = shift;
90 # return $self->{Model};
91 # }
92  
93 sub _getResource {
94 my ($self, $x) = @_;
95 return (ref $x && $x->isa("RDF::Core::Resource")) ? $x :
96 $self->factory->newResource($x);
97 }
98  
99 =pod
100  
101 =item * getClasses
102  
103 Return all classes defined in the model.
104  
105 =cut
106  
107 sub getClasses {
108 my $self = shift;
109 my $ret = [];
110 my $p = $self->factory->newResource(RDF_TYPE);
111 my $o = $self->factory->newResource(RDFS_CLASS);
112 my $enum = $self->getStmts(undef, $p, $o);
113 my $stmt = $enum->getFirst;
114 while ($stmt) {
115 push @$ret, $stmt->getSubject;
116 $stmt = $enum->getNext;
117 }
118 return $ret;
119 }
120  
121 =pod
122  
123 =item * getSubClasses($class, $deep)
124  
125 Get all subclasses of given $class. $class may be either URI string or
126 RDF::Core::Resource. If $deep is true, inheritance takes a deal.
127  
128 =cut
129  
130 sub doGetSubClasses {
131 #$path is used to prevent a circular reference
132 my ($self, $class, $deep, $ret, $path, $depth) = @_;
133  
134 #check circular ref in subclassing
135 if ($$path{$class->getURI}) {
136 my $u = $class->getURI;
137 die "Circular reference in RDF scheme (sublassing of [$u])";
138 }
139  
140 my $p = $self->factory->newResource(RDFS_SUBCLASS_OF);
141 my $enum = $self->getStmts(undef, $p, $class);
142 my $stmt = $enum->getFirst;
143 while ($stmt) {
144 my $s = $stmt->getSubject;
145 unless (exists $$ret{$s->getURI}) {
146 $$ret{$s->getURI} = [$s, $depth];
147 #recursive processing
148 if ($deep) {
149 $$path{$class->getURI}++;
150 $self->doGetSubClasses($s, $deep, $ret, $path, $depth + 1);
151 $$path{$class->getURI}--;
152 }
153 }
154 $stmt = $enum->getNext;
155 }
156 }
157  
158 sub getSubClasses {
159 my ($self, $class, $deep) = @_;
160 my $aux = {};
161 my $c = $self->_getResource($class);
162 $self->doGetSubClasses($c, $deep, $aux, {});
163 return [map {$$aux{$_}[0]}
164 sort {$$aux{$a}[1] <=> $$aux{$b}[1]} keys %$aux];
165 }
166  
167 =pod
168  
169 =item * getAncestors($class, $deep)
170  
171 Get all ancestors of a given $class. $class may be either URI string
172 or RDF::Core::Resource. If $deep is true, inheritance takes a deal.
173  
174 =cut
175  
176 sub doGetAncestors {
177 #$path is used to prevent a circular reference
178 #if $match is defined, method just searches for given resource
179 #and return immediatelly after $match is found
180 my ($self, $class, $deep, $ret, $path, $match, $depth) = @_;
181  
182 #check circular ref in subclassing
183 if ($$path{$class->getURI}) {
184 my $u = $class->getURI;
185 die "Circular reference in RDF scheme (sublassing of [$u])";
186 }
187  
188 my $p = $self->factory->newResource(RDFS_SUBCLASS_OF);
189 my $enum = $self->getStmts($class, $p, undef);
190 my $stmt = $enum->getFirst;
191 while ($stmt) {
192 my $o = $stmt->getObject;
193 #functon mode
194 return 1 if defined $match && $match->getURI eq $o->getURI;
195 unless (exists $$ret{$o->getURI}) {
196 $$ret{$o->getURI} = [$o, $depth];
197 if ($deep || $match) {
198 #recursive processing
199 $$path{$class->getURI}++;
200 return 1 if $self->doGetAncestors($o, $deep, $ret,
201 $path, $match, $depth + 1);
202 $$path{$class->getURI}--;
203 }
204 }
205 $stmt = $enum->getNext;
206
207 }
208 return 0;
209 }
210  
211 sub getAncestors {
212 my ($self, $class, $deep) = @_;
213 my $aux = {};
214 my $c = $self->_getResource($class);
215 $self->doGetAncestors($c, $deep, $aux, {});
216 return [map {$$aux{$_}[0]}
217 sort {$$aux{$a}[1] <=> $$aux{$b}[1]} keys %$aux];
218 }
219  
220 =pod
221  
222 =item * isSubClassOf($what, $whos)
223  
224 Tells, whether $what is a subless of $whom. Both of parameters may be
225 either of RDF::Core::Resource or URI string.
226  
227 =cut
228  
229 sub isSubClassOf {
230 my ($self, $what, $whos) = @_;
231 my $c1 = $self->_getResource($what);
232 my $c2 = $self->_getResource($whos);
233 return $self->doGetAncestors($c1, 1, {}, {}, $c2);
234 }
235  
236 =pod
237  
238 =item * getSubProperties($property, $deep)
239  
240 Get all subproperties of given $property. $property may be either URI
241 string or RDF::Core::Resource. If $deep is true, inheritance takes a deal.
242  
243 =cut
244  
245 sub doGetSubProperties {
246 #$path is used to prevent a circular reference
247 my ($self, $prop, $deep, $ret, $path, $depth) = @_;
248  
249 #check circular ref in subclassing
250 if ($$path{$prop->getURI}) {
251 my $u = $prop->getURI;
252 die "Circular reference in RDF scheme (property subclassing of [$u])";
253 }
254  
255 my $p = $self->factory->newResource(RDFS_SUBPROPERTY_OF);
256 my $enum = $self->getStmts(undef, $p, $prop);
257 my $stmt = $enum->getFirst;
258 while ($stmt) {
259 my $s = $stmt->getSubject;
260 unless (exists $$ret{$s->getURI}) {
261 $$ret{$s->getURI} = [$s, $depth];
262 if ($deep) {
263 #recursive processing
264 $$path{$prop->getURI}++;
265 $self->doGetSubProperties($s, $deep, $ret, $path, $depth + 1);
266 $$path{$prop->getURI}--;
267 }
268 }
269 $stmt = $enum->getNext;
270 }
271 }
272  
273 sub getSubProperties {
274 my ($self, $prop, $deep) = @_;
275 my $aux = {};
276 my $p = $self->_getResource($prop);
277 $self->doGetSubProperties($p, $deep, $aux, {});
278 return [map {$$aux{$_}[0]}
279 sort {$$aux{$a}[1] <=> $$aux{$b}[1]} keys %$aux];
280 }
281  
282 =pod
283  
284 =item * getAncestorProperties($property, $deep)
285  
286 Get all ancestor properties of given $property. $property may be either URI string or RDF::Core::Resource. If $deep is true, inheritance takes a deal.
287  
288 =cut
289  
290 sub doGetAncestorProperties {
291 #$path is used to prevent a circular reference
292 #if $match is defined, method just searches for given resource
293 #and return immediatelly after $match is found
294 my ($self, $prop, $deep, $ret, $path, $match, $depth) = @_;
295  
296 #check circular ref in subclassing
297 if ($$path{$prop->getURI}) {
298 my $u = $prop->getURI;
299 die "Circular reference in RDF scheme (property subclassing of [$u])";
300 }
301  
302 my $p = $self->factory->newResource(RDFS_SUBPROPERTY_OF);
303 my $enum = $self->getStmts($prop, $p, undef);
304 my $stmt = $enum->getFirst;
305 while ($stmt) {
306 my $o = $stmt->getObject;
307 return 1 if defined $match && $match->getURI eq $o->getURI;
308 unless (exists $$ret{$o->getURI}) {
309 $$ret{$o->getURI} = [$o, $depth];
310 if ($deep || $match) {
311 #recursive processing
312 $$path{$prop->getURI}++;
313 return 1
314 if $self->doGetAncestorProperties($o, $deep, $ret,
315 $path, $match, $depth + 1);
316 $$path{$prop->getURI}--;
317 }
318 }
319 $stmt = $enum->getNext;
320 }
321 return 0;
322 }
323  
324 sub getAncestorProperties {
325 my ($self, $prop, $deep) = @_;
326 my $aux = {};
327 my $c = $self->_getResource($prop);
328 $self->doGetAncestorProperties($c, $deep, $aux, {});
329 return [map {$$aux{$_}[0]}
330 sort {$$aux{$a}[1] <=> $$aux{$b}[1]} keys %$aux];
331 }
332  
333 =pod
334  
335 =item * isSubClassOf($waht, $whos)
336  
337 Tells, whether $what is a subless of $whom. Both of parameters may be
338 either of RDF::Core::Resource or URI string.
339  
340 =cut
341  
342 sub isSubPropertyOf {
343 my ($self, $what, $whos) = @_;
344 my $c1 = $self->_getResource($what);
345 my $c2 = $self->_getResource($whos);
346 return $self->doGetAncestorProperties($c1, 1, {}, {}, $c2);
347 }
348  
349 =pod
350  
351 =item getPropertiesOf($class, $deep)
352  
353 Get all properties of given $class. No inheritance rules are applied,
354 since they are not defined in RDF Schema. $class may be either URI
355 string or RDF::Core::Resource. If $deep is true, inheritance takes a deal.
356  
357 =cut
358  
359 sub getPropertiesOf {
360 my ($self, $class, $deep) = @_;
361  
362 my $c = $self->_getResource($class);
363 my $classes = $deep ? $self->getAncestors($c, 1) : [];
364 push @$classes, $c;
365  
366 my %ret;
367 my $idx;
368 my $p = $self->factory->newResource(RDFS_DOMAIN);
369 foreach my $x (@$classes) {
370 my $enum = $self->getStmts(undef, $p, $x);
371 my $stmt = $enum->getFirst;
372 while ($stmt) {
373 my $uri = $stmt->getSubject->getURI;
374 die "Duplicit property [$uri] for @{[$x->getURI]}"
375 if exists $ret{$uri};
376 $ret{$uri} = [$idx++, $stmt->getSubject];
377 $stmt = $enum->getNext;
378 }
379 }
380  
381 return [map {$ret{$_}[1]} sort {$ret{$a}[0] <=> $ret{$b}[0]} keys %ret];
382 }
383  
384 =pod
385  
386 =back
387  
388 =cut