easy-rules icon indicating copy to clipboard operation
easy-rules copied to clipboard

how does Yml return a value exclude fact.put('result' , xx ) ?

Open friends110110 opened this issue 3 years ago • 1 comments

reference FAQ#2: I would like to return a value upon a rule execution, how to do that?.

The java MyRule Class(in the following) introduce executed and result variable. And After firing rules, you query the executed flag on your rule instance and get the execution result.

But how the executed and result variable could be realized in the yml file ? Yml file could introduce global variable? Or only use fact.put('results', xx) in the yml file ?

@Rule(name = "my rule")
public class MyRule<T> {

    private boolean executed;

    private T result;

    //@Condition
    public boolean when() {
        return true;
    }

    //@Action
    public void then() throws MyException {
        try {
            System.out.println("my rule has been executed");
            result = null; // assign your result here
            executed = true;
        } catch (MyException e) {
            // executed flag will remain false if an exception occurs
            throw e;
        }
    }

    public boolean isExecuted() {
        return executed;
    }

    public T getResult() {
        return result;
    }

}

friends110110 avatar Feb 27 '22 11:02 friends110110

Updating maps/adding new value to list or other mutable objects should work fine. Also you can use printing action in yaml files.

dvgaba avatar Aug 13 '22 02:08 dvgaba