// Generated from: // // <% var i, j, outerIf = 'if', innerIf = 'if', initialStateName; for (i = 0; i < stateMachine.states.length; i += 1) { if (stateMachine.states[i].id === stateMachine.initialState) { initialStateName = stateMachine.states[i].name; break; } } %> using System; using System.Collections.Generic; namespace FSM { public class Program { public static void Main(string[] args) { System.Console.WriteLine("State machine for <%=stateMachine.name%>. Type 'exit' to exit the program any time."); List finalStates = new List(); <%for (i = 0; i < stateMachine.finalStates.length; i += 1) {%> finalStates.Add("<%=stateMachine.finalStates[i]%>"); <%}%> string currentStateId = "<%=stateMachine.initialState%>"; string currentState = "<%=initialStateName%>"; string currentInput = ""; while (currentInput != "exit") { System.Console.WriteLine("Current state: {0} ({1})", currentState, currentStateId); System.Console.Write("Enter an event: "); currentInput = System.Console.ReadLine(); <%for (i = 0; i < stateMachine.states.length; i += 1) { var eventStr = ''; if (stateMachine.states[i].transitions.length === 0) { continue; } innerIf = 'if';%> <%=outerIf%> (currentStateId == "<%=stateMachine.states[i].id%>") { <%for (j = 0; j < stateMachine.states[i].transitions.length; j += 1) { var transition = stateMachine.states[i].transitions[j]; eventStr += ', ' + transition.event; %> <%=innerIf%> (currentInput == "<%=transition.event%>") { System.Console.WriteLine("Switching state to <%=transition.targetName%> (<%=transition.targetId%>)"); currentStateId = "<%=transition.targetId%>"; currentState = "<%=transition.targetName%>"; }<%if (j === stateMachine.states[i].transitions.length - 1) {%> else { System.Console.WriteLine("Possible events for transition(s): <%=eventStr.substring(2)%>"); }<%}%> <%innerIf = 'else if';}%> } <%outerIf = 'else if';}%> // Break the loop at a final state. if (finalStates.Contains(currentStateId)) { System.Console.WriteLine("At a final state {0} ({1})", currentState, currentStateId); break; } } System.Console.WriteLine("Press enter to exit"); System.Console.ReadLine(); } } }