| 1 | package FAIR::Base; | ||||||||||||||||||||||||||||||||
| 2 | 6 | $FAIR::Base::VERSION = '0.41'; | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | # ABSTRACT: libraries for creating and parsing FAIR Data Profiles (see http://datafairport.org for more details) | ||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use RDF::NS '20131205'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use RDF::Trine::Store::Memory; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use RDF::Trine::Model; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use RDF::Trine::Namespace; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use RDF::Trine::Statement; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use RDF::Trine::Node::Resource; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use RDF::Trine::Node::Literal; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 18 | 6 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 19 | 6 | our @EXPORT = qw(statement); | |||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 6 | ▶ | use FAIR::NAMESPACES; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 6 | our %predicate_namespaces = qw{ | |||||||||||||||||||||||||||||||
| 24 | type RDF | ||||||||||||||||||||||||||||||||
| 25 | title DC | ||||||||||||||||||||||||||||||||
| 26 | description DC | ||||||||||||||||||||||||||||||||
| 27 | issued DC | ||||||||||||||||||||||||||||||||
| 28 | modified DC | ||||||||||||||||||||||||||||||||
| 29 | identifier DC | ||||||||||||||||||||||||||||||||
| 30 | keyword DC | ||||||||||||||||||||||||||||||||
| 31 | language DC | ||||||||||||||||||||||||||||||||
| 32 | contactPoint DC | ||||||||||||||||||||||||||||||||
| 33 | temporal DC | ||||||||||||||||||||||||||||||||
| 34 | spatial DC | ||||||||||||||||||||||||||||||||
| 35 | accrualPeriodicity DC | ||||||||||||||||||||||||||||||||
| 36 | landingPage DCAT | ||||||||||||||||||||||||||||||||
| 37 | license DC | ||||||||||||||||||||||||||||||||
| 38 | rights DC | ||||||||||||||||||||||||||||||||
| 39 | accessURL DCAT | ||||||||||||||||||||||||||||||||
| 40 | downloadURL DCAT | ||||||||||||||||||||||||||||||||
| 41 | mediaType DCAT | ||||||||||||||||||||||||||||||||
| 42 | format DC | ||||||||||||||||||||||||||||||||
| 43 | byteSize DCAT | ||||||||||||||||||||||||||||||||
| 44 | homepage FOAF | ||||||||||||||||||||||||||||||||
| 45 | publisher DC | ||||||||||||||||||||||||||||||||
| 46 | theme DCAT | ||||||||||||||||||||||||||||||||
| 47 | inScheme SKOS | ||||||||||||||||||||||||||||||||
| 48 | themeTaxonomy DCAT | ||||||||||||||||||||||||||||||||
| 49 | dataset DCAT | ||||||||||||||||||||||||||||||||
| 50 | record DCAT | ||||||||||||||||||||||||||||||||
| 51 | distribution DCAT | ||||||||||||||||||||||||||||||||
| 52 | primaryTopic FOAF | ||||||||||||||||||||||||||||||||
| 53 | provenance DC | ||||||||||||||||||||||||||||||||
| 54 | ProvenanceStatement DC | ||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||
| 56 | label RDFS | ||||||||||||||||||||||||||||||||
| 57 | organization DC | ||||||||||||||||||||||||||||||||
| 58 | hasClass FAIR | ||||||||||||||||||||||||||||||||
| 59 | hasProperty FAIR | ||||||||||||||||||||||||||||||||
| 60 | onClassType FAIR | ||||||||||||||||||||||||||||||||
| 61 | onPropertyType FAIR | ||||||||||||||||||||||||||||||||
| 62 | allowedValues FAIR | ||||||||||||||||||||||||||||||||
| 63 | maxCount FAIR | ||||||||||||||||||||||||||||||||
| 64 | minCount FAIR | ||||||||||||||||||||||||||||||||
| 65 | FAIRClass FAIR | ||||||||||||||||||||||||||||||||
| 66 | FAIRProperty FAIR | ||||||||||||||||||||||||||||||||
| 67 | FAIRProfile FAIR | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | schemardfs_URL FAIR | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | sub _toTriples { | ||||||||||||||||||||||||||||||||
| 75 | 20 | ▶ | my ($self, $model) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_toTriples: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||||||
| 76 | 20 | ▶ | unless ($model){ # this is a recursive sub, so sometimes the preexisting model is passed in to be filled | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 5 | my $store = RDF::Trine::Store::Memory->new(); | |||||||||||||||||||||||||||||||
| 78 | 5 | $model = RDF::Trine::Model->new($store); | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | 20 | my %namespaces; | |||||||||||||||||||||||||||||||
| 81 | 20 | my $dct = RDF::Trine::Namespace->new( DC); # from shared exported constants in NAMESPACES.pm | |||||||||||||||||||||||||||||||
| 82 | 20 | $namespaces{DC} = $dct; | |||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||
| 84 | 20 | my $dcat = RDF::Trine::Namespace->new( DCAT); | |||||||||||||||||||||||||||||||
| 85 | 20 | $namespaces{DCAT} = $dcat; | |||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||
| 87 | 20 | my $skos = RDF::Trine::Namespace->new( SKOS); | |||||||||||||||||||||||||||||||
| 88 | 20 | $namespaces{SKOS} = $skos; | |||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||
| 90 | 20 | my $foaf = RDF::Trine::Namespace->new( FOAF); | |||||||||||||||||||||||||||||||
| 91 | 20 | $namespaces{FOAF} = $foaf; | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 20 | my $rdfs = RDF::Trine::Namespace->new( RDFS); | |||||||||||||||||||||||||||||||
| 94 | 20 | $namespaces{RDFS} = $rdfs; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 20 | my $rdf = RDF::Trine::Namespace->new( RDF); | |||||||||||||||||||||||||||||||
| 97 | 20 | $namespaces{RDF} = $rdf; | |||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 20 | my $fair = RDF::Trine::Namespace->new( FAIR); | |||||||||||||||||||||||||||||||
| 100 | 20 | $namespaces{FAIR} = $fair; | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | # now go through all of the properties of that subject to begin constructing the triples | ||||||||||||||||||||||||||||||||
| 104 | 20 | my %attributes; | |||||||||||||||||||||||||||||||
| 105 | 20 | map {$attributes{$_} = 1} $self->meta->get_attribute_list; | |||||||||||||||||||||||||||||||
| 106 | 20 | my $sub = $self->URI; # the subject of the triples | |||||||||||||||||||||||||||||||
| 107 | 20 | delete $attributes{'URI'}; # this attribute we have taken care of | |||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 20 | my $types = $self->type; | |||||||||||||||||||||||||||||||
| 110 | 20 | foreach my $type(@$types){ | |||||||||||||||||||||||||||||||
| 111 | 25 | my $stm = statement($sub, RDF."type", $type); | |||||||||||||||||||||||||||||||
| 112 | 25 | $model->add_statement($stm); | |||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||
| 114 | 20 | delete $attributes{'type'}; # now we've taken care of that one! | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | # now process the rest - serialize the property if it is marked as "serializable"; | ||||||||||||||||||||||||||||||||
| 118 | 20 | foreach my $attributename(keys %attributes){ | |||||||||||||||||||||||||||||||
| 119 | 115 | my $attribute = $self->meta->get_attribute($attributename); | |||||||||||||||||||||||||||||||
| 120 | 115 | ▶ | next unless $attribute->does('Serializable'); # if this isn't a serializable property, skip it | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 121 | 115 | my $predicate = $attribute->name; # the FAIR Moose object predicate names are identical to the OWL/Schema predicate names, | |||||||||||||||||||||||||||||||
| 122 | 115 | my $reader = $attribute->get_read_method; # in case there is a specific reader subroutine associated with the property | |||||||||||||||||||||||||||||||
| 123 | 115 | my $values = $self->$reader; # call the subroutine. All return a list-ref; sometimes its a list of DCAT objects, sometimes a listref of strings | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 115 | ▶ | unless (ref($values) ~~ /ARRAY/){ # some properties return listrefs, others return just a string or an object | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 110 | $values = [$values] # so force it to be a listref before we iterate over the return value | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | 115 | foreach my $object(@$values){ | |||||||||||||||||||||||||||||||
| 129 | #print STDERR $object, "\n"; | ||||||||||||||||||||||||||||||||
| 130 | 120 | ▶ | next unless ($object); # might be undef | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | 105 | ▶ | if ((ref($object) ~~ /FAIR/) && $object->can('_toTriples')) { # is it a FAIR object? if so, unpack it | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $object ~~ qr/FAIR/ and $object->can("_toTriples")
MC/DC50%1/2
ref $object ~~ qr/FAIR/ and $object->can("_toTriples")
ref $object ~~ qr/FAIR/
$object->can("_toTriples")
Truth table66%2/3
A and B
A= ref $object ~~ qr/FAIR/B= $object->can("_toTriples")
| |||||||||||||||||||||||||||||||||
| 132 | 20 | my $toConnect = $object->URI; # get that objects URI | |||||||||||||||||||||||||||||||
| 133 | 20 | my $namespace = $namespaces{$predicate_namespaces{$predicate}}; # look up the namespace of that predicate | |||||||||||||||||||||||||||||||
| 134 | 20 | ▶ | die "no namespace found for $predicate\n" unless $namespace; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 20 | my $stm = statement($sub, $namespace.$predicate, $toConnect); # and create the triple joining that object to the current model | |||||||||||||||||||||||||||||||
| 136 | 20 | $model->add_statement($stm); | |||||||||||||||||||||||||||||||
| 137 | 20 | ▶ | next if ($object->isa('FAIR::Profile')); # if the sub-object refers back to the main profile object, then skip at this point to prevent infinite loops | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 138 | 15 | $object->_toTriples($model); # recursive call... unpack that FAIR object to its triples | |||||||||||||||||||||||||||||||
| 139 | } else { # if it isn't a FAIR object, then it's just a listref of strings | ||||||||||||||||||||||||||||||||
| 140 | 85 | my $namespace = $namespaces{$predicate_namespaces{$predicate}}; | |||||||||||||||||||||||||||||||
| 141 | 85 | my $stm = statement($sub, $namespace.$predicate, $object); | |||||||||||||||||||||||||||||||
| 142 | 85 | $model->add_statement($stm); | |||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||
| 145 | # next; | ||||||||||||||||||||||||||||||||
| 146 | #} else { | ||||||||||||||||||||||||||||||||
| 147 | # # print STDERR $key, "\n"; | ||||||||||||||||||||||||||||||||
| 148 | # my $namespace = $namespaces{$predicate_namespaces{$key}}; | ||||||||||||||||||||||||||||||||
| 149 | # my $value = $self->$key; | ||||||||||||||||||||||||||||||||
| 150 | # next unless defined $value; | ||||||||||||||||||||||||||||||||
| 151 | # my $stm = statement($sub, $namespace.$key, $value); | ||||||||||||||||||||||||||||||||
| 152 | # $model->add_statement($stm); | ||||||||||||||||||||||||||||||||
| 153 | #} | ||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | 20 | return $model; | |||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||
| 158 | sub toTriples { | ||||||||||||||||||||||||||||||||
| 159 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
toTriples: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 160 | 5 | my $model = $self->_toTriples; | |||||||||||||||||||||||||||||||
| 161 | 5 | my $iter = $model->get_statements(); | |||||||||||||||||||||||||||||||
| 162 | 5 | my @statements; | |||||||||||||||||||||||||||||||
| 163 | 5 | ▶ | while (my $st = $iter->next) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 164 | 130 | push @statements, $st; | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 5 | return @statements; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | sub statement { | ||||||||||||||||||||||||||||||||
| 171 | 130 | ▶ | my ($s, $p, $o) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
statement: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 172 | 130 | ▶ | unless (ref($s) =~ /Trine/){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | 130 | $s =~ s/[\<\>]//g; | |||||||||||||||||||||||||||||||
| 174 | 130 | $s = RDF::Trine::Node::Resource->new($s); | |||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||
| 176 | 130 | ▶ | unless (ref($p) =~ /Trine/){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 177 | 130 | $p =~ s/[\<\>]//g; | |||||||||||||||||||||||||||||||
| 178 | 130 | $p = RDF::Trine::Node::Resource->new($p); | |||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||
| 180 | 130 | ▶ | unless (ref($o) =~ /Trine/){ | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 181 | 130 | ▶ | if (($o =~ m'^http://') || ($o =~ m'^https://')){ | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$o =~ m[^http://] or $o =~ m[^https://]
MC/DC50%1/2
$o =~ m[^http://] or $o =~ m[^https://]
$o =~ m[^http://]
$o =~ m[^https://]
Truth table66%2/3
A or B
A= $o =~ m[^http://]B= $o =~ m[^https://]
| |||||||||||||||||||||||||||||||||
| 182 | 60 | $o =~ s/[\<\>]//g; | |||||||||||||||||||||||||||||||
| 183 | 60 | $o = RDF::Trine::Node::Resource->new($o); | |||||||||||||||||||||||||||||||
| 184 | } elsif ($o =~ /\D/) { | ||||||||||||||||||||||||||||||||
| 185 | 55 | $o = RDF::Trine::Node::Literal->new($o); | |||||||||||||||||||||||||||||||
| 186 | } else { | ||||||||||||||||||||||||||||||||
| 187 | 15 | $o = RDF::Trine::Node::Literal->new($o); | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | 130 | my $statement = RDF::Trine::Statement->new($s, $p, $o); | |||||||||||||||||||||||||||||||
| 191 | 130 | return $statement; | |||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | 6 | 1; | |||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | __END__ | ||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | =pod | ||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||
| 211 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||
| 213 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||
| 215 | FAIR::Base - libraries for creating and parsing FAIR Data Profiles (see http://datafairport.org for more details) | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||
| 219 | version 0.41 | ||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||
| 221 | =head1 FAIR Base - the root of the FAIR modules | ||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||
| 223 | The FAIR modules come from the Data FAIRport project (http://datafairport.org). | ||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||
| 225 | There are three main sections to this code: FAIR::Profiles, FAIR Accessors, and FAIR Projectors. (Projectors haven't been invented yet) | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | Mark Denis Wilkinson (markw [at] illuminae [dot] com) | ||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||
| 231 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||
| 233 | This software is Copyright (c) 2016 by Mark Denis Wilkinson. | ||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||
| 235 | This is free software, licensed under: | ||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||
| 237 | The Apache License, Version 2.0, January 2004 | ||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||
| 239 | =cut | ||||||||||||||||||||||||||||||||