1+ import inspect
12import warnings
23from copy import deepcopy
34from pathlib import Path
45from tempfile import TemporaryDirectory
56from typing import Iterator , Optional
6- import inspect
7- from urllib .parse import urldefrag
87
98import networkx as nx
109import rdflib
1110from rdflib import RDF , RDFS , DCTERMS
1211from rdflib .tools .rdf2dot import rdf2dot
1312from requests import HTTPError
1413
15-
1614from fairworkflows import namespaces
1715from fairworkflows .fairstep import FairStep
18- from fairworkflows .rdf_wrapper import RdfWrapper , replace_in_rdf
16+ from fairworkflows .rdf_wrapper import RdfWrapper
1917import nanopub
2018
2119class FairWorkflow (RdfWrapper ):
@@ -54,8 +52,7 @@ def from_rdf(cls, rdf: rdflib.Graph, uri: str,
5452 possibly it's associated steps. Should use plex ontology.
5553 uri: URI of the workflow
5654 fetch_references: toggles fetching steps. I.e. if we encounter steps
57- that are part of the workflow, but are not specified in the
58- RDF we try fetching them from nanopub
55+ that are part of the workflow we try fetching them from nanopub
5956 force: Toggle forcing creation of object even if url is not in any of the subjects of
6057 the passed RDF
6158 remove_irrelevant_triples: Toggle removing irrelevant triples for this FairWorkflow.
@@ -106,19 +103,18 @@ def from_noodles_promise(cls, noodles_promise, description: str = None, label: s
106103
107104 return self
108105
109- def _extract_steps (self , rdf , uri , fetch_steps = False ):
106+ def _extract_steps (self , rdf , uri , fetch_steps = True ):
110107 """Extract FairStep objects from rdf.
111108
112- Create FairStep objects for all steps in the passed RDF. Removes
113- triples describing steps from rdf, those will be represented in
114- the separate step RDF. Optionally try to fetch steps from nanopub.
109+ Create FairStep objects for all steps in the passed RDF.
110+ Optionally try to fetch steps from nanopub.
115111 """
116112 step_refs = rdf .subjects (predicate = namespaces .PPLAN .isStepOfPlan ,
117113 object = rdflib .URIRef (uri ))
118114 for step_ref in step_refs :
119115 step_uri = str (step_ref )
120- step = self . _extract_step_from_rdf ( step_uri , rdf )
121- if step is None and fetch_steps :
116+ step = None
117+ if fetch_steps :
122118 step = self ._fetch_step (uri = step_uri )
123119 if step is None :
124120 warnings .warn (f'Could not get detailed information for '
@@ -153,19 +149,6 @@ def _get_relevant_triples(uri, rdf):
153149 g .add (triple )
154150 return g
155151
156- @staticmethod
157- def _extract_step_from_rdf (uri , rdf : rdflib .Graph ()) -> Optional [FairStep ]:
158- relevant_triples = FairStep ._get_relevant_triples (uri , rdf )
159- step_rdf = rdflib .Graph ()
160- for triple in relevant_triples :
161- step_rdf .add (triple )
162- rdf .remove (triple )
163-
164- if len (step_rdf ) > 0 :
165- return FairStep .from_rdf (step_rdf , uri = uri , remove_irrelevant_triples = False )
166- else :
167- return None
168-
169152 @staticmethod
170153 def _fetch_step (uri : str ) -> Optional [FairStep ]:
171154 try :
@@ -207,6 +190,7 @@ def _add_step(self, step: FairStep):
207190 self ._rdf .add ((rdflib .URIRef (step .uri ), namespaces .PPLAN .isStepOfPlan ,
208191 self .self_ref ))
209192 self ._last_step_added = step
193+ step .register_workflow (self )
210194
211195 def add (self , step : FairStep , follows : FairStep = None ):
212196 """Add a step.
@@ -472,8 +456,10 @@ def draw(self, filepath):
472456 def publish_as_nanopub (self , use_test_server = False , ** kwargs ):
473457 """Publish to nanopub server.
474458
475- First publish the steps, use the URIs of the published steps in the workflow. Then
476- publish the workflow.
459+ Publish the workflow as nanopublication to the nanopub server.
460+
461+ Raises:
462+ RuntimeError: If one of the steps of the workflow was not published yet.
477463
478464 Args:
479465 use_test_server (bool): Toggle using the test nanopub server.
@@ -489,25 +475,8 @@ def publish_as_nanopub(self, use_test_server=False, **kwargs):
489475 for step in self :
490476 if step .is_modified or not step ._is_published :
491477 self ._is_modified = True # If one of the steps is modified the workflow is too.
492- old_uri = step .uri
493- var_names = [var .name for var in (step .inputs + step .outputs )]
494-
495- step .publish_as_nanopub (use_test_server = use_test_server , ** kwargs )
496- published_step_uri = step .uri
497-
498- replace_in_rdf (self .rdf , oldvalue = rdflib .URIRef (old_uri ),
499- newvalue = rdflib .URIRef (published_step_uri ))
500-
501- # Similarly replace old URIs for variable name bindings
502- published_step_uri_defrag , _ = urldefrag (published_step_uri )
503- for var_name in var_names :
504- old_var_uri = old_uri + '#' + var_name
505- new_var_uri = published_step_uri_defrag + '#' + var_name
506- replace_in_rdf (self .rdf , oldvalue = rdflib .URIRef (old_var_uri ),
507- newvalue = rdflib .URIRef (new_var_uri ))
478+ raise RuntimeError (f'{ step } was not published yet, please publish steps first' )
508479
509- del self ._steps [old_uri ]
510- self ._steps [published_step_uri ] = step
511480 return self ._publish_as_nanopub (use_test_server = use_test_server , ** kwargs )
512481
513482 def __str__ (self ):
0 commit comments