0

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 ?

user944513
  • 12,247
  • 49
  • 168
  • 318
  • Apparently, related to this prior question: [how to print all students name which have percentage more than 70% in javascript?](https://stackoverflow.com/questions/62856619/how-to-print-all-students-name-which-have-percentage-more-than-70-in-javascript). – jfriend00 Jul 12 '20 at 05:06
  • but here different conditions like using `path` – user944513 Jul 12 '20 at 05:16

0 Answers0