j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.83k stars 1.04k forks source link

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

Open friends110110 opened 2 years ago

friends110110 commented 2 years ago

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;
    }

}
dvgaba commented 1 year ago

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