lib/FAIR/Profile/Parser.pm

filter: Statementstmt 95.9% 166 / 173 Branchbran 54.6% 35 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 50.0% 1 / 2 totaltotal 85.6% 221 / 258 CC 38 SCAR 21.2
CC
38
Cov
85%
parse
14.5
_fillProfile
13.5
_fillProperty
12.4
CRAP 8.3
« blib/lib/FAIR/AccessorBase.pm ↑ Summary lib/FAIR/Base.pm »
1 package FAIR::Profile::Parser;
2 2 $FAIR::Profile::Parser::VERSION = '0.41';
3  
4 # ABSTRACT: Parser that reads FAIR Profile RDF and creates a FAIR::Profile object
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use RDF::Trine::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use RDF::Trine::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use RDF::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use FAIR::NAMESPACES;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use FAIR::Profile;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 #use vars qw /$VERSION/;
18 #$VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37 2 has filename => (
38 is => 'rw',
39 isa => "Str",
40 required => 0,
41 );
42  
43 2 has data => (
44 is => 'rw',
45 isa => "Str",
46 required => 0,
47 );
48  
49 2 has data_format => (
50 is => 'rw',
51 isa => "Str",
52 required => 0,
53 );
54  
55 has model => (
56 is => 'rw',
57 isa => 'RDF::Trine::Model',
58 1 ▶ default => sub { my $store = RDF::Trine::Store::Memory->new();
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 my $model = RDF::Trine::Model->new($store); return $model;},
60 2 );
61  
62 2 has profile => (
63 is => 'rw',
64 isa => 'FAIR::Profile',
65
66 );
67  
68 sub parse {
69
70 1 ▶ my ($self) = @_;
Subroutine parse: called CC=7 SCAR=26.8
Pod documented
71 1 my $filename = $self->filename;
72 1 my $model = $self->model;
73 1 my $data = $self->data;
74 1 ▶ if ($filename) {
Branch: 2 branches
BranchTrueFalseTotal
if ($filename) { } 0 1 1
elsif ($data) { } 1 0 1
75 0 ▶ if ($filename =~ m'^http://') {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ m[^http://]) { } 0 0 0
76 0 my $result = get($filename);
77 0 ▶ die "Nothing could be retrieved from $filename\n" unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
78 0 RDF::Trine::Parser->parse_url_into_model($filename, $model );
79
80 } else {
81
82 0 ▶ die "file $filename does not exist" unless (-e $filename);
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
83 # open(IN, "$filename") || die "can't open input file $!\n";
84 0 RDF::Trine::Parser->parse_file_into_model( "http://datafairport.org/fakepath/", $filename, $model );
85
86 }
87 } elsif ($data){
88 1 ▶ die "you can't pass data without telling me the ->data_format" unless ($self->data_format);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->data_format 0 1 1
89 1 my $parser = RDF::Trine::Parser->new($self->data_format);
90 1 $parser->parse_into_model("http://datafairport.org/fakepath/", $data, $model );
91 }
92
93
94 # $self->model($model); # this appears to be redundant...
95
96 1 my $profile = $self->getProfile();
97 1 return $profile;
98 }
99  
100 sub getProfile {
101 1 ▶ my ($self) = @_;
Subroutine getProfile: called CC=2 SCAR=6.9
Pod undocumented
102 1 my $model = $self->model;
103  
104 1 my $query = RDF::Query->new( "SELECT ?s WHERE {?s a <".FAIR."FAIRProfile>}" );
105 1 my $iterator = $query->execute( $model );
106 1 my $profile;
107 1 ▶ while (my $row = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iterator->next) 1 1 2
108 1 my $URI = $row->{ 's' };
109 1 $profile = $self->_fillProfile($URI);
110 }
111 1 $self->profile($profile);
112 1 return $profile;
113  
114 }
115  
116  
117  
118 sub _fillProfile {
119 1 ▶ my ($self, $URITrine) = @_;
Subroutine _fillProfile: called CC=12 SCAR=26.0
120 #has URI => (
121 #has type => (
122 #has hasClass => (
123 #has label => (
124 #has title => (
125 #has description => (
126 #has license => (
127 #has organization => (
128 #has identifier => (
129 #has schemardfs_URL => (
130  
131 1 my %ns = %FAIR::Base::predicate_namespaces;
132 1 my $prefixes = _generatePrefixHeader();
133 1 my $query = "SELECT ?label ?title ?description ?modified ?license ?issued ?organization ?identifier ?schemardfs_URL ?type
134 WHERE { ";
135 1 my $whereclause = "";
136 1 my $URI = $URITrine->value;
137 1 foreach my $element(qw(label title description modified license issued organization identifier schemardfs_URL type)){
138 10 $whereclause .= "OPTIONAL {<$URI> $ns{$element}:$element ?$element} .\n";
139 }
140 1 $query = $prefixes . $query . $whereclause . "}";
141 #print STDERR $query;
142 1 my $Q = RDF::Query->new( $query );
143 1 my $iterator = $Q->execute( $self->model );
144 1 my $row = $iterator->next; # should only be one!
145
146 1 ▶ my $label = $row->{label}->value if $row->{label};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"label"} 1 0 1
147 1 ▶ my $title = $row->{title}->value if $row->{title};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"title"} 1 0 1
148 1 ▶ my $description = $row->{description}->value if $row->{description};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"description"} 1 0 1
149 1 ▶ my $modified = $row->{modified}->value if $row->{modified};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"modified"} 1 0 1
150 1 ▶ my $license = $row->{license}->value if $row->{license};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"license"} 1 0 1
151 1 ▶ my $issued = $row->{issued}->value if $row->{issued};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"issued"} 1 0 1
152 1 ▶ my $organization = $row->{organization}->value if $row->{organization};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"organization"} 1 0 1
153 1 ▶ my $identifier = $row->{identifier}->value if $row->{identifier};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"identifier"} 1 0 1
154 1 ▶ my $schemardfs_URL = $row->{schemardfs_URL}->value if $row->{schemardfs_URL};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"schemardfs_URL"} 0 1 1
155 1 ▶ my $type = $row->{type}->value if $row->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"type"} 1 0 1
156 1 my $ProfileObject = FAIR::Profile->new(
157 URI => $URI,
158 label => $label,
159 title => $title,
160 description => $description,
161 modified => $modified,
162 license => $license,
163 issued => $issued,
164 organization => $organization,
165 identifier => $identifier,
166 # schemardfs_URL => $schemardfs_URL,
167 );
168
169 1 $self->profile($ProfileObject);
170
171 1 $self->_fillClasses();
172
173 1 return $self->profile();
174
175
176 }
177  
178 sub _fillClasses {
179 1 ▶ my ($self) = @_;
Subroutine _fillClasses: called CC=3 SCAR=11.0
180 1 my $ProfileObject = $self->profile;
181 1 my $model = $self->model;
182 1 my %ns = %FAIR::Base::predicate_namespaces;
183
184 1 my $ProfileURI = $ProfileObject->URI;
185  
186 1 my $prefixes = _generatePrefixHeader();
187
188 1 my $query = RDF::Query->new( "$prefixes
189 SELECT ?c WHERE {?c $ns{provenance}:provenance <$ProfileURI>}" );
190 1 my $iterator = $query->execute( $model );
191 1 my $profile;
192 1 ▶ while (my $row = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iterator->next) 1 1 2
193 1 ▶ next unless $row->{'c'};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"c"} 0 1 1
194 1 my $ClassURI = $row->{ 'c' }->value;
195 1 my $class = $self->_fillClass($ClassURI);
196 1 $ProfileObject->add_Class($class);
197
198 1 $self->_fillProperties($class);
199 }
200
201
202 }
203  
204 sub _fillClass {
205  
206 1 ▶ my ($self, $ClassURI) = @_;
Subroutine _fillClass: called CC=4 SCAR=13.9
207  
208 #label => ['Descriptor Profile Schema Class', 'read'],
209 #class_type => [undef, 'read/write'], # this is a URI to an OWL class or RDFS class
210  
211 1 my $model = $self->model;
212 1 my %ns = %FAIR::Base::predicate_namespaces;
213
214 1 my $prefixes = _generatePrefixHeader();
215 1 my $query = "SELECT ?label ?onClassType
216 WHERE { ";
217 1 my $whereclause = "";
218 1 foreach my $element(qw(label onClassType)){
219 2 $whereclause .= "OPTIONAL {<$ClassURI> $ns{$element}:$element ?$element} .\n";
220 }
221 #print STDERR $whereclause;
222 1 $query = $prefixes . $query . $whereclause . "}";
223 1 my $Q = RDF::Query->new( $query );
224 1 my $iterator = $Q->execute( $self->model );
225 1 my $row = $iterator->next; # should only be one!
226
227 1 ▶ my $label = $row->{label}->value if $row->{label};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"label"} 1 0 1
228 1 ▶ my $class_type = $row->{onClassType}->value if $row->{onClassType};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"onClassType"} 1 0 1
229 1 my $ClassObject = FAIR::Profile::Class->new(
230 URI => $ClassURI,
231 label => $label,
232 onClassType => $class_type,
233 );
234
235 1 return $ClassObject;
236  
237
238 }
239  
240 sub _fillProperties {
241 1 ▶ my ($self, $ClassObject) = @_;
Subroutine _fillProperties: called CC=3 SCAR=11.0
242 1 my $model = $self->model;
243 1 my %ns = %FAIR::Base::predicate_namespaces;
244  
245 1 my $ClassURI = $ClassObject->URI;
246
247 1 my $prefixes = _generatePrefixHeader();
248 1 my $query = RDF::Query->new( "$prefixes
249 SELECT ?p WHERE {<$ClassURI> $ns{hasProperty}:hasProperty ?p}" );
250 1 my $iterator = $query->execute( $model );
251 1 my $profile;
252 1 ▶ while (my $row = $iterator->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iterator->next) 2 1 3
253 2 ▶ next unless $row->{'p'};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"p"} 0 2 2
254 2 my $PropertyURI = $row->{ 'p' }->value;
255 2 my $property = $self->_fillProperty($PropertyURI);
256 2 $ClassObject->add_Property($property);
257 }
258  
259 }
260  
261 sub _fillProperty {
262
263 2 ▶ my ($self, $PropertyURI) = @_;
Subroutine _fillProperty: called CC=12 SCAR=25.2
264  
265 #property_type => [ undef, 'read/write' ], # a URI referring to an ontological predicate
266 #_allowed_values => [undef, 'read/write' ], # this is a list of URL references to either other Profiles, or to SKOS view on an ontology (Jupp et al, 2013)
267 #label => ['Descriptor Profile Schema Property', 'read'],
268 #allow_multiple => ['true', 'read/write'], # can this property appear multiple times?
269  
270  
271 2 my $model = $self->model;
272
273 2 my %ns = %FAIR::Base::predicate_namespaces;
274 2 my $prefixes = _generatePrefixHeader();
275 2 my $query = "SELECT ?label ?onPropertyType ?maxCount ?minCount
276 WHERE { ";
277 2 my $whereclause = "";
278 2 foreach my $element(qw(label onPropertyType maxCount minCount)){
279 8 $whereclause .= "OPTIONAL {<$PropertyURI> $ns{$element}:$element ?$element} .\n";
280 }
281 #print STDERR $whereclause;
282 2 $query = $prefixes . $query . $whereclause . "}";
283 2 my $Q = RDF::Query->new( $query );
284 2 my $iterator = $Q->execute( $self->model );
285 2 my $row = $iterator->next; # should only be one!
286
287 2 ▶ my $label = $row->{label}->value if $row->{label};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"label"} 2 0 2
288 2 ▶ my $property_type = $row->{onPropertyType}->value if $row->{onPropertyType};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"onPropertyType"} 2 0 2
289 2 ▶ my $maxCount = $row->{maxCount}->value if $row->{maxCount};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"maxCount"} 2 0 2
290 2 ▶ my $minCount = $row->{minCount}->value if $row->{minCount};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"minCount"} 1 1 2
291 2 my $PropertyObject = FAIR::Profile::Property->new(
292 URI => $PropertyURI,
293 label => $label,
294 onPropertyType => $property_type,
295 );
296 2 ▶ if (defined $maxCount) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $maxCount) 2 0 2
297 2 $PropertyObject->maxCount($maxCount);
298 }
299 2 ▶ if (defined $minCount) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $minCount) 1 1 2
300 1 $PropertyObject->minCount($minCount);
301 }
302  
303 2 my $query2 = "SELECT ?allowedValues
304 WHERE { ";
305 2 my $whereclause2 = "";
306 2 foreach my $element(qw(allowedValues)){
307 2 $whereclause2 .= "OPTIONAL {<$PropertyURI> $ns{$element}:$element ?$element} .\n";
308 }
309 #print STDERR $whereclause2;
310 2 $query2 = $prefixes . $query2 . $whereclause2 . "}";
311 2 my $Q2 = RDF::Query->new( $query2 );
312 2 my $iterator2 = $Q2->execute( $self->model );
313 2 ▶ while (my $row = $iterator2->next){
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iterator2->next) 2 2 4
314 2 ▶ my $restrictionURI = $row->{allowedValues}->value if $row->{allowedValues};
Branch: 1 branch
BranchTrueFalseTotal
if $row->{"allowedValues"} 0 2 2
315 2 ▶ next unless $restrictionURI;
Branch: 1 branch
BranchTrueFalseTotal
unless $restrictionURI 2 0 2
316 0 $PropertyObject->add_AllowedValue($restrictionURI);
317 }
318
319
320 2 return $PropertyObject;
321  
322
323 }
324  
325 sub _generatePrefixHeader {
326 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
327 6 ▶ my $header = "";
Subroutine _generatePrefixHeader: called CC=2 SCAR=6.9
328 6 foreach my $namespace (qw(DCAT
329 DC
330 DCTYPE
331 FOAF
332 RDF
333 RDFS
334 SKOS
335 VCARD
336 XSD
337 FAIR)) {
338 60 $header = $header . "PREFIX $namespace: <".&$namespace.">\n";
339 }
340 6 return $header
341 }
342  
343 2 1;
344  
345 __END__
346  
347 =pod
348  
349 =encoding UTF-8
350  
351 =head1 NAME
352  
353 FAIR::Profile::Parser - Parser that reads FAIR Profile RDF and creates a FAIR::Profile object
354  
355 =head1 VERSION
356  
357 version 0.41
358  
359 =head1 SYNOPSIS
360  
361 use FAIR::Profile::Parser;
362  
363 my $parser = FAIR::Profile::Parser->new(filename => "./ProfileSchema.rdf");
364 my $DatasetSchema = $parser->parse;
365  
366 my $schema = $DatasetSchema->serialize;
367 open(OUT, ">ProfileSchema2.rdf")
368 print OUT $schema;
369 close OUT;
370  
371 =head1 DESCRIPTION
372  
373 FAIR Profiles describe the metadata elements, and constrained values, that should be
374 associated with a given information entity. They ARE NOT containers for this metadata,
375 they only describe what that metadata should look like (meta-meta-data :-) )
376  
377 This module will parse an RDF file containing a FAIR Profile into
378 objects that can be used to construct a metadata capture interface.
379 The objects will tell you what fields are required/optional, and what possible
380 values they are allowed to contain.
381  
382 =head1 NAME
383  
384 FAIR::Profile::Parser - a module for reading FAIR Profile RDF files
385  
386 =head1 AUTHORS
387  
388 Mark Wilkinson (markw at illuminae dot com)
389  
390 =head1 METHODS
391  
392 =head2 new
393  
394 Title : new
395 Usage : my $ProfileParser = FAIR::Profile::Parser->new();
396 Function: Builds a new FAIR::Profile::Parser
397 Returns : FAIR::Profile::Parser
398 Args : filename => $filename
399 model => $model (an existing RDF::Trine::Model -
400 if you don't supply this it will be created for you)
401  
402 =head2 parse
403  
404 Title : parse
405 Usage : my $ProfileObject = $ProfileParser->parse();
406 Function: parse the file associated with the Parser
407 Returns : FAIR::Profile
408 Args : none
409  
410 =head2 filename
411  
412 Title : filename
413 Usage : $ProfileParser->filename($filename);
414 Function: associate a file with the parser
415 Returns : null
416 Args : full or relative path to the file to be parsed
417  
418 =head2 data
419  
420 Title : data
421 Usage : $ProfileParser->data($rdfdata);
422 Function: associate a data string with the parser
423 Returns : null
424 Args : string of RDF in the format specified in $Parser->data_format
425  
426 =head2 data_format
427  
428 Title : data_format
429 Usage : $ProfileParser->data_format($format);
430 Function: the format of the RDF data in ->data (if any)
431 Returns : null
432 Args : rdfxml | turtle | ntriples | nquads (or any type acceptable to RDF::Trine)
433  
434 =head2 model
435  
436 Title : model
437 Usage : $ProfileParser->model($RDFTrineModel);
438 Function: associate an RDF::Trine::Model with the parser
439 Returns : null
440 Args : RDF::Trine::Model (this will be created for you, if not supplied)
441  
442 =head2 profile
443  
444 Title : profile
445 Usage : $Profile = $ProfileParser->profile;
446 Function: retrieve the profile after a parse. Must parse first!
447 Returns : FAIR::Profile
448 Args : none
449  
450 =head1 AUTHOR
451  
452 Mark Denis Wilkinson (markw [at] illuminae [dot] com)
453  
454 =head1 COPYRIGHT AND LICENSE
455  
456 This software is Copyright (c) 2016 by Mark Denis Wilkinson.
457  
458 This is free software, licensed under:
459  
460 The Apache License, Version 2.0, January 2004
461  
462 =cut
« blib/lib/FAIR/AccessorBase.pm ↑ Summary lib/FAIR/Base.pm »