PageRenderTime 24ms CodeModel.GetById 13ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/TuringMachine/Controller.cs

#
C# | 158 lines | 132 code | 20 blank | 6 comment | 6 complexity | ff907b750f399caaa4977e28563fa533 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4using TuringMachine.StateAutomat;
  5using TuringMachine.CompilerModule;
  6
  7namespace TuringMachine
  8{
  9    public class Controller
 10    {
 11        private Tape tape = null;
 12        private Automat stateAutomat = null;
 13        private TransactionTable transactionTable = null;
 14        private Compiler compiler = null;
 15        private string input;
 16
 17        public Controller()
 18        {
 19            tape = new Tape("#",0);
 20            stateAutomat = new Automat();
 21            transactionTable = new TransactionTable();
 22            compiler = new Compiler(ref stateAutomat, ref transactionTable);
 23        }
 24
 25        public string GetTapeString()
 26        {
 27            return tape.TapeInput;
 28        }
 29
 30        public int GetTapeIndex()
 31        {
 32            return tape.Position;
 33        }
 34
 35        public void SetTapeString(string input)
 36        {
 37            tape.TapeInput = input;
 38            this.input = input;
 39        }
 40
 41        public string GetInput()
 42        {
 43            return this.input;
 44        }
 45
 46        public void SetTapeIndex(UInt16 index)
 47        {
 48            tape.Position = index;
 49        }
 50
 51        public void GetStatesList(ref int stepsCounter, ref List<string> stateNames)
 52        {
 53        }
 54
 55        public string GetAutomatState()
 56        {
 57            return stateAutomat.CurrentState.Name;
 58            
 59        }
 60
 61        public void MoveAutomatToStart()
 62        {
 63            stateAutomat.CurrentState = stateAutomat.GetStartState();
 64            this.SetTapeIndex(0);
 65        }
 66
 67        public void Compile(string script)
 68        {
 69            string[] commandsArray = null;
 70            string[] delim = {"\n","\r","\r\n"};
 71            commandsArray = script.Split(delim, StringSplitOptions.RemoveEmptyEntries);
 72            compiler.CompileScript(commandsArray);
 73        }
 74
 75        public string PerformStep()
 76        {
 77            char inputChar = tape.CurrentChar;
 78            StringBuilder _ret = new StringBuilder();
 79            //Gets current state in automat
 80            State fromState = stateAutomat.CurrentState;
 81            if (inputChar == ' ')
 82            {
 83                inputChar = '_';
 84            }
 85            _ret.AppendFormat("Automat in state '{0}'. Read '{1}'.", fromState.Name, inputChar);
 86            //Gets transaction
 87            //FromConfiguration fromConfiguration = new FromConfiguration(fromState, inputChar);
 88            FromConfiguration fromConfiguration = transactionTable.GetFromConfigurationInstance(fromState, inputChar);
 89            if (fromConfiguration == null)
 90            {
 91                ///TODO: Add stop
 92                throw new Exception ("There are no step from state " + fromState.Name + " for input "+inputChar.ToString());
 93            }
 94            ToConfiguration toConfiguration = transactionTable.GetToConfiguration(fromConfiguration);
 95
 96            State toState = toConfiguration.PairState;
 97            char tapeAction = toConfiguration.PairChar;
 98
 99            //Move automat
100            stateAutomat.CurrentState = toState;
101            _ret.AppendFormat("Move to state '{0}'.", toState.Name);
102            //Perform tape action
103            switch (tapeAction)
104            {
105                case('>'):
106                    tape.MoveRight();
107                    _ret.Append("Move right on tape");
108                    break;
109                case('<'):
110                    tape.MoveLeft();
111                    _ret.Append("Move left on tape");
112                    break;
113                case('_'):
114                    tape.CurrentChar = ' ';
115                    _ret.AppendFormat("Write '{0}' to tape",tapeAction);
116                    break;
117                default:
118                    tape.CurrentChar = tapeAction;
119                    _ret.AppendFormat("Write '{0}' to tape", tapeAction);
120                    break;
121            }
122            return _ret.ToString();
123
124        }
125
126        public bool IsStop(ref string reason)
127        {
128            bool _ret = true;
129            
130            switch (stateAutomat.CurrentState.Type)
131            {
132
133                case StateType.StartState:
134                    _ret = false;
135                    break;
136                case StateType.MiddleState:
137                    _ret = false;
138                    break;
139                case StateType.HaltState:
140                    reason = "Automat entered to Halting state";
141                    break;
142                case StateType.YesState:
143                    reason = "Automat entered to Yes state";
144                    break;
145                case StateType.NoState:
146                    reason = "Automat entered to No state";
147                    break;
148                case StateType.UnknownState:
149                    reason = "Automat entered to Unknown state";
150                    break;
151                default:
152                    break;
153            }
154            return _ret;
155        }
156
157    }
158}