Created
February 20, 2015 09:27
-
-
Save madhon/6c5106ad2f4ae2f51b22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Stateless; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Reactive.Disposables; | |
| using System.Reactive.Linq; | |
| using System.Reactive.PlatformServices; | |
| namespace ConsoleApplication1 | |
| { | |
| public enum StateName | |
| { | |
| ReadyForDevlopment, | |
| InDevelopment, | |
| ReadForTest, | |
| InTest, | |
| Closed | |
| } | |
| public enum Trigger | |
| { | |
| StartDevelopment, | |
| DevelopmentFinished, | |
| StartTest, | |
| TestPassed, | |
| TestFailed, | |
| } | |
| public class State | |
| { | |
| public StateName Name { get; private set; } | |
| public Action OnEntryStateAction { get; private set; } | |
| public Action OnExitStateAction { get; private set; } | |
| public State(StateName name, Action onEntry, Action onExit) | |
| { | |
| this.Name = name; | |
| this.OnEntryStateAction = onEntry; | |
| this.OnExitStateAction = onExit; | |
| } | |
| } | |
| public static class Program | |
| { | |
| private static Dictionary<StateName, State> states = new Dictionary<StateName, State>(); | |
| private static CompositeDisposable disposables = new CompositeDisposable(); | |
| private static StateMachine<State, Trigger> jiraMachine; | |
| private static bool simulateTestPassing = true; | |
| public static void Main(string[] args) | |
| { | |
| EnlightenmentProvider.EnsureLoaded(); | |
| states.Add(StateName.ReadyForDevlopment, new State(StateName.ReadyForDevlopment, null, null)); | |
| states.Add(StateName.InDevelopment, new State(StateName.InDevelopment, null, null)); | |
| states.Add(StateName.ReadForTest, new State(StateName.ReadForTest, null, null)); | |
| states.Add(StateName.InTest, new State(StateName.InTest, null, null)); | |
| states.Add(StateName.Closed, new State(StateName.Closed, null, null)); | |
| jiraMachine = new StateMachine<State, Trigger>(states[StateName.ReadyForDevlopment]); | |
| jiraMachine.Configure(states[StateName.ReadyForDevlopment]) | |
| .OnEntry(s => PrintStateOnEntry()) | |
| .OnExit(s => PrintStateOnExit()) | |
| .Permit(Trigger.StartDevelopment, states[StateName.InDevelopment]); | |
| jiraMachine.Configure(states[StateName.InDevelopment]) | |
| .OnEntry(s => | |
| { | |
| disposables.Add(Observable.Interval(TimeSpan.FromSeconds(1)).Subscribe(x => SendStyleCopNag(x) )); | |
| jiraMachine.State.OnEntryStateAction(); | |
| }) | |
| .OnExit(s => | |
| { | |
| disposables.Dispose(); | |
| jiraMachine.State.OnExitStateAction(); | |
| }) | |
| .Permit(Trigger.DevelopmentFinished, states[StateName.ReadForTest]); | |
| jiraMachine.Configure(states[StateName.ReadForTest]) | |
| .OnEntry(s => PrintStateOnEntry()) | |
| .OnExit(s => PrintStateOnExit()) | |
| .Permit(Trigger.StartTest, states[StateName.InTest]); | |
| jiraMachine.Configure(states[StateName.InTest]) | |
| .OnEntry(s => PrintStateOnEntry()) | |
| .OnExit(s => PrintStateOnExit()) | |
| .Permit(Trigger.TestFailed, states[StateName.InDevelopment]) | |
| .Permit(Trigger.TestPassed, states[StateName.Closed]); | |
| jiraMachine.Configure(states[StateName.Closed]) | |
| .OnEntry(s => PrintStateOnEntry()) | |
| .OnExit(s => PrintStateOnExit()); | |
| Fire(jiraMachine, Trigger.StartDevelopment); | |
| Action completeTheRemainingStates = () => | |
| { | |
| Fire(jiraMachine, Trigger.DevelopmentFinished); | |
| Fire(jiraMachine, Trigger.StartTest); | |
| if (simulateTestPassing) | |
| { | |
| Fire(jiraMachine, Trigger.TestPassed); | |
| } | |
| else | |
| { | |
| Fire(jiraMachine, Trigger.TestFailed); | |
| } | |
| }; | |
| disposables.Add(Observable.Timer(TimeSpan.FromSeconds(5)) | |
| .Subscribe(x => | |
| { | |
| completeTheRemainingStates(); | |
| })); | |
| Console.ReadKey(true); | |
| } | |
| private static void SendStyleCopNag(long value) | |
| { | |
| Console.WriteLine("Don't forget stylecop"); | |
| } | |
| private static void Fire(StateMachine<State, Trigger> machine, Trigger trigger) | |
| { | |
| Console.WriteLine("Firing: {0}", trigger); | |
| machine.Fire(trigger); | |
| } | |
| private static void PrintStateOnEntry() | |
| { | |
| Console.WriteLine("Entered State {0}", jiraMachine.State.Name); | |
| } | |
| private static void PrintStateOnExit() | |
| { | |
| Console.WriteLine("Exited State {0}", jiraMachine.State.Name); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment