| 1 | package FAIR::Profile::Parser; | ||
| 2 | $FAIR::Profile::Parser::VERSION = '0.41'; | ||
| 3 | |||
| 4 | # ABSTRACT: Parser that reads FAIR Profile RDF and creates a FAIR::Profile object | ||
| 5 | |||
| 6 | use Moose; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use Carp; | ||
| 10 | use RDF::Trine::Parser; | ||
| 11 | use RDF::Trine::Model; | ||
| 12 | use RDF::Query; | ||
| 13 | use LWP::Simple; | ||
| 14 | use FAIR::NAMESPACES; | ||
| 15 | use FAIR::Profile; | ||
| 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 | has filename => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => "Str", | ||
| 40 | required => 0, | ||
| 41 | ); | ||
| 42 | |||
| 43 | has data => ( | ||
| 44 | is => 'rw', | ||
| 45 | isa => "Str", | ||
| 46 | required => 0, | ||
| 47 | ); | ||
| 48 | |||
| 49 | 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 | default => sub { my $store = RDF::Trine::Store::Memory->new(); | ||
| 59 | my $model = RDF::Trine::Model->new($store); return $model;}, | ||
| 60 | ); | ||
| 61 | |||
| 62 | has profile => ( | ||
| 63 | is => 'rw', | ||
| 64 | isa => 'FAIR::Profile', | ||
| 65 | |||
| 66 | ); | ||
| 67 | |||
| 68 | sub parse { | ||
| 69 | |||
| 70 | my ($self) = @_; | ||
| 71 | my $filename = $self->filename; | ||
| 72 | my $model = $self->model; | ||
| 73 | my $data = $self->data; | ||
| 74 | if ($filename) { | ||
| 75 | if ($filename =~ m'^http://') { | ||
| 76 | my $result = get($filename); | ||
| 77 | die "Nothing could be retrieved from $filename\n" unless $result; | ||
| 78 | RDF::Trine::Parser->parse_url_into_model($filename, $model ); | ||
| 79 | |||
| 80 | } else { | ||
| 81 | |||
| 82 | die "file $filename does not exist" unless (-e $filename); | ||
| 83 | # open(IN, "$filename") || die "can't open input file $!\n"; | ||
| 84 | RDF::Trine::Parser->parse_file_into_model( "http://datafairport.org/fakepath/", $filename, $model ); | ||
| 85 | |||
| 86 | } | ||
| 87 | } elsif ($data){ | ||
| 88 | die "you can't pass data without telling me the ->data_format" unless ($self->data_format); | ||
| 89 | my $parser = RDF::Trine::Parser->new($self->data_format); | ||
| 90 | $parser->parse_into_model("http://datafairport.org/fakepath/", $data, $model ); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | # $self->model($model); # this appears to be redundant... | ||
| 95 | |||
| 96 | my $profile = $self->getProfile(); | ||
| 97 | return $profile; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub getProfile { | ||
| 101 | my ($self) = @_; | ||
| 102 | my $model = $self->model; | ||
| 103 | |||
| 104 | my $query = RDF::Query->new( "SELECT ?s WHERE {?s a <".FAIR."FAIRProfile>}" ); | ||
| 105 | my $iterator = $query->execute( $model ); | ||
| 106 | my $profile; | ||
| 107 | while (my $row = $iterator->next) { | ||
| 108 | my $URI = $row->{ 's' }; | ||
| 109 | $profile = $self->_fillProfile($URI); | ||
| 110 | } | ||
| 111 | $self->profile($profile); | ||
| 112 | return $profile; | ||
| 113 | |||
| 114 | } | ||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | sub _fillProfile { | ||
| 119 | my ($self, $URITrine) = @_; | ||
| 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 | my %ns = %FAIR::Base::predicate_namespaces; | ||
| 132 | my $prefixes = _generatePrefixHeader(); | ||
| 133 | my $query = "SELECT ?label ?title ?description ?modified ?license ?issued ?organization ?identifier ?schemardfs_URL ?type | ||
| 134 | WHERE { "; | ||
| 135 | my $whereclause = ""; | ||
| 136 | my $URI = $URITrine->value; | ||
| 137 | foreach my $element(qw(label title description modified license issued organization identifier schemardfs_URL type)){ | ||
| 138 | $whereclause .= "OPTIONAL {<$URI> $ns{$element}:$element ?$element} .\n"; | ||
| 139 | } | ||
| 140 | $query = $prefixes . $query . $whereclause . "}"; | ||
| 141 | #print STDERR $query; | ||
| 142 | my $Q = RDF::Query->new( $query ); | ||
| 143 | my $iterator = $Q->execute( $self->model ); | ||
| 144 | my $row = $iterator->next; # should only be one! | ||
| 145 | |||
| 146 | my $label = $row->{label}->value if $row->{label}; | ||
| 147 | my $title = $row->{title}->value if $row->{title}; | ||
| 148 | my $description = $row->{description}->value if $row->{description}; | ||
| 149 | my $modified = $row->{modified}->value if $row->{modified}; | ||
| 150 | my $license = $row->{license}->value if $row->{license}; | ||
| 151 | my $issued = $row->{issued}->value if $row->{issued}; | ||
| 152 | my $organization = $row->{organization}->value if $row->{organization}; | ||
| 153 | my $identifier = $row->{identifier}->value if $row->{identifier}; | ||
| 154 | my $schemardfs_URL = $row->{schemardfs_URL}->value if $row->{schemardfs_URL}; | ||
| 155 | my $type = $row->{type}->value if $row->{type}; | ||
| 156 | 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 | $self->profile($ProfileObject); | ||
| 170 | |||
| 171 | $self->_fillClasses(); | ||
| 172 | |||
| 173 | return $self->profile(); | ||
| 174 | |||
| 175 | |||
| 176 | } | ||
| 177 | |||
| 178 | sub _fillClasses { | ||
| 179 | my ($self) = @_; | ||
| 180 | my $ProfileObject = $self->profile; | ||
| 181 | my $model = $self->model; | ||
| 182 | my %ns = %FAIR::Base::predicate_namespaces; | ||
| 183 | |||
| 184 | my $ProfileURI = $ProfileObject->URI; | ||
| 185 | |||
| 186 | my $prefixes = _generatePrefixHeader(); | ||
| 187 | |||
| 188 | my $query = RDF::Query->new( "$prefixes | ||
| 189 | SELECT ?c WHERE {?c $ns{provenance}:provenance <$ProfileURI>}" ); | ||
| 190 | my $iterator = $query->execute( $model ); | ||
| 191 | my $profile; | ||
| 192 | while (my $row = $iterator->next) { | ||
| 193 | next unless $row->{'c'}; | ||
| 194 | my $ClassURI = $row->{ 'c' }->value; | ||
| 195 | my $class = $self->_fillClass($ClassURI); | ||
| 196 | $ProfileObject->add_Class($class); | ||
| 197 | |||
| 198 | $self->_fillProperties($class); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | sub _fillClass { | ||
| 205 | |||
| 206 | my ($self, $ClassURI) = @_; | ||
| 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 | my $model = $self->model; | ||
| 212 | my %ns = %FAIR::Base::predicate_namespaces; | ||
| 213 | |||
| 214 | my $prefixes = _generatePrefixHeader(); | ||
| 215 | my $query = "SELECT ?label ?onClassType | ||
| 216 | WHERE { "; | ||
| 217 | my $whereclause = ""; | ||
| 218 | foreach my $element(qw(label onClassType)){ | ||
| 219 | $whereclause .= "OPTIONAL {<$ClassURI> $ns{$element}:$element ?$element} .\n"; | ||
| 220 | } | ||
| 221 | #print STDERR $whereclause; | ||
| 222 | $query = $prefixes . $query . $whereclause . "}"; | ||
| 223 | my $Q = RDF::Query->new( $query ); | ||
| 224 | my $iterator = $Q->execute( $self->model ); | ||
| 225 | my $row = $iterator->next; # should only be one! | ||
| 226 | |||
| 227 | my $label = $row->{label}->value if $row->{label}; | ||
| 228 | my $class_type = $row->{onClassType}->value if $row->{onClassType}; | ||
| 229 | my $ClassObject = FAIR::Profile::Class->new( | ||
| 230 | URI => $ClassURI, | ||
| 231 | label => $label, | ||
| 232 | onClassType => $class_type, | ||
| 233 | ); | ||
| 234 | |||
| 235 | return $ClassObject; | ||
| 236 | |||
| 237 | |||
| 238 | } | ||
| 239 | |||
| 240 | sub _fillProperties { | ||
| 241 | my ($self, $ClassObject) = @_; | ||
| 242 | my $model = $self->model; | ||
| 243 | my %ns = %FAIR::Base::predicate_namespaces; | ||
| 244 | |||
| 245 | my $ClassURI = $ClassObject->URI; | ||
| 246 | |||
| 247 | my $prefixes = _generatePrefixHeader(); | ||
| 248 | my $query = RDF::Query->new( "$prefixes | ||
| 249 | SELECT ?p WHERE {<$ClassURI> $ns{hasProperty}:hasProperty ?p}" ); | ||
| 250 | my $iterator = $query->execute( $model ); | ||
| 251 | my $profile; | ||
| 252 | while (my $row = $iterator->next) { | ||
| 253 | next unless $row->{'p'}; | ||
| 254 | my $PropertyURI = $row->{ 'p' }->value; | ||
| 255 | my $property = $self->_fillProperty($PropertyURI); | ||
| 256 | $ClassObject->add_Property($property); | ||
| 257 | } | ||
| 258 | |||
| 259 | } | ||
| 260 | |||
| 261 | sub _fillProperty { | ||
| 262 | |||
| 263 | my ($self, $PropertyURI) = @_; | ||
| 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 | my $model = $self->model; | ||
| 272 | |||
| 273 | my %ns = %FAIR::Base::predicate_namespaces; | ||
| 274 | my $prefixes = _generatePrefixHeader(); | ||
| 275 | my $query = "SELECT ?label ?onPropertyType ?maxCount ?minCount | ||
| 276 | WHERE { "; | ||
| 277 | my $whereclause = ""; | ||
| 278 | foreach my $element(qw(label onPropertyType maxCount minCount)){ | ||
| 279 | $whereclause .= "OPTIONAL {<$PropertyURI> $ns{$element}:$element ?$element} .\n"; | ||
| 280 | } | ||
| 281 | #print STDERR $whereclause; | ||
| 282 | $query = $prefixes . $query . $whereclause . "}"; | ||
| 283 | my $Q = RDF::Query->new( $query ); | ||
| 284 | my $iterator = $Q->execute( $self->model ); | ||
| 285 | my $row = $iterator->next; # should only be one! | ||
| 286 | |||
| 287 | my $label = $row->{label}->value if $row->{label}; | ||
| 288 | my $property_type = $row->{onPropertyType}->value if $row->{onPropertyType}; | ||
| 289 | my $maxCount = $row->{maxCount}->value if $row->{maxCount}; | ||
| 290 | my $minCount = $row->{minCount}->value if $row->{minCount}; | ||
| 291 | my $PropertyObject = FAIR::Profile::Property->new( | ||
| 292 | URI => $PropertyURI, | ||
| 293 | label => $label, | ||
| 294 | onPropertyType => $property_type, | ||
| 295 | ); | ||
| 296 | if (defined $maxCount) { | ||
| 297 | $PropertyObject->maxCount($maxCount); | ||
| 298 | } | ||
| 299 | if (defined $minCount) { | ||
| 300 | $PropertyObject->minCount($minCount); | ||
| 301 | } | ||
| 302 | |||
| 303 | my $query2 = "SELECT ?allowedValues | ||
| 304 | WHERE { "; | ||
| 305 | my $whereclause2 = ""; | ||
| 306 | foreach my $element(qw(allowedValues)){ | ||
| 307 | $whereclause2 .= "OPTIONAL {<$PropertyURI> $ns{$element}:$element ?$element} .\n"; | ||
| 308 | } | ||
| 309 | #print STDERR $whereclause2; | ||
| 310 | $query2 = $prefixes . $query2 . $whereclause2 . "}"; | ||
| 311 | my $Q2 = RDF::Query->new( $query2 ); | ||
| 312 | my $iterator2 = $Q2->execute( $self->model ); | ||
| 313 | while (my $row = $iterator2->next){ | ||
| 314 | my $restrictionURI = $row->{allowedValues}->value if $row->{allowedValues}; | ||
| 315 | next unless $restrictionURI; | ||
| 316 | $PropertyObject->add_AllowedValue($restrictionURI); | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | return $PropertyObject; | ||
| 321 | |||
| 322 | |||
| 323 | } | ||
| 324 | |||
| 325 | sub _generatePrefixHeader { | ||
| 326 | no strict "refs"; | ||
| 327 | my $header = ""; | ||
| 328 | foreach my $namespace (qw(DCAT | ||
| 329 | DC | ||
| 330 | DCTYPE | ||
| 331 | FOAF | ||
| 332 | RDF | ||
| 333 | RDFS | ||
| 334 | SKOS | ||
| 335 | VCARD | ||
| 336 | XSD | ||
| 337 | FAIR)) { | ||
| 338 | $header = $header . "PREFIX $namespace: <".&$namespace.">\n"; | ||
| 339 | } | ||
| 340 | return $header | ||
| 341 | } | ||
| 342 | |||
| 343 | 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 |