Search in sources :

Example 1 with ExecutableCalledDecision

use of io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCalledDecision in project zeebe by zeebe-io.

the class BpmnDecisionBehavior method evaluateDecision.

/**
 * Evaluate a decision during the processing of a bpmn element.
 *
 * @param element the called decision of the current bpmn element
 * @param context process instance-related data of the element that is executed
 * @return either an evaluated decision's result or a failure
 */
public Either<Failure, DecisionEvaluationResult> evaluateDecision(final ExecutableCalledDecision element, final BpmnElementContext context) {
    final var scopeKey = context.getElementInstanceKey();
    final var decisionIdOrFailure = evalDecisionIdExpression(element, scopeKey);
    if (decisionIdOrFailure.isLeft()) {
        return Either.left(decisionIdOrFailure.getLeft());
    }
    final var decisionId = decisionIdOrFailure.get();
    // todo(#8571): avoid parsing drg every time
    final var decisionOrFailure = findDecisionById(decisionId);
    final var resultOrFailure = decisionOrFailure.flatMap(this::findDrgByDecision).mapLeft(failure -> new Failure("Expected to evaluate decision '%s', but %s".formatted(decisionId, failure.getMessage()))).flatMap(drg -> parseDrg(drg.getResource())).mapLeft(f -> new Failure(f.getMessage(), ErrorType.CALLED_DECISION_ERROR, scopeKey)).flatMap(drg -> {
        final var evaluationResult = evaluateDecisionInDrg(drg, decisionId, scopeKey);
        final var decision = decisionOrFailure.get();
        writeDecisionEvaluationEvent(decision, evaluationResult, context);
        if (evaluationResult.isFailure()) {
            metrics.increaseFailedEvaluatedDmnElements(evaluationResult.getEvaluatedDecisions().size());
            return Either.left(new Failure(evaluationResult.getFailureMessage(), ErrorType.DECISION_EVALUATION_ERROR, scopeKey));
        } else {
            metrics.increaseSuccessfullyEvaluatedDmnElements(evaluationResult.getEvaluatedDecisions().size());
            return Either.right(evaluationResult);
        }
    });
    resultOrFailure.ifRight(result -> {
        // The output mapping behavior determines what to do with the decision result. Since the
        // output mapping may fail and raise an incident, we need to write the variable to a
        // record. This is because we want to evaluate the decision on element activation, while
        // the output mapping happens on element completion. We don't want to re-evaluate the
        // decision for output mapping related incidents.
        triggerProcessEventWithResultVariable(context, element.getResultVariable(), result);
    });
    return resultOrFailure;
}
Also used : BpmnElementContext(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext) ErrorType(io.camunda.zeebe.protocol.record.value.ErrorType) DecisionEvaluationResult(io.camunda.zeebe.dmn.DecisionEvaluationResult) ExecutableCalledDecision(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCalledDecision) VariableState(io.camunda.zeebe.engine.state.immutable.VariableState) ZeebeState(io.camunda.zeebe.engine.state.immutable.ZeebeState) MatchedRule(io.camunda.zeebe.dmn.MatchedRule) ByteArrayInputStream(java.io.ByteArrayInputStream) Failure(io.camunda.zeebe.engine.processing.common.Failure) DecisionEngine(io.camunda.zeebe.dmn.DecisionEngine) Either(io.camunda.zeebe.util.Either) DecisionState(io.camunda.zeebe.engine.state.immutable.DecisionState) StateWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.StateWriter) EvaluatedInput(io.camunda.zeebe.dmn.EvaluatedInput) MsgPackConverter(io.camunda.zeebe.protocol.impl.encoding.MsgPackConverter) BufferUtil.bufferAsString(io.camunda.zeebe.util.buffer.BufferUtil.bufferAsString) EvaluatedDecision(io.camunda.zeebe.dmn.EvaluatedDecision) MatchedRuleRecord(io.camunda.zeebe.protocol.impl.record.value.decision.MatchedRuleRecord) EvaluatedDecisionRecord(io.camunda.zeebe.protocol.impl.record.value.decision.EvaluatedDecisionRecord) PersistedDecisionRequirements(io.camunda.zeebe.engine.state.deployment.PersistedDecisionRequirements) KeyGenerator(io.camunda.zeebe.engine.state.KeyGenerator) DecisionEvaluationIntent(io.camunda.zeebe.protocol.record.intent.DecisionEvaluationIntent) ExpandableArrayBuffer(org.agrona.ExpandableArrayBuffer) Collectors(java.util.stream.Collectors) MsgPackWriter(io.camunda.zeebe.msgpack.spec.MsgPackWriter) ExpressionProcessor(io.camunda.zeebe.engine.processing.common.ExpressionProcessor) VariablesContext(io.camunda.zeebe.dmn.impl.VariablesContext) ParsedDecisionRequirementsGraph(io.camunda.zeebe.dmn.ParsedDecisionRequirementsGraph) DecisionEvaluationRecord(io.camunda.zeebe.protocol.impl.record.value.decision.DecisionEvaluationRecord) ProcessEngineMetrics(io.camunda.zeebe.engine.metrics.ProcessEngineMetrics) PersistedDecision(io.camunda.zeebe.engine.state.deployment.PersistedDecision) EventTriggerBehavior(io.camunda.zeebe.engine.processing.common.EventTriggerBehavior) EvaluatedOutput(io.camunda.zeebe.dmn.EvaluatedOutput) BufferUtil(io.camunda.zeebe.util.buffer.BufferUtil) DirectBuffer(org.agrona.DirectBuffer) Failure(io.camunda.zeebe.engine.processing.common.Failure)

Aggregations

DecisionEngine (io.camunda.zeebe.dmn.DecisionEngine)1 DecisionEvaluationResult (io.camunda.zeebe.dmn.DecisionEvaluationResult)1 EvaluatedDecision (io.camunda.zeebe.dmn.EvaluatedDecision)1 EvaluatedInput (io.camunda.zeebe.dmn.EvaluatedInput)1 EvaluatedOutput (io.camunda.zeebe.dmn.EvaluatedOutput)1 MatchedRule (io.camunda.zeebe.dmn.MatchedRule)1 ParsedDecisionRequirementsGraph (io.camunda.zeebe.dmn.ParsedDecisionRequirementsGraph)1 VariablesContext (io.camunda.zeebe.dmn.impl.VariablesContext)1 ProcessEngineMetrics (io.camunda.zeebe.engine.metrics.ProcessEngineMetrics)1 BpmnElementContext (io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext)1 EventTriggerBehavior (io.camunda.zeebe.engine.processing.common.EventTriggerBehavior)1 ExpressionProcessor (io.camunda.zeebe.engine.processing.common.ExpressionProcessor)1 Failure (io.camunda.zeebe.engine.processing.common.Failure)1 ExecutableCalledDecision (io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCalledDecision)1 StateWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.StateWriter)1 KeyGenerator (io.camunda.zeebe.engine.state.KeyGenerator)1 PersistedDecision (io.camunda.zeebe.engine.state.deployment.PersistedDecision)1 PersistedDecisionRequirements (io.camunda.zeebe.engine.state.deployment.PersistedDecisionRequirements)1 DecisionState (io.camunda.zeebe.engine.state.immutable.DecisionState)1 VariableState (io.camunda.zeebe.engine.state.immutable.VariableState)1