Search in sources :

Example 1 with PvmScope

use of org.camunda.bpm.engine.impl.pvm.PvmScope in project camunda-bpm-platform by camunda.

the class AbstractInstantiationCmd method execute.

public Void execute(final CommandContext commandContext) {
    ExecutionEntity processInstance = commandContext.getExecutionManager().findExecutionById(processInstanceId);
    final ProcessDefinitionImpl processDefinition = processInstance.getProcessDefinition();
    CoreModelElement elementToInstantiate = getTargetElement(processDefinition);
    EnsureUtil.ensureNotNull(NotValidException.class, describeFailure("Element '" + getTargetElementId() + "' does not exist in process '" + processDefinition.getId() + "'"), "element", elementToInstantiate);
    // rebuild the mapping because the execution tree changes with every iteration
    final ActivityExecutionTreeMapping mapping = new ActivityExecutionTreeMapping(commandContext, processInstanceId);
    // before instantiating an activity, two things have to be determined:
    // 
    // activityStack:
    // For the activity to instantiate, we build a stack of parent flow scopes
    // for which no executions exist yet and that have to be instantiated
    // 
    // scopeExecution:
    // This is typically the execution under which a new sub tree has to be created.
    // if an explicit ancestor activity instance is set:
    // - this is the scope execution for that ancestor activity instance
    // - throws exception if that scope execution is not in the parent hierarchy
    // of the activity to be started
    // if no explicit ancestor activity instance is set:
    // - this is the execution of the first parent/ancestor flow scope that has an execution
    // - throws an exception if there is more than one such execution
    ScopeImpl targetFlowScope = getTargetFlowScope(processDefinition);
    // prepare to walk up the flow scope hierarchy and collect the flow scope activities
    ActivityStackCollector stackCollector = new ActivityStackCollector();
    FlowScopeWalker walker = new FlowScopeWalker(targetFlowScope);
    walker.addPreVisitor(stackCollector);
    ExecutionEntity scopeExecution = null;
    // if no explicit ancestor activity instance is set
    if (ancestorActivityInstanceId == null) {
        // walk until a scope is reached for which executions exist
        walker.walkWhile(new ReferenceWalker.WalkCondition<ScopeImpl>() {

            public boolean isFulfilled(ScopeImpl element) {
                return !mapping.getExecutions(element).isEmpty() || element == processDefinition;
            }
        });
        Set<ExecutionEntity> flowScopeExecutions = mapping.getExecutions(walker.getCurrentElement());
        if (flowScopeExecutions.size() > 1) {
            throw new ProcessEngineException("Ancestor activity execution is ambiguous for activity " + targetFlowScope);
        }
        scopeExecution = flowScopeExecutions.iterator().next();
    } else {
        ActivityInstance tree = commandContext.runWithoutAuthorization(new Callable<ActivityInstance>() {

            public ActivityInstance call() throws Exception {
                return new GetActivityInstanceCmd(processInstanceId).execute(commandContext);
            }
        });
        ActivityInstance ancestorInstance = findActivityInstance(tree, ancestorActivityInstanceId);
        EnsureUtil.ensureNotNull(NotValidException.class, describeFailure("Ancestor activity instance '" + ancestorActivityInstanceId + "' does not exist"), "ancestorInstance", ancestorInstance);
        // determine ancestor activity scope execution and activity
        final ExecutionEntity ancestorScopeExecution = getScopeExecutionForActivityInstance(processInstance, mapping, ancestorInstance);
        final PvmScope ancestorScope = getScopeForActivityInstance(processDefinition, ancestorInstance);
        // walk until the scope of the ancestor scope execution is reached
        walker.walkWhile(new ReferenceWalker.WalkCondition<ScopeImpl>() {

            public boolean isFulfilled(ScopeImpl element) {
                return (mapping.getExecutions(element).contains(ancestorScopeExecution) && element == ancestorScope) || element == processDefinition;
            }
        });
        Set<ExecutionEntity> flowScopeExecutions = mapping.getExecutions(walker.getCurrentElement());
        if (!flowScopeExecutions.contains(ancestorScopeExecution)) {
            throw new NotValidException(describeFailure("Scope execution for '" + ancestorActivityInstanceId + "' cannot be found in parent hierarchy of flow element '" + elementToInstantiate.getId() + "'"));
        }
        scopeExecution = ancestorScopeExecution;
    }
    List<PvmActivity> activitiesToInstantiate = stackCollector.getActivityStack();
    Collections.reverse(activitiesToInstantiate);
    // We have to make a distinction between
    // - "regular" activities for which the activity stack can be instantiated and started
    // right away
    // - interrupting or cancelling activities for which we have to ensure that
    // the interruption and cancellation takes place before we instantiate the activity stack
    ActivityImpl topMostActivity = null;
    ScopeImpl flowScope = null;
    if (!activitiesToInstantiate.isEmpty()) {
        topMostActivity = (ActivityImpl) activitiesToInstantiate.get(0);
        flowScope = topMostActivity.getFlowScope();
    } else if (ActivityImpl.class.isAssignableFrom(elementToInstantiate.getClass())) {
        topMostActivity = (ActivityImpl) elementToInstantiate;
        flowScope = topMostActivity.getFlowScope();
    } else if (TransitionImpl.class.isAssignableFrom(elementToInstantiate.getClass())) {
        TransitionImpl transitionToInstantiate = (TransitionImpl) elementToInstantiate;
        flowScope = transitionToInstantiate.getSource().getFlowScope();
    }
    if (!supportsConcurrentChildInstantiation(flowScope)) {
        throw new ProcessEngineException("Concurrent instantiation not possible for " + "activities in scope " + flowScope.getId());
    }
    ActivityStartBehavior startBehavior = ActivityStartBehavior.CONCURRENT_IN_FLOW_SCOPE;
    if (topMostActivity != null) {
        startBehavior = topMostActivity.getActivityStartBehavior();
        if (!activitiesToInstantiate.isEmpty()) {
            // this is in BPMN relevant if there is an interrupting event sub process.
            // we have to distinguish between instantiation of the start event and any other activity.
            // instantiation of the start event means interrupting behavior; instantiation
            // of any other task means no interruption.
            PvmActivity initialActivity = topMostActivity.getProperties().get(BpmnProperties.INITIAL_ACTIVITY);
            PvmActivity secondTopMostActivity = null;
            if (activitiesToInstantiate.size() > 1) {
                secondTopMostActivity = activitiesToInstantiate.get(1);
            } else if (ActivityImpl.class.isAssignableFrom(elementToInstantiate.getClass())) {
                secondTopMostActivity = (PvmActivity) elementToInstantiate;
            }
            if (initialActivity != secondTopMostActivity) {
                startBehavior = ActivityStartBehavior.CONCURRENT_IN_FLOW_SCOPE;
            }
        }
    }
    switch(startBehavior) {
        case CANCEL_EVENT_SCOPE:
            {
                ScopeImpl scopeToCancel = topMostActivity.getEventScope();
                ExecutionEntity executionToCancel = getSingleExecutionForScope(mapping, scopeToCancel);
                if (executionToCancel != null) {
                    executionToCancel.deleteCascade("Cancelling activity " + topMostActivity + " executed.", skipCustomListeners, skipIoMappings);
                    instantiate(executionToCancel.getParent(), activitiesToInstantiate, elementToInstantiate);
                } else {
                    ExecutionEntity flowScopeExecution = getSingleExecutionForScope(mapping, topMostActivity.getFlowScope());
                    instantiateConcurrent(flowScopeExecution, activitiesToInstantiate, elementToInstantiate);
                }
                break;
            }
        case INTERRUPT_EVENT_SCOPE:
            {
                ScopeImpl scopeToCancel = topMostActivity.getEventScope();
                ExecutionEntity executionToCancel = getSingleExecutionForScope(mapping, scopeToCancel);
                executionToCancel.interrupt("Interrupting activity " + topMostActivity + " executed.", skipCustomListeners, skipIoMappings);
                executionToCancel.setActivity(null);
                executionToCancel.leaveActivityInstance();
                instantiate(executionToCancel, activitiesToInstantiate, elementToInstantiate);
                break;
            }
        case INTERRUPT_FLOW_SCOPE:
            {
                ScopeImpl scopeToCancel = topMostActivity.getFlowScope();
                ExecutionEntity executionToCancel = getSingleExecutionForScope(mapping, scopeToCancel);
                executionToCancel.interrupt("Interrupting activity " + topMostActivity + " executed.", skipCustomListeners, skipIoMappings);
                executionToCancel.setActivity(null);
                executionToCancel.leaveActivityInstance();
                instantiate(executionToCancel, activitiesToInstantiate, elementToInstantiate);
                break;
            }
        default:
            {
                // or this execution has ended executing its scope, it can be reused
                if (!scopeExecution.hasChildren() && (scopeExecution.getActivity() == null || scopeExecution.isEnded())) {
                    // reuse the scope execution
                    instantiate(scopeExecution, activitiesToInstantiate, elementToInstantiate);
                } else {
                    // if the activity is not cancelling/interrupting, it can simply be instantiated as
                    // a concurrent child of the scopeExecution
                    instantiateConcurrent(scopeExecution, activitiesToInstantiate, elementToInstantiate);
                }
                break;
            }
    }
    return null;
}
Also used : TransitionImpl(org.camunda.bpm.engine.impl.pvm.process.TransitionImpl) NotValidException(org.camunda.bpm.engine.exception.NotValidException) ProcessDefinitionImpl(org.camunda.bpm.engine.impl.pvm.process.ProcessDefinitionImpl) ActivityInstance(org.camunda.bpm.engine.runtime.ActivityInstance) PvmActivity(org.camunda.bpm.engine.impl.pvm.PvmActivity) ActivityStackCollector(org.camunda.bpm.engine.impl.tree.ActivityStackCollector) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException) NotValidException(org.camunda.bpm.engine.exception.NotValidException) PvmScope(org.camunda.bpm.engine.impl.pvm.PvmScope) ExecutionEntity(org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity) ReferenceWalker(org.camunda.bpm.engine.impl.tree.ReferenceWalker) ActivityImpl(org.camunda.bpm.engine.impl.pvm.process.ActivityImpl) ActivityExecutionTreeMapping(org.camunda.bpm.engine.impl.ActivityExecutionTreeMapping) FlowScopeWalker(org.camunda.bpm.engine.impl.tree.FlowScopeWalker) CoreModelElement(org.camunda.bpm.engine.impl.core.model.CoreModelElement) ScopeImpl(org.camunda.bpm.engine.impl.pvm.process.ScopeImpl) ActivityStartBehavior(org.camunda.bpm.engine.impl.pvm.process.ActivityStartBehavior) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 2 with PvmScope

use of org.camunda.bpm.engine.impl.pvm.PvmScope in project camunda-bpm-platform by camunda.

the class ThrowEscalationEventActivityBehavior method executeEscalationHandler.

protected void executeEscalationHandler(EscalationEventDefinition escalationEventDefinition, ActivityExecutionMappingCollector activityExecutionMappingCollector) {
    PvmActivity escalationHandler = escalationEventDefinition.getEscalationHandler();
    PvmScope escalationScope = getScopeForEscalation(escalationEventDefinition);
    ActivityExecution escalationExecution = activityExecutionMappingCollector.getExecutionForScope(escalationScope);
    if (escalationEventDefinition.getEscalationCodeVariable() != null) {
        escalationExecution.setVariable(escalationEventDefinition.getEscalationCodeVariable(), escalation.getEscalationCode());
    }
    escalationExecution.executeActivity(escalationHandler);
}
Also used : PvmScope(org.camunda.bpm.engine.impl.pvm.PvmScope) ActivityExecution(org.camunda.bpm.engine.impl.pvm.delegate.ActivityExecution) PvmActivity(org.camunda.bpm.engine.impl.pvm.PvmActivity)

Example 3 with PvmScope

use of org.camunda.bpm.engine.impl.pvm.PvmScope in project camunda-bpm-platform by camunda.

the class DefaultHistoryEventProducer method initActivityInstanceEvent.

protected void initActivityInstanceEvent(HistoricActivityInstanceEventEntity evt, ExecutionEntity execution, HistoryEventType eventType) {
    PvmScope eventSource = execution.getActivity();
    if (eventSource == null) {
        eventSource = (PvmScope) execution.getEventSource();
    }
    String activityInstanceId = execution.getActivityInstanceId();
    String parentActivityInstanceId = null;
    ExecutionEntity parentExecution = execution.getParent();
    if (parentExecution != null && CompensationBehavior.isCompensationThrowing(parentExecution) && execution.getActivity() != null) {
        parentActivityInstanceId = CompensationBehavior.getParentActivityInstanceId(execution);
    } else {
        parentActivityInstanceId = execution.getParentActivityInstanceId();
    }
    initActivityInstanceEvent(evt, execution, eventSource, activityInstanceId, parentActivityInstanceId, eventType);
}
Also used : PvmScope(org.camunda.bpm.engine.impl.pvm.PvmScope) ExecutionEntity(org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity) CaseExecutionEntity(org.camunda.bpm.engine.impl.cmmn.entity.runtime.CaseExecutionEntity)

Example 4 with PvmScope

use of org.camunda.bpm.engine.impl.pvm.PvmScope in project camunda-bpm-platform by camunda.

the class PvmAtomicOperationActivityEnd method execute.

public void execute(PvmExecutionImpl execution) {
    // restore activity instance id
    if (execution.getActivityInstanceId() == null) {
        execution.setActivityInstanceId(execution.getParentActivityInstanceId());
    }
    PvmActivity activity = execution.getActivity();
    Map<ScopeImpl, PvmExecutionImpl> activityExecutionMapping = execution.createActivityExecutionMapping();
    PvmExecutionImpl propagatingExecution = execution;
    if (execution.isScope() && activity.isScope()) {
        if (!LegacyBehavior.destroySecondNonScope(execution)) {
            execution.destroy();
            if (!execution.isConcurrent()) {
                execution.remove();
                propagatingExecution = execution.getParent();
                propagatingExecution.setActivity(execution.getActivity());
            }
        }
    }
    propagatingExecution = LegacyBehavior.determinePropagatingExecutionOnEnd(propagatingExecution, activityExecutionMapping);
    PvmScope flowScope = activity.getFlowScope();
    // 1. flow scope = Process Definition
    if (flowScope == activity.getProcessDefinition()) {
        // 1.1 concurrent execution => end + tryPrune()
        if (propagatingExecution.isConcurrent()) {
            propagatingExecution.remove();
            propagatingExecution.getParent().tryPruneLastConcurrentChild();
            propagatingExecution.getParent().forceUpdate();
        } else {
            // 1.2 Process End
            propagatingExecution.setEnded(true);
            if (!propagatingExecution.isPreserveScope()) {
                propagatingExecution.performOperation(PROCESS_END);
            }
        }
    } else {
        // 2. flowScope != process definition
        PvmActivity flowScopeActivity = (PvmActivity) flowScope;
        ActivityBehavior activityBehavior = flowScopeActivity.getActivityBehavior();
        if (activityBehavior instanceof CompositeActivityBehavior) {
            CompositeActivityBehavior compositeActivityBehavior = (CompositeActivityBehavior) activityBehavior;
            // 2.1 Concurrent execution => composite behavior.concurrentExecutionEnded()
            if (propagatingExecution.isConcurrent() && !LegacyBehavior.isConcurrentScope(propagatingExecution)) {
                compositeActivityBehavior.concurrentChildExecutionEnded(propagatingExecution.getParent(), propagatingExecution);
            } else {
                // 2.2 Scope Execution => composite behavior.complete()
                propagatingExecution.setActivity(flowScopeActivity);
                compositeActivityBehavior.complete(propagatingExecution);
            }
        } else {
            // activity behavior is not composite => this is unexpected
            throw new ProcessEngineException("Expected behavior of composite scope " + activity + " to be a CompositeActivityBehavior but got " + activityBehavior);
        }
    }
}
Also used : PvmScope(org.camunda.bpm.engine.impl.pvm.PvmScope) PvmExecutionImpl(org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl) CompositeActivityBehavior(org.camunda.bpm.engine.impl.pvm.delegate.CompositeActivityBehavior) ActivityBehavior(org.camunda.bpm.engine.impl.pvm.delegate.ActivityBehavior) CompositeActivityBehavior(org.camunda.bpm.engine.impl.pvm.delegate.CompositeActivityBehavior) ScopeImpl(org.camunda.bpm.engine.impl.pvm.process.ScopeImpl) PvmActivity(org.camunda.bpm.engine.impl.pvm.PvmActivity) ProcessEngineException(org.camunda.bpm.engine.ProcessEngineException)

Example 5 with PvmScope

use of org.camunda.bpm.engine.impl.pvm.PvmScope in project camunda-bpm-platform by camunda.

the class ActivityExecutionHierarchyWalker method nextElement.

@Override
protected ActivityExecutionTuple nextElement() {
    PvmScope currentScope = getCurrentElement().getScope();
    PvmScope flowScope = currentScope.getFlowScope();
    if (flowScope != null) {
        // walk to parent scope
        PvmExecutionImpl execution = activityExecutionMapping.get(flowScope);
        return new ActivityExecutionTuple(flowScope, execution);
    } else {
        // this is the process instance, look for parent
        PvmExecutionImpl currentExecution = activityExecutionMapping.get(currentScope);
        PvmExecutionImpl superExecution = currentExecution.getSuperExecution();
        if (superExecution != null) {
            // walk to parent process instance
            activityExecutionMapping = superExecution.createActivityExecutionMapping();
            return createTupel(superExecution);
        } else {
            // this is the top level process instance
            return null;
        }
    }
}
Also used : PvmScope(org.camunda.bpm.engine.impl.pvm.PvmScope) PvmExecutionImpl(org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl)

Aggregations

PvmScope (org.camunda.bpm.engine.impl.pvm.PvmScope)7 ExecutionEntity (org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity)3 PvmActivity (org.camunda.bpm.engine.impl.pvm.PvmActivity)3 ScopeImpl (org.camunda.bpm.engine.impl.pvm.process.ScopeImpl)3 ProcessEngineException (org.camunda.bpm.engine.ProcessEngineException)2 CaseExecutionEntity (org.camunda.bpm.engine.impl.cmmn.entity.runtime.CaseExecutionEntity)2 PvmExecutionImpl (org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl)2 NotValidException (org.camunda.bpm.engine.exception.NotValidException)1 ActivityExecutionTreeMapping (org.camunda.bpm.engine.impl.ActivityExecutionTreeMapping)1 CoreModelElement (org.camunda.bpm.engine.impl.core.model.CoreModelElement)1 MigratingActivityInstance (org.camunda.bpm.engine.impl.migration.instance.MigratingActivityInstance)1 ActivityBehavior (org.camunda.bpm.engine.impl.pvm.delegate.ActivityBehavior)1 ActivityExecution (org.camunda.bpm.engine.impl.pvm.delegate.ActivityExecution)1 CompositeActivityBehavior (org.camunda.bpm.engine.impl.pvm.delegate.CompositeActivityBehavior)1 ActivityImpl (org.camunda.bpm.engine.impl.pvm.process.ActivityImpl)1 ActivityStartBehavior (org.camunda.bpm.engine.impl.pvm.process.ActivityStartBehavior)1 ProcessDefinitionImpl (org.camunda.bpm.engine.impl.pvm.process.ProcessDefinitionImpl)1 TransitionImpl (org.camunda.bpm.engine.impl.pvm.process.TransitionImpl)1 ActivityStackCollector (org.camunda.bpm.engine.impl.tree.ActivityStackCollector)1 FlowScopeWalker (org.camunda.bpm.engine.impl.tree.FlowScopeWalker)1