ChainedEvaluationFunction
This is a implementation of the EvaluationFunction.
Challenge :
The situation where an implementation of the EvaluationFunction is done to simulate a linear statemachine is a common one. This leads to the requirement for a default structure that already has the logic needed to apply a statemachine structure with linear processing/state changing.
Solution :
The ChainedEvaluationFunction. It holds child implementations of the EvaluationFunction, which are representing single steps in the processing. This implementations are build by a factory that must fulfill a specific interface, the EvaluationFunctionStateFactory. This structure has the following helping sub structures:
-
List of EvaluationFunctionStateFactory ( + state timeout) : for each turnaround of the statemachine, the factories are creating a fresh instance of the state EvaluationFunction. When one of this EvaluationFunction is executed and it returns resulting Event, than this means the switch to the next state is reached. When a timeout is set for a state and the timeout is raised while execution, than the whole statemachine is reset.
-
over all timeout : This timeout can be set for the entire processing duration of the statemachine. When the timout is raised, the whole statemachine is reset.
-
StateErrorExtractor : extracts a resulting Event, when a timeout was raised.
-
ChainedProcessingCompleteExtractor : extracts resulting Events, after all states have been successfully processed.
Code Example :
Instantiation:
Long overallTimeoutMs = 2400;
// the Tuple2 instances in the List are holding the EvaluationFunction implementations
// and the state timeout for each state
List<Tuple2<EvaluationFunctionStateFactory, Long>> stateFactories = new ArrayList<>();
...
// here a previously created result Event is used, in generall the user has to build an own Event as result
StateErrorExtractor errorExtractor =
(StateErrorExtractor) (events, ex, context) -> context.collect(errorEvent);
// here a previously created result Event is used, in generall the user has to build an own Event as result
ChainProcessingCompleteExtractor stateCompleteExtractor =
(ChainProcessingCompleteExtractor) (events, context) -> context.collect(completeEvent);
...
ChainedEvaluationFunction function = ChainedEvaluationFunction.builder()
.withStateFactoriesAndTimeouts(stateFactories)
.withOverallTimeoutMs(overallTimeoutMs)
.withErrorExtractor(errorExtractor)
.withStateCompleteExtractor(stateCompleteExtractor)
.build();
Execution :
Since the ChainedEvaluationFunction is a implementation of the EvaluationFunction, it can be executed as such. (see EvaluationFunction)
