I am using json-rule-engine
.
https://www.npmjs.com/package/json-rules-engine
I am having a student list which have name , age and their percentage.Also I have business rule
the percentage should be greater than or equal to than 70
and age should in [25
,28
].
so I want to print all students name those have percentage more than 70 and age in [25,28]
so I tried like this
const fastify = require("fastify");
const app = fastify();
const { Engine } = require("json-rules-engine");
let engine = new Engine()
engine.addRule({
conditions: {
all: [
{
fact: "percentage1",
operator: "greaterThanInclusive",
value: 70,
path: '$.percen'
},{
fact: 'percentage1',
operator: 'in',
value: [25,28],
path: '$.age'
}
]
},
event: {
type: "message",
params: {
data: "hello-world! (greater than 70)"
}
}
});
let facts = [
{
name:"naveen",
percentage:65,
age:30
},
{
name:"rajat",
percentage:50,
age:30
},
{
name:"ravi",
percentage:75,
age:25
},
{
name:"kaushal",
percentage:64,
age:13
},
{
name:"piush",
percentage:89,
age:12
}
]
engine.addFact('percentage1', function (params, almanac) {
console.log('*****',almanac.factValue('percentage'))
return almanac
})
// Run the engine to evaluate
facts.forEach((fact) => {
engine
.run(fact)
.then(results => {
if (results.events.length) {
console.log("Student,", results.almanac.factMap.get('name').value, ", meet the event(s)", results.events)
}
})
})
https://repl.it/repls/SnappyOverjoyedStaff#index.js
expected output is
{
name:"ravi",
percentage:75,
age:25
},
i want to use $
or path: '$.percen'
for understanding ..can you please tell me where i am doing wrong ?
any update ?