ripple-down-rules 0.5.95__py3-none-any.whl → 0.5.97__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 +23 -9
- ripple_down_rules/rdr_decorators.py +17 -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.97.dist-info}/METADATA +1 -1
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.97.dist-info}/RECORD +12 -12
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.97.dist-info}/WHEEL +0 -0
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.97.dist-info}/licenses/LICENSE +0 -0
- {ripple_down_rules-0.5.95.dist-info → ripple_down_rules-0.5.97.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
|
@@ -673,12 +676,12 @@ class SingleClassRDR(RDRWithCodeWriter):
|
|
673
676
|
:param case_query: The case query containing the case and the target category to compare the case with.
|
674
677
|
"""
|
675
678
|
pred = self.evaluate(case)
|
676
|
-
conclusion = pred.conclusion(case) if pred is not None else
|
679
|
+
conclusion = pred.conclusion(case) if pred is not None else self.default_conclusion
|
677
680
|
if pred is not None and pred.fired and case_query is not None:
|
678
681
|
if pred.corner_case_metadata is None and conclusion is not None \
|
679
682
|
and type(conclusion) in case_query.core_attribute_type:
|
680
683
|
pred.corner_case_metadata = CaseFactoryMetaData.from_case_query(case_query)
|
681
|
-
return conclusion
|
684
|
+
return conclusion
|
682
685
|
|
683
686
|
def evaluate(self, case: Case) -> SingleClassRule:
|
684
687
|
"""
|
@@ -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):
|
@@ -31,7 +31,8 @@ class RDRDecorator:
|
|
31
31
|
expert: Optional[Expert] = None,
|
32
32
|
update_existing_rules: bool = True,
|
33
33
|
viewer: Optional[RDRCaseViewer] = None,
|
34
|
-
package_name: Optional[str] = None
|
34
|
+
package_name: Optional[str] = None,
|
35
|
+
use_generated_classifier: bool = False):
|
35
36
|
"""
|
36
37
|
:param models_dir: The directory to save/load the RDR models.
|
37
38
|
:param output_type: The type of the output. This is used to create the RDR model.
|
@@ -47,6 +48,7 @@ class RDRDecorator:
|
|
47
48
|
even if they gave an output.
|
48
49
|
:param viewer: The viewer to use for the RDR model. If None, no viewer will be used.
|
49
50
|
:param package_name: The package name to use for relative imports in the RDR model.
|
51
|
+
:param use_generated_classifier: If True, the function will use the generated classifier instead of the RDR model.
|
50
52
|
:return: A decorator to use a GeneralRDR as a classifier that monitors and modifies the function's output.
|
51
53
|
"""
|
52
54
|
self.rdr_models_dir = models_dir
|
@@ -60,6 +62,8 @@ class RDRDecorator:
|
|
60
62
|
self.update_existing_rules = update_existing_rules
|
61
63
|
self.viewer = viewer
|
62
64
|
self.package_name = package_name
|
65
|
+
self.use_generated_classifier = use_generated_classifier
|
66
|
+
self.generated_classifier: Optional[Callable] = None
|
63
67
|
self.load()
|
64
68
|
|
65
69
|
def decorator(self, func: Callable) -> Callable:
|
@@ -67,17 +71,17 @@ class RDRDecorator:
|
|
67
71
|
@wraps(func)
|
68
72
|
def wrapper(*args, **kwargs) -> Optional[Any]:
|
69
73
|
|
70
|
-
if len(self.parsed_output_type) == 0:
|
71
|
-
self.parsed_output_type = self.parse_output_type(func, self.output_type, *args)
|
72
74
|
if self.model_name is None:
|
73
75
|
self.initialize_rdr_model_name_and_load(func)
|
74
|
-
if self.expert is None:
|
75
|
-
self.expert = Human(viewer=self.viewer,
|
76
|
-
answers_save_path=self.rdr_models_dir + f'/{self.model_name}/expert_answers')
|
77
76
|
|
78
77
|
func_output = {self.output_name: func(*args, **kwargs)}
|
79
78
|
|
80
79
|
if self.fit:
|
80
|
+
if len(self.parsed_output_type) == 0:
|
81
|
+
self.parsed_output_type = self.parse_output_type(func, self.output_type, *args)
|
82
|
+
if self.expert is None:
|
83
|
+
self.expert = Human(viewer=self.viewer,
|
84
|
+
answers_save_path=self.rdr_models_dir + f'/{self.model_name}/expert_answers')
|
81
85
|
case_query = self.create_case_query_from_method(func, func_output,
|
82
86
|
self.parsed_output_type,
|
83
87
|
self.mutual_exclusive,
|
@@ -87,7 +91,13 @@ class RDRDecorator:
|
|
87
91
|
viewer=self.viewer)
|
88
92
|
else:
|
89
93
|
case, case_dict = self.create_case_from_method(func, func_output, *args, **kwargs)
|
90
|
-
|
94
|
+
if self.use_generated_classifier:
|
95
|
+
if self.generated_classifier is None:
|
96
|
+
model_path = os.path.join(self.rdr_models_dir, self.model_name)
|
97
|
+
self.generated_classifier = self.rdr.get_rdr_classifier_from_python_file(model_path)
|
98
|
+
output = self.generated_classifier(case)
|
99
|
+
else:
|
100
|
+
output = self.rdr.classify(case)
|
91
101
|
|
92
102
|
if self.output_name in output:
|
93
103
|
return output[self.output_name]
|
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.97
|
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=S8_aIL6wZaWbPQRl_qf9q65NR3nW8mnLIrjc-Tqvvaw,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=
|
5
|
-
ripple_down_rules/rdr_decorators.py,sha256=
|
6
|
-
ripple_down_rules/rules.py,sha256=
|
4
|
+
ripple_down_rules/rdr.py,sha256=CPSjSmdzUN5meBakr9T6B-ZT1KmLjRAJ6JIL3UIRzyI,56132
|
5
|
+
ripple_down_rules/rdr_decorators.py,sha256=JWJn2MAKJG1YbLLWUZMzYfS3t1mEOAi21S8NUY7hy0Q,9913
|
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.97.dist-info/licenses/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
|
21
|
+
ripple_down_rules-0.5.97.dist-info/METADATA,sha256=ATXJ3z-nJQQ3aB9aFaEnEfXLJ7njVA1Kh1s40tJPBV0,48214
|
22
|
+
ripple_down_rules-0.5.97.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
23
|
+
ripple_down_rules-0.5.97.dist-info/top_level.txt,sha256=VeoLhEhyK46M1OHwoPbCQLI1EifLjChqGzhQ6WEUqeM,18
|
24
|
+
ripple_down_rules-0.5.97.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|