Code Monkey home page Code Monkey logo

Comments (9)

alank64 avatar alank64 commented on May 14, 2024

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.

CacheControl avatar CacheControl commented on May 14, 2024

@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.

NathanHazout avatar NathanHazout commented on May 14, 2024

@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.

knalbandianbrightgrove avatar knalbandianbrightgrove commented on May 14, 2024

@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.

NathanHazout avatar NathanHazout commented on May 14, 2024

@knalbandianbrightgrove can you fix the markdown syntax, I can't read the code snippet ...

from json-rules-engine.

NathanHazout avatar NathanHazout commented on May 14, 2024

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.

knalbandianbrightgrove avatar knalbandianbrightgrove commented on May 14, 2024

@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.

NathanHazout avatar NathanHazout commented on May 14, 2024

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.

alank64 avatar alank64 commented on May 14, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.