Skip to content

Commit d41ba2a

Browse files
committed
some cr changes
1 parent 91e82a0 commit d41ba2a

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

docs/engine.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ The Engine stores and executes rules, emits events, and maintains state.
88
* [engine.addFact(String id, Function [definitionFunc], Object [options])](#engineaddfactstring-id-function-definitionfunc-object-options)
99
* [engine.removeFact(String id)](#engineremovefactstring-id)
1010
* [engine.addRule(Rule instance|Object options)](#engineaddrulerule-instanceobject-options)
11-
* [engine.removeRule(Rule instance)](#engineremoverulerule-instance)
11+
* [engine.updateRule(Rule instance|Object options)](#engineupdaterulerule-instanceobject-options)
12+
* [engine.removeRule(Rule instance | String ruleId)](#engineremoverulerule-instance)
1213
* [engine.addOperator(String operatorName, Function evaluateFunc(factValue, jsonValue))](#engineaddoperatorstring-operatorname-function-evaluatefuncfactvalue-jsonvalue)
1314
* [engine.removeOperator(String operatorName)](#engineremoveoperatorstring-operatorname)
1415
* [engine.run([Object facts], [Object options]) -> Promise ({ events: [], failureEvents: [], almanac: Almanac, results: [], failureResults: []})](#enginerunobject-facts-object-options---promise--events--failureevents--almanac-almanac-results--failureresults-)
@@ -44,8 +45,6 @@ undefined facts as `undefined`. (default: false)
4445

4546
`pathResolver` - Allows a custom object path resolution library to be used. (default: `json-path` syntax). See [custom path resolver](./rules.md#condition-helpers-custom-path-resolver) docs.
4647

47-
`ruleKeyField` - By default this field is set to 'id', this field is the rule property that will be used when removing a rule by a key string or when updating rule
48-
4948
### engine.addFact(String id, Function [definitionFunc], Object [options])
5049

5150
```js
@@ -75,7 +74,7 @@ engine.removeFact('speed-of-light')
7574
### engine.addRule(Rule instance|Object options)
7675

7776
Adds a rule to the engine. The engine will execute the rule upon the next ```run()```
78-
if the rule doesn't have value set at rule[engine.ruleKeyField], a random string will be set instead.
77+
if the rule doesn't have value set at rule.id, a random string will be set instead.
7978

8079
```js
8180
let Rule = require('json-rules-engine').Rule
@@ -117,6 +116,8 @@ engine.removeRule(rule.id)
117116
// adds a rule
118117
let rule = new Rule()
119118
engine.addRule(rule)
119+
120+
// change rule condition
120121
rule.conditions.all = []
121122

122123
//update it

src/engine.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class Engine extends EventEmitter {
2525
this.operators = new Map()
2626
this.facts = new Map()
2727
this.status = READY
28-
this.ruleKeyField = options.ruleKeyField || 'id'
2928
rules.map(r => this.addRule(r))
3029
defaultOperators.map(o => this.addOperator(o))
3130
}
@@ -51,7 +50,7 @@ class Engine extends EventEmitter {
5150
rule = new Rule(properties)
5251
}
5352
rule.setEngine(this)
54-
if (!rule[this.ruleKeyField]) rule[this.ruleKeyField] = '_' + Math.random().toString(36).substr(2, 9)
53+
if (!rule.id) rule.id = '_' + Math.random().toString(36).substr(2, 9)
5554
this.rules.push(rule)
5655
this.prioritizedRules = null
5756
return this
@@ -62,25 +61,27 @@ class Engine extends EventEmitter {
6261
* @param {object|Rule} rule - rule definition. Must be a instance of Rule
6362
*/
6463
updateRule (rule) {
65-
const ruleIndex = this.rules.findIndex(ruleInEngine => ruleInEngine[this.ruleKeyField] === rule[this.ruleKeyField])
64+
const ruleIndex = this.rules.findIndex(ruleInEngine => (ruleInEngine.id === rule.id) && rule.id)
6665
if (ruleIndex > -1) {
6766
this.rules.splice(ruleIndex, 1)
6867
this.addRule(rule)
68+
this.prioritizedRules = null
6969
} else {
7070
throw new Error('Engine: updateRule() rule not found')
7171
}
7272
}
7373

7474
/**
7575
* Remove a rule from the engine
76-
* @param {object|Rule||string} rule - rule definition. Must be a instance of Rule
76+
* @param {object|Rule|string} rule - rule definition. Must be a instance of Rule
7777
*/
7878
removeRule (rule) {
7979
if (!(rule instanceof Rule)) {
80-
this.rules = this.rules.filter(ruleInEngine => ruleInEngine[this.ruleKeyField] !== rule)
80+
this.rules = this.rules.filter(ruleInEngine => ruleInEngine.id !== rule)
8181
this.prioritizedRules = null
8282
return Boolean(this.rules.length)
8383
} else {
84+
if (!rule) return false
8485
const index = this.rules.indexOf(rule)
8586
if (index === -1) return false
8687
this.prioritizedRules = null

src/rule.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Rule extends EventEmitter {
1515
* @param {string} options.event.params - parameters to pass to the event listener
1616
* @param {Object} options.conditions - conditions to evaluate when processing this rule
1717
* @param {any} options.name - identifier for a particular rule, particularly valuable in RuleResult output
18+
* @param {any} options.id - identifier for a particular rule, particularly valuable in RuleResult output
1819
* @return {Rule} instance
1920
*/
2021
constructor (options) {
@@ -35,6 +36,10 @@ class Rule extends EventEmitter {
3536
this.setName(options.name)
3637
}
3738

39+
if (options && (options.id)) {
40+
this.setId(options.id)
41+
}
42+
3843
const priority = (options && options.priority) || 1
3944
this.setPriority(priority)
4045

@@ -53,6 +58,18 @@ class Rule extends EventEmitter {
5358
return this
5459
}
5560

61+
/**
62+
* Sets the unique id of the rule
63+
* @param {any} id - any truthy input
64+
*/
65+
setId (id) {
66+
if (!id && id !== 0) {
67+
throw new Error('Rule "id" must be defined')
68+
}
69+
this.id = id
70+
return this
71+
}
72+
5673
/**
5774
* Sets the name of the rule
5875
* @param {any} name - any truthy input and zero is allowed

test/engine.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ describe('Engine', () => {
9393
engine.updateRule(rule)
9494
expect(engine.rules[0].conditions.all.length).to.equal(0)
9595
})
96+
it('should throw error if rule not found', () => {
97+
const rule1 = new Rule(factories.rule())
98+
engine.addRule(rule1)
99+
const rule2 = new Rule(factories.rule())
100+
expect(() => {
101+
engine.updateRule(rule2)
102+
}).to.throw(/Engine: updateRule\(\) rule not found/)
103+
})
96104
})
97105

98106
describe('removeRule()', () => {

0 commit comments

Comments
 (0)