Skip to content

Commit f0452b0

Browse files
committed
Simplify decorator (return the decorated func instead of wrapper that returns func) and use hyphens in output var names to avoid breaking URI rules.
1 parent 30be416 commit f0452b0

1 file changed

Lines changed: 46 additions & 47 deletions

File tree

fairworkflows/fairstep.py

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def _add_variable(self, variable: FairVariable, relation_to_step):
236236
self.set_attribute(relation_to_step, var_ref, overwrite=False)
237237
self._rdf.add((var_ref, RDF.type, rdflib.term.Literal(variable.type)))
238238
self._rdf.add((var_ref, RDF.type, namespaces.PPLAN.Variable))
239+
self._rdf.add((var_ref, RDFS.label, rdflib.term.Literal(variable.name)))
239240

240241
@property
241242
def inputs(self) -> List[FairVariable]:
@@ -362,46 +363,46 @@ def __str__(self):
362363
return s
363364

364365

365-
def mark_as_fairstep(label: str = None, is_pplan_step: bool = True, is_manual_task: bool = None,
366-
is_script_task: bool = None):
367-
"""Mark a function as a FAIR step.
368-
369-
Use as decorator to mark a function as a FAIR step. Set properties of the fair step using
370-
arguments to the decorator.
371-
372-
The raw code of the function will be used to set the description of the fair step.
373-
374-
The type annotations of the input arguments and return statement will be used to
375-
automatically set inputs and outputs of the FAIR step.
376-
377-
Args:
378-
label (str): Label of the fair step (corresponds to rdfs:label predicate)
379-
is_pplan_step (str): Denotes whether this step is a pplan:Step
380-
is_manual_task (str): Denotes whether this step is a bpmn.ManualTask
381-
is_script_task (str): Denotes whether this step is a bpmn.ScriptTask
382-
383-
"""
384-
def _modify_function(func):
385-
"""
386-
Store FairStep object as _fairstep attribute of the function. Use inspection to get the
387-
description, inputs, and outputs of the step based on the function specification.
388-
"""
389-
def _wrapper(*args, **kwargs):
390-
return func(*args, **kwargs)
391-
# Description of step is the raw function code
392-
description = inspect.getsource(func)
393-
inputs = _extract_inputs_from_function(func)
394-
outputs = _extract_outputs_from_function(func)
395-
_wrapper._fairstep = FairStep(label=label,
396-
description=description,
397-
is_pplan_step=is_pplan_step,
398-
is_manual_task=is_manual_task,
399-
is_script_task=is_script_task,
400-
inputs=inputs,
401-
outputs=outputs)
402-
_wrapper._fairstep.validate()
403-
return _wrapper
404-
return _modify_function
366+
#def mark_as_fairstep(label: str = None, is_pplan_step: bool = True, is_manual_task: bool = None,
367+
# is_script_task: bool = None):
368+
# """Mark a function as a FAIR step.
369+
#
370+
# Use as decorator to mark a function as a FAIR step. Set properties of the fair step using
371+
# arguments to the decorator.
372+
#
373+
# The raw code of the function will be used to set the description of the fair step.
374+
#
375+
# The type annotations of the input arguments and return statement will be used to
376+
# automatically set inputs and outputs of the FAIR step.
377+
#
378+
# Args:
379+
# label (str): Label of the fair step (corresponds to rdfs:label predicate)
380+
# is_pplan_step (str): Denotes whether this step is a pplan:Step
381+
# is_manual_task (str): Denotes whether this step is a bpmn.ManualTask
382+
# is_script_task (str): Denotes whether this step is a bpmn.ScriptTask
383+
#
384+
# """
385+
# def _modify_function(func):
386+
# """
387+
# Store FairStep object as _fairstep attribute of the function. Use inspection to get the
388+
# description, inputs, and outputs of the step based on the function specification.
389+
# """
390+
# def _wrapper(*args, **kwargs):
391+
# return func(*args, **kwargs)
392+
# # Description of step is the raw function code
393+
# description = inspect.getsource(func)
394+
# inputs = _extract_inputs_from_function(func)
395+
# outputs = _extract_outputs_from_function(func)
396+
# _wrapper._fairstep = FairStep(label=label,
397+
# description=description,
398+
# is_pplan_step=is_pplan_step,
399+
# is_manual_task=is_manual_task,
400+
# is_script_task=is_script_task,
401+
# inputs=inputs,
402+
# outputs=outputs)
403+
# _wrapper._fairstep.validate()
404+
# return _wrapper
405+
# return _modify_function
405406

406407

407408
def is_fairstep(label: str = None, is_pplan_step: bool = True, is_manual_task: bool = False,
@@ -431,23 +432,21 @@ def _modify_function(func):
431432
432433
Returns this function decorated with the noodles schedule decorator.
433434
"""
434-
def _wrapper(*args, **kwargs):
435-
return noodles.schedule(func)(*args, **kwargs)
436-
437435
# Description of step is the raw function code
438436
description = inspect.getsource(func)
439437
inputs = _extract_inputs_from_function(func)
440438
outputs = _extract_outputs_from_function(func)
441439

442-
_wrapper._fairstep = FairStep(label=label,
440+
func._fairstep = FairStep(uri='www.example.org/unpublished-'+func.__name__,
441+
label=label,
443442
description=description,
444443
is_pplan_step=is_pplan_step,
445444
is_manual_task=is_manual_task,
446445
is_script_task=is_script_task,
447446
inputs=inputs,
448447
outputs=outputs)
449448

450-
return _wrapper
449+
return noodles.schedule(func)
451450

452451
return _modify_function
453452

@@ -480,10 +479,10 @@ def _extract_outputs_from_function(func) -> List[FairVariable]:
480479
'FAIR step functions MUST have type hinting, '
481480
'see https://docs.python.org/3/library/typing.html')
482481
if _is_generic_tuple(return_annotation):
483-
return [FairVariable(name=func.__name__ + '_output' + str(i + 1), type=annotation.__name__)
482+
return [FairVariable(name=func.__name__ + '-output' + str(i + 1), type=annotation.__name__)
484483
for i, annotation in enumerate(return_annotation.__args__)]
485484
else:
486-
return [FairVariable(name=func.__name__ + '_output1', type=return_annotation.__name__)]
485+
return [FairVariable(name=func.__name__ + '-output1', type=return_annotation.__name__)]
487486

488487

489488
def _is_generic_tuple(type_):

0 commit comments

Comments
 (0)