Skip to content

Commit

Permalink
Merge pull request #10 from mattskinosix/9-add-empty-operator
Browse files Browse the repository at this point in the history
Add empty operator
  • Loading branch information
mattskinosix authored Feb 1, 2023
2 parents df8ac09 + 4ed98ae commit 5ecadbf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
14 changes: 14 additions & 0 deletions assets/test_condition_empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"root": {
"variableType": "string",
"variableName": "temperatura",
"leafs": [
{
"id": "9c784070-9c7c-4c58-a35f-c5c73bdb66e8",
"value": "true",
"operator": "empty",
"leafs": [{ "result": true }]
}
]
}
}
1 change: 0 additions & 1 deletion assets/test_is_not_null.json

This file was deleted.

14 changes: 12 additions & 2 deletions jdtree/condition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List
from typing import List, Any
from numbers import Number

from distutils.util import strtobool

class Operator:

Expand All @@ -15,3 +15,13 @@ def not_between(self, x: Number, y: str):
if len(list) != 2:
return False
return not eval(f'{list[0]} <= {x} <= {list[1]}')

def empty(self, x: Any, y: str):

if x and strtobool(y):
return True

if not x and not strtobool(y):
return True

return False
21 changes: 9 additions & 12 deletions jdtree/jdt_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,23 @@ def __compute_condition(
except AttributeError:
pass
try:
eval_string = self.get_eval_string(variable_type, target_value, operator, variable_value)
eval_string = self.get_eval_string(
variable_type, target_value, operator, variable_value)
print(eval_string)
result = eval(eval_string)
if result:
logging.info(f"TRUE {variable_type}('{target_value}') {operator} {variable_type}('{variable_value}')")
logging.info(
f"TRUE {variable_type}('{target_value}') {operator} {variable_type}('{variable_value}')")
return result
except SyntaxError:
raise UnsupportedOperation(f"{operator} not supported")

def get_eval_string(self, variable_type, target_value, operator, variable_value) -> str:

variable_type_python = TO_PYTHON_TYPE.get(variable_type)
if variable_type == STRING_TYPE or variable_type == NUMBER_TYPE:
return f"{variable_type_python}('{target_value}') {operator} {variable_type_python}('{variable_value}')"
if variable_type == OBJECT_TYPE:
return f"{target_value} {operator} {variable_value}"

if variable_value == 'None':
variable_value = None

if variable_value:
variable_value = f"{variable_type_python}('{variable_value}')"

if target_value:
target_value = f"{variable_type_python}('{target_value}')"

return f"{target_value} {operator} {variable_value}"
raise Exception
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='jdtree',
packages=['jdtree'],
version='0.0.3',
version='0.0.4',
description='Json Decision Table/Tree',
author='Matteo Greco',
license='MIT',
Expand Down
4 changes: 2 additions & 2 deletions tests/test_jdtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_decision(self):
})
assert result == aspected_result

def test_decision_is_not_null(self):
tree = JDTEngine(file_path="assets/test_is_not_null.json")
def test_condition_empty(self):
tree = JDTEngine(file_path="assets/test_condition_empty.json")

aspected_result = [{'result': True}]
result = tree.decide(
Expand Down

0 comments on commit 5ecadbf

Please sign in to comment.