Search in sources :

Example 1 with BpmnElementContext

use of io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext 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)

Example 2 with BpmnElementContext

use of io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext in project zeebe by camunda.

the class BpmnStateTransitionBehavior method takeSequenceFlow.

public void takeSequenceFlow(final BpmnElementContext context, final ExecutableSequenceFlow sequenceFlow) {
    verifyTransition(context, ProcessInstanceIntent.SEQUENCE_FLOW_TAKEN);
    final var target = sequenceFlow.getTarget();
    followUpInstanceRecord.wrap(context.getRecordValue());
    followUpInstanceRecord.setElementId(sequenceFlow.getId()).setBpmnElementType(sequenceFlow.getElementType());
    // take the sequence flow
    final var sequenceFlowKey = keyGenerator.nextKey();
    stateWriter.appendFollowUpEvent(sequenceFlowKey, ProcessInstanceIntent.SEQUENCE_FLOW_TAKEN, followUpInstanceRecord);
    final BpmnElementContext sequenceFlowTaken = context.copy(sequenceFlowKey, followUpInstanceRecord, ProcessInstanceIntent.SEQUENCE_FLOW_TAKEN);
    if (canActivateTargetElement(context, target)) {
        activateElementInstanceInFlowScope(sequenceFlowTaken, target);
    }
}
Also used : BpmnElementContext(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext)

Example 3 with BpmnElementContext

use of io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext in project zeebe by camunda.

the class SubProcessProcessor method onActivate.

@Override
public void onActivate(final ExecutableFlowElementContainer element, final BpmnElementContext activating) {
    variableMappingBehavior.applyInputMappings(activating, element).flatMap(ok -> eventSubscriptionBehavior.subscribeToEvents(element, activating)).ifRightOrLeft(ok -> {
        final var activated = stateTransitionBehavior.transitionToActivated(activating);
        final ExecutableStartEvent startEvent = element.getNoneStartEvent();
        if (startEvent == null) {
            throw new BpmnProcessingException(activated, NO_NONE_START_EVENT_ERROR_MSG);
        }
        stateTransitionBehavior.activateChildInstance(activated, startEvent);
    }, failure -> incidentBehavior.createIncident(failure, activating));
}
Also used : BpmnElementContext(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext) BpmnStateTransitionBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateTransitionBehavior) ExecutableStartEvent(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableStartEvent) BpmnIncidentBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnIncidentBehavior) BpmnElementContainerProcessor(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContainerProcessor) BpmnStateBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateBehavior) ProcessInstanceIntent(io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent) BpmnEventSubscriptionBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnEventSubscriptionBehavior) BpmnBehaviors(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnBehaviors) BpmnProcessingException(io.camunda.zeebe.engine.processing.bpmn.BpmnProcessingException) BpmnVariableMappingBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnVariableMappingBehavior) ExecutableFlowElementContainer(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableFlowElementContainer) ExecutableStartEvent(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableStartEvent) BpmnProcessingException(io.camunda.zeebe.engine.processing.bpmn.BpmnProcessingException)

Example 4 with BpmnElementContext

use of io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext in project zeebe by zeebe-io.

the class StartEventProcessor method onComplete.

@Override
public void onComplete(final ExecutableStartEvent element, final BpmnElementContext context) {
    final var flowScope = (ExecutableCatchEventSupplier) element.getFlowScope();
    final BpmnElementContextImpl flowScopeInstanceContext = buildContextForFlowScopeInstance(context);
    variableMappingBehavior.applyOutputMappings(context, element).flatMap(ok -> eventSubscriptionBehavior.subscribeToEvents(flowScope, flowScopeInstanceContext)).flatMap(ok -> stateTransitionBehavior.transitionToCompleted(element, context)).ifRightOrLeft(completed -> stateTransitionBehavior.takeOutgoingSequenceFlows(element, completed), failure -> incidentBehavior.createIncident(failure, context));
}
Also used : BpmnElementContext(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext) BpmnStateTransitionBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateTransitionBehavior) ExecutableStartEvent(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableStartEvent) BpmnIncidentBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnIncidentBehavior) BpmnStateBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateBehavior) BpmnEventSubscriptionBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnEventSubscriptionBehavior) BpmnBehaviors(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnBehaviors) BpmnElementContextImpl(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContextImpl) ExecutableCatchEventSupplier(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCatchEventSupplier) BpmnElementProcessor(io.camunda.zeebe.engine.processing.bpmn.BpmnElementProcessor) BpmnVariableMappingBehavior(io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnVariableMappingBehavior) ExecutableCatchEventSupplier(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCatchEventSupplier) BpmnElementContextImpl(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContextImpl)

Example 5 with BpmnElementContext

use of io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext in project zeebe by zeebe-io.

the class BpmnStateTransitionBehavior method getParentProcessScope.

private ExecutableCallActivity getParentProcessScope(final BpmnElementContext callActivityContext, final BpmnElementContext childContext) {
    final var processDefinitionKey = callActivityContext.getProcessDefinitionKey();
    final var elementId = callActivityContext.getElementId();
    return stateBehavior.getProcess(processDefinitionKey).map(DeployedProcess::getProcess).map(process -> process.getElementById(elementId, BpmnElementType.CALL_ACTIVITY, ExecutableCallActivity.class)).orElseThrow(() -> new BpmnProcessingException(childContext, String.format(NO_PROCESS_FOUND_MESSAGE, processDefinitionKey)));
}
Also used : BpmnElementContext(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext) ExecutableFlowNode(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableFlowNode) TypedCommandWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.TypedCommandWriter) Arrays(java.util.Arrays) ProcessInstanceIntent(io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent) KeyGenerator(io.camunda.zeebe.engine.state.KeyGenerator) ElementInstanceState(io.camunda.zeebe.engine.state.immutable.ElementInstanceState) ProcessInstanceLifecycle(io.camunda.zeebe.engine.processing.bpmn.ProcessInstanceLifecycle) Function(java.util.function.Function) BpmnProcessingException(io.camunda.zeebe.engine.processing.bpmn.BpmnProcessingException) BpmnElementContainerProcessor(io.camunda.zeebe.engine.processing.bpmn.BpmnElementContainerProcessor) DeployedProcess(io.camunda.zeebe.engine.state.deployment.DeployedProcess) ProcessInstanceRecord(io.camunda.zeebe.protocol.impl.record.value.processinstance.ProcessInstanceRecord) ExecutableCallActivity(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCallActivity) BpmnElementType(io.camunda.zeebe.protocol.record.value.BpmnElementType) Failure(io.camunda.zeebe.engine.processing.common.Failure) ProcessEngineMetrics(io.camunda.zeebe.engine.metrics.ProcessEngineMetrics) Writers(io.camunda.zeebe.engine.processing.streamprocessor.writers.Writers) Either(io.camunda.zeebe.util.Either) ExecutableSequenceFlow(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableSequenceFlow) StateWriter(io.camunda.zeebe.engine.processing.streamprocessor.writers.StateWriter) ExecutableFlowElement(io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableFlowElement) DeployedProcess(io.camunda.zeebe.engine.state.deployment.DeployedProcess) BpmnProcessingException(io.camunda.zeebe.engine.processing.bpmn.BpmnProcessingException)

Aggregations

BpmnElementContext (io.camunda.zeebe.engine.processing.bpmn.BpmnElementContext)20 ExecutableFlowElement (io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableFlowElement)6 ProcessEngineMetrics (io.camunda.zeebe.engine.metrics.ProcessEngineMetrics)5 Failure (io.camunda.zeebe.engine.processing.common.Failure)5 StateWriter (io.camunda.zeebe.engine.processing.streamprocessor.writers.StateWriter)5 KeyGenerator (io.camunda.zeebe.engine.state.KeyGenerator)5 Either (io.camunda.zeebe.util.Either)5 BpmnElementContainerProcessor (io.camunda.zeebe.engine.processing.bpmn.BpmnElementContainerProcessor)4 BpmnProcessingException (io.camunda.zeebe.engine.processing.bpmn.BpmnProcessingException)4 ProcessInstanceIntent (io.camunda.zeebe.protocol.record.intent.ProcessInstanceIntent)4 ProcessInstanceLifecycle (io.camunda.zeebe.engine.processing.bpmn.ProcessInstanceLifecycle)3 BpmnBehaviors (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnBehaviors)3 BpmnEventSubscriptionBehavior (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnEventSubscriptionBehavior)3 BpmnIncidentBehavior (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnIncidentBehavior)3 BpmnStateBehavior (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateBehavior)3 BpmnStateTransitionBehavior (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnStateTransitionBehavior)3 BpmnVariableMappingBehavior (io.camunda.zeebe.engine.processing.bpmn.behavior.BpmnVariableMappingBehavior)3 ExecutableCallActivity (io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableCallActivity)3 ExecutableFlowNode (io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableFlowNode)3 ExecutableSequenceFlow (io.camunda.zeebe.engine.processing.deployment.model.element.ExecutableSequenceFlow)3