Пример #1
0
		public void Should_include_all_the_state_events()
		{
			_workflow = StateMachineWorkflow.New<MyActorWorkflow, MyActor>(x =>
				{
					x.AccessCurrentState(i => i.CurrentState);

					x.Initially()
						.When(w => w.Start)
						.TransitionTo(y => y.Running)
						.When(y => y.Stop)
						.TransitionTo(y => y.Stopped);

					x.During(y => y.Running)
						.When(y => y.AReceived)
						.Then(i => i.OnA)
						.When(y => y.BReceived)
						.Then(i => i.OnB)
						.When(y => y.Stop)
						.TransitionTo(y => y.Stopped);

					x.During(y => y.Stopped)
						.When(y => y.Start)
						.TransitionTo(y => y.Running)
						.When(y => y.OnExit)
						.Finalize();
				});

			_visitor = new StateMachineWorkflowEventVisitor<MyActorWorkflow, MyActor>();
			_workflow.Accept(_visitor);
		}
Пример #2
0
		public void Throwing_an_exception_from_an_event_handler()
		{
			_workflow = StateMachineWorkflow.New<SubjectWorkflow, Subject>(x =>
				{
					x.AccessCurrentState(y => y.CurrentState);

					x.Initially()
						.When(e => e.Create)
						.Then(i => i.Create)
						.TransitionTo(s => s.Created)
						.InCaseOf()
						.Exception<SubjectException>()
						.Then(i =>
							{
								_instanceHandlerCalled = true;
							})
						.Then(() => _handlerCalled = true)
						.TransitionTo(s => s.Failed);
				});

			_subject = new Subject();

			_instance = _workflow.GetInstance(_subject);

			_instance.RaiseEvent(x => x.Create);
		}
Пример #3
0
        public void Throwing_an_exception_from_an_event_handler()
        {
            _workflow = StateMachineWorkflow.New <SubjectWorkflow, Subject>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);

                x.Initially()
                .When(e => e.Create)
                .Then(i => i.Create)
                .TransitionTo(s => s.Created)
                .InCaseOf()
                .Exception <SubjectException>()
                .Then(i =>
                {
                    _instanceHandlerCalled = true;
                })
                .Then(() => _handlerCalled = true)
                .TransitionTo(s => s.Failed);
            });

            _subject = new Subject();

            _instance = _workflow.GetInstance(_subject);

            _instance.RaiseEvent(x => x.Create);
        }
Пример #4
0
		public void Creating_a_new_workflow()
		{
			_workflow = StateMachineWorkflow.New<TestWorkflow, TestInstance>(x =>
				{
					x.AccessCurrentState(y => y.CurrentState);
				});

			_instance = _workflow.GetInstance(new TestInstance());
		}
Пример #5
0
        public void Creating_a_new_workflow()
        {
            _workflow = StateMachineWorkflow.New <TestWorkflow, TestInstance>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);
            });

            _instance = _workflow.GetInstance(new TestInstance());
        }
Пример #6
0
        static IDictionary <Event, StateMachineWorkflowEventBinder <TActor> > BuildStateEventBinders(
            StateMachineWorkflow <TWorkflow, TActor> workflow)
        {
            var visitor = new StateMachineWorkflowEventVisitor <TWorkflow, TActor>();

            workflow.Accept(visitor);

            return(visitor.GetBinders().ToDictionary(x => x.Event));
        }
        protected virtual bool Visit(StateMachineWorkflow <TWorkflow, TActor> workflow)
        {
            // this is the first thing hit, so let's make sure we are clean in case we get reused
            _activities.Clear();
            _events.Clear();

            _workflow = workflow;

            return(true);
        }
Пример #8
0
        public void An_event_causing_a_state_transition()
        {
            _workflow = StateMachineWorkflow.New <TestWorkflow, TestInstance>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);

                x.During(y => y.Initial)
                .When(y => y.Finish)
                .TransitionTo(y => y.Completed);
            });

            _instance = _workflow.GetInstance(new TestInstance());

            _instance.RaiseEvent(x => x.Finish);
        }
Пример #9
0
		public void An_event_causing_a_state_transition()
		{
			_workflow = StateMachineWorkflow.New<TestWorkflow, TestInstance>(x =>
				{
					x.AccessCurrentState(y => y.CurrentState);

					x.During(y => y.Initial)
						.When(y => y.Finish)
						.TransitionTo(y => y.Completed);
				});

			_instance = _workflow.GetInstance(new TestInstance());

			_instance.RaiseEvent(x => x.Finish);
		}
Пример #10
0
        public void Should_be_easy()
        {
            StateMachineWorkflow <RemoteRequestEngineWorkflow, RemoteRequestEngine> workflow =
                StateMachineWorkflow.New <RemoteRequestEngineWorkflow, RemoteRequestEngine>(x =>
            {
                x.AccessCurrentState(i => i.CurrentState);

                x.Initially()
                .When(w => w.Start)
                .TransitionTo(y => y.Running)
                .When(y => y.Stop)
                .TransitionTo(y => y.Stopped);

                x.During(y => y.Running)
                .When(y => y.Interrupted)
                .Then(i => i.CancelPendingRequest)
                .When(y => y.Stop)
                .Then(i => i.CancelAllPendingRequests)
                .TransitionTo(y => y.Stopped);

                x.During(y => y.Stopped)
                .When(y => y.Start)
                .TransitionTo(y => y.Running)
                .When(y => y.Dispose)
                .Finalize();

                x.Finally()
                .Then(instance => Trace.WriteLine("Completed"));
            });

            var visitor = new TraceStateMachineVisitor();

            workflow.Accept(visitor);

            var engine = new RemoteRequestEngine();
            WorkflowInstance <RemoteRequestEngineWorkflow> engineInstance = workflow.GetInstance(engine);

            engineInstance.RaiseEvent(x => x.Start);
            engineInstance.RaiseEvent(x => x.Interrupted, new InterruptImpl {
                Source = "End of the world"
            });
            engineInstance.RaiseEvent(x => x.Stop);
            engineInstance.RaiseEvent(x => x.Dispose);

            Trace.WriteLine("Final State: " + engineInstance.CurrentState);
        }
Пример #11
0
        public void A_message_event_changes_the_state()
        {
            _workflow = StateMachineWorkflow.New <TestWorkflow, TestInstance>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);

                x.During(y => y.Initial)
                .When(y => y.Finish)
                .TransitionTo(y => y.Completed);
            });

            _instance = _workflow.GetInstance(new TestInstance());

            _instance.RaiseEvent(x => x.Finish, new Result
            {
                Value = "Success"
            });
        }
Пример #12
0
        public void A_simple_event_is_raised()
        {
            _workflow = StateMachineWorkflow.New <TestWorkflow, TestInstance>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);

                x.During(y => y.Initial)
                .When(y => y.Finish)
                .Then(() => _nonInstanceValue            = true)
                .Then(instance => instance.ConstantValue = "Success")
                .TransitionTo(y => y.Completed);
            });

            _testInstance = new TestInstance();

            _instance = _workflow.GetInstance(_testInstance);

            _instance.RaiseEvent(x => x.Finish);
        }
Пример #13
0
        public void Raising_multiple_events()
        {
            _workflow = StateMachineWorkflow.New <TestWorkflow, TestInstance>(x =>
            {
                x.AccessCurrentState(y => y.CurrentState);

                x.During(y => y.Initial)
                .When(y => y.Start)
                .TransitionTo(y => y.Running);

                x.During(y => y.Running)
                .When(y => y.Finish)
                .TransitionTo(y => y.Completed);
            });

            _instance = _workflow.GetInstance(new TestInstance());

            _instance.RaiseEvent(x => x.Start);
            _instance.RaiseEvent(x => x.Finish);
        }
Пример #14
0
		public void A_message_event_is_raised()
		{
			_workflow = StateMachineWorkflow.New<TestWorkflow, TestInstance>(x =>
			{
				x.AccessCurrentState(y => y.CurrentState);

				x.During(y => y.Initial)
					.When(y => y.Finish)
					.Then(() => _nonInstanceValue = true)
					.Then(instance => instance.MessageValue = "Success")
					.Then((instance, message) => instance.MessageValue = message.Value)
					.TransitionTo(y => y.Completed);
			});

			_testInstance = new TestInstance();

			_instance = _workflow.GetInstance(_testInstance);

			_instance.RaiseEvent(x => x.Finish, new Result
			{
				Value = "Success"
			});
		}
Пример #15
0
        public static ActorFactory <TActor> Create <TWorkflow, TActor>(Func <Inbox, TActor> createInstance,
                                                                       Action <StateMachineConfigurator <TWorkflow, TActor> >
                                                                       configurationAction)
            where TActor : class, Actor
            where TWorkflow : class
        {
            StateMachineWorkflow <TWorkflow, TActor> workflow = StateMachineWorkflow.New(configurationAction);

            var workflowBinder = new StateMachineWorkflowBinder <TWorkflow, TActor>(workflow);

            ActorFactory <TActor> factory = ActorFactory.Create <TActor>(x =>
            {
                x.ConstructedBy(inbox =>
                {
                    TActor instance = createInstance(inbox);

                    workflowBinder.Bind(inbox, instance);

                    return(instance);
                });
            });

            return(factory);
        }
Пример #16
0
        public StateMachineWorkflowBinder(StateMachineWorkflow <TWorkflow, TActor> workflow)
        {
            _workflow = workflow;

            _binders = BuildStateEventBinders(workflow);
        }
Пример #17
0
        public ServiceControllerFactory()
        {
            _workflow = StateMachineWorkflow.New <IServiceWorkflow, IServiceController>(ConfigureWorkflow);

            _workflowBinder = new StateMachineWorkflowBinder <IServiceWorkflow, IServiceController>(_workflow);
        }
Пример #18
0
 public WorkflowInstance(StateMachineWorkflow <TWorkflow, TInstance> workflow, TInstance instance)
 {
     _workflow = workflow;
     _instance = instance;
 }
Пример #19
0
		public void Raising_multiple_events()
		{
			_workflow = StateMachineWorkflow.New<TestWorkflow, TestInstance>(x =>
				{
					x.AccessCurrentState(y => y.CurrentState);

					x.During(y => y.Initial)
						.When(y => y.Start)
						.TransitionTo(y => y.Running);

					x.During(y => y.Running)
						.When(y => y.Finish)
						.TransitionTo(y => y.Completed);
				});

			_instance = _workflow.GetInstance(new TestInstance());

			_instance.RaiseEvent(x => x.Start);
			_instance.RaiseEvent(x => x.Finish);
		}
Пример #20
0
		public void A_message_event_changes_the_state()
		{
			_workflow = StateMachineWorkflow.New<TestWorkflow, TestInstance>(x =>
				{
					x.AccessCurrentState(y => y.CurrentState);

					x.During(y => y.Initial)
						.When(y => y.Finish)
						.TransitionTo(y => y.Completed);
				});

			_instance = _workflow.GetInstance(new TestInstance());

			_instance.RaiseEvent(x => x.Finish, new Result
				{
					Value = "Success"
				});
		}
Пример #21
0
        public void Should_be_easy()
        {
            StateMachineWorkflow <RemoteRequestEngineWorkflow, RemoteRequestEngine> workflow =
                StateMachineWorkflow.New <RemoteRequestEngineWorkflow, RemoteRequestEngine>(x =>
            {
                x.AccessCurrentState(i => i.CurrentState);

                x.Initially(c =>
                {
                    c.When(e => e.Start, t =>
                    {
                        t.TransitionTo(y => y.Running);
                    });

                    c.When(e => e.Stop, t =>
                    {
                        t.TransitionTo(y => y.Stopped);
                    });
                });

                x.During(s => s.Running, c =>
                {
                    c.When(e => e.Interrupted, t =>
                    {
                        t.TransitionTo(y => y.Stopped);
                    });

                    c.When(e => e.Stop, t =>
                    {
                        t.TransitionTo(y => y.Stopped);
                    });
                });

                x.During(s => s.Stopped, c =>
                {
                    c.When(e => e.Start, t =>
                    {
                        t.TransitionTo(y => y.Running);
                    });

                    c.When(e => e.Dispose, t =>
                    {
                        t.Finalize();
                    });
                });

                x.DuringAny(c =>
                {
                    c.When(e => e.Dispose)
                    .Then(() => {});
                });

                x.Finally(t =>
                {
                    t.Then(instance => Trace.WriteLine("Completed"));
                });
            });

            var visitor = new TraceStateMachineVisitor();

            workflow.Accept(visitor);

            var engine = new RemoteRequestEngine();
            WorkflowInstance <RemoteRequestEngineWorkflow> engineInstance = workflow.GetInstance(engine);

            engineInstance.RaiseEvent(x => x.Start);
            engineInstance.RaiseEvent(x => x.Stop);
            engineInstance.RaiseEvent(x => x.Dispose);

            Trace.WriteLine("Final State: " + engineInstance.CurrentState);
        }