ripple-down-rules 0.0.12__py3-none-any.whl → 0.0.13__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/rdr.py CHANGED
@@ -17,7 +17,7 @@ from .utils import draw_tree, make_set, get_attribute_by_type, copy_case, \
17
17
  get_hint_for_attribute, SubclassJSONSerializer, is_iterable, make_list
18
18
 
19
19
 
20
- class RippleDownRules(ABC):
20
+ class RippleDownRules(SubclassJSONSerializer, ABC):
21
21
  """
22
22
  The abstract base class for the ripple down rules classifiers.
23
23
  """
@@ -175,10 +175,7 @@ class RippleDownRules(ABC):
175
175
  return conclusion_type in case
176
176
 
177
177
 
178
- RDR = RippleDownRules
179
-
180
-
181
- class RDRWithCodeWriter(RDR, ABC):
178
+ class RDRWithCodeWriter(RippleDownRules, ABC):
182
179
 
183
180
  @abstractmethod
184
181
  def write_rules_as_source_code_to_file(self, rule: Rule, file, parent_indent: str = ""):
@@ -260,7 +257,7 @@ class RDRWithCodeWriter(RDR, ABC):
260
257
  return type(self.start_rule.conclusion)
261
258
 
262
259
 
263
- class SingleClassRDR(RDRWithCodeWriter, SubclassJSONSerializer):
260
+ class SingleClassRDR(RDRWithCodeWriter):
264
261
 
265
262
  def fit_case(self, case_query: CaseQuery, expert: Optional[Expert] = None, **kwargs) \
266
263
  -> Union[CaseAttribute, CallableExpression]:
@@ -353,17 +350,15 @@ class MultiClassRDR(RDRWithCodeWriter):
353
350
  The conditions of the stopping rule if needed.
354
351
  """
355
352
 
356
- def __init__(self, start_rules: Optional[List[Rule]] = None,
353
+ def __init__(self, start_rule: Optional[Rule] = None,
357
354
  mode: MCRDRMode = MCRDRMode.StopOnly, session: Optional[Session] = None):
358
355
  """
359
- :param start_rules: The starting rules for the classifier, these are the rules that are at the top of the tree
360
- and are always checked, in contrast to the refinement and alternative rules which are only checked if the
361
- starting rules fire or not.
356
+ :param start_rule: The starting rules for the classifier.
362
357
  :param mode: The mode of the classifier, either StopOnly or StopPlusRule, or StopPlusRuleCombined.
363
358
  :param session: The sqlalchemy orm session.
364
359
  """
365
- self.start_rules = [MultiClassTopRule()] if not start_rules else start_rules
366
- super(MultiClassRDR, self).__init__(self.start_rules[0], session=session)
360
+ start_rule = MultiClassTopRule() if not start_rule else start_rule
361
+ super(MultiClassRDR, self).__init__(start_rule, session=session)
367
362
  self.mode: MCRDRMode = mode
368
363
 
369
364
  def classify(self, case: Union[Case, SQLTable]) -> List[Any]:
@@ -614,6 +609,17 @@ class MultiClassRDR(RDRWithCodeWriter):
614
609
  """
615
610
  self.start_rule.alternative = MultiClassTopRule(conditions, conclusion, corner_case=corner_case)
616
611
 
612
+ def _to_json(self) -> Dict[str, Any]:
613
+ return {"start_rule": self.start_rule.to_json()}
614
+
615
+ @classmethod
616
+ def _from_json(cls, data: Dict[str, Any]) -> Self:
617
+ """
618
+ Create an instance of the class from a json
619
+ """
620
+ start_rule = SingleClassRule.from_json(data["start_rule"])
621
+ return cls(start_rule)
622
+
617
623
 
618
624
  class GeneralRDR(RippleDownRules):
619
625
  """
@@ -265,14 +265,17 @@ class MultiClassStopRule(Rule, HasAlternativeRule):
265
265
 
266
266
  def _to_json(self) -> Dict[str, Any]:
267
267
  self.json_serialization = {**Rule._to_json(self),
268
- "top_rule": self.top_rule.to_json(),
269
268
  "alternative": self.alternative.to_json() if self.alternative is not None else None}
270
269
  return self.json_serialization
271
270
 
272
271
  @classmethod
273
272
  def _from_json(cls, data: Dict[str, Any]) -> MultiClassStopRule:
274
- loaded_rule = Rule._from_json(data)
275
- loaded_rule.top_rule = MultiClassTopRule.from_json(data["top_rule"])
273
+ loaded_rule = super(MultiClassStopRule, cls)._from_json(data)
274
+ # The following is done to prevent re-initialization of the top rule,
275
+ # so the top rule that is already initialized is passed in the data instead of its json serialization.
276
+ loaded_rule.top_rule = data['top_rule']
277
+ if data['alternative'] is not None:
278
+ data['alternative']['top_rule'] = data['top_rule']
276
279
  loaded_rule.alternative = MultiClassStopRule.from_json(data["alternative"])
277
280
  return loaded_rule
278
281
 
@@ -312,7 +315,11 @@ class MultiClassTopRule(Rule, HasRefinementRule, HasAlternativeRule):
312
315
 
313
316
  @classmethod
314
317
  def _from_json(cls, data: Dict[str, Any]) -> MultiClassTopRule:
315
- loaded_rule = Rule._from_json(data)
318
+ loaded_rule = super(MultiClassTopRule, cls)._from_json(data)
319
+ # The following is done to prevent re-initialization of the top rule,
320
+ # so the top rule that is already initialized is passed in the data instead of its json serialization.
321
+ if data['refinement'] is not None:
322
+ data['refinement']['top_rule'] = loaded_rule
316
323
  loaded_rule.refinement = MultiClassStopRule.from_json(data["refinement"])
317
324
  loaded_rule.alternative = MultiClassTopRule.from_json(data["alternative"])
318
325
  return loaded_rule
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ripple_down_rules
3
- Version: 0.0.12
3
+ Version: 0.0.13
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
@@ -3,16 +3,16 @@ ripple_down_rules/datasets.py,sha256=qYX7IF7ACm0VRbaKfEgQ32j0YbUUyt2GfGU5Lo42CqI
3
3
  ripple_down_rules/experts.py,sha256=wg1uY0ox9dUMR4s1RdGjzpX1_WUqnCa060r1U9lrKYI,11214
4
4
  ripple_down_rules/failures.py,sha256=E6ajDUsw3Blom8eVLbA7d_Qnov2conhtZ0UmpQ9ZtSE,302
5
5
  ripple_down_rules/prompt.py,sha256=QAmxg4ssrGUAlK7lbyKw2nuRczTZColpjc9uMC1ts3I,4210
6
- ripple_down_rules/rdr.py,sha256=pczC9h5jgT810U1QYGnavqUzbbdRMGqJ9DBnyeGZxGE,37443
7
- ripple_down_rules/rules.py,sha256=PaQMPhMZFlwQnx9Iz4TFonldsSFX8fz4-SKxtAMulbA,13080
6
+ ripple_down_rules/rdr.py,sha256=4HB_H8PNh8pqucmX9JAOeOql7EF5ajYSYUzZ08HG4Z4,37549
7
+ ripple_down_rules/rules.py,sha256=SWqnmP9BN2JlKPOWXviEvVjcP0A4b44Sxhv3y7He-YY,13615
8
8
  ripple_down_rules/utils.py,sha256=pmjIPjF9wq6dmCHCoUKFRsJmSaI0b-hRLn375WHgWgQ,18010
9
9
  ripple_down_rules/datastructures/__init__.py,sha256=zpmiYm4WkwNHaGdTIfacS7llN5d2xyU6U-saH_TpydI,103
10
10
  ripple_down_rules/datastructures/callable_expression.py,sha256=TN6bi4VYjyLlSLTEA3dRo5ENfEdQYc8Fjj5nbnsz-C0,9058
11
11
  ripple_down_rules/datastructures/case.py,sha256=UYHz-Ag7TjwP9MRQuUTVH0nmph6ddutTSZMEXGFkxf4,13676
12
12
  ripple_down_rules/datastructures/dataclasses.py,sha256=EVQ1jBKW7K7q7_JNgikHX9fm3EmQQKA74sNjEQ4rXn8,2449
13
13
  ripple_down_rules/datastructures/enums.py,sha256=l0Eu-TeJ6qB2XHoJycXmUgLw-3yUebQ8SsEbW8bBZdM,4543
14
- ripple_down_rules-0.0.12.dist-info/licenses/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
15
- ripple_down_rules-0.0.12.dist-info/METADATA,sha256=7h-kRihJGfF19iAvIZJa_iy7esfmmP982yO_AsYeXFo,42519
16
- ripple_down_rules-0.0.12.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
17
- ripple_down_rules-0.0.12.dist-info/top_level.txt,sha256=VeoLhEhyK46M1OHwoPbCQLI1EifLjChqGzhQ6WEUqeM,18
18
- ripple_down_rules-0.0.12.dist-info/RECORD,,
14
+ ripple_down_rules-0.0.13.dist-info/licenses/LICENSE,sha256=ixuiBLtpoK3iv89l7ylKkg9rs2GzF9ukPH7ynZYzK5s,35148
15
+ ripple_down_rules-0.0.13.dist-info/METADATA,sha256=HplzC77nnv95bL4x1bxRQI2CHzQMyBOFRJNHff2brzs,42519
16
+ ripple_down_rules-0.0.13.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
17
+ ripple_down_rules-0.0.13.dist-info/top_level.txt,sha256=VeoLhEhyK46M1OHwoPbCQLI1EifLjChqGzhQ6WEUqeM,18
18
+ ripple_down_rules-0.0.13.dist-info/RECORD,,