Closed jorchg closed 4 years ago
In fact, if in your example I change the microsoftRule
to use a selectn
syntax it works like a charm:
const microsoftRule = {
conditions: {
all: [{
fact: 'account-information',
operator: 'equal',
value: 'microsoft',
path: '$.company' // access the 'company' property of "account-information"
}, {
fact: 'account-information',
operator: 'in',
value: ['active', 'paid-leave'], // 'status'' can be active or paid-leave
path: '$.status' // access the 'status' property of "account-information"
}, {
fact: 'account-information',
operator: 'contains',
value: '2016-12-25',
path: '.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information"
}]
},
event: {
type: 'microsoft-christmas-pto',
params: {
message: 'current microsoft employee taking christmas day off'
}
}
}
engine::addOperator name:equal
engine::addOperator name:notEqual
engine::addOperator name:in
engine::addOperator name:notIn
engine::addOperator name:contains
engine::addOperator name:doesNotContain
engine::addOperator name:lessThan
engine::addOperator name:lessThanInclusive
engine::addOperator name:greaterThan
engine::addOperator name:greaterThanInclusive
engine::addFact id:account-information
engine::run started
engine::run runtimeFacts:
almanac::constructor initialized runtime fact:accountId with lincoln<string>
almanac::constructor initialized runtime fact:success-events with undefined<undefined>
almanac::factValue cache miss for fact:account-information; calculating
condition::evaluate extracting object property $.company
almanac::factValue cache hit for fact:account-information
condition::evaluate extracting object property $.status
almanac::factValue cache hit for fact:account-information
loading account information for "lincoln"
condition::evaluate extracting object property $.company, received: microsoft
condition::evaluate extracting object property $.status, received: active
condition::evaluate extracting object property .ptoDaysTaken, received: 2016-02-21,2016-12-25,2016-03-28
condition::evaluate <microsoft equal microsoft?> (true)
condition::evaluate <active in active,paid-leave?> (true)
condition::evaluate <2016-02-21,2016-12-25,2016-03-28 contains 2016-12-25?> (true)
rule::evaluateConditions results
engine::run ruleResult:true
almanac::factValue cache miss for fact:success-events; calculating
engine::run completed
almanac::factValue cache miss for fact:success-events; calculating
lincoln is a current microsoft employee taking christmas day off
This is totally a issue with jsonpath-plus
which incosistently returns an element when the input is an array with a single element, while if the input array contains more than one element it returns an array :S
I don't think that follows jsonpath specification. A common online checker does return an array when the input is an array. For example:
Should raise an issue on jsonpath-plus
repository 👍🏻
Oh I see this is fixed in https://github.com/s3u/JSONPath/issues/98 but v3.0.0 it's not yet published to npm. Hope there is an update soon.
Hello everybody,
I have been trying this engine for a couple of days and I think is awesome! Congratulations on the effort of maintaining such a project.
Yesterday doing a test with some rules for my business case I realized about what for my is an unexpected output with
contains
operator.I've made a POC https://codesandbox.io/s/gracious-elgamal-1od2s (maybe you have to login, but the code can be copied and pasted and run in a local environment).
I have this rule:
And I have this three users:
Which are stored in DB, so I feed the engine with a fact like so:
So I have realized of an unexpected behaviour. When a user only has one element on his array of
coursesDone
the rule does not match, but if it has more than one element it correctly matches the rule.Also, searching in your docs and examples if I have maybe misunderstood something I have realized that cloning the repo and executing the example
./examples/03-dynamic-facts.js
does not output what it should output, and the condition that is failing also uses acontains
operator.Debug output of the example:
I haven't had time to take a look and see if this is a bug and maybe submitting a PR, but today I will be looking into this. Maybe an issue about JsonPath? I read a related issue where the package jsonpath-plus was not consistently returning an array when it should be, but seems like it was already fixed.
Thank you very much!