diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d01c03..af7b69f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.2.0] - 2021-11-02 + +### Added + +- added add_rule method to RuleParser + + ## [0.1.3] - 2021-06-15 ### Fixed @@ -74,6 +81,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 Initial release +[0.2.0]: https://github.com/manfred-kaiser/business-rule-engine/compare/0.1.3...0.2.0 [0.1.3]: https://github.com/manfred-kaiser/business-rule-engine/compare/0.1.2...0.1.3 [0.1.2]: https://github.com/manfred-kaiser/business-rule-engine/compare/0.1.1...0.1.2 [0.1.1]: https://github.com/manfred-kaiser/business-rule-engine/compare/0.1.0...0.1.1 diff --git a/business_rule_engine/__init__.py b/business_rule_engine/__init__.py index 81ca2aa..4a1f918 100644 --- a/business_rule_engine/__init__.py +++ b/business_rule_engine/__init__.py @@ -122,6 +122,14 @@ def parsestr(self, text: Text) -> None: if rulename and is_action and not ignore_line: self.rules[rulename].actions.append(line.strip()) + def add_rule(self, rulename: Text, condition: Text, action: Text) -> None: + if rulename in self.rules: + raise DuplicateRuleName("Rule '{}' already exists!".format(rulename)) + rule = Rule(rulename) + rule.conditions.append(condition) + rule.actions.append(action) + self.rules[rulename] = rule + @classmethod def register_function(cls, function: Any, function_name: Optional[Text] = None) -> None: custom_function_name = function_name or function.__name__ diff --git a/setup.py b/setup.py index 498009e..e57df6c 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setup( name='business-rule-engine', - version='0.1.3', + version='0.2.0', author='Manfred Kaiser', author_email='manfred.kaiser@logfile.at', description='Python DSL for setting up business intelligence rules', diff --git a/tests/test_rules.py b/tests/test_rules.py index cbfb2be..02434b2 100644 --- a/tests/test_rules.py +++ b/tests/test_rules.py @@ -94,3 +94,12 @@ def test_rule(): assert rule.check_condition(params) == True assert rule.run_action(params) == 5 + + +def test_add_rule(): + parser = RuleParser() + parser.add_rule('testrule', 'products_in_stock < 20', '2 + 3') + params = { + 'products_in_stock': 10 + } + parser.execute(params)