CacheControl / json-rules-engine

A rules engine expressed in JSON
ISC License
2.6k stars 461 forks source link

problems with the execution of my rules #350

Closed crobles closed 1 year ago

crobles commented 1 year ago

Is it possible to execute the rules in series and not in parallel? or is there any technique to help me?

I need that if only one of my rules is not fulfilled, it exits immediately and does not continue evaluating the other rules, but from what I see they are all executed in parallel at the same time

chris-pardy commented 1 year ago

You can set priorities, and call engine.stop to achieve this.

crobles commented 1 year ago

Any example of how to use it? I really tried to use it as you told me, but I can't find the solution :(

chris-pardy commented 1 year ago

@crobles There's an example of setting priority in examples/07-rule-chaining.js in this case it's being used to have one rule run before another rule. You could set the rule that determines if other rules run to have a higher priority, then you could run the remaining rules at the default priority.

To stop the execution of the rules engine you call engine.stop() this will prevent the next priority set of rules from running. One thing to be aware of is that there's no guarantee that this will prevent subsequent rules from running.

The code looks like this:

const engine = new Engine();

// addRules
const highPriorityRule = new Rule(highPriorityRuleProperties);
engine.addRule(highPriorityRule);
// shut down the engine when this rule fails.
highPriorityRule.on('failure', () => engine.stop())

// run the engine.
await engine.run();

Alternatively and probably better for your use case you can create two engine instances and run one with your high prioirty rule first, then the other with your other rules conditionally based on the result of the first run.

crobles commented 1 year ago

thank you. you cleared my doubts.