LS/RDF/Metadata.pm untested

filter: Statementstmt 0.0% 0 / 125 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 237 CC 14 SCAR 29.8
CC
14
Cov
0%
new
72.0
getInstancesFormat
12.0
lsid
6.0
CRAP 19.8
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::Metadata;
10  
11  
12  
13 package LS::RDF::MetadataDocument;
14  
15 use strict;
16 use warnings;
17  
18 use LS::RDF::Constants;
19 use LS::RDF::ComplexDocument;
20  
21  
22 use base 'LS::RDF::ComplexDocument';
23  
24  
25  
26  
27 #
28 # new( %options ) -
29 #
30 sub new {
31  
32 my $self = shift;
33  
34 my %options = @_;
35  
36 unless(ref $self) {
37  
38 unless($options{'lsid'}) {
39  
40 unless(UNIVERSAL::isa($options{'lsid'}, 'LS::ID')) {
41  
42 $options{'lsid'} = LS::ID->new($options{'lsid'});
43 }
44
45 return undef unless($options{'lsid'});
46 }
47  
48 unless($options{'rdf'}) {
49  
50 #return undef;
51 }
52  
53 require RDF::Core::Model;
54 require RDF::Core::Model::Parser;
55 require RDF::Core::Storage::Memory;
56 require RDF::Core::Evaluator;
57 require RDF::Core::NodeFactory;
58 require RDF::Core::Query;
59  
60 $self = bless {
61  
62 _lsid=> undef,
63
64 _model=> undef,
65 _query=> undef,
66  
67 _err=> undef,
68 }, $self;
69  
70 return undef unless( ($self->{'_model'} = RDF::Core::Model->new(Storage=> RDF::Core::Storage::Memory->new())) );
71  
72 $self->lsid($options{'lsid'});
73  
74 unless($self->parse($options{'rdf'})) {
75  
76 #return undef;
77 }
78 }
79  
80 return $self;
81 }
82  
83  
84 #
85 # lsid( $lsid ) -
86 #
87 sub lsid {
88  
89 my $self = shift;
90  
91 @_ ? $self->{'_lsid'} = shift : return $self->{'_lsid'};
92 }
93  
94  
95 #
96 # parse( $rdf_string ) -
97 #
98 sub parse {
99  
100 my $self = shift;
101
102 my $rdf_string = shift || return;
103  
104  
105 my $rdf_model = $self->{'_model'};
106  
107 my $parser = RDF::Core::Model::Parser->new(
108 Model => $rdf_model,
109 Source => $rdf_string,
110 SourceType => 'string',
111 BaseURI => ${ LS::RDF::Constants::BASE_URI },
112 );
113  
114 eval { $parser->parse(); };
115  
116 if ($@) {
117
118 my $err_string = $@;
119 $err_string =~ s/^\s+|\s+$//g;
120  
121 $self->{'_err'} = $err_string;
122 return undef;
123 }
124
125 my $evaluator = RDF::Core::Evaluator->new(
126 Model=> $rdf_model,
127 Factory=> RDF::Core::NodeFactory->new,
128 Namespaces=> {
129 rdf=> 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
130 biopathwaysp=> 'urn:lsid:lsid.biopathways.org:predicates:',
131 dc=> 'http://purl.org/dc/elements/1.1/#',
132 }
133 );
134  
135 $self->{'_query'} = RDF::Core::Query->new(Evaluator=> $evaluator);
136
137 $self->{'_err'} = undef;
138 }
139  
140  
141 #
142 # query( $query_string ) -
143 #
144 sub query {
145  
146 my $self = shift;
147 my ($query_string) = @_;
148
149 unless ($self->{'_model'} &&
150 $self->{'_query'}) {
151  
152 return undef;
153 }
154  
155 my $rs = $self->{'_query'}->query($query_string);
156
157 return $rs;
158 }
159  
160  
161 sub statements {
162  
163 my $self = shift;
164  
165 if($self->SUPER::statements()) {
166  
167 return $self->SUPER::statements();
168 }
169 else {
170  
171 return $self->{'_model'}->getStmts();
172 }
173  
174 return undef;
175 }
176  
177  
178 #
179 # getTypes -
180 #
181 sub getTypes {
182  
183 my $self = shift;
184  
185 my $rs = $self->query('Select ?type from [' . $self->lsid() . ']->rdf:type{?type}') || return;
186
187 my @types;
188  
189 foreach my $row (@$rs) {
190 my $resource = $row->[0] || next;
191
192 push(@types, $resource->getURI());
193 }
194  
195 $self->{'_err'} = undef;
196  
197 return \@types;
198 }
199  
200  
201 #
202 # getTitle -
203 #
204 sub getTitle {
205  
206 my $self = shift;
207  
208 my $rs = $self->query('Select ?title from [' . $self->lsid() . ']->dc:title{?title}') || return;
209  
210 my $row = $rs->[0] || return;
211 my $resource = $row->[0] || return;
212  
213 $self->{'_err'} = undef;
214  
215 return $resource->getValue();
216 }
217  
218  
219 #
220 # getFormat -
221 #
222 sub getFormat {
223  
224 my $self = shift;
225  
226 my $rs = $self->query('Select ?format from [' . $self->lsid() . ']->dc:format{?format}') || return;
227  
228 my $row = $rs->[0] || return;
229 my $resource = $row->[0] || return;
230  
231 $self->{'_err'} = undef;
232  
233 return $resource->getURI();
234 }
235  
236  
237 #
238 # getInstances -
239 #
240 sub getInstances {
241  
242 my $self = shift;
243  
244 my $rs = $self->query('Select ?instance, ?format from [' . $self->lsid() . ']->i3cp:storedas{?instance}->dc:format{?format}') || return;
245
246 my @lsids;
247  
248 foreach my $row (@$rs) {
249 my $resource = $row->[0] || next;
250 my $format = $row->[1];
251
252 push(@lsids, [$resource->getURI(), $format->getURI()]);
253 }
254  
255 $self->{'_err'} = undef;
256
257 return \@lsids;
258 }
259  
260  
261 #
262 # getAbstract -
263 #
264 sub getAbstract {
265  
266 my $self = shift;
267  
268 my $rs = $self->query('Select ?abstract from ?abstract->i3cp:storedas=>[' . $self->lsid() . ']') || return;
269  
270 my $row = $rs->[0] || return;
271 my $resource = $row->[0] || return;
272  
273 $self->{'_err'} = undef;
274  
275 return $resource->getURI();
276 }
277  
278  
279 #
280 # getInstanceFormat -
281 #
282 sub getInstancesFormat {
283  
284 my $self = shift;
285 my ($format) = @_;
286  
287 $format = LS::ID->new($format) unless ref $format;
288
289 if (!$format) {
290 $self->{'_err'} = "Invalid format";
291 return;
292 }
293
294 my $rs = $self->query('Select ?instance from [' . $self->lsid() . "]->i3cp:storedas{?instance}->dc:format=>[" . $format->canonical. "]") || return;
295
296 my @lsids;
297  
298 foreach my $row (@$rs) {
299 my $resource = $row->[0] || next;
300
301 push(@lsids, $resource->getURI());
302 }
303  
304 $self->{'_err'} = undef;
305
306 return \@lsids;
307 }
308  
309 1;
310  
311 __END__