Skip to content

Commit

Permalink
Added support for #3
Browse files Browse the repository at this point in the history
  • Loading branch information
iPieter committed Aug 31, 2018
1 parent 6d94896 commit 13c31df
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [0.0.3] - 31/08/2018

- Added one environmental symbol (`$ruleName`)
- Removed unnecessary output

## [0.0.2] - 30/08/2018
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ So each clause matches one type of fact, followed by zero or more conditions or

In the above clause, the attribute `amount` is assigned to a symbol with name `$amount`.

For convenience, the rule name is by default available as `$ruleName`, this can be overwritten. Support for additional variables is planned, but not yet implemented.

- **Conditions**: both sides have to be comparable, and the condition should be true for the clause to finish.
The environment supports equality for strings, numbers and objects and comparisons for numbers.

Expand Down
1 change: 1 addition & 0 deletions lib/src/rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Rule {
bool firstFact = true;
Map<Clause, Fact> clauseMap = new Map();
Map<String, dynamic> _symbolTable = new Map();
_symbolTable["\$ruleName"] = _name;

var iterator = _clauses.iterator;
while (iterator.moveNext()) {
Expand Down
64 changes: 64 additions & 0 deletions test/symbol_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import 'package:rule_engine/src/fact.dart';
import 'package:rule_engine/rule_engine.dart';
import 'package:test/test.dart';

void _basicTest() {
String code = r"""
rule "Bob saved some money"
when
not Achievement( title == $ruleName )
Expense( amount > 10, $amount: amount )
then
publish Achievement( "01", $ruleName, $amount )
end
""";
var ruleEngine = new RuleEngine(code);

var results = new List();
ruleEngine.registerListener((t, a) {
if (t == "Achievement") results.add(new Achievement(a[0], a[1], a[2]));
});

ruleEngine.insertFact(new Expense("Bob", 1000, "Cheese", new DateTime.now()));
expect(results.length, equals(1));
expect(results[0].attributeMap()["title"], "Bob saved some money");
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 symbol of rule name', _basicTest);
}

class Expense extends Fact {
Map<String, dynamic> attributes = new Map<String, dynamic>();

Expense(name, amount, category, created) {
attributes["name"] = name;
attributes["amount"] = amount;
attributes["category"] = category;
attributes["created"] = created;
}

@override
Map<String, dynamic> attributeMap() {
return attributes;
}
}

class Achievement extends Fact {
Map<String, dynamic> attributes = new Map<String, dynamic>();

Achievement(badge, title, descr) {
attributes["badge"] = badge;
attributes["title"] = title;
attributes["descr"] = descr;
}

@override
Map<String, dynamic> attributeMap() {
return attributes;
}
}

0 comments on commit 13c31df

Please sign in to comment.