jruizgit / rules

Durable Rules Engine
MIT License
1.15k stars 206 forks source link

[Proposal] Put the facts and rules into the redis server. #53

Open snowyu opened 7 years ago

snowyu commented 7 years ago

Why not put the facts and rules into the redis server since the alpha and beta nodes have been stored?

First this should be supported:

var d = require('durable');
var Redis = require('ioredis');
var redis = new Redis();

with (d.ruleset('a0')) {
    whenAll(m.amount.lt(100), function (c) {
        console.log('general fact approved from ' + c.s.sid);
    });
    whenAll(m['/AI/ruleset/a0/fact/amount'].lt(100), function (c) {
        console.log('a0 approved from ' + c.s.sid);
    });
    whenStart(function (host) {
        redis.set('amount', 10);
        redis.set('/AI/ruleset/a0/fact/amount', 10);//related to the ruleset
        redis.set('/AI/session/1/fact/amount', 10);//related to the session
        // host.post('a0', {id: 1, sid: 1, amount: 10});
    });
}
d.runAll();

The storage of the rules are similar. It's so easy if the rules are running on the redis via lua. And it will make the redis become the distributed inference engine completely. But if not:

redis.config('set', 'notify-keyspace-events', 'KEA');

redis.psubscribe('__keyspace@*__:/AI/ruleset/*/rule/*', function (err, count) {
  if (err) console.log(err)
})

redis.on('pmessage', function (pattern, channel, message) {
    if(pattern is '__keyspace@*__:/AI/ruleset/*/rule/*'){
        //Here, update the rulesets to the script engine.
    }
})
jruizgit commented 7 years ago

Hi, thanks a lot for the suggestion. I appreciate your insight.

I really like the idea of providing better integration with redis primitives. That is, being able to reason about changes in keys and hashsets without having to worry about a session scope. I will experiment with the idea later this month.

Pushing the entire tree into redis (as lua script) would break a couple of core principles: 1. Being able to write actions in scripting languages (JScript, Ruby and Python). 2. Distributing the tree evaluation such that a large number of events can be evaluated without hitting the redis backend.

Jesus Ruiz

snowyu commented 7 years ago

Glad to hear you say and do so soon.

Pushing the entire tree into redis (as lua script) would break a couple of core principles:

  1. Being able to write actions in scripting languages (JScript, Ruby and Python).

Yes, totally agree. In fact, it's more perfect if the rule engine can be database-neutral too. The problem is that not all NOSQL databases support the key events notification. the rule engine must have its own PUBSUB mechanism.

  1. Distributing the tree evaluation such that a large number of events can be evaluated without hitting the redis backend.

Redis itself can be a distributed cluster. The lua redis client in the server should support so.

In terms of performance, it is best to use a custom partitioning mechanism, keep the /AI/ruleset/:ruleset-id/* in a server together. The related alpha and beta nodes should be stored together, maybe like this /AI/ruleset/:ruleset-id/rete/*.