javascriptnode.jsnode-modulesrule-enginebusiness-rules

how to print all students name which have percentage more than 70% in javascript?


I am using json-rule-engine . https://www.npmjs.com/package/json-rules-engine I am having a student list which have name and their percentage, Also I have business rule the percentage should be greater thank or equal to than 70 . so I want to print all students name those have percentage more than 70

here is my code https://repl.it/repls/AlienatedLostEntropy#index.js

student list

const students = [
  {
    name:"naveen",
    percentage:70
  },
  {
    name:"rajat",
    percentage:50
  },
  {
    name:"ravi",
    percentage:75
  },
  {
    name:"kaushal",
    percentage:64
  },
  {
    name:"piush",
    percentage:89
  }
] 

rule

engine.addRule({
  conditions: {
    all: [
      {
        fact: "percentage",
        operator: "greaterThanInclusive",
        value: 70
      }
    ]
  },
  onSuccess(){
    console.log('on success called')
  },
  onFailure(){
    console.log('on failure called')
  },
  event: {
    type: "message",
    params: {
      data: "hello-world!"
    }
  }
});

code https://repl.it/repls/AlienatedLostEntropy#index.js any update


Solution

  • The json-rules-engine module takes data in a different format. In your Repl.it you have not defined any facts.

    Facts should be:

    let facts = [
      {
        name:"naveen",
        percentage:70
      },
      [...]
    

    Also, the module itself doesn't seem to process an array of facts. You have to adapt it to achieve this. This can be done with:

    facts.forEach((fact) => {
      engine
        .run(fact)
        [...]
    

    Finally, the student data is found inside the almanac. You can get these values with: results.almanac.factMap.get('[name|percentage|age|school|etc]').value

    Here is the updated Repl.it: https://repl.it/@adelriosantiago/json-rules-example