Skip to content

Commit

Permalink
Merge pull request #388 from sartography/improvement/script-engine-cl…
Browse files Browse the repository at this point in the history
…eanup

clean up script engine methods
  • Loading branch information
essweine authored Feb 15, 2024
2 parents 85cfa08 + b8e6a0c commit ae3ed3e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 38 deletions.
36 changes: 18 additions & 18 deletions SpiffWorkflow/bpmn/script_engine/feel_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ def feelConvertTime(datestr,parsestr):

class FeelInterval():
def __init__(self, begin, end, leftOpen=False, rightOpen=False):
warnings.warn(
'The FEEL script engine is deprecated and will be removed in the next release',
DeprecationWarning,
stacklevel=2,
)
# pesky thing with python floats and Decimal comparison
if isinstance(begin,float):
begin = Decimal("%0.5f"%begin)
Expand Down Expand Up @@ -274,6 +269,11 @@ class FeelLikeScriptEngine(PythonScriptEngine):
expressions in a mini-language of your own.
"""
def __init__(self, environment=None):
warnings.warn(
'The FEEL script engine is deprecated and will be removed in the next release',
DeprecationWarning,
stacklevel=2,
)
super().__init__(environment=environment)

def validate(self, expression):
Expand All @@ -294,17 +294,10 @@ def patch_expression(self, invalid_python, lhs=''):
proposed_python = lhs + proposed_python
return proposed_python

def _evaluate(self, expression, context, task=None, external_context=None):
"""
Evaluate the given expression, within the context of the given task and
return the result.
"""
def evaluate(self, task, expression, external_context=None):
if external_context is None:
external_context = {}

revised = self.patch_expression(expression)
external_context.update(externalFuncs)
return super()._evaluate(revised, context, external_context=external_context)
return self._evaluate(expression, task.data, external_context=external_context)

def execute(self, task, script, data, external_context=None):
"""
Expand All @@ -313,8 +306,15 @@ def execute(self, task, script, data, external_context=None):
if external_context is None:
external_context = {}
external_context.update(externalFuncs)
super().execute(task, script, external_context)



return super().execute(task, script, external_context)

def _evaluate(self, expression, context, task=None, external_context=None):
"""
Evaluate the given expression, within the context of the given task and
return the result.
"""
if external_context is None:
external_context = {}
external_context.update(externalFuncs)
revised = self.patch_expression(expression)
return self.environment.evaluate(revised, context, external_context=external_context)
22 changes: 11 additions & 11 deletions SpiffWorkflow/bpmn/script_engine/python_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ast
import sys
import traceback
import warnings

from SpiffWorkflow.exceptions import SpiffWorkflowException
from SpiffWorkflow.bpmn.exceptions import WorkflowTaskException
Expand Down Expand Up @@ -49,7 +50,7 @@ def evaluate(self, task, expression, external_context=None):
return the result.
"""
try:
return self._evaluate(expression, task.data, external_context)
return self.environment.evaluate(expression, task.data, external_context)
except SpiffWorkflowException as se:
se.add_note(f"Error evaluating expression '{expression}'")
raise se
Expand All @@ -59,15 +60,20 @@ def evaluate(self, task, expression, external_context=None):
def execute(self, task, script, external_context=None):
"""Execute the script, within the context of the specified task."""
try:
return self._execute(script, task.data, external_context or {})
return self.environment.execute(script, task.data, external_context or {})
except Exception as err:
wte = self.create_task_exec_exception(task, script, err)
raise wte

def call_service(self, operation_name, operation_params, task_data):
"""Override to control how external services are called from service
tasks."""
raise NotImplementedError("To call external services override the script engine and implement `call_service`.")
"""Override to control how external services are called from service tasks."""
warnings.warn(
'In the next release, implementation of this method will be moved to the scripting environment',
DeprecationWarning,
stacklevel=2,
)
# Ideally, this method would look like call_service(self, task, operation_name, operation_params)
return self.environment.call_service(operation_name, operation_params, task_data)

def create_task_exec_exception(self, task, script, err):
line_number, error_line = self.get_error_line_number_and_content(script, err)
Expand Down Expand Up @@ -97,9 +103,3 @@ def get_error_line_number_and_content(self, script, err):
if line_number > 0:
error_line = script.splitlines()[line_number - 1]
return line_number, error_line

def _evaluate(self, expression, context, external_context=None):
return self.environment.evaluate(expression, context, external_context)

def _execute(self, script, context, external_context=None):
return self.environment.execute(script, context, external_context)
3 changes: 3 additions & 0 deletions SpiffWorkflow/bpmn/script_engine/python_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def evaluate(self, expression, context, external_context=None):
def execute(self, script, context, external_context=None):
raise NotImplementedError("Subclass must implement this method")

def call_service(self, operation_name, operation_params, task_data):
raise NotImplementedError("To call external services override the script engine and implement `call_service`.")


class TaskDataEnvironment(BasePythonScriptEngineEnvironment):

Expand Down
2 changes: 1 addition & 1 deletion SpiffWorkflow/bpmn/specs/event_definitions/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_correlations(self, task, payload):
if key not in correlations:
correlations[key] = {}
try:
correlations[key][property.name] = task.workflow.script_engine._evaluate(property.retrieval_expression, payload)
correlations[key][property.name] = task.workflow.script_engine.environment.evaluate(property.retrieval_expression, payload)
except WorkflowException:
# Just ignore missing keys. The dictionaries have to match exactly
pass
Expand Down
3 changes: 1 addition & 2 deletions SpiffWorkflow/dmn/engine/DMNEngine.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ def evaluate(self, input_expr, match_expr, task):
external_context = {
'dmninputexpr': script_engine.evaluate(task, input_expr)
}
return script_engine.evaluate(task, match_expr,
external_context=external_context)
return script_engine.evaluate(task, match_expr, external_context=external_context)

# The input expression just has to be something that can be parsed as is by the engine.
script_engine.validate(input_expr)
Expand Down
6 changes: 0 additions & 6 deletions tests/SpiffWorkflow/dmn/feel_engine/FeelBoolDecisionTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,3 @@ def test_bool_decision_string_output2(self):
def test_bool_decision_string_output3(self):
res = self.runner.decide(None)
self.assertEqual(res.description, 'ELSE Row Annotation')

def suite():
return unittest.TestLoader().loadTestsFromTestCase(FeelBoolDecisionTestClass)

if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())

0 comments on commit ae3ed3e

Please sign in to comment.