// Copyright © 2008 John M Rusk (http://dotnet.agilekiwi.com) // // You may use this source code ("The Software") in any manner you wish, // subject to the following conditions: // // (a) The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // (b) THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; // ReSharper disable FieldCanBeMadeReadOnly #pragma warning disable 169 namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } // Defining the classes which do the work public class Event { public Event(string code) { Code = code; } public readonly string Code; public static implicit operator Event(string code) { return new Event(code); } public Transition TransitionTo(State s) { return new Transition(this, s); // from this to s } } public class Command { public Command(string code) { Code = code; } public readonly string Code; public static implicit operator Command(string code) { return new Command(code); } } public class Transition { public Transition(Event e, State targetState) { FromEvent = e; ToState = targetState; } public readonly Event FromEvent; public readonly State ToState; } public class State: IEnumerable { public void Add(Command action){} public void Add(Transition transition){} public IEnumerator GetEnumerator() { throw new System.NotImplementedException(); } } // *** The DSL example itself public Event DoorClosed = "D1CL", DrawOpened = "D2OP", LightOn = "L1ON"; public Command LockPanel = "PNLK", UnlockDoor = "D1UL"; public State Idle, Active; protected void Setup() { Idle = new State { UnlockDoor, LockPanel, DoorClosed.TransitionTo(Active) }; Active = new State { DrawOpened.TransitionTo(WaitingForLight), LightOn.TransitionTo(WaitingForDraw) }; } // a couple of extra states which were referred to but not defined in Martin Fowler's original example State WaitingForLight, WaitingForDraw; } #pragma warning restore 169 // ReSharper restore FieldCanBeMadeReadOnly }