Comments (9)
I think you want to use the
engine.on('success'...)
engine.on('failure', (event, almanac, ruleResult) => {
console.log('failure event', event, almanac, ruleResult);
res.data.events.failed.push(event);
});
engine.run(facts).then(events) => {....}
So that on any failed event, or success event you can collect them and report them... that's if I understood your question correctly?
from json-rules-engine.
@nasht00 I believe the on.('failure')
hook is indeed what you're looking for. See docs for the spec and also this example. Thanks @alank64 for the quick response.
from json-rules-engine.
@alank64 I'm not clear about res.data.events.failed.push(event);
.
What is that?
What I am trying to achieve is pretty simple:
I am writing a method called checkEligibility(facts)
which should return a boolean (or a Promise of boolean if necessary).
The run
method only returns success events.
My current workaround is like this:
// run the engine
return engine
.run(facts)
.then(function (triggeredEvents) { // engine returns a list of events with truthy conditions
if (triggeredEvents.length > 0) { //there was a success
return Promise.resolve(true);
} else {
return Promise.resolve(false);
}
});
I am basically assuming that if the triggeredEvents
array is empty, it must mean that the result should be false
.
Not sure if that looks like a best practice ...
from json-rules-engine.
@nasht00
Try this one
// run the engine
return engine
.run(facts)
.then(function (triggeredEvents) { // engine returns a list of events with truthy conditions
if (triggeredEvents.length > 0) { //there was a success
return Promise.resolve(true);
}
}, function () {
return Promise.resolve(false);
});
@nasht00 Don't know why it doesn't work properly
from json-rules-engine.
@knalbandianbrightgrove can you fix the markdown syntax, I can't read the code snippet ...
from json-rules-engine.
Thanks @knalbandianbrightgrove , I think it's pretty much the same algorithm that I posted above no?
Except you are also handling the case where run
fails completely?
from json-rules-engine.
@nasht00
No it's not the same
Engine returns his own Promise but you handle only success and missed reject callback
The second function in then is Promise reject callback that means that event is false
Hope this is clear
from json-rules-engine.
I understand. Yet in my tests, when the rule is false, it still reached the then
part, just with empty events array.
My assumption is that it reaches the failure callback only if something goes seriously wrong during the run
(still a good thing to catch indeed), and not simply when no success event was triggered.
That's what I understood from the documentation (https://github.com/CacheControl/json-rules-engine/blob/master/docs/engine.md#enginerunobject-facts-object-options---promise-events):
Returns a promise which resolves when all rules have been run.
from json-rules-engine.
@nasht00 sorry, I used a part of my code here, in that I'm simply pushing the failed or success events into an array (res.data.events.failed or res.data.events.success) to return to the API caller, add them to what ever your callback or response is.
The idea is that any failed events or success are returned in the .on hook, use them to record event failures or successes. However you brought up a good question which I noticed the other day, and maybe I need to perform more testing, is that if one event fails does json-rules-engine continue on other events. Something I'm about to test now.
from json-rules-engine.
Related Issues (20)
- Adding path values to events HOT 2
- is this project still active HOT 9
- Not able to resolve JsonPath when it includes a where condition HOT 1
- Operator availability - between operator HOT 1
- OnSuccess/OnFailure properties as part of the rule - Security Issue
- Slow performance while having large array of facts. HOT 9
- How to feed engine multiple facts as array! HOT 1
- Complex rule for the fact HOT 2
- woops HOT 1
- Feat: Type-safety using a FactTypeMapping HOT 1
- Is it possible to make sure that a fact triggers at most one rule? HOT 2
- Uncaught TypeError: (0 , _hashIt2.default) is not a function HOT 8
- Error: Cannot find module 'lodash' HOT 1
- problems with the execution of my rules HOT 4
- Example issue - undefined parameter HOT 1
- RuleResult class missing from type declarations HOT 1
- What the best way to launch the rule engine
- Parent child rule
- Ability to stop rules execution after success for array of inputs
- How to create multiple 'aggregate' facts on a 'parent' object calculated from child objects (composition)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-rules-engine.