Exemplo n.º 1
0
        public void TestDelegateDecision()
        {
            using (var fake = new AutoFake())
            {
                var      provider             = fake.Resolve <IServiceLocator>();
                var      transitionRepository = new InMemoryRepository <Transition>(new List <Transition>());
                FakeNode node = new FakeNode();
                transitionRepository.Add(new Transition()
                {
                    From = node, Name = "pass"
                });
                A.CallTo(() => provider.GetInstance <IRepository <Transition> >()).Returns(transitionRepository);
                ServiceLocator.SetLocatorProvider(() => provider);

                DelegationHelper delegationHelper = new DelegationHelper();

                DelegationDef delegationDef = new DelegationDef();
                delegationDef.ClassName = "DecisionTest";

                ExecutionContext context = new ExecutionContext(null, null, null);
                context.Node = node;

                Assert.AreEqual("pass", delegationHelper.DelegateDecision(delegationDef, context).Name);
            }
        }
Exemplo n.º 2
0
        private static void ProcessFork(Fork fork, ExecutionContext executionContext)
        {
            Flow          flow = executionContext.Flow;
            var           delegationRepository = ServiceLocator.Current.GetInstance <IRepository <DelegationDef> >();
            DelegationDef delegation           = delegationRepository.Get(fork.ForkDelegationId);

            if (delegation != null)
            {
                var delegationHelper = ServiceLocator.Current.GetInstance <IDelegationHelper>();
                delegationHelper.DelegateFork(delegation, executionContext);
            }
            else
            {
                var transitionRepository = ServiceLocator.Current.GetInstance <IRepository <Transition> >();
                var leavingTransitions   = transitionRepository.With(w => w.To).Where(q => q.From.Id == fork.Id);

                foreach (var transition in leavingTransitions)
                {
                    executionContext.ForkFlow(transition);
                }
            }

            Flow parentFlow = executionContext.Flow;

            executionContext.Flow = parentFlow;
            IList <ForkedFlow> forkedFlows = executionContext.ForkedFlows;

            foreach (var forkedFlow in forkedFlows)
            {
                executionContext.RunActionsForEvent(EventType.FORK, fork.Id);

                executionContext.Flow = forkedFlow.Flow;
                ProcessTransition(forkedFlow.Transition, executionContext);
            }
        }
Exemplo n.º 3
0
        public Transition DelegateDecision(DelegationDef delegation, ExecutionContext executionContext)
        {
            IRepository <Transition> transitionRepository = ServiceLocator.Current.GetInstance <IRepository <Transition> >();

            //return transitionRepository.With(s => s.To).SingleOrDefault(s => s.To is EndState);
            return(transitionRepository.With(s => s.To).SingleOrDefault(s => s.To.Name == "approved holiday fork"));
        }
Exemplo n.º 4
0
        private IDictionary <string, object> ParseConfiguration(DelegationDef delegation)
        {
            IDictionary <string, object> parameters = new Dictionary <string, object>();

            try
            {
                string configuration = delegation.Configuration;
                if (!string.IsNullOrEmpty(configuration))
                {
                    XElement xElement = XElement.Parse(configuration);

                    var parameterXmlElements = xElement.Elements("parameter");
                    foreach (XElement element in parameterXmlElements)
                    {
                        string name = element.Attribute("name").Value;
                        if (string.IsNullOrEmpty(name))
                        {
                            throw new SystemException("invalid delegation-configuration : " + configuration);
                        }

                        parameters.Add(name, element.Value);
                    }
                }
            }
            catch (Exception t)
            {
                Log.Error("can't parse configuration : ", t);
                throw new SystemException("can't parse configuration : " + t.Message);
            }

            return(parameters);
        }
Exemplo n.º 5
0
        public void TestDelegateFork()
        {
            DelegationHelper delegationHelper = new DelegationHelper();

            DelegationDef delegationDef = new DelegationDef();

            delegationDef.ClassName = "ForkTest";

            ExecutionContext context = new ExecutionContext(null, null, null);

            delegationHelper.DelegateFork(delegationDef, context);
        }
Exemplo n.º 6
0
        public void TestDelegateSerializer()
        {
            DelegationHelper delegationHelper = new DelegationHelper();

            DelegationDef delegationDef = new DelegationDef();

            delegationDef.ClassName = "SerializerTest";

            var        serializer = delegationHelper.DelegateSerializer(delegationDef);
            Evaluation result     = (Evaluation)serializer.Deserialize("approve");

            Assert.AreEqual(Evaluation.APPROVE, result);
        }
Exemplo n.º 7
0
        private static void ProcessActivityState(ActivityState destination, ExecutionContext executionContext)
        {
            Flow flow = executionContext.Flow;

            executionContext.RunActionsForEvent(EventType.BEFORE_ACTIVITYSTATE_ASSIGNMENT, destination.Id);

            IActor        actor = null;
            string        role  = destination.ActorRoleName;
            var           delegationRepository = ServiceLocator.Current.GetInstance <IRepository <DelegationDef> >();
            DelegationDef assignmentDelegation = delegationRepository.Get(destination.AssignmentDelegationId);
            var           delegationHelper     = ServiceLocator.Current.GetInstance <IDelegationHelper>();

            if (assignmentDelegation != null)
            {
                // delegate the assignment of the activity-state
                actor = delegationHelper.DelegateAssignment(assignmentDelegation, executionContext);
                if (actor == null)
                {
                    throw new SystemException("invalid process definition : assigner of activity-state '" + destination.Name + "' returned null instead of a valid actorId");
                }
                //log.Debug("setting actor of flow " + flow + " to " + actorId);
            }
            else
            {
                // get the assigned actor from the specified attribute instance
                if (!string.IsNullOrEmpty(role))
                {
                    actor = (IActor)executionContext.GetAttribute(role);
                    if (actor == null)
                    {
                        throw new SystemException("invalid process definition : activity-state must be assigned to role '" + role + "' but that attribute instance is null");
                    }
                }
                else
                {
                    throw new SystemException("invalid process definition : activity-state '" + destination.Name + "' does not have an assigner or a role");
                }
            }

            flow.ActorId = actor.Id;

            if ((actor != null) && (assignmentDelegation != null))
            {
                executionContext.StoreRole(actor, destination);
            }

            executionContext.AssignedFlows.Add(flow);

            executionContext.RunActionsForEvent(EventType.AFTER_ACTIVITYSTATE_ASSIGNMENT, destination.Id);
        }
Exemplo n.º 8
0
        public void TestDelegateJoin()
        {
            DelegationHelper delegationHelper = new DelegationHelper();

            DelegationDef delegationDef = new DelegationDef();

            delegationDef.ClassName = "JoinTest";

            ExecutionContext context = new ExecutionContext(null, null, null);

            bool reactiveFlow = delegationHelper.DelegateJoin(delegationDef, context);

            Assert.IsFalse(reactiveFlow);
        }
Exemplo n.º 9
0
 public void DelegateFork(DelegationDef delegation, ExecutionContext executionContext)
 {
     try
     {
         foreach (var forkHandler in ForkHandlers)
         {
             if ((string)forkHandler.Metadata.ClassName == delegation.ClassName)
             {
                 executionContext.Configuration = (ParseConfiguration(delegation));
                 forkHandler.Value.Fork(executionContext);
             }
         }
     }
     catch (Exception t)
     {
         HandleException(delegation, executionContext, t);
     }
 }
Exemplo n.º 10
0
        private IAuthorizationHandler GetAuthorizationHandler(ProcessDefinition processDefinition)
        {
            IAuthorizationHandler authorizationHandler = null;
            var           delegationRepository         = ServiceLocator.Current.GetInstance <IRepository <DelegationDef> >();
            DelegationDef delegation = delegationRepository.Get(processDefinition.AuthorizationDelegationId);

            if (delegation != null)
            {
                foreach (var handler in AuthorizationHandlers)
                {
                    if (handler.Metadata.ClassName == delegation.ClassName)
                    {
                        authorizationHandler = handler.Value;
                    }
                }
            }
            return(authorizationHandler);
        }
Exemplo n.º 11
0
        public void HelloWorld2()
        {
            XmlDocument xmlDocument = new XmlDocument();

            xmlDocument.Load("Definitions\\HelloWorld2.xml");

            using (var unitWork = UnitOfWork.Start())
            {
                ProcessDefinitionCreationContext creationContext = new ProcessDefinitionCreationContext();
                ProcessDefinition processDefinition = creationContext.CreateProcessDefinition(xmlDocument);
                creationContext.ResolveReferences();

                Node          start = null, end = null;
                ActivityState activityState = null;
                foreach (Node node in processDefinition.Nodes)
                {
                    if (node.Name == "start")
                    {
                        start = node;
                    }
                    else if (node.Name == "first activity state")
                    {
                        activityState = node as ActivityState;
                    }
                    else if (node.Name == "end")
                    {
                        end = node;
                    }
                }
                Assert.IsNotNull(start);
                Assert.IsNotNull(activityState);
                Assert.IsNotNull(activityState.AssignmentDelegation);

                DelegationDef delegationDef = activityState.AssignmentDelegation;
                Assert.AreEqual("ActorAssignment", delegationDef.ClassName);

                Assert.AreEqual("<cfg><parametername=\"expression\">processInitiator</parameter></cfg>", Regex.Replace(delegationDef.Configuration, @"\s|\t|\n|\r", ""));

                var processBlockRepository = new EFRepository <ProcessBlock>();
                processBlockRepository.Add(processDefinition);
                unitWork.Flush();
            }
        }
Exemplo n.º 12
0
        public Transition DelegateDecision(DelegationDef delegation, ExecutionContext executionContext)
        {
            Transition selectedTransition = null;

            try
            {
                string           transitionName = null;
                IDecisionHandler decision       = null;
                foreach (var decisionHandler in DecisionHandlers)
                {
                    if ((string)decisionHandler.Metadata.ClassName == delegation.ClassName)
                    {
                        decision = decisionHandler.Value;
                        executionContext.Configuration = (ParseConfiguration(delegation));
                        transitionName = decision.Decide(executionContext);
                    }
                }


                if (string.IsNullOrEmpty(transitionName))
                {
                    throw new SystemException("Decision-delegate for decision '" + executionContext.Node + "' returned null instead of a transition-name : " + decision.GetType().FullName);
                }

                try
                {
                    var transitionRepository = ServiceLocator.Current.GetInstance <IRepository <Transition> >();

                    selectedTransition = transitionRepository.With(s => s.To)
                                         .Query(new Specification <Transition>(s => s.From.Id == executionContext.Node.Id && s.Name == transitionName)).Single();
                }
                catch (Exception t)
                {
                    throw new SystemException("couldn't find transition '" + transitionName + "' that was selected by the decision-delegate of activity '" + executionContext.Node.Name + "' : " + t.Message);
                }
            }
            catch (Exception t)
            {
                HandleException(delegation, executionContext, t);
            }

            return(selectedTransition);
        }
Exemplo n.º 13
0
        public ISerializer DelegateSerializer(DelegationDef delegation)
        {
            try
            {
                foreach (var serializer in Serializers)
                {
                    if ((string)serializer.Metadata.ClassName == delegation.ClassName)
                    {
                        return(serializer.Value);
                    }
                }
            }
            catch (Exception t)
            {
                //HandleException(delegation, executionContext, t);
            }

            return(null);
        }
Exemplo n.º 14
0
        private static void ProcessJoin(Join join, ExecutionContext executionContext)
        {
            Flow joiningFlow = executionContext.Flow;

            joiningFlow.End     = DateTime.Now;
            joiningFlow.ActorId = null;
            joiningFlow.Node    = join;

            if (joiningFlow.ParentReactivation)
            {
                bool         parentReactivation = false;
                IList <Flow> concurrentFlows    = executionContext.GetOtherActiveConcurrentFlows();
                if (concurrentFlows.Count == 0)
                {
                    parentReactivation = true;
                }
                else
                {
                    var           delegationRepository = ServiceLocator.Current.GetInstance <IRepository <DelegationDef> >();
                    DelegationDef joinDelegation       = delegationRepository.Get(join.JoinDelegationId);
                    if (joinDelegation != null)
                    {
                        var delegationHelper = ServiceLocator.Current.GetInstance <IDelegationHelper>();
                        parentReactivation = delegationHelper.DelegateJoin(joinDelegation, executionContext);
                    }
                }

                if (parentReactivation)
                {
                    foreach (var concurrentFlow in concurrentFlows)
                    {
                        concurrentFlow.ParentReactivation = false;
                    }

                    Flow parentFlow = joiningFlow.Parent;
                    executionContext.Flow = parentFlow;

                    var transitionRepository = ServiceLocator.Current.GetInstance <IRepository <Transition> >();
                    var leavingTransition    = transitionRepository.With(w => w.To).First(f => f.From.Id == join.Id);
                    ProcessTransition(leavingTransition, executionContext);
                }
            }
        }
Exemplo n.º 15
0
 public void DelegateAction(DelegationDef delegation, ExecutionContext executionContext)
 {
     try
     {
         //executionContext.CreateLog(EventType.ACTION);
         //executionContext.AddLogDetail(new DelegateCallImpl(delegation, typeof(IAction)));
         foreach (var actionHandler in ActionHanders)
         {
             if (actionHandler.Metadata.ClassName == delegation.ClassName)
             {
                 executionContext.Configuration = (ParseConfiguration(delegation));
                 actionHandler.Value.Run(executionContext);
             }
         }
     }
     catch (Exception t)
     {
         HandleException(delegation, executionContext, t);
     }
 }
Exemplo n.º 16
0
        public void TestDelegateAction()
        {
            DelegationHelper delegationHelper = new DelegationHelper();

            DelegationDef delegationDef = new DelegationDef();

            delegationDef.ClassName     = "ActionTest";
            delegationDef.Configuration = "<cfg>" +
                                          "<parameter name = \"to\" > previousActor </parameter>" +
                                          "<parameter name = \"subject\" > you requested a holiday </parameter>" +
                                          "<parameter name = \"message\" > you requested a holiday from ${ start date}" +
                                          "to ${ end date}" +
                                          "with comment ${ comment}</parameter>" +
                                          "</cfg> ";
            ExecutionContext context = new ExecutionContext(null, null, null);

            delegationHelper.DelegateAction(delegationDef, context);

            Assert.IsTrue(context.Configuration.ContainsKey("test"));
            Assert.AreEqual("1234", context.Configuration["test"]);
        }
Exemplo n.º 17
0
        public IActor DelegateAssignment(DelegationDef delegation, ExecutionContext executionContext)
        {
            IActor actor = null;

            try
            {
                foreach (var assignmentHandler in AssignmentHandlers)
                {
                    if ((string)assignmentHandler.Metadata.ClassName == delegation.ClassName)
                    {
                        executionContext.Configuration = (ParseConfiguration(delegation));
                        actor = assignmentHandler.Value.SelectActor(executionContext);
                    }
                }
            }
            catch (Exception t)
            {
                HandleException(delegation, executionContext, t);
            }

            return(actor);
        }
Exemplo n.º 18
0
        public bool DelegateJoin(DelegationDef delegation, ExecutionContext executionContext)
        {
            bool reactivateParent = false;

            try
            {
                foreach (var joinHandler in JoinHandlers)
                {
                    if ((string)joinHandler.Metadata.ClassName == delegation.ClassName)
                    {
                        executionContext.Configuration = (ParseConfiguration(delegation));
                        reactivateParent = joinHandler.Value.Join(executionContext);
                    }
                }
            }
            catch (Exception t)
            {
                HandleException(delegation, executionContext, t);
            }

            return(reactivateParent);
        }
Exemplo n.º 19
0
        private void HandleException(DelegationDef delegation, ExecutionContext executionContext, Exception exception)
        {
            Log.Debug("handling delegation exception :", exception);

            string exceptionClassName  = exception.GetType().FullName;
            string delegationClassName = delegation.ClassName;

            ExceptionHandlingType exceptionHandlingType = delegation.ExceptionHandlingType;

            if (exceptionHandlingType != 0)
            {
                if (exceptionHandlingType == ExceptionHandlingType.IGNORE)
                {
                    Log.Debug("ignoring '" + exceptionClassName + "' in delegation '" + delegationClassName + "' : " + exception.Message);
                }
                else if (exceptionHandlingType == ExceptionHandlingType.LOG)
                {
                    Log.Debug("logging '" + exceptionClassName + "' in delegation '" + delegationClassName + "' : " + exception.Message);
                    //executionContext.AddLogDetail(new ExceptionReportImpl(exception));
                }
                else if (exceptionHandlingType == ExceptionHandlingType.ROLLBACK)
                {
                    Log.Debug("rolling back for '" + exceptionClassName + "' in delegation '" + delegationClassName + "' : " + exception.Message);
                    throw new SystemException("rolling back for '" + exceptionClassName + "' in delegation '" + delegationClassName + "' : " + exception.Message);
                }
                else
                {
                    throw new SystemException("unknown exception handler '" + exceptionHandlingType + "' : " + exception.Message);
                }
            }
            else
            {
                Log.Debug("'" + exceptionClassName + "' in delegation '" + delegationClassName + "' : " + exception.Message);
                //executionContext.AddLogDetail(new ExceptionReportImpl(exception));
            }
        }
Exemplo n.º 20
0
 public bool DelegateJoin(DelegationDef delegation, ExecutionContext executionContext)
 {
     return(false);
 }
Exemplo n.º 21
0
 public IActor DelegateAssignment(DelegationDef delegation, ExecutionContext executionContext)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 22
0
 public ISerializer DelegateSerializer(DelegationDef delegation)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 23
0
 public void DelegateAction(DelegationDef delegation, ExecutionContext executionContext)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 24
0
 public void DelegateFork(DelegationDef delegation, ExecutionContext executionContext)
 {
 }