EzDevInfo.com

nools

Rete based rules engine written in javascript nools

how deal with dynamic rules in a flow

I'm using nools rule engine and I have the necessity of modify rules on the fly without impacting the rule engine integrity.

So far I was using the default 'main' action group to store my default rules and using an specific action group to store rules that belongs for a company. But if a company want to change/add/delete rules I would need to create again the entire flow. I wasn't able to find something about it in the documentation. For me remove a flow ( that will contain the rules for all my companies ) and create it again seems to much work for rules crud operations. Because of that I started to think that maybe a flow per company would be a better strategy but still, if they want to change the rules the flow itself need to be removed and added again but with the new rules, the altered rules and without the deleted rules. The problem is that the rules could be modified/deleted/add on the fly.

My questions:

  1. How other rules engines deal with dynamical crud operations over the rules?
  2. Should I be using a flow per company ?
  3. Is there a way to add/delete/modify rules for a flow dynamically in nools?
  4. Is there a more rule engine style solution for this?

Any help would be appreciated.

Thanks


Source: (StackOverflow)

How to define Nools in Nodejs?

I am looking to create nools rules in nodejs. I am new to nools rule-engine.I am trying to write a rule but i don't know anything about nools. I have studied whole documentation but it is confusing me. So please can anyone help me how to create nools rules in server side and how to connect that rules with client side?


Source: (StackOverflow)

Advertisements

design pattern with rule engine and node.js to handle permissions

I have the requirement to use a rule engine to implement role permissions in the system ( could this be an overkill? ) however the permissions are kind of complicated and complex itself. I got confused in how to grant access or not using a rule-engine.

I also have doubts about the design that I should use in order to implement it in a scalable and maintainable way. So any help in the design or explain to me how to use the rule engine would be great.

Using nools, mongoDB, node.js as backend.

I was thinking in creating a rule engine instance encapsulating Nools ( would be an anti-pattern inner-platform maybe?) in the bootstrap of my node.js app and let it as a global variable.

something like:

'use strict';

var nools = require('nools');
var flows = require('./rule-engine.flows');

// A flow is a container of rules, so each flow could be a category of rules 
// In the same flow could have some more specific subcategories would be actionGroups?

// I'm creating a ruleEngine instance that would contain nools but I'm not sure
// if that would be a good practice, I have that to maybe encapsulate boilerplate code
// or some straight forward operations in the future.. don't sure of this.
var ruleEngine = function(){
    this.nools = nools;
};

ruleEngine.prototype.getFlowSession = function(flow){
    if(this.nools.hasFlow(flow)){
        return this.nools.getFlow(flow).getSession();
    }else{
        var fl = this.nools.flow(flow, flows[flow]);
        return fl.getSession();
    }
};

ruleEngine.prototype.createRule = function(flow, rule){
    if(this.nools.hasFlow(flow)){
    // implementation to add rules to a flow
    }
};

ruleEngine.prototype.editRule = function(flow, rule, update){};
ruleEngine.prototype.retractRule = function(flow, rule){};

//could be global object, or cache object but certainly should be a single object.    
if(!GLOBAL.ruleEngine){ 
    var ruleEngineInstance = new ruleEngine();
    GLOBAL.ruleEngine = ruleEngineInstance;
}

//module.exports = GLOBAL.ruleEngine;

rule-engine.flow:

'use strict';

var flowName = function(flow){
// query the rules to database or to cache.. then add the rules to the flow.
// query bla bla function(results){
for(Var i=0; i<results.length; i++)
    flow.rule(results[i].name, results[i].constraints, results[i].action);

// alternately, I could just from the bootstrap create a flow, 
// and create a function to add, modify or retract rules of a specific flow. 
// What would be the better design approach ? or combine two approach ? 
// bring from database the first time, and later use ruleModify, 
// ruleCreate or rule retract functions.
};

module.exports = {
    flowName: flowName, 
// each would be a flow that would be a category of rules for the system
    flowName2: flowName2
};

How to use it to implement permissions, is the only way to communicate the rule engine and external app / code through events?

these are some rules I created just to mess about ( at the same time are the ones used to create the flowName simulating the cache rules or MongoDB rules ).

var results = [
    {
        name: 'userAllow',
        constraints: [Object, 'obj', 'obj.systemRole === \'user\''],
        action: function(facts, session, next){
            session.emit('event:userAllow', {data: 'user is allow'});
            next();
        }
    },
    {
        name: 'userNotAllow',
        constraints: [Object, 'obj', 'obj.systemRole !== \'user\''],
        action: function(facts, session, next){
            session.emit('event:userNotAllow', {data: 'user is not allow'});
            next();
        }
    },
    {
        name: 'adminAllow',
        constraints: [Object, 'obj', 'obj.systemRole === \'admin\''],
        action: function(facts, session, next){
            session.emit('event:adminAllow', {data: 'admin is allow!'});
            next();
        }
    },
    {
        name: 'adminNotAllow',
        constraints: [Object, 'obj', 'obj.systemRole !== \'admin\''],
        action: function(facts, session, next){
            session.emit('event:adminNotAllow', {data: 'admin not allow'});
            next();
        }
    }
];

So with this few rules, I just want to grant access when user.systemRole is admin for example.. should I use events in the following way?

X-method in the system:

//validate delete with ruleEngine... supposed only admin would be able to delete
var self = this;
var session = ruleEngine.getFlowSession('flowName');
session.assert({systemRole: User.role}); //User.role = 'user' || 'admin'
session.on('event:adminAllow', function(d){
    console.log('do the job because the user is admin');
    // Delete implementation.
});
session.on('event:adminNotAllow', function(d){
    console.log('User not allow because is not admin');
});
session.on('fire',function(name){
    console.log(name);
});
session.match().then(function(){
    session.dispose();
});

So far I have some problems with this implementation.. events can fire more than once and I can't allow it to fire twice on a delete operation or a create operation or things like that.

So besides that error that I need to fix ( don't sure how ) Edit:

I commented the last next() of my rules, and after that the events are fired once. I have other doubts:

  1. Have good practices broken or anti-patterns?
  2. this is scalable and easy to maintain?
  3. Is the normal way of working with rule-engines?
  4. Pros and Cons of this implementation?
  5. Is there a better way?

Thanks in advance for any help.


Source: (StackOverflow)

Creating a Discount Rule using Nools rule engine

Am trying to create a new discount rule in my e-commerce website. I thought nools rule engine was the best to implement. but i dont know how to start and where to start. i have read the documentation also . am not getting anything. Can any one help me with sample code or how to start.? Thanks in advance to all


Source: (StackOverflow)

Converting a rule from node-rules engine to nools rule engine

var RuleEngine = require('node-rules');
var titan = {
      "product": "Titan",
      "amount":"500",
      "base":1,
      "conversation":0.91,
      };
      var oxicash = {

      }

      var rules = [{
        "name": "Product rule",
        "description": "when the product matches Titan",
        "priority": 1,
        "on":1,
        "condition":
            function(fact,cb) {
                cb(fact && (fact.product = "Titan"));
            },
        "consequence":
            function(cb) {
                console.log("Rule 1 matched for "+this.product);
                var a = this.amount;
                var b = this.base;
                var c = this.conversation;

                var result1= (a*c)/b;
                console.log("the promotion cost is:"+result1);
                this.result = true;
                this.process = true;
                cb();
            }
    }];
    var R = new RuleEngine(rules);

R.execute(titan,function(result){ 

    if(result.result) 
        console.log("\n-----u have discount----\n"); 
    else 
        console.log("\n-----u dont have discount----\n");

    console.log(result); 
    console.log(result1); 


});

Am new to rule-engine. am trying to write a rule and i am made in "node-rules". but i came to know that "nools" was the best to write rules and it is light weight. but i don't know anything about nools. i am studied whole documentation but it is confusing me. So please can anyone help me by converting this rule which I have written in "node-rules" to "nools".Thanking you in advance for your kind information.


Source: (StackOverflow)

Is nools still maintained?

I was thinking of using nools in a node.js project for a rules engine, however I noticed the project activity slowed down. (https://github.com/C2FO/nools)

I am wondering if nools is the only viable rules engine in node.js world.

I did a module search and it seems like the only other similar/active one is node-rules (https://github.com/mithunsatheesh/node-rules)

I want to make sure I am using (and hopefully can contribute) a maintained rules engine for this one project, so I am wondering if the project inactivity is due to a showblocker I am not seeing.


Source: (StackOverflow)