Skip to content

Commit

Permalink
chore: refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: develop-cs <[email protected]>
  • Loading branch information
develop-cs committed Jul 29, 2024
1 parent 1c0a9b4 commit 977ae87
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 201 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Features

* Add a new parameter `config_dict` in the `RulesEngine`'s constructor. It can be used when you have already loaded the YAML configuration in a dictionary and want to use it straightforward.
* Add a new parameter `ignored_rules` in the `RulesEngine`'s constructor. It can be used to easily disable a rule.
* Add a new parameter `ignored_rules` in the `apply_rules()` method. It can be used to easily disable a rule by its id.
* Split a rule set in two (or more) files (keep the rules organized by their file names [alphabetically sorted]).

### Fixes
Expand Down
26 changes: 14 additions & 12 deletions src/arta/_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def __init__(
rules_dict: dict[str, dict[str, Any]] | None = None,
config_path: str | None = None,
config_dict: dict[str, Any] | None = None,
ignored_rules: list[str] | None = None,
) -> None:
"""Initialize the rules.
Expand All @@ -64,16 +63,13 @@ def __init__(
rules_dict: A dictionary containing the rules' definitions.
config_path: Path of a directory containing the YAML files.
config_dict: A dictionary containing the configuration (same as YAML files but already
parsed in a dictionary).
parsed in a dictionary).
Raises:
KeyError: Key not found.
TypeError: Wrong type.
ValueError: Bad given parameters.
"""
# Set attributes
self.ignored_rules = set(ignored_rules) if ignored_rules is not None else set()

# Var init.
factory_mapping_classes: dict[str, type[BaseCondition]] = {}
std_condition_instances: dict[str, StandardCondition] = {}
Expand Down Expand Up @@ -159,7 +155,13 @@ def __init__(
)

def apply_rules(
self, input_data: dict[str, Any], *, rule_set: str | None = None, verbose: bool = False, **kwargs: Any
self,
input_data: dict[str, Any],
*,
rule_set: str | None = None,
ignored_rules: set[str] | None = None,
verbose: bool = False,
**kwargs: Any,
) -> dict[str, Any]:
"""Apply the rules and return results.
Expand All @@ -174,6 +176,7 @@ def apply_rules(
Args:
input_data: Input data to apply rules on.
rule_set: Apply rules associated with the specified rule set.
ignored_rules: A set/list of rule's ids to be ignored/disabled during evaluation.
verbose: If True, add extra ids (group_id, rule_id) for result explicability.
**kwargs: For user extra arguments.
Expand All @@ -194,6 +197,7 @@ def apply_rules(

# Var init.
input_data_copy: dict[str, Any] = copy.deepcopy(input_data)
ignored_ids: set[str] = ignored_rules if ignored_rules is not None else set()

# Prepare the result key
input_data_copy["output"] = {}
Expand All @@ -219,6 +223,10 @@ def apply_rules(

# Rules' loop (inside a group)
for rule in rules_list:
if rule._rule_id in ignored_ids:
# Ignore that rule
continue

# Apply rules
action_result, rule_details = rule.apply(
input_data_copy, parsing_error_strategy=self._parsing_error_strategy, **kwargs
Expand Down Expand Up @@ -308,9 +316,6 @@ def _build_rules(

# Looping through rules (inside a group)
for rule_id, rule_dict in group_rules.items():
if rule_id in self.ignored_rules:
continue

# Get action function
action_function_name: str = rule_dict[self.CONST_ACTION_CONF_KEY]

Expand Down Expand Up @@ -406,9 +411,6 @@ def _adapt_user_rules_dict(self, rules_dict: dict[str, dict[str, Any]]) -> dict[

# Looping through rules (inside a group)
for rule_id, rule_dict in group_rules.items():
if rule_id in self.ignored_rules:
continue

# Get action function
action = rule_dict["action"]

Expand Down
84 changes: 84 additions & 0 deletions tests/examples/ignored_rules/ignored_rules.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
rules:
default_rule_set:
admission:
ADM_OK:
condition: HAS_SCHOOL_AUTHORIZED_POWER
action: set_admission
action_parameters:
value: true
ADM_KO:
condition: null
action: set_admission
action_parameters:
value: false
course:
COURSE_ENGLISH:
condition: IS_SPEAKING_ENGLISH and not(IS_AGE_UNKNOWN)
action: set_student_course
action_parameters:
course_id: "english"
COURSE_SENIOR:
condition: IS_AGE_UNKNOWN
action: set_student_course
action_parameters:
course_id: "senior"
COURSE_INTERNATIONAL:
condition: not(IS_SPEAKING_ENGLISH)
action: set_student_course
action_parameters:
course_id: "international"
email:
EMAIL_COOK:
condition: HAS_SCHOOL_AUTHORIZED_POWER
action: send_email
action_parameters:
mail_to: "[email protected]"
mail_content: "Thanks for preparing once a month the following dish:"
meal: input.favorite_meal

conditions:
HAS_SCHOOL_AUTHORIZED_POWER:
description: "Does it have school authorized power?"
validation_function: has_authorized_super_power
condition_parameters:
authorized_powers:
- "strength"
- "fly"
- "immortality"
candidate_powers: input.powers
IS_SPEAKING_FRENCH:
description: "Does it speak french?"
validation_function: is_speaking_language
condition_parameters:
value: "french"
spoken_language: input.language
IS_SPEAKING_ENGLISH:
description: "Does it speak english?"
validation_function: is_speaking_language
condition_parameters:
value: "english"
spoken_language: input.language
IS_AGE_UNKNOWN:
description: "Do we know his age?"
validation_function: is_age_unknown
condition_parameters:
age: input.age
HAS_FAVORITE_MEAL:
description: "Does it have a favorite meal?"
validation_function: has_favorite_meal
condition_parameters:
favorite_meal: input.favorite_meal


conditions_source_modules:
- "tests.examples.code.conditions"
actions_source_modules:
- "tests.examples.code.actions"

parsing_error_strategy: raise

custom_classes_source_modules:
- "tests.examples.code.custom_class"
condition_factory_mapping:
custom_condition: "CustomCondition"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ rules:
action_parameters:
value: true
IGNORED_RULE_2:
simple_condition: input.age>1
simple_condition: input.dummy>1
action: set_admission
action_parameters:
value: true
Expand All @@ -22,3 +22,5 @@ rules:
action: set_admission
action_parameters:
value: false

parsing_error_strategy: raise
Loading

0 comments on commit 977ae87

Please sign in to comment.