yantra-core / Sutra.js

A JavaScript behavior tree library for easily creating and managing complex behavior patterns
178 stars 2 forks source link

nested if/then/else rules #3

Open mirkopeloso opened 4 months ago

mirkopeloso commented 4 months ago

Hi, seems that Sutra is unable to process nested rule functions, here an example:


import { Sutra } from '@yantra-core/sutra';

// creates a new sutra instance
const s = new Sutra();

s.addCondition('gt 0', (e) => e > 0);
s.addCondition('gt 10', (e) => e > 10);
s.addCondition('gt 20', (e) => e > 20);

s
    .if('gt 0')
    .then(rules =>
        rules
            .if('gt 20')
            .then('ok greater then 20')
            .else(
                rules => rules
                    .if('gt 10')
                    .then('OK greater than 10')
                    .else('KO lower than 10')
            )

    ).else('KO lower than 0');

s.onAny((ev, data, node) => {
    console.log('onAny', ev);
});

s.tick(1);
s.tick(14);
s.tick(-5);

OUTPUT:

onAny [Function (anonymous)]
onAny [Function (anonymous)]
onAny KO lower than 0

It's my fault constructing behavior tree or it's a bug?

Thank you so much

Marak commented 2 months ago

I'm not sure. There are lots of passing tests for nested. If you can check in a failing test we could look at it.

I would recommend looking at the tests for usage.