Skip to content

Commit

Permalink
Fixed various issues with execution order
Browse files Browse the repository at this point in the history
  • Loading branch information
iPieter committed Aug 31, 2018
1 parent 33b2a31 commit 6d94896
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 13 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@
- 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

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:
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/performance/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
5 changes: 1 addition & 4 deletions lib/src/rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ class Rule {
Map<String, dynamic> _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) {
Expand All @@ -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))}");
}
}

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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<[email protected]>
homepage: https://github.com/iPieter/rule_engine_dart

Expand Down
26 changes: 26 additions & 0 deletions test/not_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6d94896

Please sign in to comment.