protected internal override PvmExecutionImpl EventNotificationsStarted(PvmExecutionImpl execution)
        {
            execution.IncrementSequenceCounter();

            // hack around execution tree structure not being in sync with activity instance concept:
            // if we end a scope activity, take remembered activity instance from parent and set on
            // execution before calling END listeners.
            var          parent   = execution.Parent;
            IPvmActivity activity = execution.Activity;

            if ((parent != null) && execution.IsScope && (activity != null) && activity.IsScope &&
                (activity.ActivityBehavior is ICompositeActivityBehavior ||
                 (CompensationBehavior.IsCompensationThrowing(execution) &&
                  !LegacyBehavior.IsCompensationThrowing(execution))))
            {
                Log.DebugLeavesActivityInstance(execution, execution.ActivityInstanceId);

                // use remembered activity instance id from parent
                execution.ActivityInstanceId = parent.ActivityInstanceId;
                // make parent go one scope up.
                parent.LeaveActivityInstance();
            }

            return(execution);
        }
Esempio n. 2
0
        public virtual void Execute(PvmExecutionImpl execution)
        {
            // Assumption: execution is scope
            var cancellingActivity = execution.NextActivity;

            execution.NextActivity = null;

            // first, cancel and destroy the current scope
            execution.IsActive = true;

            PvmExecutionImpl propagatingExecution = null;

            if (LegacyBehavior.IsConcurrentScope(execution))
            {
                // this is legacy behavior
                LegacyBehavior.CancelConcurrentScope(execution, (IPvmActivity)cancellingActivity.EventScope);
                propagatingExecution = execution;
            }
            else
            {
                // Unlike PvmAtomicOperationTransitionDestroyScope this needs to use delete() (instead of destroy() and remove()).
                // The reason is that PvmAtomicOperationTransitionDestroyScope is executed when a scope (or non scope) is left using
                // a sequence flow. In that case the execution will have completed all the work inside the current activity
                // and will have no more child executions. In PvmAtomicOperationCancelScope the scope is cancelled due to
                // a boundary event firing. In that case the execution has not completed all the work in the current scope / activity
                // and it is necessary to delete the complete hierarchy of executions below and including the execution itself.
                execution.DeleteCascade("Cancel scope activity " + cancellingActivity + " executed.");
                propagatingExecution = (PvmExecutionImpl)execution.Parent;
            }

            propagatingExecution.Activity = (ActivityImpl)(cancellingActivity);
            propagatingExecution.IsActive = true;
            propagatingExecution.IsEnded  = false;
            ActivityCancelled(propagatingExecution);
        }
Esempio n. 3
0
        private void appendActivityExecutionMapping(ActivityExecution execution)
        {
            if (execution.Activity != null && !LegacyBehavior.hasInvalidIntermediaryActivityId((PvmExecutionImpl)execution))
            {
//JAVA TO C# CONVERTER TODO TASK: There is no .NET Dictionary equivalent to the Java 'putAll' method:
                activityExecutionMapping.putAll(execution.createActivityExecutionMapping());
            }
        }
 public override void Complete(IActivityExecution scopeExecution)
 {
     // check whether legacy behavior needs to be performed.
     if (!LegacyBehavior.EventSubprocessComplete(scopeExecution))
     {
         base.Complete(scopeExecution);
     }
 }
 public virtual void Visit(PvmExecutionImpl obj)
 {
     if ((obj.NonEventScopeExecutions.Count == 0) ||
         ((obj.Activity != null) && !LegacyBehavior.HasInvalidIntermediaryActivityId(obj)))
     {
         leaves.Add(obj);
     }
 }
 public override void ConcurrentChildExecutionEnded(IActivityExecution scopeExecution,
                                                    IActivityExecution endedExecution)
 {
     // Check whether legacy behavior needs to be performed.
     // Legacy behavior means that the event subprocess is not a scope and as a result does not
     // join concurrent executions on it's own. Instead it delegates to the the subprocess activity behavior in which it is embedded.
     if (!LegacyBehavior.EventSubprocessConcurrentChildExecutionEnded(scopeExecution, endedExecution))
     {
         base.ConcurrentChildExecutionEnded(scopeExecution, endedExecution);
     }
 }
Esempio n. 7
0
 private void AppendActivityExecutionMapping(IActivityExecution execution)
 {
     if ((execution.Activity != null) &&
         !LegacyBehavior.HasInvalidIntermediaryActivityId((PvmExecutionImpl)execution))
     {
         foreach (var it in execution.CreateActivityExecutionMapping())
         {
             //_activityExecutionMapping.Add(it.Key, it.Value);
             _activityExecutionMapping[it.Key] = it.Value;
         }
     }
 }
 public override void Signal(IActivityExecution execution, string signalName, object signalData)
 {
     if (LegacyBehavior.SignalCancelBoundaryEvent(signalName))
     {
         if (!execution.HasChildren())
         {
             Leave(execution);
         }
         else
         {
             base.Signal(execution, signalName, signalData);
         }
     }
 }
Esempio n. 9
0
        protected internal virtual void UpdateJobDeclarations(IList <IJobDeclaration> jobDeclarations, ProcessDefinitionEntity processDefinition, bool isNewDeployment)
        {
            if (jobDeclarations == null || jobDeclarations.Count == 0)
            {
                return;
            }

            IJobDefinitionManager jobDefinitionManager = JobDefinitionManager;

            if (isNewDeployment)
            {
                // create new job definitions:
                foreach (IJobDeclaration jobDeclaration in jobDeclarations)
                {
                    CreateJobDefinition(processDefinition, jobDeclaration);
                }
            }
            else
            {
                // query all job definitions and update the declarations with their Ids
                IList <JobDefinitionEntity> existingDefinitions = jobDefinitionManager.FindByProcessDefinitionId(processDefinition.Id);

                LegacyBehavior.MigrateMultiInstanceJobDefinitions(processDefinition, existingDefinitions);

                foreach (IJobDeclaration jobDeclaration in jobDeclarations)
                {
                    bool jobDefinitionExists = false;
                    foreach (JobDefinitionEntity jobDefinitionEntity in existingDefinitions)
                    {
                        // <!> Assumption: there can be only one job definition per activity and type
                        if (jobDeclaration.ActivityId.Equals(jobDefinitionEntity.ActivityId) && jobDeclaration.JobHandlerType.Equals(jobDefinitionEntity.JobType))
                        {
                            jobDeclaration.JobDefinitionId = jobDefinitionEntity.Id;
                            jobDefinitionExists            = true;
                            break;
                        }
                    }

                    if (!jobDefinitionExists)
                    {
                        // not found: create new definition
                        CreateJobDefinition(processDefinition, jobDeclaration);
                    }
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        ///     Creates and inserts a subscription entity depending on the message type of this declaration.
        /// </summary>
        public virtual EventSubscriptionEntity CreateSubscriptionForExecution(ExecutionEntity execution)
        {
            var eventSubscriptionEntity = new EventSubscriptionEntity(execution, eventType);

            eventSubscriptionEntity.EventName = ResolveExpressionOfEventName(execution);
            if (activityId != null)
            {
                ActivityImpl activity = execution.GetProcessDefinition().FindActivity(activityId) as ActivityImpl;
                eventSubscriptionEntity.Activity = activity;
            }

            eventSubscriptionEntity.Insert();
            LegacyBehavior.RemoveLegacySubscriptionOnParent(execution, eventSubscriptionEntity);

            return(eventSubscriptionEntity);
            //return null;
        }
Esempio n. 11
0
        /// <summary>
        /// Creates and inserts a subscription entity depending on the message type of this declaration.
        /// </summary>
        public virtual EventSubscriptionEntity createSubscriptionForExecution(ExecutionEntity execution)
        {
            EventSubscriptionEntity eventSubscriptionEntity = new EventSubscriptionEntity(execution, eventType);

            string eventName = resolveExpressionOfEventName(execution);

            eventSubscriptionEntity.EventName = eventName;
            if (!string.ReferenceEquals(activityId, null))
            {
                ActivityImpl activity = execution.getProcessDefinition().findActivity(activityId);
                eventSubscriptionEntity.Activity = activity;
            }

            eventSubscriptionEntity.insert();
            LegacyBehavior.removeLegacySubscriptionOnParent(execution, eventSubscriptionEntity);

            return(eventSubscriptionEntity);
        }
Esempio n. 12
0
        public virtual void execute(AsyncContinuationConfiguration configuration, ExecutionEntity execution, CommandContext commandContext, string tenantId)
        {
            LegacyBehavior.repairMultiInstanceAsyncJob(execution);

            PvmAtomicOperation atomicOperation = findMatchingAtomicOperation(configuration.AtomicOperation);

            ensureNotNull("Cannot process job with configuration " + configuration, "atomicOperation", atomicOperation);

            // reset transition id.
            string transitionId = configuration.TransitionId;

            if (!string.ReferenceEquals(transitionId, null))
            {
                PvmActivity    activity   = execution.getActivity();
                TransitionImpl transition = (TransitionImpl)activity.findOutgoingTransition(transitionId);
                execution.Transition = transition;
            }

            Context.CommandInvocationContext.performOperation(atomicOperation, execution);
        }
        public virtual void Execute(IJobHandlerConfiguration configuration, ExecutionEntity execution,
                                    CommandContext commandContext, string tenantId)
        {
            var config = (AsyncContinuationConfiguration)configuration;

            LegacyBehavior.RepairMultiInstanceAsyncJob(execution);

            var atomicOperation = FindMatchingAtomicOperation(config.AtomicOperation);

            EnsureUtil.EnsureNotNull("Cannot process job with configuration " + configuration, "atomicOperation",
                                     atomicOperation);

            // reset transition id.
            var transitionId = config.TransitionId;

            if (!ReferenceEquals(transitionId, null))
            {
                IPvmActivity activity   = execution.GetActivity();
                var          transition = (TransitionImpl)activity.FindOutgoingTransition(transitionId);
                execution.Transition = transition;
            }

            Context.CommandInvocationContext.PerformOperation(atomicOperation, execution);
        }
Esempio n. 14
0
        public virtual void execute(PvmExecutionImpl execution)
        {
            // restore activity instance id
            if (string.ReferenceEquals(execution.ActivityInstanceId, null))
            {
                execution.ActivityInstanceId = execution.ParentActivityInstanceId;
            }

            PvmActivity activity = execution.getActivity();
            IDictionary <ScopeImpl, PvmExecutionImpl> activityExecutionMapping = execution.createActivityExecutionMapping();

            PvmExecutionImpl propagatingExecution = execution;

            if (execution.Scope && activity.Scope)
            {
                if (!LegacyBehavior.destroySecondNonScope(execution))
                {
                    execution.destroy();
                    if (!execution.Concurrent)
                    {
                        execution.remove();
                        propagatingExecution = execution.Parent;
                        propagatingExecution.setActivity(execution.getActivity());
                    }
                }
            }

            propagatingExecution = LegacyBehavior.determinePropagatingExecutionOnEnd(propagatingExecution, activityExecutionMapping);
            PvmScope flowScope = activity.FlowScope;

            // 1. flow scope = Process Definition
            if (flowScope == activity.ProcessDefinition)
            {
                // 1.1 concurrent execution => end + tryPrune()
                if (propagatingExecution.Concurrent)
                {
                    propagatingExecution.remove();
                    propagatingExecution.Parent.tryPruneLastConcurrentChild();
                    propagatingExecution.Parent.forceUpdate();
                }
                else
                {
                    // 1.2 Process End
                    propagatingExecution.Ended = true;
                    if (!propagatingExecution.PreserveScope)
                    {
                        propagatingExecution.performOperation(PvmAtomicOperation_Fields.PROCESS_END);
                    }
                }
            }
            else
            {
                // 2. flowScope != process definition
                PvmActivity flowScopeActivity = (PvmActivity)flowScope;

                ActivityBehavior activityBehavior = flowScopeActivity.ActivityBehavior;
                if (activityBehavior is CompositeActivityBehavior)
                {
                    CompositeActivityBehavior compositeActivityBehavior = (CompositeActivityBehavior)activityBehavior;
                    // 2.1 Concurrent execution => composite behavior.concurrentExecutionEnded()
                    if (propagatingExecution.Concurrent && !LegacyBehavior.isConcurrentScope(propagatingExecution))
                    {
                        compositeActivityBehavior.concurrentChildExecutionEnded(propagatingExecution.Parent, 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);
                }
            }
        }
Esempio n. 15
0
 public virtual void visit(PvmExecutionImpl obj)
 {
     if (obj.NonEventScopeExecutions.Count == 0 || (obj.getActivity() != null && !LegacyBehavior.hasInvalidIntermediaryActivityId(obj)))
     {
         leaves.Add(obj);
     }
 }
Esempio n. 16
0
        public virtual void Execute(PvmExecutionImpl execution)
        {
            // restore activity instance id
            if (execution.ActivityInstanceId == null)
            {
                execution.ActivityInstanceId = execution.ParentActivityInstanceId;
            }

            IPvmActivity activity = execution.Activity;
            var          activityExecutionMapping = execution.CreateActivityExecutionMapping();

            var propagatingExecution = execution;

            if (execution.IsScope && activity.IsScope)
            {
                if (!LegacyBehavior.DestroySecondNonScope(execution))
                {
                    execution.Destroy();
                    if (!execution.IsConcurrent)
                    {
                        execution.Remove();
                        propagatingExecution          = (PvmExecutionImpl)execution.Parent;
                        propagatingExecution.Activity = (execution.Activity);
                    }
                }
            }

            propagatingExecution = (PvmExecutionImpl)LegacyBehavior.DeterminePropagatingExecutionOnEnd(propagatingExecution,
                                                                                                       activityExecutionMapping);
            IPvmScope flowScope = activity.FlowScope;

            // 1. flow scope = Process Definition
            if (flowScope == activity.ProcessDefinition)
            {
                // 1.1 concurrent execution => end + tryPrune()
                if (propagatingExecution.IsConcurrent)
                {
                    propagatingExecution.Remove();
                    propagatingExecution.Parent.TryPruneLastConcurrentChild();
                    propagatingExecution.Parent.ForceUpdate();
                }
                else
                {
                    // 1.2 Process End
                    if (!propagatingExecution.PreserveScope)
                    {
                        propagatingExecution.PerformOperation(PvmAtomicOperationFields.ProcessEnd);
                    }
                }
            }
            else
            {
                // 2. flowScope != process definition
                var flowScopeActivity = (IPvmActivity)flowScope;

                var activityBehavior = flowScopeActivity.ActivityBehavior;
                if (activityBehavior is ICompositeActivityBehavior)
                {
                    var compositeActivityBehavior = (ICompositeActivityBehavior)activityBehavior;
                    // 2.1 Concurrent execution => composite behavior.concurrentExecutionEnded()
                    if (propagatingExecution.IsConcurrent && !LegacyBehavior.IsConcurrentScope(propagatingExecution))
                    {
                        compositeActivityBehavior.ConcurrentChildExecutionEnded(propagatingExecution.Parent,
                                                                                propagatingExecution);
                    }
                    else
                    {
                        // 2.2 Scope Execution => composite behavior.complete()
                        propagatingExecution.Activity = (ActivityImpl)(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);
                }
            }
        }
Esempio n. 17
0
        public virtual void Execute(PvmExecutionImpl execution)
        {
            // calculate the propagating execution
            var propagatingExecution = execution;

            IPvmActivity activity          = execution.Activity;
            var          transitionsToTake = execution.TransitionsToTake;

            execution.TransitionsToTake = null;

            // check whether the current scope needs to be destroyed
            if (execution.IsScope && activity.IsScope)
            {
                if (!LegacyBehavior.DestroySecondNonScope(execution))
                {
                    if (execution.IsConcurrent)
                    {
                        // legacy behavior
                        LegacyBehavior.DestroyConcurrentScope(execution);
                    }
                    else
                    {
                        propagatingExecution            = (PvmExecutionImpl)execution.Parent;
                        propagatingExecution.Activity   = (execution.Activity);
                        propagatingExecution.Transition = (execution.Transition);
                        propagatingExecution.IsActive   = true;
                        Log.DebugDestroyScope(execution, propagatingExecution);
                        execution.Destroy();
                        execution.Remove();
                    }
                }
            }
            else
            {
                // activity is not scope => nothing to do
                propagatingExecution = execution;
            }

            // take the specified transitions
            if (transitionsToTake == null || transitionsToTake.Count == 0)
            {
                throw new ProcessEngineException(execution + ": No outgoing transitions from " + "activity " + activity);
            }
            if (transitionsToTake.Count == 1)
            {
                propagatingExecution.Transition = (transitionsToTake[0]);
                propagatingExecution.Take();
            }
            else
            {
                propagatingExecution.InActivate();

                IList <OutgoingExecution> outgoingExecutions = new List <OutgoingExecution>();

                for (var i = 0; i < transitionsToTake.Count; i++)
                {
                    var transition = transitionsToTake[i];

                    var scopeExecution = propagatingExecution.IsScope ? propagatingExecution : propagatingExecution.Parent;

                    // reuse concurrent, propagating execution for first transition
                    IActivityExecution concurrentExecution = null;
                    if (i == 0)
                    {
                        concurrentExecution = propagatingExecution;
                    }
                    else
                    {
                        concurrentExecution = scopeExecution.CreateConcurrentExecution();

                        if ((i == 1) && !propagatingExecution.IsConcurrent)
                        {
                            outgoingExecutions.RemoveAt(0);
                            // get a hold of the concurrent execution that replaced the scope propagating execution
                            IActivityExecution replacingExecution = null;
                            foreach (var concurrentChild in scopeExecution.NonEventScopeExecutions)
                            {
                                if (!(concurrentChild == propagatingExecution))
                                {
                                    replacingExecution = concurrentChild;
                                    break;
                                }
                            }

                            outgoingExecutions.Add(new OutgoingExecution(replacingExecution, transitionsToTake[0]));
                        }
                    }

                    outgoingExecutions.Add(new OutgoingExecution(concurrentExecution, transition));
                }

                // start executions in reverse order (order will be reversed again in command context with the effect that they are
                // actually be started in correct order :) )
                outgoingExecutions = outgoingExecutions.Reverse().ToList();

                foreach (var outgoingExecution in outgoingExecutions)
                {
                    outgoingExecution.Take();
                }
            }
        }
Esempio n. 18
0
        public virtual void execute(PvmExecutionImpl execution)
        {
            // calculate the propagating execution
            PvmExecutionImpl propagatingExecution = execution;

            PvmActivity           activity          = execution.getActivity();
            IList <PvmTransition> transitionsToTake = execution.TransitionsToTake;

            execution.TransitionsToTake = null;

            // check whether the current scope needs to be destroyed
            if (execution.Scope && activity.Scope)
            {
                if (!LegacyBehavior.destroySecondNonScope(execution))
                {
                    if (execution.Concurrent)
                    {
                        // legacy behavior
                        LegacyBehavior.destroyConcurrentScope(execution);
                    }
                    else
                    {
                        propagatingExecution = execution.Parent;
                        LOG.debugDestroyScope(execution, propagatingExecution);
                        execution.destroy();
                        propagatingExecution.setActivity(execution.getActivity());
                        propagatingExecution.setTransition(execution.getTransition());
                        propagatingExecution.Active = true;
                        execution.remove();
                    }
                }
            }
            else
            {
                // activity is not scope => nothing to do
                propagatingExecution = execution;
            }

            // take the specified transitions
            if (transitionsToTake.Count == 0)
            {
                throw new ProcessEngineException(execution.ToString() + ": No outgoing transitions from " + "activity " + activity);
            }
            else if (transitionsToTake.Count == 1)
            {
                propagatingExecution.setTransition(transitionsToTake[0]);
                propagatingExecution.take();
            }
            else
            {
                propagatingExecution.inactivate();

                IList <OutgoingExecution> outgoingExecutions = new List <OutgoingExecution>();

                for (int i = 0; i < transitionsToTake.Count; i++)
                {
                    PvmTransition transition = transitionsToTake[i];

                    PvmExecutionImpl scopeExecution = propagatingExecution.Scope ? propagatingExecution : propagatingExecution.Parent;

                    // reuse concurrent, propagating execution for first transition
                    PvmExecutionImpl concurrentExecution = null;
                    if (i == 0)
                    {
                        concurrentExecution = propagatingExecution;
                    }
                    else
                    {
                        concurrentExecution = scopeExecution.createConcurrentExecution();

                        if (i == 1 && !propagatingExecution.Concurrent)
                        {
                            outgoingExecutions.RemoveAt(0);
                            // get a hold of the concurrent execution that replaced the scope propagating execution
                            PvmExecutionImpl replacingExecution = null;
                            foreach (PvmExecutionImpl concurrentChild in scopeExecution.NonEventScopeExecutions)
                            {
                                if (!(concurrentChild == propagatingExecution))
                                {
                                    replacingExecution = concurrentChild;
                                    break;
                                }
                            }

                            outgoingExecutions.Add(new OutgoingExecution(replacingExecution, transitionsToTake[0]));
                        }
                    }

                    outgoingExecutions.Add(new OutgoingExecution(concurrentExecution, transition));
                }

                // start executions in reverse order (order will be reversed again in command context with the effect that they are
                // actually be started in correct order :) )
                outgoingExecutions.Reverse();

                foreach (OutgoingExecution outgoingExecution in outgoingExecutions)
                {
                    outgoingExecution.take();
                }
            }
        }
Esempio n. 19
0
        public virtual IActivityInstance Execute(CommandContext commandContext)
        {
            EnsureUtil.EnsureNotNull("processInstanceId", ProcessInstanceId);
            IList <ExecutionEntity> executionList = LoadProcessInstance(ProcessInstanceId, commandContext);

            if (executionList.Count == 0)
            {
                return(null);
            }

            CheckGetActivityInstance(ProcessInstanceId, commandContext);

            IList <ExecutionEntity> nonEventScopeExecutions = FilterNonEventScopeExecutions(executionList);
            IList <ExecutionEntity> leaves = FilterLeaves(nonEventScopeExecutions);//.OrderBy(m => m.Id).ToList();

            // Leaves must be ordered in a predictable way (e.g. by ID)
            // in order to return a stable execution tree with every repeated invocation of this command.
            // For legacy process instances, there may miss scope executions for activities that are now a scope.
            // In this situation, there may be multiple scope candidates for the same instance id; which one
            // can depend on the order the leaves are iterated.
            OrderById(leaves);

            ExecutionEntity processInstance = FilterProcessInstance(executionList);

            if (processInstance.IsEnded)
            {
                return(null);
            }

            //create act instance for process instance

            ActivityInstanceImpl processActInst = CreateActivityInstance(processInstance, processInstance.ProcessDefinition, ProcessInstanceId, null);
            IDictionary <string, ActivityInstanceImpl> activityInstances = new Dictionary <string, ActivityInstanceImpl>();

            activityInstances[ProcessInstanceId] = processActInst;

            IDictionary <string, TransitionInstanceImpl> transitionInstances = new Dictionary <string, TransitionInstanceImpl>();

            foreach (ExecutionEntity leaf in leaves)
            {
                // skip leafs without activity, e.g. if only the process instance exists after cancellation
                // it will not have an activity set
                if (leaf.Activity == null)
                {
                    continue;
                }

                IDictionary <ScopeImpl, PvmExecutionImpl> activityExecutionMapping = leaf.CreateActivityExecutionMapping();
                IDictionary <ScopeImpl, PvmExecutionImpl> scopeInstancesToCreate   = new Dictionary <ScopeImpl, PvmExecutionImpl>(activityExecutionMapping);

                // create an activity/transition instance for each leaf that executes a non-scope activity
                // and does not throw compensation
                if (leaf.ActivityInstanceId != null)
                {
                    if (!CompensationBehavior.IsCompensationThrowing(leaf) || LegacyBehavior.IsCompensationThrowing(leaf, activityExecutionMapping))
                    {
                        string parentActivityInstanceId = null;

                        parentActivityInstanceId = activityExecutionMapping[leaf.Activity.FlowScope].ParentActivityInstanceId;

                        ActivityInstanceImpl leafInstance = CreateActivityInstance(leaf, (ScopeImpl)leaf.Activity, leaf.ActivityInstanceId, parentActivityInstanceId);
                        activityInstances[leafInstance.Id] = leafInstance;

                        scopeInstancesToCreate.Remove((ScopeImpl)leaf.Activity);
                    }
                }
                else
                {
                    TransitionInstanceImpl transitionInstance = CreateTransitionInstance(leaf);
                    transitionInstances[transitionInstance.Id] = transitionInstance;

                    scopeInstancesToCreate.Remove((ScopeImpl)leaf.Activity);
                }

                LegacyBehavior.RemoveLegacyNonScopesFromMapping(scopeInstancesToCreate);
                scopeInstancesToCreate.Remove(leaf.ProcessDefinition);

                // create an activity instance for each scope (including compensation throwing executions)
                foreach (KeyValuePair <ScopeImpl, PvmExecutionImpl> scopeExecutionEntry in scopeInstancesToCreate)
                {
                    ScopeImpl        scope          = scopeExecutionEntry.Key;
                    PvmExecutionImpl scopeExecution = scopeExecutionEntry.Value;

                    string activityInstanceId       = null;
                    string parentActivityInstanceId = null;

                    activityInstanceId       = scopeExecution.ParentActivityInstanceId;
                    parentActivityInstanceId = activityExecutionMapping[scope.FlowScope].ParentActivityInstanceId;

                    if (activityInstances.ContainsKey(activityInstanceId))
                    {
                        continue;
                    }
                    else
                    {
                        // regardless of the tree structure (compacted or not), the scope's activity instance id
                        // is the activity instance id of the parent execution and the parent activity instance id
                        // of that is the actual parent activity instance id
                        ActivityInstanceImpl scopeInstance = CreateActivityInstance(scopeExecution, scope, activityInstanceId, parentActivityInstanceId);
                        activityInstances[activityInstanceId] = scopeInstance;
                    }
                }
            }

            LegacyBehavior.RepairParentRelationships(activityInstances.Values, ProcessInstanceId);
            PopulateChildInstances(activityInstances, transitionInstances);

            return(processActInst);
        }