Interface CompiledCondition
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Several runs can evaluate one compiled condition at the same time, each with its own Session, which only one
run uses at a time. Keep what changes while the condition runs in the session.
The engine evaluates a condition with evaluateWithDetail(io.github.brantunger.unruly.api.language.EvaluationContext, io.github.brantunger.unruly.api.language.Session), which a language overrides to explain the result
as well as return it.
Implemented by expression languages. A method added to this interface is a default method, so an
existing language keeps compiling and working.
-
Method Summary
Modifier and TypeMethodDescription@Nullable Objectevaluate(EvaluationContext context, Session session) Evaluates the condition.default ConditionResultevaluateWithDetail(EvaluationContext context, Session session) Evaluates the condition and explains its result.
-
Method Details
-
evaluate
Evaluates the condition. The engine doesn't call this method itself: it callsevaluateWithDetail(io.github.brantunger.unruly.api.language.EvaluationContext, io.github.brantunger.unruly.api.language.Session), whose default calls this one, so for a language that doesn't overrideevaluateWithDetail, an exception this method throws and a result that isn't aBooleanfail the rule asevaluateWithDetail(io.github.brantunger.unruly.api.language.EvaluationContext, io.github.brantunger.unruly.api.language.Session)describes. -
evaluateWithDetail
default ConditionResult evaluateWithDetail(EvaluationContext context, Session session) throws Exception Evaluates the condition and explains its result. The engine calls this method, neverevaluate(io.github.brantunger.unruly.api.language.EvaluationContext, io.github.brantunger.unruly.api.language.Session)directly. When the value is aBoolean, it records the detail, if there is one, on the rule'sevaluationin the run's result, unless the run stops first; any other value fails the run, and the detail is dropped. By default, returnsConditionResult.of(Object)of whatevaluate(io.github.brantunger.unruly.api.language.EvaluationContext, io.github.brantunger.unruly.api.language.Session)returns, with no detail, so a language that implements onlyevaluateworks as it did.A language that can explain a condition overrides this method and returns the value and the detail from one evaluation. Its
evaluatemust return the same value. The detail must not be the session or hold it: the engine keeps the detail after the run, when the session is used by another run or closed.A decorator that wraps another language's condition must override this method too, and call the wrapped condition's
evaluateWithDetail. A wrapper written as a lambda implements onlyevaluate, so the wrapped condition's detail is dropped.- Parameters:
context- The facts of the runsession- The run's session for this condition's language, created by the compiler that compiled it- Returns:
- The result, never
null. A value that isn't aBoolean, includingnull, fails the rule, as does anullresult. - Throws:
Exception- if the condition fails. It fails the rule with aRuleExecutionException, except a fatalError, whichrun()rethrows unchanged.
-