- Type Parameters:
O- The type of the output object
RulesEngine.runWithResult(FactStore), and given to RuleListener.afterRun(RunContext, RunResult).
A run of an engine's own rules returns a result whose output() is null exactly when
firedRules() is empty, which happens when the rule list is empty or no rule matched. A failed run throws
instead of returning a partial result.
It's a final class rather than a record, so a later 2.x release can add accessors without breaking code compiled
against this one. equals compares every field except startedAt(); a field added in a later release
takes part too. Leaving out the start lets results compare equal whatever the engine's clock, such as a test
double's result and a real engine's from a run without tags, when their other fields are equal too. The output is
compared with its own equals, so an array output is compared by reference. The evaluations are compared as
RuleEvaluation compares them: by rule and outcome, not by detail.
The output takes part in hashCode too, so a result whose output is changed afterwards, such as a map an
action filled, no longer hashes the same: don't use a result as a key in a hash-based collection while its output
can change.
-
Method Summary
Modifier and TypeMethodDescriptionbooleanCompares every field exceptstartedAt(), which the class description explains.Returns what each rule's condition evaluated to: one evaluation for every loaded rule, in evaluation order, the sameRuleinstances asRuleSetInfo.rules().Returns the rules whose actions ran, in firing order.inthashCode()Combines the hash codes of the output, the checksum and the tags with the number of fired rules, consistently withequals(Object).static <O> RunResult<O> Creates a result without evaluations.static <O> RunResult<O> of(@Nullable O output, List<Rule> firedRules, List<RuleEvaluation> evaluations, String ruleSetChecksum) Creates a result.@Nullable Ooutput()Returns the output object the run produced.Returns the checksum of the rules this run used.@Nullable InstantReturns when the run started, by the engine's clock, asRunContext.startedAt()describes it: the instant that judged every rule's validity window.tags()Returns the tags the run was given, asRunContext.tags()describes them: the run used only the rules that carry at least one of them.toString()Describes the result, such asRunResult(output={rate=4.5}, firedRules=[prime-rate], evaluations=[prime-rate=MATCHED], ruleSetChecksum=9f2c..., tags=[], startedAt=2027-06-01T00:00:00Z).withRun(RunContext run)
-
Method Details
-
of
public static <O> RunResult<O> of(@Nullable O output, List<Rule> firedRules, String ruleSetChecksum) Creates a result without evaluations. The engine creates its own; this is for a class that implementsRulesEngine, such as a decorator or a test double. Itsevaluations()andtags()are empty and itsstartedAt()isnull; useof(Object, List, List, String)to report the evaluations.RunContextis sealed, its one implementation is internal to the engine, and the engine creates the contexts it passes to listeners, so an implementation carries a run's tags and start withwithRun(RunContext)and a context an engine gave it, such as one its listener received.- Type Parameters:
O- The type of the output object- Parameters:
output- The output object, ornullif no rule firedfiredRules- The rules whose actions ran, in firing order; copiedruleSetChecksum- Thechecksumof the rules the run used- Returns:
- The result
- Throws:
NullPointerException- iffiredRules, one of its elements, orruleSetChecksumisnull
-
of
public static <O> RunResult<O> of(@Nullable O output, List<Rule> firedRules, List<RuleEvaluation> evaluations, String ruleSetChecksum) Creates a result. The engine creates its own; this is for a class that implementsRulesEngine, such as a decorator or a test double. Itstags()are empty and itsstartedAt()isnull.RunContextis sealed, its one implementation is internal to the engine, and the engine creates the contexts it passes to listeners, so an implementation carries a run's tags and start withwithRun(RunContext)and a context an engine gave it, such as one its listener received.- Type Parameters:
O- The type of the output object- Parameters:
output- The output object, ornullif no rule firedfiredRules- The rules whose actions ran, in firing order; copiedevaluations- What each rule's condition evaluated to, in evaluation order; copiedruleSetChecksum- Thechecksumof the rules the run used- Returns:
- The result
- Throws:
NullPointerException- iffiredRules,evaluations, one of their elements, orruleSetChecksumisnull
-
withRun
Returns a copy of this result that carriesrun'stagsandstart; everything else is the same. The engine calls it on the result of each of its runs beforeRuleListener.afterRun(RunContext, RunResult)sees it.RunContextis sealed, its one implementation is internal to the engine, and the engine creates the contexts it passes to listeners, so another implementation ofRulesEnginecalls it with a context an engine gave it, such as one its listener received.- Parameters:
run- The run whose tags and start the copy carries- Returns:
- The copy
- Throws:
NullPointerException- ifrunisnull
-
output
Returns the output object the run produced.- Returns:
- The output, or
nullif no rule fired
-
firedRules
Returns the rules whose actions ran, in firing order. An engine that fires the first match returns at most one.- Returns:
- The fired rules; unmodifiable, and empty if none fired
-
evaluations
Returns what each rule's condition evaluated to: one evaluation for every loaded rule, in evaluation order, the sameRuleinstances asRuleSetInfo.rules(). On a first-match engine, the rules after the match areRuleEvaluation.Outcome.NOT_EVALUATED; to know whether they would have matched, use an all-matches or a unique-match engine, which evaluate every condition of the rules the run uses. A rule the run skipped isRuleEvaluation.Outcome.SKIPPEDon every engine. A result created withof(Object, List, String)has none.- Returns:
- The evaluations; unmodifiable, and empty if the rule list is empty
-
ruleSetChecksum
Returns the checksum of the rules this run used. During a reload it can differ from the engine's currentRuleSetInfo.checksum(), because a run finishes with the rules it started with.- Returns:
- The checksum, as
RuleSetInfo.checksum()describes it
-
tags
Returns the tags the run was given, asRunContext.tags()describes them: the run used only the rules that carry at least one of them.- Returns:
- The tags, in
Stringorder; unmodifiable, and empty if the run used every rule or the result was created withof(...)and notgiven a run
-
startedAt
Returns when the run started, by the engine's clock, asRunContext.startedAt()describes it: the instant that judged every rule's validity window. A fixed clock gives every run the same instant. It isn't comparable toRuleSetInfo.loadedAt()or the run's deadline.- Returns:
- The instant, or
nullif the result was created withof(...)and notgiven a run
-
equals
Compares every field exceptstartedAt(), which the class description explains. -
hashCode
public int hashCode()Combines the hash codes of the output, the checksum and the tags with the number of fired rules, consistently withequals(Object). It hashes only these, so that it doesn't walk every evaluation. -
toString
Describes the result, such asRunResult(output={rate=4.5}, firedRules=[prime-rate], evaluations=[prime-rate=MATCHED], ruleSetChecksum=9f2c..., tags=[], startedAt=2027-06-01T00:00:00Z). Rule names and tags are shortened to 200 characters, then escaped, as the engine's error messages show names.
-