ripple-down-rules 0.5.95__py3-none-any.whl → 0.5.96__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ripple_down_rules/__init__.py +1 -1
- ripple_down_rules/datastructures/callable_expression.py +7 -1
- ripple_down_rules/datastructures/dataclasses.py +1 -1
- ripple_down_rules/rdr.py +21 -7
- ripple_down_rules/rules.py +19 -0
- ripple_down_rules/utils.py +15 -1
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.96.dist-info}/METADATA +1 -1
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.96.dist-info}/RECORD +11 -11
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.96.dist-info}/WHEEL +0 -0
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.96.dist-info}/licenses/LICENSE +0 -0
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.96.dist-info}/top_level.txt +0 -0
ripple_down_rules/__init__.py
CHANGED
@@ -267,10 +267,16 @@ class CallableExpression(SubclassJSONSerializer):
|
|
267
267
|
|
268
268
|
@classmethod
|
269
269
|
def _from_json(cls, data: Dict[str, Any]) -> CallableExpression:
|
270
|
+
scope = {}
|
271
|
+
for k, v in data['scope'].items():
|
272
|
+
try:
|
273
|
+
scope[k] = get_type_from_string(v)
|
274
|
+
except ModuleNotFoundError:
|
275
|
+
pass
|
270
276
|
return cls(user_input=data["user_input"],
|
271
277
|
conclusion_type=tuple(get_type_from_string(t) for t in data["conclusion_type"])
|
272
278
|
if data["conclusion_type"] else None,
|
273
|
-
scope=
|
279
|
+
scope=scope,
|
274
280
|
conclusion=SubclassJSONSerializer.from_json(data["conclusion"]),
|
275
281
|
mutually_exclusive=data["mutually_exclusive"])
|
276
282
|
|
@@ -139,7 +139,7 @@ class CaseQuery:
|
|
139
139
|
attribute_types_str = f"Union[{', '.join([t.__name__ for t in self.core_attribute_type])}]"
|
140
140
|
else:
|
141
141
|
attribute_types_str = self.core_attribute_type[0].__name__
|
142
|
-
if
|
142
|
+
if all(t in self.attribute_type for t in [list, set]) and len(self.core_attribute_type) > 2:
|
143
143
|
return f"List[{attribute_types_str}]"
|
144
144
|
else:
|
145
145
|
return attribute_types_str
|
ripple_down_rules/rdr.py
CHANGED
@@ -4,6 +4,7 @@ import importlib
|
|
4
4
|
import os
|
5
5
|
from abc import ABC, abstractmethod
|
6
6
|
from copy import copy
|
7
|
+
from types import NoneType
|
7
8
|
|
8
9
|
from ripple_down_rules.datastructures.dataclasses import CaseFactoryMetaData
|
9
10
|
from . import logger
|
@@ -144,7 +145,7 @@ class RippleDownRules(SubclassJSONSerializer, ABC):
|
|
144
145
|
try:
|
145
146
|
rdr.update_from_python(model_dir, package_name=package_name)
|
146
147
|
rdr.to_json_file(json_file)
|
147
|
-
except (FileNotFoundError, ValueError) as e:
|
148
|
+
except (FileNotFoundError, ValueError, SyntaxError) as e:
|
148
149
|
logger.warning(f"Could not load the python file for the model {model_name} from {model_dir}. "
|
149
150
|
f"Make sure the file exists and is valid.")
|
150
151
|
rdr.save(save_dir=load_dir, model_name=model_name, package_name=package_name)
|
@@ -546,6 +547,8 @@ class RDRWithCodeWriter(RippleDownRules, ABC):
|
|
546
547
|
main_types = set()
|
547
548
|
main_types.add(self.case_type)
|
548
549
|
main_types.update(make_set(self.conclusion_type))
|
550
|
+
main_types.update({Union, Optional})
|
551
|
+
defs_types.add(Union)
|
549
552
|
main_types.update({Case, create_case})
|
550
553
|
main_types = main_types.difference(defs_types)
|
551
554
|
return main_types, defs_types, cases_types
|
@@ -689,9 +692,8 @@ class SingleClassRDR(RDRWithCodeWriter):
|
|
689
692
|
|
690
693
|
def _write_to_python(self, model_dir: str, package_name: Optional[str] = None):
|
691
694
|
super()._write_to_python(model_dir, package_name=package_name)
|
692
|
-
|
693
|
-
|
694
|
-
f.write(f"{' ' * 4}else:\n{' ' * 8}return {self.default_conclusion}\n")
|
695
|
+
with open(model_dir + f"/{self.generated_python_file_name}.py", "a") as f:
|
696
|
+
f.write(f"{' ' * 4}else:\n{' ' * 8}return {self.default_conclusion}\n")
|
695
697
|
|
696
698
|
def write_rules_as_source_code_to_file(self, rule: SingleClassRule, filename: str, parent_indent: str = "",
|
697
699
|
defs_file: Optional[str] = None, cases_file: Optional[str] = None,
|
@@ -719,7 +721,19 @@ class SingleClassRDR(RDRWithCodeWriter):
|
|
719
721
|
|
720
722
|
@property
|
721
723
|
def conclusion_type_hint(self) -> str:
|
722
|
-
|
724
|
+
all_types = set(list(self.conclusion_type) + [type(self.default_conclusion)])
|
725
|
+
if NoneType in all_types:
|
726
|
+
return f"Optional[{', '.join([t.__name__ for t in all_types if t is not NoneType])}]"
|
727
|
+
return f"Union[{', '.join([t.__name__ for t in all_types])}]"
|
728
|
+
|
729
|
+
def _get_types_to_import(self) -> Tuple[Set[Type], Set[Type], Set[Type]]:
|
730
|
+
main_types, def_types, case_types = super()._get_types_to_import()
|
731
|
+
main_types.add(type(self.default_conclusion))
|
732
|
+
def_types.add(type(self.default_conclusion))
|
733
|
+
if self.default_conclusion is None:
|
734
|
+
main_types.add(Optional)
|
735
|
+
def_types.add(Optional)
|
736
|
+
return main_types, def_types, case_types
|
723
737
|
|
724
738
|
@property
|
725
739
|
def conclusion_type(self) -> Tuple[Type]:
|
@@ -857,8 +871,8 @@ class MultiClassRDR(RDRWithCodeWriter):
|
|
857
871
|
|
858
872
|
def _get_types_to_import(self) -> Tuple[Set[Type], Set[Type], Set[Type]]:
|
859
873
|
main_types, defs_types, cases_types = super()._get_types_to_import()
|
860
|
-
main_types.update({Set,
|
861
|
-
defs_types.
|
874
|
+
main_types.update({Set, make_set})
|
875
|
+
defs_types.update({List, Set})
|
862
876
|
return main_types, defs_types, cases_types
|
863
877
|
|
864
878
|
def update_start_rule(self, case_query: CaseQuery, expert: Expert):
|
ripple_down_rules/rules.py
CHANGED
@@ -4,6 +4,7 @@ import logging
|
|
4
4
|
import re
|
5
5
|
from abc import ABC, abstractmethod
|
6
6
|
from pathlib import Path
|
7
|
+
from types import NoneType
|
7
8
|
from uuid import uuid4
|
8
9
|
|
9
10
|
from anytree import NodeMixin
|
@@ -162,6 +163,22 @@ class Rule(NodeMixin, SubclassJSONSerializer, ABC):
|
|
162
163
|
# use regex to replace the function name
|
163
164
|
new_function_name = f"def conclusion_{self.uid}"
|
164
165
|
conclusion_lines[0] = re.sub(r"def (\w+)", new_function_name, conclusion_lines[0])
|
166
|
+
# add type hint
|
167
|
+
if len(self.conclusion.conclusion_type) == 1:
|
168
|
+
hint = self.conclusion.conclusion_type[0].__name__
|
169
|
+
else:
|
170
|
+
if (all(t in self.conclusion.conclusion_type for t in [list, set])
|
171
|
+
and len(self.conclusion.conclusion_type) > 2):
|
172
|
+
type_names = [t.__name__ for t in self.conclusion.conclusion_type if t not in [list, set]]
|
173
|
+
hint = f"List[{', '.join(type_names)}]"
|
174
|
+
else:
|
175
|
+
if NoneType in self.conclusion.conclusion_type:
|
176
|
+
type_names = [t.__name__ for t in self.conclusion.conclusion_type if t is not NoneType]
|
177
|
+
hint = f"Optional[{', '.join(type_names)}]"
|
178
|
+
else:
|
179
|
+
type_names = [t.__name__ for t in self.conclusion.conclusion_type]
|
180
|
+
hint = f"Union[{', '.join(type_names)}]"
|
181
|
+
conclusion_lines[0] = conclusion_lines[0].replace("):", f") -> {hint}:")
|
165
182
|
func_call = f"{parent_indent} return {new_function_name.replace('def ', '')}(case)\n"
|
166
183
|
return "\n".join(conclusion_lines).strip(' '), func_call
|
167
184
|
else:
|
@@ -184,6 +201,8 @@ class Rule(NodeMixin, SubclassJSONSerializer, ABC):
|
|
184
201
|
# use regex to replace the function name
|
185
202
|
new_function_name = f"def conditions_{self.uid}"
|
186
203
|
conditions_lines[0] = re.sub(r"def (\w+)", new_function_name, conditions_lines[0])
|
204
|
+
# add type hint
|
205
|
+
conditions_lines[0] = conditions_lines[0].replace('):', ') -> bool:')
|
187
206
|
def_code = "\n".join(conditions_lines)
|
188
207
|
with open(defs_file, 'a') as f:
|
189
208
|
f.write(def_code.strip() + "\n\n\n")
|
ripple_down_rules/utils.py
CHANGED
@@ -918,6 +918,8 @@ def get_imports_from_types(type_objs: Iterable[Type],
|
|
918
918
|
name = type(tp).__qualname__
|
919
919
|
else:
|
920
920
|
continue
|
921
|
+
if name == "NoneType":
|
922
|
+
module = "types"
|
921
923
|
if module is None or module == 'builtins' or module.startswith('_')\
|
922
924
|
or module in sys.builtin_module_names or module in excluded_modules or "<" in module \
|
923
925
|
or name in exclueded_names:
|
@@ -1053,7 +1055,19 @@ def get_type_from_string(type_path: str):
|
|
1053
1055
|
:param type_path: The path to the type.
|
1054
1056
|
"""
|
1055
1057
|
module_path, class_name = type_path.rsplit(".", 1)
|
1056
|
-
|
1058
|
+
try:
|
1059
|
+
module = importlib.import_module(module_path)
|
1060
|
+
except ModuleNotFoundError:
|
1061
|
+
module_path_parts = module_path.split(".")
|
1062
|
+
idx = -1
|
1063
|
+
while True:
|
1064
|
+
try:
|
1065
|
+
module = importlib.import_module('.'.join(module_path_parts[:idx]))
|
1066
|
+
break
|
1067
|
+
except ModuleNotFoundError:
|
1068
|
+
idx -= 1
|
1069
|
+
if abs(idx) > len(module_path_parts):
|
1070
|
+
raise
|
1057
1071
|
if module == builtins and class_name == 'NoneType':
|
1058
1072
|
return type(None)
|
1059
1073
|
return getattr(module, class_name)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: ripple_down_rules
|
3
|
-
Version: 0.5.
|
3
|
+
Version: 0.5.96
|
4
4
|
Summary: Implements the various versions of Ripple Down Rules (RDR) for knowledge representation and reasoning.
|
5
5
|
Author-email: Abdelrhman Bassiouny <abassiou@uni-bremen.de>
|
6
6
|
License: GNU GENERAL PUBLIC LICENSE
|
@@ -1,15 +1,15 @@
|
|
1
|
-
ripple_down_rules/__init__.py,sha256=
|
1
|
+
ripple_down_rules/__init__.py,sha256=Vzq2IW363zvjMh2-NN_nmo2dJIZaUhzlFSMP3GpB8i0,100
|
2
2
|
ripple_down_rules/experts.py,sha256=4-dMIVeMzFXCLYl_XBG_P7_Xs4sZih9-vZxCIPri6dA,12958
|
3
3
|
ripple_down_rules/helpers.py,sha256=sY8nFbYdLOO6EG5UQugCCxjSjcCQsDUCPgawfQA4Ui8,4495
|
4
|
-
ripple_down_rules/rdr.py,sha256=
|
4
|
+
ripple_down_rules/rdr.py,sha256=JqNPCUHDJ9xBg8pReYAMas9hlC_BBRZdqtl1cO2NMmU,56177
|
5
5
|
ripple_down_rules/rdr_decorators.py,sha256=riAFmL8jc1rw9dFkNbT023CfB5FbmOWHNiVKO2bAXsE,9195
|
6
|
-
ripple_down_rules/rules.py,sha256=
|
6
|
+
ripple_down_rules/rules.py,sha256=Dk4yGCy5oV10mOv5rRLcmtIu9J60WBPol9b7ELFn6fY,21522
|
7
7
|
ripple_down_rules/start-code-server.sh,sha256=otClk7VmDgBOX2TS_cjws6K0UwvgAUJhoA0ugkPCLqQ,949
|
8
|
-
ripple_down_rules/utils.py,sha256=
|
8
|
+
ripple_down_rules/utils.py,sha256=vFoz5HcRgkf7_s-zdWSg54xI9PCMcdDzcdFGdas7KBA,62350
|
9
9
|
ripple_down_rules/datastructures/__init__.py,sha256=V2aNgf5C96Y5-IGghra3n9uiefpoIm_QdT7cc_C8cxQ,111
|
10
|
-
ripple_down_rules/datastructures/callable_expression.py,sha256=
|
10
|
+
ripple_down_rules/datastructures/callable_expression.py,sha256=YHlEYc9Ye_4gYAqqj_TyCB363WpZ8lXH2LLRl1CCGe8,12934
|
11
11
|
ripple_down_rules/datastructures/case.py,sha256=PJ7_-AdxYic6BO5z816piFODj6nU5J6Jt1YzTFH-dds,15510
|
12
|
-
ripple_down_rules/datastructures/dataclasses.py,sha256=
|
12
|
+
ripple_down_rules/datastructures/dataclasses.py,sha256=D-nrVEW_27njmDGkyiHRnq5lmqEdO8RHKnLb1mdnwrA,11054
|
13
13
|
ripple_down_rules/datastructures/enums.py,sha256=ce7tqS0otfSTNAOwsnXlhsvIn4iW_Y_N3TNebF3YoZs,5700
|
14
14
|
ripple_down_rules/user_interface/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
15
15
|
ripple_down_rules/user_interface/gui.py,sha256=_lgZAUXxxaBUFQJAHjA5TBPp6XEvJ62t-kSN8sPsocE,27379
|
@@ -17,8 +17,8 @@ ripple_down_rules/user_interface/ipython_custom_shell.py,sha256=yp-F8YRWGhj1PLB3
|
|
17
17
|
ripple_down_rules/user_interface/object_diagram.py,sha256=FEa2HaYR9QmTE6NsOwBvZ0jqmu3DKyg6mig2VE5ZP4Y,4956
|
18
18
|
ripple_down_rules/user_interface/prompt.py,sha256=JceEUGYsd0lIvd-v2y3D3swoo96_C0lxfp3CxM7Vfts,8900
|
19
19
|
ripple_down_rules/user_interface/template_file_creator.py,sha256=kwBbFLyN6Yx2NTIHPSwOoytWgbJDYhgrUOVFw_jkDQ4,13522
|
20
|
-
ripple_down_rules-0.5.
|
21
|
-
ripple_down_rules-0.5.
|
22
|
-
ripple_down_rules-0.5.
|
23
|
-
ripple_down_rules-0.5.
|
24
|
-
ripple_down_rules-0.5.
|
20
|
+
ripple_down_rules-0.5.96.dist-info/licenses/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
|
21
|
+
ripple_down_rules-0.5.96.dist-info/METADATA,sha256=k8TDjtJGrXRcD7pOO6fxVD2kqEJf4Em7_ro1h55jjZ0,48214
|
22
|
+
ripple_down_rules-0.5.96.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
23
|
+
ripple_down_rules-0.5.96.dist-info/top_level.txt,sha256=VeoLhEhyK46M1OHwoPbCQLI1EifLjChqGzhQ6WEUqeM,18
|
24
|
+
ripple_down_rules-0.5.96.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|