private StateMachine <Instance> CreateStateMachine() { return(AutomatonymousStateMachine <Instance> .New(builder => builder .State("Waiting", out Waiting) .Event("Start", out Start) .Event("First", out First) .Event("Second", out Second) .Event("Third", out Third) .Initially() .When(Start, b => b.TransitionTo(Waiting)) .During(Waiting) .When(First, b => b.Then(context => { context.Instance.First = true; context.Instance.CalledAfterAll = false; })) .When(Second, b => b.Then(context => { context.Instance.SecondFirst = !context.Instance.First; context.Instance.Second = true; context.Instance.CalledAfterAll = false; })) .CompositeEvent(Third, b => b.CompositeStatus, First, Second) .During(Waiting) .When(Third, context => context.Instance.SecondFirst, b => b .Then(context => { context.Instance.Called = true; context.Instance.CalledAfterAll = true; }) .Finalize() ) )); }
public void Specifying_an_event_activity_with_data() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .Event("PassedValue", out PassedValue) .During(builder.Initial) .When(Initialized, b => b .Then(context => context.Instance.Value = context.Data.Value) .TransitionTo(Running) ) .During(Running) .When(PassedValue, b => b.Then(context => context.Instance.OtherValue = context.Data)) ); _machine.RaiseEvent(_instance, Initialized, new Init { Value = "Hello" }).Wait(); _machine.RaiseEvent(_instance, PassedValue, 47) .Wait(); }
public void Specifying_an_event_activity_with_data() { _instance = new MyState(); _top = AutomatonymousStateMachine <MyState> .New(builder => builder .State("Greeted", out TopGreeted) .Event("Initialized", out TopInitialized) .InstanceState(b => b.Top) .During(builder.Initial) .When(TopInitialized, b => b.TransitionTo(TopGreeted)) ); _bottom = AutomatonymousStateMachine <MyState> .New(builder => builder .State("Ignored", out BottomIgnored) .Event("Initialized", out BottomInitialized) .InstanceState(b => b.Bottom) .During(builder.Initial) .When(BottomInitialized, b => b.TransitionTo(BottomIgnored)) ); _top.RaiseEvent(_instance, TopInitialized, new Init { Value = "Hello" }).Wait(); _bottom.RaiseEvent(_instance, BottomInitialized, new Init { Value = "Goodbye" }).Wait(); }
public void Setup() { _instance = new PitStopInstance(); _machine = AutomatonymousStateMachine <PitStopInstance> .New(builder => builder .State("BeingServiced", out BeingServiced) .Event("VehicleArrived", out VehicleArrived) .InstanceState(b => b.OverallState) .During(builder.Initial) .When(VehicleArrived, b => b .Then(context => { context.Instance.VehicleMake = context.Data.Make; context.Instance.VehicleModel = context.Data.Model; }) .TransitionTo(BeingServiced)) ); var vehicle = new Vehicle { Make = "Audi", Model = "A6", }; _machine.RaiseEvent(_instance, VehicleArrived, vehicle).Wait(); }
public async Task Should_transition_to_the_proper_state() { State True = null; State False = null; Event <Data> Thing = null; var instance = new Instance(); var machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("True", out True) .State("False", out False) .Event("Thing", out Thing) .InstanceState(b => b.CurrentState) .During(builder.Initial) .When(Thing, context => context.Data.Condition, b => b.TransitionTo(True)) .When(Thing, context => !context.Data.Condition, b => b.TransitionTo(False)) ); await machine.RaiseEvent(instance, Thing, new Data { Condition = true }); Assert.AreEqual(True, instance.CurrentState); // reset instance.CurrentState = machine.Initial; await machine.RaiseEvent(instance, Thing, new Data { Condition = false }); Assert.AreEqual(False, instance.CurrentState); }
public void Specifying_an_event_activity() { _claim = new ClaimAdjustmentInstance(); _machine = AutomatonymousStateMachine <ClaimAdjustmentInstance> .New(builder => builder .State("Running", out Running) .Event("Create", out Create) .InstanceState(x => x.CurrentState) .During(builder.Initial) .When(Create, b => b .Execute(context => new CalculateValueActivity(new LocalCalculator())) .Execute(context => new ActionActivity <ClaimAdjustmentInstance>(x => { })) .TransitionTo(Running) ) ); var data = new CreateClaim { X = 56, Y = 23, }; _machine.RaiseEvent(_claim, Create, data) .Wait(); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .State("Initialized", out Initialized) .State("ShouldNotBeHere", out ShouldNotBeHere) .Event("Started", out Started) .Event("ExplicitFilterStarted", out ExplicitFilterStarted) .Event("Finish", out Finish) .During(builder.Initial) .When(Started, b => b .Then(context => context.Instance.InitializeOnly = context.Data.InitializeOnly) .If(context => context.Data.InitializeOnly, x => x.Then(context => Console.WriteLine("Initializing Only!"))) .TransitionTo(Initialized) ) .During(builder.Initial) .When(ExplicitFilterStarted, context => true, b => b .IfElse(context => false, binder => binder .Then(context => Console.WriteLine("Should not be here!")) .TransitionTo(ShouldNotBeHere), binder => binder .Then(context => context.Instance.ShouldBeCalled = true) .Then(context => Console.WriteLine("Initializing Only!"))) ) .During(Running) .When(Finish, b => b.Finalize()) .WhenEnter(Initialized, b => b.If(context => !context.Instance.InitializeOnly, b => b.TransitionTo(Running))) ); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Failed", out Failed) .Event("Initialized", out Initialized) .InstanceState(b => b.CurrentState) .During(builder.Initial) .When(Initialized, b => b .Then(context => context.Instance.Called = true) .Then(_ => { throw new ApplicationException("Boom!"); }) .Then(context => context.Instance.NotCalled = false) .Catch <Exception>(ex => ex .Then(context => { context.Instance.ExceptionMessage = context.Exception.Message; context.Instance.ExceptionType = context.Exception.GetType(); }) .TransitionTo(Failed) ) ) ); _machine.RaiseEvent(_instance, Initialized).Wait(); }
public async Task Should_properly_handle_the_state_property() { State True = null; State False = null; Event <Data> Thing = null; var instance = new Instance(); var machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("True", out True) .State("False", out False) .Event("Thing", out Thing) .During(builder.Initial) .When(Thing, context => context.Data.Condition, b => b.TransitionTo(True)) .When(Thing, context => !context.Data.Condition, b => b.TransitionTo(False)) ); await machine.RaiseEvent(instance, Thing, new Data { Condition = true }); Assert.AreEqual(True, instance.CurrentState); var serializer = new JsonStateSerializer <StateMachine <Instance>, Instance>(machine); string body = serializer.Serialize(instance); Console.WriteLine("Body: {0}", body); var reInstance = serializer.Deserialize <Instance>(body); Assert.AreEqual(True, reInstance.CurrentState); }
public void Specifying_an_event_activity() { Event Initialized = null; Event Finish = null; _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .Event("Finish", out Finish) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) .During(Running) .When(Finish, b => b.Finalize()) ); _observer = new StateChangeObserver <Instance>(); using (IDisposable subscription = _machine.ConnectStateObserver(_observer)) { _machine.RaiseEvent(_instance, Initialized).Wait(); _machine.RaiseEvent(_instance, Finish).Wait(); } }
public void A_state_is_declared() { _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .InstanceState(x => x.CurrentState) ); }
public void A_state_is_declared() { _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .Event("Hello", out Hello) .Event("EventA", out EventA) .Event("EventInt", out EventInt) ); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .InstanceState(b => b.CurrentState) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) ); _machine.RaiseEvent(_instance, Initialized); }
public void A_state_is_declared() { _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Start", out Start) .Event("Charge", out Charge) .Initially() .When(Start, b => b.TransitionTo(Running)) .During(Running) .Ignore(Start) .Ignore(Charge) ); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) ); EventLift <Instance> eventLift = _machine.CreateEventLift(Initialized); eventLift.Raise(_instance).Wait(); }
public void A_state_is_declared() { _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Started", out Started) .InstanceState(x => x.CurrentState, Running) .Initially() .When(Started, b => b.TransitionTo(Running)) ); _instance = new Instance(); _machine.RaiseEvent(_instance, Started).Wait(); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .Event("Initialized", out Initialized) .State("Running", out Running) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) ); _observer = new EventRaisedObserver <Instance>(); using (IDisposable subscription = _machine.ConnectEventObserver(Initialized, _observer)) _machine.RaiseEvent(_instance, Initialized).Wait(); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Failed", out Failed) .Event("Initialized", out Initialized) .During(builder.Initial) .When(Initialized, b => b .Then(context => context.Instance.Called = true) .Then(_ => { throw new ApplicationException("Boom!"); }) .Then(context => context.Instance.NotCalled = false) .Catch <ApplicationException>(ex => ex .If(context => true, c => c .Then(context => context.Instance.CalledThenClause = true) ) .IfAsync(context => Task.FromResult(true), c => c .Then(context => context.Instance.CalledThenClauseAsync = true) ) .IfElse(context => false, c => c.Then(context => context.Instance.ThenShouldNotBeCalled = true), c => c.Then(context => context.Instance.ElseShouldBeCalled = true) ) .IfElseAsync(context => Task.FromResult(false), c => c.Then(context => context.Instance.ThenAsyncShouldNotBeCalled = true), c => c.Then(context => context.Instance.ElseAsyncShouldBeCalled = true) ) .Then(context => { context.Instance.ExceptionMessage = context.Exception.Message; context.Instance.ExceptionType = context.Exception.GetType(); }) .ThenAsync(context => { context.Instance.ThenAsyncShouldBeCalled = true; return(Task.CompletedTask); }) .TransitionTo(Failed) ) .Catch <Exception>(ex => ex .Then(context => context.Instance.ShouldNotBeCalled = true) ) ) ); _machine.RaiseEvent(_instance, Initialized).Wait(); }
public void Specifying_an_event_activity() { const string Finalized = "Finalized"; _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .InstanceState(b => b.CurrentState) .During(builder.Initial) .When(Initialized, b => b.Finalize()) .Finally(b => b.Then(context => context.Instance.Value = Finalized)) ); _machine.RaiseEvent(_instance, Initialized) .Wait(); }
public void Specifying_an_event_activity() { Event Initialized = null; Event LegCramped = null; Event Finish = null; Event Recovered = null; _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .Event("LegCramped", out LegCramped) .Event("Finish", out Finish) .Event("Recovered", out Recovered) .SubState("Resting", Running, out Resting) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) .During(Running) .When(LegCramped, b => b.TransitionTo(Resting)) .When(Finish, b => b.Finalize()) .During(Resting) .When(Recovered, b => b.TransitionTo(Running)) .WhenEnter(Running, b => b.Then(context => { })) .WhenLeave(Running, b => b.Then(context => { })) .BeforeEnter(Running, b => b.Then(context => { })) .AfterLeave(Running, b => b.Then(context => { })) ); _observer = new StateChangeObserver <Instance>(); _eventObserver = new EventRaisedObserver <Instance>(); using (IDisposable subscription = _machine.ConnectStateObserver(_observer)) using (IDisposable beforeEnterSub = _machine.ConnectEventObserver(Running.BeforeEnter, _eventObserver)) using (IDisposable afterLeaveSub = _machine.ConnectEventObserver(Running.AfterLeave, _eventObserver)) { _machine.RaiseEvent(_instance, Initialized).Wait(); _machine.RaiseEvent(_instance, LegCramped).Wait(); _machine.RaiseEvent(_instance, Recovered).Wait(); _machine.RaiseEvent(_instance, Finish).Wait(); } }
public void Setup() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .Event("Initialized", out Initialized) .Event("Finish", out Finish) .InstanceState(b => b.CurrentState) .During(builder.Initial) .When(Initialized, b => b.TransitionTo(Running)) .During(Running) .When(Finish, b => b.Finalize()) .BeforeEnterAny(b => b.Then(context => context.Instance.LastEntered = context.Data)) .AfterLeaveAny(b => b.Then(context => context.Instance.LastLeft = context.Data)) ); _machine.RaiseEvent(_instance, Initialized) .Wait(); }
public void A_state_is_declared() { _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Ready", out Ready) .Event("Init", out Init) .Event("Hello", out Hello) .Event("EventA", out EventA) .Initially() .When(Init, b => b.TransitionTo(Ready)) .DuringAny() .When(Hello, b => b .Then(context => context.Instance.HelloCalled = true) .Finalize() ) .When(EventA, b => b .Then(context => context.Instance.AValue = context.Data.Value) .Finalize() ) ); }
public void Specifying_an_event_activity() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .State("Running", out Running) .State("Initializing", out Initializing) .Event("Initialized", out Initialized) .InstanceState(b => b.CurrentState) .During(Initializing) .When(Initialized, b => b.TransitionTo(Running)) .DuringAny() .When(builder.Initial.Enter, b => b.TransitionTo(Initializing)) .When(builder.Initial.AfterLeave, b => b.Then(context => context.Instance.LeftState = context.Data)) .When(Initializing.BeforeEnter, b => b.Then(context => context.Instance.EnteredState = context.Data)) .When(Running.Enter, b => b.Finalize()) .When(builder.Final.BeforeEnter, b => b.Then(context => context.Instance.FinalState = context.Instance.CurrentState)) ); _machine.RaiseEvent(_instance, Initialized) .Wait(); }
public StateMachine <PrincessModelTelephone> CreateDynamically() { return(AutomatonymousStateMachine <PrincessModelTelephone> .New(builder => builder .State("OffHook", out OffHook) .State("Ringing", out Ringing) .State("Connected", out Connected) .State("PhoneDestroyed", out PhoneDestroyed) .Event("ServiceEstablished", out ServiceEstablished) .Event("CallDialed", out CallDialed) .Event("HungUp", out HungUp) .Event("CallConnected", out CallConnected) .Event("LeftMessage", out LeftMessage) .Event("PlacedOnHold", out PlacedOnHold) .Event("TakenOffHold", out TakenOffHold) .Event("PhoneHurledAgainstWall", out PhoneHurledAgainstWall) .InstanceState(x => x.CurrentState) .SubState("OnHold", Connected, out OnHold) .Initially() .When(ServiceEstablished, b => b .Then(context => context.Instance.Number = context.Data.Digits) .TransitionTo(OffHook)) .During(OffHook) .When(CallDialed, b => b.TransitionTo(Ringing)) .During(Ringing) .When(HungUp, b => b.TransitionTo(OffHook)) .When(CallConnected, b => b.TransitionTo(Connected)) .During(Connected) .When(LeftMessage, b => b.TransitionTo(OffHook)) .When(HungUp, b => b.TransitionTo(OffHook)) .When(PlacedOnHold, b => b.TransitionTo(OnHold)) .During(OnHold) .When(TakenOffHold, b => b.TransitionTo(Connected)) .When(PhoneHurledAgainstWall, b => b.TransitionTo(PhoneDestroyed)) .DuringAny() .When(Connected.Enter, b => b.Then(context => StartCallTimer(context.Instance))) .When(Connected.Leave, b => b.Then(context => StopCallTimer(context.Instance))) )); }
public async Task Should_property_initialize() { Request <GetQuote, Quote> QuoteRequest = null; Event <RequestQuote> QuoteRequested = null; var machine = AutomatonymousStateMachine <TestState> .New(builder => builder .InstanceState(x => x.CurrentState) .Event("QuoteRequested", out QuoteRequested) .Request(x => x.ServiceAddress = new Uri("loopback://localhost/my_queue"), "QuoteRequest", out QuoteRequest) .Initially() .When(QuoteRequested, b => b .Then(context => Console.WriteLine("Quote requested: {0}", context.Data.Symbol)) .Request(QuoteRequest, context => new GetQuote { Symbol = context.Message.Symbol }) .TransitionTo(QuoteRequest.Pending) ) .During(QuoteRequest.Pending) .When(QuoteRequest.Completed, b => b.Then((context) => Console.WriteLine("Request Completed!"))) .When(QuoteRequest.Faulted, b => b.Then((context) => Console.WriteLine("Request Faulted"))) .When(QuoteRequest.TimeoutExpired, b => b.Then((context) => Console.WriteLine("Request timed out"))) ); var instance = new TestState(); var requestQuote = new RequestQuote { Symbol = "MSFT", TicketNumber = "8675309", }; ConsumeContext <RequestQuote> consumeContext = new InternalConsumeContext <RequestQuote>(requestQuote); await machine.RaiseEvent(instance, QuoteRequested, requestQuote, consumeContext); await machine.RaiseEvent(instance, QuoteRequest.Completed, new Quote { Symbol = requestQuote.Symbol }); }
public void A_state_is_declared() { _instance = new Instance(); _machine = AutomatonymousStateMachine <Instance> .New(builder => builder .Event("Ignored", out Ignored) .Event("Handshake", out Handshake) .Event("Hello", out Hello) .Event("YelledAt", out YelledAt) .State("Greeted", out Greeted) .State("Pissed", out Pissed) .State("Loved", out Loved) .Initially() .When(Hello, b => b.TransitionTo(Greeted)) .During(Greeted) .When(Handshake, b => b.TransitionTo(Loved)) .When(Ignored, b => b.TransitionTo(Pissed)) .WhenEnter(Greeted, b => b.Then(context => { })) .DuringAny() .When(YelledAt, b => b.TransitionTo(builder.Final)) ); _machine.RaiseEvent(_instance, Hello); }
private StateMachine <Instance> CreateStateMachine() => AutomatonymousStateMachine <Instance> .New(builder => builder .State("ThisIsAState", out ThisIsAState) .Event("ThisIsASimpleEvent", out ThisIsASimpleEvent) .Event("ThisIsAnEventConsumingData", out ThisIsAnEventConsumingData) );