Skip to content

Conversation

CacheControl
Copy link
Owner

@CacheControl CacheControl commented Sep 12, 2016

Adds support for traversing an object returned from a fact.

@ctaity @slmmm - will this work for solving your use cases?

@slmmm
Copy link

slmmm commented Sep 12, 2016

@CacheControl Thanks for doing this! I'll try it out :-)

@slmmm
Copy link

slmmm commented Sep 13, 2016

@CacheControl , I tested a couple of paths.
I thought the first was working, but after a couple of tests, I can't seem to get it to trigger - simple property path for example, "address.zipcode".

The second doesn't seem to be triggering either - properties with depth for example, "address.prop.test[0].zipcode". I was attempting to trigger on empty values.

Might I be setting up the Condition incorrectly for the object?

// test object
{
"firstName": "Good",
"lastName": "Test",
"address": {
"prop": {"test": [
{"street": "", "zipcode": ""},
{"street": "", "zipcode": ""},
{"street": "", "zipcode": ""},
{"street": "", "zipcode": ""}
]},
"street": "lane",
"zipcode": "11111"
}
}

// rule
"conditions": {
"any": [
{
"fact": "address",
"operator": "equal",
"value": "",
"path": ".prop.test[0].street"
},
{
"fact": "address",
"operator": "equal",
"value": "",
"path": ".prop.test[0].zipcode"
}
]
}

@CacheControl
Copy link
Owner Author

@slmmm Thanks for taking a crack at this! Your sample looks correct, perhaps you're testing against version 1.1, which I released earlier this week? These changes haven't been pushed to npm.

If that was the case, I've just published 1.2.0-beta for your convenience.

If something else is going on, maybe double check against this test, which demonstrates a similar situation.

@slmmm
Copy link

slmmm commented Sep 13, 2016

Yes, you're correct. I should have pulled rather than install from npm.
And now after installing the 1.2.0-beta version, it DOES work wonderfully!! THANK YOU, this will allow us to remove a few custom facts (functions) and make the rule conditions cleaner. 👍

A Question - how might it be possible for us to also reference this path functionality from within a custom fact where we're getting facts from a call to almanac.factValue()?

@CacheControl
Copy link
Owner Author

Cool, glad it works.

Regarding accessing this functionality within a custom fact - I'd like to know more about your motivations. For example::

// why would this:
engine.addFact('account-information', function (params, almanac) {
  return almanac.factValue('address', {}, '.prop.test[0].zipcode')  // path as the third arg, for example
})

// be preferable to this:
engine.addFact('account-information', function (params, almanac) {
  return almanac.factValue('address', {}).then(address => address.prop.test[0].zipcode)
})

@slmmm
Copy link

slmmm commented Sep 13, 2016

The preference is for dynamic rule building of its conditions.

// In this case, the fact value reference is hard-coded, and only changed programmatically. This fact can only be used in one scenario in which address object has the specific path.
engine.addFact('account-information', function (params, almanac) {
return almanac.factValue('address', {}).then(address => address.prop.test[0].zipcode)
})

But in our case, we are attempting to utilize generalized custom facts that allow for reuse across more than a specific object. In order to do this, we've been using "params" to carry the various "paths" we're interested in.

As you point out though, it would be outside the scope of a "fact" to comprehend the multitude of "rules" and their varying properties like 'path'.

Thinking on this a bit more, for our current cases, we may actually be able to use the combination of the new "path" functionality and the new "custom operator" functionality to accomplish what we're doing in our generalized custom facts.
I'll try this combo approach first. :-)

@CacheControl CacheControl merged commit c03f949 into master Sep 24, 2016
@CacheControl CacheControl deleted the fact-paths branch September 24, 2016 16:28
@CacheControl CacheControl mentioned this pull request Sep 25, 2016
@ganeshcse2991
Copy link

Hi My Fact JSON is this :
{
status: "closed"
item:{
id: 1,
model: "something"
}
}

and My Rules is :
{
conditions: {
all: [
{
fact: "status",
operator: "equal",
value: "closed"
}
]
},
event: {
type: "sendEmail",
params: {
}
}
}

I am getting an error while running it. Engine is throwing error that it could not convert ObjectType to String. Since JSON object item is in the fact it trying to compare it as a string to my condition.

Please help where I am doing it wrong.

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

Successfully merging this pull request may close these issues.

3 participants