static void Main() { //set up the FST FiniteStateTable FST_Task_2 = new FiniteStateTable(); FST_Task_2.SetNextState(S0, E0, S1); FST_Task_2.SetActions(S0, E0, "ActionX", "ActionY"); FST_Task_2.SetNextState(S1, E0, S0); FST_Task_2.SetActions(S1, E0, "ActionW"); FST_Task_2.SetNextState(S2, E0, S0); FST_Task_2.SetActions(S2, E0, "ActionW"); FST_Task_2.SetNextState(S1, E1, S2); FST_Task_2.SetActions(S1, E1, "ActionX", "ActionZ"); FST_Task_2.SetNextState(S2, E2, S1); FST_Task_2.SetActions(S2, E2, "ActionX", "ActionY"); char keyEntered = '0'; int keyEvent; while (true) { keyEntered = (char)Console.Read(); keyEvent = -1; if (keyEntered == ('a')) { keyEvent = E0; } if (keyEntered == ('b')) { keyEvent = E1; } if (keyEntered == ('c')) { keyEvent = E2; } if (keyEntered == ('q')) { keyEvent = 4; } if (keyEvent >= 0) { string time = System.DateTime.Now.ToString(); actionLog.Add(time + " User Entered: " + keyEntered); if ((keyEntered == 'a') || (keyEntered == 'b') || (keyEntered == 'c')) { execute(FST_Task_2.GetActions(FST_Task_2.state, keyEvent, 1)); execute(FST_Task_2.GetActions(FST_Task_2.state, keyEvent, 2)); execute(FST_Task_2.GetActions(FST_Task_2.state, keyEvent, 3)); if (FST_Task_2.state != FST_Task_2.GetNextState(FST_Task_2.state, keyEvent)) { FST_Task_2.state = FST_Task_2.GetNextState(FST_Task_2.state, keyEvent); Console.WriteLine("Now in State " + FST_Task_2.state); string timeStamp = System.DateTime.Now.ToString(); actionLog.Add(timeStamp + " Entered State " + FST_Task_2.state); } } else if (keyEntered == 'q') { Console.WriteLine("Please enter the file path of the file where you wish to have activity logged"); string filePath = ""; while (filePath == "") { filePath = Console.ReadLine(); } try { quit(filePath); } catch { Console.WriteLine("File path not recognised, please try again"); } } else { Console.WriteLine("Command not recognised. Please try again."); } } } }
/* --------------------------------------------------------------------------------------------------------------------- * MAIN * --------------------------------------------------------------------------------------------------------------------- */ private static void Main() { // INITIALISE Multi Threading Thread th = Thread.CurrentThread; th.Name = "MainThread"; // CREATE FST // Task 2 FSM FiniteStateTable FST_X = new FiniteStateTable(); //define FST_X FST_X.SetNextState(S0, E0, S1); FST_X.SetActions(S0, E0, "ActionX", "ActionY"); FST_X.SetNextState(S1, E0, S0); FST_X.SetActions(S1, E0, "ActionW"); FST_X.SetNextState(S2, E0, S0); FST_X.SetActions(S2, E0, "ActionW"); FST_X.SetNextState(S1, E1, S2); FST_X.SetActions(S1, E1, "ActionX", "ActionZ"); FST_X.SetNextState(S2, E2, S1); FST_X.SetActions(S2, E2, "ActionX", "ActionY"); // Task 3 FSM FiniteStateTable FST_Y = new FiniteStateTable(S0 + SB); //define FST_Y FST_Y.SetNextState(0, E0, SB); FST_Y.SetNextState(1, E0, SB); FST_Y.SetNextState(2, E0, SB); FST_Y.SetNextState(4, E0, SA); FST_Y.SetActions(4, E0, "ActionJ", "ActionK", "ActionL"); FST_Y.SetNextState(4, E1, SA); FST_Y.SetActions(4, E1, "ActionJ", "ActionK", "ActionL"); FST_Y.SetNextState(4, E2, SA); FST_Y.SetActions(4, E2, "ActionJ", "ActionK", "ActionL"); // DECLARE AND INITIALISE VARIABLES char keyEntered = '0'; int keyEvent; int colX; int rowX; int colY; int rowY; // RUN THE APPLICATION while (true) { // Receive Key Press keyEntered = (char)Console.Read(); // Set Event Value keyEvent = -1; if (keyEntered == ('a')) { keyEvent = E0; } if (keyEntered == ('b')) { keyEvent = E1; } if (keyEntered == ('c')) { keyEvent = E2; } if (keyEntered == ('q')) { keyEvent = 4; } // If Event Driven Key Press if (keyEvent >= 0) { // Set Row and Column Values; // X FSM rowX = keyEvent; colX = FST_X.state; // Y FSM rowY = keyEvent; colY = FST_X.state + FST_Y.state; if (keyEntered != 'q') { // Execute the Actions // X FSM execute(FST_X.GetActions(colX, rowX)); // Y FSM execute(FST_Y.GetActions(colY, rowY)); // Evaluate if there is a change of state // X FSM if (FST_X.state != FST_X.GetNextState(colX, rowX)) { FST_X.state = FST_X.GetNextState(colX, rowX); string message = ""; if (FST_X.state == 0) { message = "S0"; } if (FST_X.state == 1) { message = "S1"; } if (FST_X.state == 2) { message = "S2"; } Console.WriteLine("Now in State " + message); } // Y FSM if ((FST_Y.GetNextState(colY, rowY) > 2 && FST_Y.state <= 2) || (FST_Y.GetNextState(colY, rowY) < 3 && FST_Y.state >= 3)) { FST_Y.state = FST_Y.GetNextState(colY, rowY); string message = ""; if (FST_Y.state == 0) { message = "SA"; } if (FST_Y.state == 3) { message = "SB"; } Console.WriteLine("Now in State " + message); } } else { // Create the File to Log File for Time Stamping User Activity // Acquire File Path Console.WriteLine("Please enter the file path of the file where you wish to have activity logged"); string filePath = ""; while (filePath == "") { filePath = Console.ReadLine(); } try // Save File { quit(filePath); } catch // Invalid File Path { Console.WriteLine("File path not recognised, please try again"); } } } } }
static void Main() { //set up the FST FiniteStateTable FST_Task_2 = new FiniteStateTable(); FST_Task_2.SetNextState(S0, E0, S1); FST_Task_2.SetActions(S0, E0, "ActionX", "ActionY"); FST_Task_2.SetNextState(S1, E0, S0); FST_Task_2.SetActions(S1, E0, "ActionW"); FST_Task_2.SetNextState(S2, E0, S0); FST_Task_2.SetActions(S2, E0, "ActionW"); FST_Task_2.SetNextState(S1, E1, S2); FST_Task_2.SetActions(S1, E1, "ActionX", "ActionZ"); FST_Task_2.SetNextState(S2, E2, S1); FST_Task_2.SetActions(S2, E2, "ActionX", "ActionY"); //set up local variables char keyEntered = '0'; int keyEvent; while (true) { //Read single characters keyEntered = (char)Console.Read(); keyEvent = -1; //interpret events if (keyEntered == ('a')) { keyEvent = E0; } if (keyEntered == ('b')) { keyEvent = E1; } if (keyEntered == ('c')) { keyEvent = E2; } if (keyEntered == ('q')) { keyEvent = 4; } //if a valid key has been entered if (keyEvent >= 0) { //log key reception string time = System.DateTime.Now.ToString(); actionLog.Add(time + " User Entered: " + keyEntered); //execute corresponding actions to commands and update stae if ((keyEntered == 'a') || (keyEntered == 'b') || (keyEntered == 'c')) { //execute actions execute(FST_Task_2.GetActions(FST_Task_2.state, keyEvent)); //update state if (FST_Task_2.state != FST_Task_2.GetNextState(FST_Task_2.state, keyEvent)) { FST_Task_2.state = FST_Task_2.GetNextState(FST_Task_2.state, keyEvent); Console.WriteLine("Now in State " + FST_Task_2.state); //log state change string timeStamp = System.DateTime.Now.ToString(); actionLog.Add(timeStamp + " Entered State " + FST_Task_2.state); } } else if (keyEntered == 'q') //if quit command is entered { //acquire log file path Console.WriteLine("Please enter the file path of the file where you wish to have activity logged"); string filePath = ""; while (filePath == "") { filePath = Console.ReadLine(); } //execute logging and closing sequence try { quit(filePath); } catch { Console.WriteLine("File path not recognised, please try again"); } } } } }