2

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

user944513
  • 12,247
  • 49
  • 168
  • 318

3 Answers3

1

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

adelriosantiago
  • 7,762
  • 7
  • 38
  • 71
  • thanks for help ..can you please suggest where we use `$` in path https://github.com/CacheControl/json-rules-engine/blob/master/examples/03-dynamic-facts.js – user944513 Jul 12 '20 at 04:11
  • can you please give me a simple example where I will use `$` in path – user944513 Jul 12 '20 at 04:12
  • could you please check it is not working when I am using `path` https://repl.it/repls/SnappyOverjoyedStaff#index.js – user944513 Jul 12 '20 at 04:20
  • The `$` symbol helps you define "dynamic facts" so I don't think it can be used in the example above. Could you please create a new question with more details about the requirements? – adelriosantiago Jul 12 '20 at 18:22
  • @aderios see new question https://stackoverflow.com/questions/62857207/how-to-validate-age-and-percentage-of-student-in-javascript – user944513 Jul 12 '20 at 23:34
0

I might have submitted a completely unrelated answer, but here goes. Since the students object is an array, you could just loop through it and then use an if else statement.

for (let i = 0; i < students.length; i++) {
    if (students[i].percentage >= 70) {
        console.log(students[i].name);
    }
}

Sorry if this is incorrect!

0

Here is a working example.

Counting success and failed cases

const { Engine } = require("json-rules-engine");


let engine = new Engine();

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

engine.addRule({
    conditions: {
            all: [{
            fact: 'percentage',
            operator: 'greaterThanInclusive',
            value: 70
        }]
    },
    event: { type: 'procedure_result'}
})

let result = {success_count : 0 , failed_count : 0}

engine.on('success', () => result.success_count++)
    .on('failure', () => result.failed_count++)

const getResults = function(){
    return new Promise((resolve, reject) => {
        students.forEach(fact => {
            return engine.run(fact)
            .then(() => resolve())
        })
    })
}

getResults().then(() => console.log(result));

xMayank
  • 1,875
  • 2
  • 5
  • 19