diff --git a/assets/test_condition_empty.json b/assets/test_condition_empty.json new file mode 100644 index 0000000..e4443b6 --- /dev/null +++ b/assets/test_condition_empty.json @@ -0,0 +1,14 @@ +{ + "root": { + "variableType": "string", + "variableName": "temperatura", + "leafs": [ + { + "id": "9c784070-9c7c-4c58-a35f-c5c73bdb66e8", + "value": "true", + "operator": "empty", + "leafs": [{ "result": true }] + } + ] + } +} diff --git a/assets/test_is_not_null.json b/assets/test_is_not_null.json deleted file mode 100644 index 7c9e5df..0000000 --- a/assets/test_is_not_null.json +++ /dev/null @@ -1 +0,0 @@ -{"root":{"variableType":"string","variableName":"temperatura","leafs":[{"id":"9c784070-9c7c-4c58-a35f-c5c73bdb66e8","value":"None","operator":"is not","leafs":[{"result":true}]}]}} \ No newline at end of file diff --git a/jdtree/condition.py b/jdtree/condition.py index 5564ed6..8c2107d 100644 --- a/jdtree/condition.py +++ b/jdtree/condition.py @@ -1,6 +1,6 @@ -from typing import List +from typing import List, Any from numbers import Number - +from distutils.util import strtobool class Operator: @@ -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 \ No newline at end of file diff --git a/jdtree/jdt_engine.py b/jdtree/jdt_engine.py index 5da2123..df99d95 100644 --- a/jdtree/jdt_engine.py +++ b/jdtree/jdt_engine.py @@ -94,11 +94,13 @@ 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") @@ -106,14 +108,9 @@ def __compute_condition( 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 diff --git a/setup.py b/setup.py index 5389240..6744351 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/test_jdtree.py b/tests/test_jdtree.py index aab7be0..42880f3 100644 --- a/tests/test_jdtree.py +++ b/tests/test_jdtree.py @@ -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(