Skip to content

Incomplete ruleResult.conditions, empty almanac in Node-Red #42

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

Closed
JonSilver opened this issue May 27, 2017 · 4 comments
Closed

Incomplete ruleResult.conditions, empty almanac in Node-Red #42

JonSilver opened this issue May 27, 2017 · 4 comments

Comments

@JonSilver
Copy link

Hi there... I tried and loved your json rules engine in node.js, so I tried installing it into the node-red environment using:

functionGlobalContext: {
        rulesEngine:require("json-rules-engine")
 }

Using a function node, I got the engine processing rules immediately... but strange things happen after the event has finished processing. Although it evaluates all the conditions accurately and fires a success or failure event, ruleResult.conditions received by the callback function is a strangely coded string (as though it's been double-stringified) and incomplete (it carries no evaluation status info, just the original conditions). Additionally, the almanac object is empty.

This is what it looks like:

conditions: "{"priority":1,"all":["{\"operator\":\"greaterThanInclusive\",\"value\":43,\"fact\":\"brian\"}","{\"operator\":\"greaterThanInclusive\",\"value\":23,\"fact\":\"sally\"}"]}"
Obviously this lack of information makes it hard to debug rules. Any ideas?

@CacheControl
Copy link
Owner

@JonSilver Weird! I'm a bit busy at the moment, but I will look into this when I have a chance. In the meantime, if you come across any additional information about this issue which may be relevant or are able to provide a snippet that reproduces the issue, please post it.

@JonSilver
Copy link
Author

JonSilver commented Jun 5, 2017

@CacheControl Thanks in advance. I've racked my brain and torn my hair out over this one. The code I'm using is an adapted form of 09-rule-results.js so I'm sticking to the letter of how you intended the library to be used. If there was another way of getting at the complete run-log (which conditions were truthy/falsey) for the rule, I'd use it instead, but as far as I can see, there's not.

I've done some investigation in the source, and I note that ruleResult is initially populated by using deepClone (lodash _cloneDeep) to copy the conditions structure. So something is going wrong in deepClone and it's managing to somehow return a string instead of an object - but only when the library is instantiated as a global object inside node-red.

This is indeed weird. There's nothing particularly special about the environment, and everything else I've tried to load in this way works flawlessly. I'm running node-red 16.2 under nodejs 6.10.2, but Babel transpiling should take care of all the awaits and other new features used in the source, and it's exactly the same nodejs environment whether run inside or outside node-red.

@CacheControl
Copy link
Owner

@JonSilver that should do it, please re-open if 2.0.1 does not work for you

@JonSilver
Copy link
Author

Just commenting for closure and completeness - the problem was actually two problems.

Node-Red runs user-supplied code inside a JavaScript VM which upsets json-rules-engine in a few ways. The solution is to use a third-party unsafe code node for rules engine ops.

And the second problem was the serialisation bug, which is now fixed. Thanks!

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

No branches or pull requests

2 participants