Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Returning the custom added param in successes #65

Closed
JagadishaBS opened this issue Feb 14, 2018 · 1 comment
Closed

Question: Returning the custom added param in successes #65

JagadishaBS opened this issue Feb 14, 2018 · 1 comment
Labels

Comments

@JagadishaBS
Copy link

JagadishaBS commented Feb 14, 2018

Hi
we have the number of a rule set for single questions like below

`conditions:

{
any: [{
    all: [{
      fact: 'gameDuration',
      operator: 'equal',
      value: '1',
factResult: [
{
"action": "do-something for gameDuration show ",
// i can add my custom params
}]
  },
    {
      fact: 'personalFoulCount',
      operator: 'equal',
      value: '2',
factResult: [
{
"action": "do-something for personalFoulCount show",
}]
    }],
  },
  {
     all: [{
      fact: 'gameDuration',
      operator: 'Equal',
      value: '2',
factResult: [
{
"action": "do-something for gameDuration hide",
}]
    },
      {
      fact: 'personalFoulCount',
      operator: 'equal',
      value: '4',
factResult: [
{
"action": "do-something for personalFoulCount hide",
}]
    }]
  },
{
     all: [{
      fact: 'gameDuration',
      operator: 'Equal',
      value: '8',
factResult: [
{
"action": "do-something for gameDuration hide show",
}]
    },
      {
      fact: 'personalFoulCount',
      operator: 'equal',
      value: '6',
factResult: [
{
"action": "do-something for gameDuration show hide",
}]
    }]
  },
]
  },
event: {
type: 'json rule',
params: {
message: 'json rule example'
}
}`

and

let facts = { gameDuration: '2', personalFoulCount: '4' }

the rule will success for given facts

on success, i want the success "factResult" separately. is there any way we can access the succeed custom param.we tried with below example
https://github.com/CacheControl/json-rules-engine/blob/master/examples/09-rule-results.js
but could not able to succeed.

@CacheControl
Copy link
Owner

CacheControl commented Feb 19, 2018

Hi @JagadishaBS factResults are generated by the engine when it runs. It is only accessible via the on('success') event you already referenced.

I'll note that from the example you gave above, it looks to me like you're trying to pre-define factResults in the rule conditions, and have the values carry through as the engine runs (Let me know if I'm mis-interpreting). Rule conditions aren't meant as a means to store state - that's why they have a fairly rigid data structure. If you need to associate additional data with each rule, look to the events, where you can provide params. Finally, when needing to associate more complex data structures with a rule, don't be afraid to structure the data outside the engine - sometimes simply hashing the data by the event type in a variable, and then pulling out the data you need in the on('success') handler is a good way to go.

If you are still having problems or I'm misunderstanding your problem, it may helps to post a script/repo I can run that demonstrates your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants