Simple/OutputModel.pm untested

filter: Statementstmt 0.0% 0 / 181 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 266 CC 21 SCAR 32.6
CC
21
Cov
0%
_add_statement
72.0
_add_provenance
56.0
nanopublish_result_for
20.0
CRAP 26.0
1 package SADI::Simple::OutputModel;
2 {
3 $SADI::Simple::OutputModel::VERSION = '0.15';
4 }
5  
6 use SADI::Simple::Utils;
7 use RDF::Trine::Model 0.135;
8 use RDF::Trine::Parser 0.135;
9 use RDF::Trine::Node::Resource;
10 use Log::Log4perl;
11 use Template;
12 use Encode;
13 use Digest::MD5 qw(md5 md5_hex md5_base64);
14  
15 use constant RDF_TYPE_URI => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
16 use constant RDF_SUBGRAPH_OF => 'http://www.w3.org/2004/03/trix/rdfg-1/subGraphOf';
17
18 use base 'RDF::Trine::Model';
19  
20 sub _init {
21 my $self = shift;
22 my $service_base = shift; # this is awful! Need to refactor the code properly at some point
23 $self->{'service_base'} = $service_base;
24 $self->setInvocationTime();
25
26 my $root_uri = "http://example.org/sadi_service/"; # set to a temporary value
27 if ($self->{'service_base'}->{Signature}->URL) { $root_uri = $self->{'service_base'}->{Signature}->URL};
28 $root_uri =~ s/\/$//; # REMOVE TRAILING SLASH IF IT EXISTS
29
30 my $default_collection_graph = $root_uri."/nanopub_collection/".$self->getInvocationTime();
31 $self->{'default_collection_graph'} = $default_collection_graph; # http://my.service.org/servicename/provenance/1238758
32  
33 my $named_graph = $root_uri."/provenance/".$self->getInvocationTime();
34 $self->{'provenance_named_graph'} = $named_graph; # http://my.service.org/servicename/provenance/1238758
35  
36 my $publ_named_graph = $root_uri."/publication_info/".$self->getInvocationTime();
37 $self->{'pubinfo_named_graph'} = $publ_named_graph; # http://my.service.org/servicename/provenance/1238758
38
39 my $assertion_root = $root_uri."/assertion/";
40 $self->{'assertion_statement_root'} = $assertion_root; # http://my.service.org/servicename/assertion/
41
42 my $nanopublication_root = $root_uri."/nanopublication/";
43 $self->{'nanopublication_root'} = $nanopublication_root; # http://my.service.org/servicename/nanopublication/
44
45 my $nanopubCollection_root = $root_uri."/nanopublicationCollection/";
46 $self->{'nanopubCollection_root'} = $$nanopubCollection_root; # http://my.service.org/servicename/nanopublication/
47
48 $self->{'nanopub_ontology_uri'} = "http://www.nanopub.org/nschema#";
49
50 $self->{'assertion_context_hashref'} = {};
51
52 }
53  
54  
55 #THIS IS WHAT WE'RE AIMING FOR :-)
56 #
57 #
58 #:head a np:NanopublicationCollection.
59 #
60 #:ABC {
61 # :ABC a np:Nanopublication ;
62 # np:hasAssertion :Ass1 ;
63 # np:hasProvenance :Prov ;
64 # np:hasPublicationInfo :Info .
65 # :ABC rdfg:subGraphOf :head .
66 # }
67 #
68 #:DEF {
69 # :DEF a np:Nanopublication ;
70 # np:hasAssertion :Ass2 ;
71 # np:hasProvenance :Prov ;
72 # np:hasPublicationInfo :Info .
73 # :DEF rdfg:subGraphOf :head .
74 # }
75 #
76 # :Ass1 {
77 # ex:mosquito ex:transmits ex:malaria .
78 # }
79 #
80 # :Ass2 {
81 # ex:tick ex:transmits ex:limedisease .
82 # }
83 #
84 # :Prov { // Any/All of these will be allowed
85 # :head ex:generatedBy ev:abcdefg .
86 # :ABC ex:somePredicate ev:qwerty .
87 # :DEF ex:somePredicate ev:qwerty1 .
88 # :Ass1 ex:usingAlgorithm ev:myalgo .
89 # }
90 #
91 # :Info {
92 # :head pav:authoredBy auth:wxyz
93 # }
94 #
95  
96  
97 sub add_statement {
98  
99 my ($self, $statement, $context_info) = @_; # context info is the $input RDF::Trine::Resource currently being analyzed. Provides the basis of a unique context URI for the quads
100 #print STDERR "in add statement with self of type ", ref$self, " statement $statement stmpred ", $statement->predicate, " input $context_info \n";
101  
102 # but the same for all assertions of that input
103 if (($context_info) && (ref($context_info) =~ /trine/i)){
104 #print STDERR "recognized that it is a quad\n\n";
105 my $inputURIstring = $context_info->uri();
106  
107 my $assertion_context = $self->getCurrentAssertionContext($inputURIstring);
108
109 my $sub = $statement->subject;
110 my $pred = $statement->predicate;
111 my $obj = $statement->object;
112 my $context = RDF::Trine::Node::Resource->new($assertion_context);
113 my $named_statement = RDF::Trine::Statement::Quad->new($sub, $pred, $obj, $context); # subj pred obj stm_in_assert:AHhfj847hKHJRF
114 RDF::Trine::Model::add_statement($self, $named_statement)
115 } else {
116 RDF::Trine::Model::add_statement($self, $statement);
117 }
118 #print STDERR "STATEMENT ADDED\n\n";
119  
120 }
121  
122 # problem is that assertion context needs to be re-generated at a later time for a given input... so need to store it in a hash
123 sub getCurrentAssertionContext {
124 my ($self, $URI) = @_;
125 my $assertion_context_hashref = $self->{'assertion_context_hashref'};
126 if (ref($URI) =~ /trine/i){$URI = $URI->uri()} # need the stringified for the hash
127 # print STDOUT "\n\ncontext of $URI ...";
128 if ($assertion_context_hashref->{$URI}){
129 # print STDOUT "found context $assertion_context_hashref->{$URI}\n\n\n";
130 return $assertion_context_hashref->{$URI}
131 } else {
132 # print STDOUT " context not found ...\n\n\n";
133 my $AssertionContextID = md5_hex($URI . $self->getInvocationTime()); # invocation time increments with every new nanopub, so each gets a unique assertion context
134 my $assertion_context = $self->{'assertion_statement_root'} . $AssertionContextID; # this will be different for every input
135 $assertion_context_hashref->{$URI} = $assertion_context;
136 return $assertion_context;
137 }
138 }
139  
140  
141 sub getCurrentNanopubID {
142 my ($self, $URI) = @_;
143 return $self->{'nanopublication_root'} . md5_hex($URI . $self->getInvocationTime()); # a hash of the input and timestamp of service invocation
144
145 }
146  
147 sub resetInvocationTime {
148 return setInvocationTime();
149 }
150 sub setInvocationTime {
151 my ($self) = @_;
152 $self->{'invocation_timestamp'} = time;
153 return $self->{'invocation_timestamp'};
154
155 }
156  
157 sub getInvocationTime {
158 my ($self) = @_;
159 return $self->{'invocation_timestamp'};
160 }
161  
162  
163 sub nanopublish_result_for {
164 my ($self, $input) = @_; # self is the output model RDF::Trine::Model
165 return unless ($self->{'service_base'}->get_response_content_type =~ /quads/i); # only do this if requesting quads
166 return unless ($self->{'service_base'}->{'Signature'}->NanoPublisher);
167 # nanopub:AHhfj847hKHJRF np:hasAassertion assert:AHhfj847hKHJRF
168 # nanopub:AHhfj847hKHJRF np:hasProvenance $self->{'provenance_named_graph'} (this is the same for all different nanopubs in a multiplexed invocation of the service)
169 # nanopub:AHhfj847hKHJRF rdf:type np:Nanopublication
170 # assert:AHhfj847hKHJRF rdf:type np:Assertion
171  
172 my $np = $self->{'nanopub_ontology_uri'};
173  
174 my $NanopubCollection = RDF::Trine::Node::Resource->new($self->{'default_collection_graph'});
175 my $NanopublicationCollectionType = RDF::Trine::Node::Resource->new($np."NanopublicationCollection");
176 my $NanopublicationType = RDF::Trine::Node::Resource->new($np."Nanopublication");
177 my $AssertionType = RDF::Trine::Node::Resource->new($np."Assertion");
178 my $ProvenanceType = RDF::Trine::Node::Resource->new($np."Provenance");
179 my $PubInfoType = RDF::Trine::Node::Resource->new($np."PublicationInfo");
180 my $rdfType = RDF::Trine::Node::Resource->new(RDF_TYPE_URI);
181 my $rdfSubgraphOf = RDF::Trine::Node::Resource->new(RDF_SUBGRAPH_OF);
182  
183  
184 my $inputURIstring = $input->uri();
185
186
187 my $AssertionContextURI = $self->getCurrentAssertionContext($inputURIstring);
188
189 my $NanopublicationURI = $self->getCurrentNanopubID($inputURIstring);
190
191
192 my $Nanopub = RDF::Trine::Node::Resource->new($NanopublicationURI);
193 my $hasAssertion = RDF::Trine::Node::Resource->new($np."hasAssertion");
194 my $Assertion = RDF::Trine::Node::Resource->new($AssertionContextURI);
195 my $np_hasAssertion_Assertion = RDF::Trine::Statement::Quad->new($Nanopub, $hasAssertion, $Assertion, $Nanopub);
196 $self->add_statement($np_hasAssertion_Assertion);
197
198 my $provenance_named_graph = RDF::Trine::Node::Resource->new($self->{'provenance_named_graph'});
199 my $hasProvenance = RDF::Trine::Node::Resource->new($np."hasProvenance");
200 my $np_hasProvenance_Provenance = RDF::Trine::Statement::Quad->new($Nanopub, $hasProvenance, $provenance_named_graph, $Nanopub);
201 $self->add_statement($np_hasProvenance_Provenance);
202  
203 my $pubinfo_named_graph = RDF::Trine::Node::Resource->new($self->{'pubinfo_named_graph'});
204 my $hasPubInfo = RDF::Trine::Node::Resource->new($np."hasPublicationInfo");
205 my $np_hasPubInfo_Pubinfo = RDF::Trine::Statement::Quad->new($Nanopub, $hasPubInfo, $pubinfo_named_graph, $Nanopub);
206 $self->add_statement($np_hasPubInfo_Pubinfo);
207  
208  
209 $self->add_statement(RDF::Trine::Statement::Quad->new($Nanopub, $rdfType, $NanopublicationType, $Nanopub));
210 $self->add_statement(RDF::Trine::Statement::Quad->new($pubinfo_named_graph, $rdfType, $PubInfoType, $Nanopub));
211 $self->add_statement(RDF::Trine::Statement::Quad->new($Assertion, $rdfType, $AssertionType, $Nanopub));
212 $self->add_statement(RDF::Trine::Statement::Quad->new($provenance_named_graph, $rdfType, $ProvenanceType, $Nanopub));
213 $self->add_statement(RDF::Trine::Statement::Quad->new($Nanopub, $rdfSubgraphOf, $NanopubCollection, $Nanopub));
214 $self->add_statement(RDF::Trine::Statement::Quad->new($NanopubCollection, $rdfType,$NanopublicationCollectionType, $Nanopub));
215  
216  
217 unless ($self->{provenance_added}){
218 $self->_add_provenance($NanopubCollection, $provenance_named_graph);
219 $self->_add_pubinfo($NanopubCollection, $pubinfo_named_graph);
220
221 }
222
223 $self->resetInvocationTime(); # be ready to create a new NanoPublication URI
224
225 }
226  
227  
228 sub _add_provenance {
229 my ($self, $NanopubCollection, $provenance_named_graph) = @_;
230 use DateTime;
231 # name - the name of the SADI service
232 # uri - the service uri
233 # type - the service type
234 # input - the input class URI
235 # output - the output class URI
236 # desc - a description for this service
237 # id - a unique identifier (LSID, etc)
238 # email - the providers email address
239 # format - the category of service (sadi)
240 # nanopublisher - can the service publish nquads for nanopubs?
241 # url - the service url
242 # authoritative - whether or not the service is authoritative
243 # authority - the service authority URI
244 # sigURL - the url to the service signature
245 my $signature = $self->{'service_base'}->{Signature};
246  
247 # remember this is adding quads!
248 my $Identifier = RDF::Trine::Node::Literal->new($signature->ServiceURI,"","http://www.w3.org/2001/XMLSchema#string" ); # this is a URI, so I need to cast it as a string before sending it into the statement, otherwise it will be cast as a resource
249 $self->_add_statement($NanopubCollection, "http://purl.org/dc/terms/created", [DateTime->now,"", "http://www.w3.org/2001/XMLSchema#dateTime",""], $provenance_named_graph);
250 $self->_add_statement($NanopubCollection, "http://purl.org/dc/terms/creator", [$signature->Authority, "", "http://www.w3.org/2001/XMLSchema#string",], $provenance_named_graph) if $signature->Authority ;
251 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/coverage", ["Output from SADI Service", "en"], $provenance_named_graph);
252 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/description", ["Service Description: ".$signature->Description, "en"], $provenance_named_graph) if $signature->Description;
253 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/identifier", [$Identifier], $provenance_named_graph) if $signature->ServiceURI;
254 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/publisher", [$signature->Authority,"", "http://www.w3.org/2001/XMLSchema#string" ], $provenance_named_graph) if $signature->Authority;
255 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/source", [$signature->URL], $provenance_named_graph) if $signature->URL;
256 $self->_add_statement($NanopubCollection, "http://purl.org/dc/elements/1.1/title", [$signature->ServiceName, "","http://www.w3.org/2001/XMLSchema#string"], $provenance_named_graph) if $signature->ServiceName;
257
258 $self->{provenance_added} = 1; # raise a flag so that this routine is not called again!
259 }
260  
261  
262  
263 sub _add_pubinfo {
264 my ($self, $Nanopub, $pubinfo_named_graph) = @_;
265 use DateTime;
266 # remember this is adding quads!
267 my $signature = $self->{'service_base'}->{Signature};
268 my $Identifier = RDF::Trine::Node::Literal->new($signature->ServiceURI,"","http://www.w3.org/2001/XMLSchema#string" ); # this is a URI, so I need to cast it as a string before sending it into the statement, otherwise it will be cast as a resource
269
270 $self->_add_statement($Nanopub, "http://purl.org/dc/terms/created", [DateTime->now,"", "http://www.w3.org/2001/XMLSchema#dateTime",""], $pubinfo_named_graph);
271 $self->_add_statement($Nanopub, "http://purl.org/dc/terms/creator", ["Perl SADI::Simple Library version $VERSION", "", "http://www.w3.org/2001/XMLSchema#string",], $pubinfo_named_graph);
272 # $self->_add_statement($Nanopub, "http://swan.mindinformatics.org/ontologies/1.2/pav/createdBy", [$Identifier], $pubinfo_named_graph);
273 $self->_add_statement($Nanopub, "http://swan.mindinformatics.org/ontologies/1.2/pav/createdBy", [$signature->ServiceURI], $pubinfo_named_graph);
274
275 }
276  
277 sub _add_statement {
278 my ($self, $s, $p, $o, $c) = @_;
279 my ($obj, $lang, $datatype, $canonicalflag) = @$o;
280 unless (ref($s) =~ /trine/i){
281 $s = RDF::Trine::Node::Resource->new($s);
282 }
283 unless (ref($p) =~ /trine/i){
284 $p = RDF::Trine::Node::Resource->new($p);
285 }
286 if (ref($obj) =~ /trine/i){
287 $o = $obj;
288 } else {
289 if (($obj =~ /^http:\/\//i) || ($obj =~ /^\<http:\/\//i)){
290 $o = RDF::Trine::Node::Resource->new($obj);
291 } else {
292 $o = RDF::Trine::Node::Literal->new($obj, $lang, $datatype, $canonicalflag);
293 }
294 }
295 if ($c){
296 unless (ref($c) =~ /trine/i){
297 $c = RDF::Trine::Node::Resource->new($c);
298 }
299 }
300 if ($c){
301 my $stm = RDF::Trine::Statement::Quad->new($s, $p, $o, $c);
302 $self->add_statement($stm);
303 } else {
304 my $stm = RDF::Trine::Statement->new($s, $p, $o);
305 $self->add_statement($stm);
306 }
307  
308 }
309 1;
310  
311 __END__