Exemple #1
0
        public Task Execute(CMMNExecutionContext executionContext, BaseCaseEltInstance instance, Type type, CancellationToken token)
        {
            var genericType = typeof(IProcessor <>).MakeGenericType(type);
            var processor   = _serviceProvider.GetService(genericType);

            return((Task)genericType.GetMethod("Execute").Invoke(processor, new object[] { executionContext, instance, token }));
        }
        protected override async Task <bool> ProtectedProcess(CMMNExecutionContext executionContext, HumanTaskElementInstance elt, CancellationToken cancellationToken)
        {
            var handler = _humanTaskHandlers.FirstOrDefault(_ => _.Implementation == elt.Implemention);

            if (handler != null)
            {
                if (!executionContext.Instance.WorkerTaskExists(elt.Id))
                {
                    var workerTaskId = await handler.Create(executionContext, elt, cancellationToken);

                    executionContext.Instance.TryAddWorkerTask(elt.Id, workerTaskId);
                }
            }

            var completeSubscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Complete, cancellationToken);

            if (completeSubscription.IsCaptured)
            {
                var sub = await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.Complete, cancellationToken);

                executionContext.Instance.MakeTransition(elt, CMMNTransitions.Complete, incomingTokens: MergeParameters(executionContext, completeSubscription.Parameters));
                return(true);
            }

            return(false);
        }
Exemple #3
0
        protected override async Task Handle(CMMNExecutionContext executionContext, T elt, CancellationToken cancellationToken)
        {
            var entryCriteria = executionContext.Instance.IsEntryCriteriaSatisfied(elt);

            if (!entryCriteria.IsSatisfied)
            {
                return;
            }

            bool firstInstance = false;

            if (elt.LatestTransition == null)
            {
                executionContext.Instance.MakeTransition(elt, CMMNTransitions.Create);
                firstInstance = true;
            }

            var newExecutionContext = executionContext.NewExecutionContext(entryCriteria.Data);

            if (newExecutionContext.Instance.IsRepetitionRuleSatisfied(elt) && firstInstance && !entryCriteria.IsEmpty)
            {
                var result = newExecutionContext.Instance.TryCreateInstance(elt) as T;
                await Handle(newExecutionContext, result, cancellationToken);
            }

            var mustCreate = await Process(executionContext, elt, cancellationToken);

            if (mustCreate && entryCriteria.IsEmpty && newExecutionContext.Instance.IsRepetitionRuleSatisfied(elt))
            {
                var result = newExecutionContext.Instance.TryCreateInstance(elt) as T;
                await Handle(newExecutionContext, result, cancellationToken);
            }
        }
        public Task <ExecutionResult> Execute(CMMNExecutionContext executionContext, CaseEltInstance instance, CancellationToken token)
        {
            var genericType = typeof(IEnumerable <ICaseEltInstanceProcessor>);
            var processors  = (IEnumerable <ICaseEltInstanceProcessor>)_serviceProvider.GetService(genericType);
            var processor   = processors.First(_ => _.Type == instance.Type);

            return(processor.Execute(executionContext, instance, token));
        }
        public Task Execute(CMMNExecutionContext executionContext, T elt, CancellationToken cancellationToken)
        {
            if (elt.LatestTransition == null)
            {
                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Create);
            }

            return(Handle(executionContext, elt, cancellationToken));
        }
Exemple #6
0
        private async Task Init(CMMNExecutionContext executionContext, TimerEventListener elt, CancellationToken token)
        {
            var currentDateTime   = DateTime.UtcNow;
            var elapsedTime       = ISO8601Parser.ParseTime(elt.TimerExpression.Body);
            var repeatingInterval = ISO8601Parser.ParseRepeatingTimeInterval(elt.TimerExpression.Body);

            if (repeatingInterval != null)
            {
                if (currentDateTime >= repeatingInterval.Interval.EndDateTime)
                {
                    return;
                }

                var startDate = currentDateTime;
                if (startDate < repeatingInterval.Interval.StartDateTime)
                {
                    startDate = repeatingInterval.Interval.StartDateTime;
                }

                var diff        = repeatingInterval.Interval.EndDateTime.Subtract(startDate);
                var newTimespan = new TimeSpan(diff.Ticks / (repeatingInterval.RecurringTimeInterval));
                for (var i = 0; i < repeatingInterval.RecurringTimeInterval; i++)
                {
                    currentDateTime = currentDateTime.Add(newTimespan);
                    var newInstance = elt;
                    if (i > 0)
                    {
                        newInstance = executionContext.Instance.TryCreateInstance(elt) as TimerEventListener;
                    }

                    await TrySubscribe(executionContext, newInstance, CMMNConstants.ExternalTransitionNames.Occur, token);

                    await _messageBroker.ScheduleExternalEvt(CMMNConstants.ExternalTransitionNames.Occur,
                                                             executionContext.Instance.AggregateId,
                                                             newInstance.Id,
                                                             currentDateTime,
                                                             token);
                }
            }

            if (elapsedTime != null)
            {
                if (currentDateTime >= elapsedTime)
                {
                    return;
                }

                await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Occur, token);

                await _messageBroker.ScheduleExternalEvt(CMMNConstants.ExternalTransitionNames.Occur,
                                                         executionContext.Instance.AggregateId,
                                                         elt.Id,
                                                         elapsedTime.Value,
                                                         token);
            }
        }
Exemple #7
0
        protected override async Task ProtectedProcess(CMMNExecutionContext executionContext, HumanTaskElementInstance elt, CancellationToken cancellationToken)
        {
            executionContext.CasePlanInstance.TryAddWorkerTask(elt.Id);
            var completeSubscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Complete, cancellationToken);

            if (completeSubscription.IsCaptured)
            {
                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Complete);
            }
        }
        public CMMNExecutionContext NewExecutionContext(Dictionary <string, string> incomingTokens)
        {
            var result = new CMMNExecutionContext
            {
                IncomingTokens = incomingTokens,
                Instance       = Instance
            };

            return(result);
        }
        protected override async Task Handle(CMMNExecutionContext executionContext, T elt, CancellationToken cancellationToken)
        {
            var isNewElt = elt.LatestTransition == CMMNTransitions.Create;

            if (!executionContext.CasePlanInstance.IsEntryCriteriaSatisfied(elt))
            {
                return;
            }

            await Process(executionContext, elt, cancellationToken);

            if (isNewElt && executionContext.CasePlanInstance.IsRepetitionRuleSatisfied(elt))
            {
                executionContext.CasePlanInstance.TryCreateInstance(elt);
            }
        }
Exemple #10
0
        protected override async Task Process(CMMNExecutionContext executionContext, T elt, CancellationToken token)
        {
            var terminateSubscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Terminate, token);

            if (elt.State == MilestoneEventStates.Available)
            {
                await ProtectedProcess(executionContext, elt, token);

                if (terminateSubscription.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Terminate);
                }

                return;
            }
        }
Exemple #11
0
        private async Task ExecuteBranch(CMMNExecutionContext executionContext, ExecutionBranch branch, CancellationToken cancellationToken)
        {
            var taskLst = new List <Task>();

            foreach (var node in branch.Nodes)
            {
                taskLst.Add(HandleCasePlan(executionContext, node, cancellationToken));
            }

            await Task.WhenAll(taskLst);

            if (branch.NextBranch != null)
            {
                await ExecuteBranch(executionContext, branch.NextBranch, cancellationToken);
            }
        }
Exemple #12
0
        protected override async Task ProtectedProcess(CMMNExecutionContext executionContext, StageElementInstance stageElt, CancellationToken cancellationToken)
        {
            var executionBranch = ExecutionBranch.Build(stageElt.Children);

            await ExecuteBranch(executionContext, executionBranch, cancellationToken);

            if (stageElt.Children.All(_ => IsElementCompleted(_)))
            {
                executionContext.CasePlanInstance.MakeTransition(stageElt, CMMNTransitions.Complete);
                return;
            }

            if (executionContext.CasePlanInstance.IsExitCriteriaSatisfied(stageElt))
            {
                executionContext.CasePlanInstance.MakeTransition(stageElt, CMMNTransitions.Terminate);
            }
        }
        public async Task Execute(CasePlanInstanceAggregate casePlanInstance, CancellationToken cancellationToken)
        {
            var terminateSub = await _subscriberRepository.TrySubscribe(casePlanInstance.AggregateId, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken);

            if (casePlanInstance.State == null)
            {
                casePlanInstance.MakeTransition(CMMNTransitions.Create);
            }

            if (casePlanInstance.State == CaseStates.Active)
            {
                var executionContext = new CMMNExecutionContext {
                    Instance = casePlanInstance
                };
                foreach (var fileItem in casePlanInstance.FileItems)
                {
                    await _processorFactory.Execute(executionContext, fileItem, cancellationToken);
                }

                await _processorFactory.Execute(executionContext, casePlanInstance.StageContent, cancellationToken);

                if (casePlanInstance.StageContent.TakeStageState == TaskStageStates.Completed)
                {
                    casePlanInstance.MakeTransition(CMMNTransitions.Complete, false);
                    return;
                }

                if (casePlanInstance.StageContent.TakeStageState == TaskStageStates.Terminated)
                {
                    casePlanInstance.MakeTransition(CMMNTransitions.Terminate, false);
                    return;
                }

                if (terminateSub.IsCaptured)
                {
                    await _subscriberRepository.TryReset(casePlanInstance.AggregateId, null, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken);

                    casePlanInstance.MakeTransition(CMMNTransitions.Terminate);
                }
            }

            await Publish <CaseInstanceWorkerTaskAddedEvent>(casePlanInstance, cancellationToken);
            await Publish <CaseInstanceWorkerTaskRemovedEvent>(casePlanInstance, cancellationToken);
        }
        protected override async Task Process(CMMNExecutionContext executionContext, T elt, CancellationToken cancellationToken)
        {
            var terminate = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken);

            var manualStart = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.ManualStart, cancellationToken);

            if (elt.State == null)
            {
                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Create);
            }

            if (elt.State == TaskStageStates.Available)
            {
                if (elt.ManualActivationRule != null && elt.IsManualActivationRuleSatisfied(executionContext.CasePlanInstance.ExecutionContext))
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Enable);
                    return;
                }

                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Start);
            }

            if (elt.State == TaskStageStates.Enabled)
            {
                if (!manualStart.IsCaptured)
                {
                    return;
                }

                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.ManualStart);
            }

            if (elt.State == TaskStageStates.Active)
            {
                await ProtectedProcess(executionContext, elt, cancellationToken);

                if (terminate.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Terminate);
                    return;
                }
            }
        }
Exemple #15
0
        protected override async Task <bool> Process(CMMNExecutionContext executionContext, T elt, CancellationToken token)
        {
            var terminateSubscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Terminate, token);

            if (elt.State == MilestoneEventStates.Available)
            {
                await ProtectedProcess(executionContext, elt, token);

                if (terminateSubscription.IsCaptured)
                {
                    var sub = await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.Terminate, token);

                    executionContext.Instance.MakeTransition(elt, CMMNTransitions.Terminate, incomingTokens: MergeParameters(executionContext, sub.Parameters));
                }

                return(true);
            }

            return(false);
        }
        protected override async Task ProtectedProcess(CMMNExecutionContext executionContext, TimerEventListener elt, CancellationToken cancellationToken)
        {
            if (elt.NbOccurrence == 0)
            {
                var subExists = (await SubscriberRepository.Get(executionContext.CasePlanInstance.AggregateId, elt.Id, CMMNConstants.ExternalTransitionNames.Occur, cancellationToken) != null);
                if (!subExists)
                {
                    await Init(executionContext, elt, cancellationToken);

                    return;
                }
            }

            var subscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Occur, cancellationToken);

            if (subscription.IsCaptured)
            {
                executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Occur);
            }
        }
Exemple #17
0
        private void ConsumeTransitionEvts(CMMNExecutionContext executionContext, CaseFileItemInstance node)
        {
            var domainEvts = executionContext.Instance.DomainEvents.Where((evt) =>
            {
                var r = evt as CaseElementTransitionRaisedEvent;
                if (r == null)
                {
                    return(false);
                }

                return(r.ElementId == node.Id);
            }).Cast <CaseElementTransitionRaisedEvent>()
                             .Select(_ => new IncomingTransition(_.Transition, _.IncomingTokens)).ToList();
            var nextNodes = executionContext.Instance.GetNextCasePlanItems(node);

            foreach (var nextNode in nextNodes)
            {
                executionContext.Instance.ConsumeTransitionEvts(nextNode, node.Id, domainEvts);
            }
        }
        protected Dictionary <string, string> MergeParameters(CMMNExecutionContext executionContext, Dictionary <string, string> parameters)
        {
            var result = new Dictionary <string, string>();

            if (parameters != null)
            {
                foreach (var kvp in parameters)
                {
                    result.Add(kvp.Key, kvp.Value);
                }
            }

            if (executionContext.IncomingTokens != null)
            {
                foreach (var kvp in executionContext.IncomingTokens)
                {
                    result.Add(kvp.Key, kvp.Value);
                }
            }

            return(result);
        }
        protected override async Task ProtectedProcess(CMMNExecutionContext executionContext, CaseEltInstance elt, CancellationToken cancellationToken)
        {
            if (elt.NbOccurrence == 0)
            {
                var subExists = (await SubscriberRepository.Get(executionContext.Instance.AggregateId, elt.Id, CMMNConstants.ExternalTransitionNames.Occur, cancellationToken) != null);
                if (!subExists)
                {
                    await Init(executionContext, elt, cancellationToken);

                    return;
                }
            }

            var subscription = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Occur, cancellationToken);

            if (subscription.IsCaptured)
            {
                var sub = await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.Occur, cancellationToken);

                executionContext.Instance.MakeTransition(elt, CMMNTransitions.Occur, incomingTokens: MergeParameters(executionContext, sub.Parameters));
            }
        }
        protected override async Task <bool> ProtectedProcess(CMMNExecutionContext executionContext, CaseEltInstance stageElt, CancellationToken cancellationToken)
        {
            var rootNodes = stageElt.Children.Where(_ => _.IsLeaf()).ToList();

            foreach (var rootNode in rootNodes)
            {
                await ExecuteNode(executionContext, rootNode, cancellationToken);
            }

            if (stageElt.Children.All(_ => IsElementCompleted(_)))
            {
                executionContext.Instance.MakeTransition(stageElt, CMMNTransitions.Complete);
                return(true);
            }

            if (executionContext.Instance.IsExitCriteriaSatisfied(stageElt).IsSatisfied)
            {
                executionContext.Instance.MakeTransition(stageElt, CMMNTransitions.Terminate);
                return(true);
            }

            return(false);
        }
        private async Task ExecuteNode(CMMNExecutionContext executionContext, CaseEltInstance node, CancellationToken token)
        {
            await _processorFactory.Execute(executionContext, node, token);

            var domainEvts = executionContext.Instance.DomainEvents.Where((evt) =>
            {
                var r = evt as CaseElementTransitionRaisedEvent;
                if (r == null)
                {
                    return(false);
                }

                return(r.ElementId == node.Id);
            }).Cast <CaseElementTransitionRaisedEvent>()
                             .Select(_ => new IncomingTransition(_.Transition, _.IncomingTokens)).ToList();
            var nextNodes = executionContext.Instance.GetNextCasePlanItems(node);

            foreach (var nextNode in nextNodes)
            {
                executionContext.Instance.ConsumeTransitionEvts(nextNode, node.Id, domainEvts);
                await ExecuteNode(executionContext, nextNode, token);
            }
        }
        public async Task Execute(CasePlanInstanceAggregate casePlanInstance, CancellationToken cancellationToken)
        {
            var terminateSub = await _subscriberRepository.TrySubscribe(casePlanInstance.AggregateId, CMMNConstants.ExternalTransitionNames.Terminate, cancellationToken);

            if (casePlanInstance.State == null)
            {
                casePlanInstance.MakeTransition(CMMNTransitions.Create);
            }

            if (casePlanInstance.State == CaseStates.Active)
            {
                var executionContext = new CMMNExecutionContext {
                    CasePlanInstance = casePlanInstance
                };
                foreach (var child in casePlanInstance.Children)
                {
                    await _processorFactory.Execute(executionContext, child, child.GetType(), cancellationToken);
                }

                if (casePlanInstance.StageContent.State == TaskStageStates.Completed)
                {
                    casePlanInstance.MakeTransition(CMMNTransitions.Complete, false);
                    return;
                }

                if (casePlanInstance.StageContent.State == TaskStageStates.Terminated)
                {
                    casePlanInstance.MakeTransition(CMMNTransitions.Terminate, false);
                    return;
                }

                if (terminateSub.IsCaptured)
                {
                    casePlanInstance.MakeTransition(CMMNTransitions.Terminate);
                }
            }
        }
        protected override async Task Handle(CMMNExecutionContext executionContext, CaseFileItemInstance elt, CancellationToken cancellationToken)
        {
            var update = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Update, cancellationToken);

            var replace = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Replace, cancellationToken);

            var removeChild = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.RemoveChild, cancellationToken);

            var addChild = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.AddChild, cancellationToken);

            var addReference = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.AddReference, cancellationToken);

            var removeReference = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.RemoveReference, cancellationToken);

            var delete = await TrySubscribe(executionContext, elt, CMMNConstants.ExternalTransitionNames.Delete, cancellationToken);

            if (elt.State == CaseFileItemStates.Available)
            {
                var caseFileItemStore = _caseFileItemStores.FirstOrDefault(_ => _.CaseFileItemType == elt.DefinitionType);
                if (caseFileItemStore == null)
                {
                    // TODO : THROW EXCEPTION.
                }

                await caseFileItemStore.TryAddCaseFileItem(elt, executionContext.CasePlanInstance, cancellationToken);

                if (update.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Update);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.Update, cancellationToken);

                    return;
                }

                if (replace.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Replace);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.Replace, cancellationToken);

                    return;
                }

                if (removeChild.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.RemoveChild);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.RemoveChild, cancellationToken);

                    return;
                }

                if (addChild.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.AddChild);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.AddChild, cancellationToken);

                    return;
                }

                if (addReference.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.AddReference);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.AddReference, cancellationToken);

                    return;
                }

                if (removeReference.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.RemoveReference);
                    await TryReset(executionContext, elt, CMMNConstants.ExternalTransitionNames.RemoveReference, cancellationToken);

                    return;
                }

                if (delete.IsCaptured)
                {
                    executionContext.CasePlanInstance.MakeTransition(elt, CMMNTransitions.Delete);
                }
            }
        }
Exemple #24
0
 public Task Execute <T>(CMMNExecutionContext executionContext, T instance, CancellationToken token) where T : BaseCaseEltInstance
 {
     return(Execute(executionContext, instance, typeof(T), token));
 }
 protected abstract Task Process(CMMNExecutionContext executionContext, T elt, CancellationToken cancellationToken);
 protected override Task <bool> ProtectedProcess(CMMNExecutionContext executionContext, EmptyTaskElementInstance elt, CancellationToken cancellationToken)
 {
     executionContext.Instance.MakeTransition(elt, CMMNTransitions.Complete);
     return(Task.FromResult(true));
 }
        public async Task <ExecutionResult> Execute(CMMNExecutionContext executionContext, TElt elt, CancellationToken cancellationToken)
        {
            await Handle(executionContext, elt, cancellationToken);

            return(ExecutionResult.Next());
        }
 protected override Task ProtectedProcess(CMMNExecutionContext executionContext, MilestoneElementInstance elt, CancellationToken cancellationToken)
 {
     executionContext.Instance.MakeTransition(elt, CMMNTransitions.Occur);
     return(Task.CompletedTask);
 }
 protected abstract Task Handle(CMMNExecutionContext executionContext, TElt elt, CancellationToken cancellationToken);
 protected Task <Subscription> TryReset(CMMNExecutionContext executionContext, TElt casePlanElementInstance, string evtName, CancellationToken cancellationToken)
 {
     return(SubscriberRepository.TryReset(executionContext.Instance.AggregateId, casePlanElementInstance.Id, evtName, cancellationToken));
 }