jruizgit / rules

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

Rule Chaining #197

Open saikiranbandaru opened 5 years ago

saikiranbandaru commented 5 years ago

Hey,

I was wondering if it was possible to perform rule chaining with this engine?

For example { sub: "abc", subrule: { obj: "xyz", age: 23 } }

jruizgit commented 5 years ago

Hi, thanks for posting the question. Would you mind elaborating more on the scenario you have in mind?

durable_rules does rules forward chaining. In the example below the engine will assert that 'Kermit' is a frog and because 'Kermit' is a frog, it is green (this last assertion is when forward chaining kicks in).

var d = require('durable');

d.ruleset('animal', function() {
    whenAll: {
        first = m.predicate == 'eats' && m.object == 'flies' 
        m.predicate == 'lives' && m.object == 'water' && m.subject == first.subject
    }
    run: assert({ subject: first.subject, predicate: 'is', object: 'frog' })

    whenAll: m.predicate == 'is' && m.object == 'frog'
    run: assert({ subject: m.subject, predicate: 'is', object: 'green' })

    whenAll: +m.subject
    run: console.log('fact: ' + m.subject + ' ' + m.predicate + ' ' + m.object)

    whenStart: {
        assert('animal', { subject: 'Kermit', predicate: 'eats', object: 'flies' });
        assert('animal', { subject: 'Kermit', predicate: 'lives', object: 'water' });
    }
});

d.runAll();