From 6d9489644e088f9749b5c0778183d698b81ef728 Mon Sep 17 00:00:00 2001 From: Pieter Delobelle Date: Fri, 31 Aug 2018 10:28:32 +0200 Subject: [PATCH] Fixed various issues with execution order --- CHANGELOG.md | 6 +++++- README.md | 12 +++++++----- example/performance/lib/main.dart | 2 +- lib/src/rule.dart | 5 +---- pubspec.yaml | 4 ++-- test/not_test.dart | 26 ++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fd5648..c7a3ad5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## [0.0.2] - 31/08/2018 +## [0.0.3] - 31/08/2018 + +- Removed unnecessary output + +## [0.0.2] - 30/08/2018 - Fixed incorrect behavior of `not`-operator with multiple types of facts. - Improved code style. diff --git a/README.md b/README.md index 1416c95..59bd619 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ - Allows multiple callbacks for end results - Supports variables inside rules - Supports rolling windows on all `DateTime` objects -- supports aggregates on all numerical attributes (sum, average, min, max) +- Supports aggregates on all numerical attributes (sum, average, min, max) +- Allows for negation of facts by offering a `not` keyword ## Getting Started @@ -19,12 +20,9 @@ Get started by adding the package to your project as a dependency: ``` dependencies: - rule_engine: - git: git://github.com/iPieter/rule_engine_dart + rule_engine: ^0.0.3 ``` -At the moment, only the git version is available, since it is not yet published. - ## Rule syntax This rule engine follows basically the same syntax as Drools, with some minor differences. It has the following boilerplate: @@ -138,9 +136,13 @@ end RuleEngine ruleEngine = new RuleEngine(code); +//You can register multiple listeners, which are all called in the order they are registered ruleEngine.registerListener((type, arguments) { print("insert $type with arguments $arguments"); }); + +//insert a fact that implements from [Fact] +ruleEngine.insertFact( new SimpleFact("Bob", 1000, "Cheese", new DateTime.now()) ); ``` ## FAQ diff --git a/example/performance/lib/main.dart b/example/performance/lib/main.dart index 6ac8e09..604d8c3 100644 --- a/example/performance/lib/main.dart +++ b/example/performance/lib/main.dart @@ -94,7 +94,7 @@ end """; main() { - for (var code in [code1]) { + for (var code in [code2]) { print(code.substring(0, 20)); for (var j = 0; j < 8; j++) { diff --git a/lib/src/rule.dart b/lib/src/rule.dart index c4ea9ce..bd52d0e 100644 --- a/lib/src/rule.dart +++ b/lib/src/rule.dart @@ -25,12 +25,11 @@ class Rule { Map _symbolTable = new Map(); var iterator = _clauses.iterator; - while (firstFact && iterator.moveNext()) { + while (iterator.moveNext()) { var clause = iterator.current; bool isTrueFact = clause.evaluateClause(_symbolTable, _matchedFacts[clause], fact); firstFact = firstFact && (isTrueFact || clause.negated); - print("$fact : $isTrueFact : $firstFact"); //isTrueFact = clause.negated ? !isTrueFact : isTrueFact; if (isTrueFact) { @@ -55,8 +54,6 @@ class Rule { allClausesHaveAFact = allClausesHaveAFact && ((clauseMap.containsKey(clause) && !clause.negated) || (!clauseMap.containsKey(clause) && clause.negated)); - print( - "$clause : ${((clauseMap.containsKey(clause) && !clause.negated) || (!clauseMap.containsKey(clause) && clause.negated))}"); } } diff --git a/pubspec.yaml b/pubspec.yaml index ad37f84..48c3a6a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: rule_engine -description: A rule engine for Dart and Flutter. -version: 0.0.2 +description: A zero-dependency rule engine for Dart and Flutter with Drools-like syntax. +version: 0.0.3 author: Pieter Delobelle homepage: https://github.com/iPieter/rule_engine_dart diff --git a/test/not_test.dart b/test/not_test.dart index 998dcbf..978ae12 100644 --- a/test/not_test.dart +++ b/test/not_test.dart @@ -27,8 +27,34 @@ end expect(results.length, equals(1)); } +void _reverseBasicTest() { + String code = r""" +rule "expense" + when + Expense( amount > 10, $amount: amount ) + not Achievement( title == "Bob saved some money" ) + then + publish Achievement( "01", "Bob saved some money", $amount ) +end +"""; + var ruleEngine = new RuleEngine(code); + + var results = new List(); + ruleEngine.registerListener((t, a) { + results.add(a[0]); + }); + + ruleEngine.insertFact(new Expense("Bob", 1000, "Cheese", new DateTime.now())); + expect(results.length, equals(1)); + ruleEngine.insertFact(new Achievement("01", "Bob saved some money", 100)); + + ruleEngine.insertFact(new Expense("Bob", 2000, "Cheese", new DateTime.now())); + expect(results.length, equals(1)); +} + void main() { test('Test not statement', _basicTest); + test('Test not statement in reverse', _reverseBasicTest); } class Expense extends Fact {