PageRenderTime 23ms CodeModel.GetById 13ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/TuringMachine/StateAutomatModule/Transaction.cs

#
C# | 138 lines | 123 code | 14 blank | 1 comment | 9 complexity | 8c86685a3d6caa1cbe39fe30bd0fad6b MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace TuringMachine.StateAutomat
  6{
  7    class TransactionTable
  8    {
  9        private Dictionary<FromConfiguration, ToConfiguration> transactionTable = null;
 10
 11        public TransactionTable()
 12        {
 13            transactionTable = new Dictionary<FromConfiguration, ToConfiguration>();
 14        }
 15
 16        public void AddTransaction(State fromState, char inputChar, State toState, char tapeAction)
 17        {
 18            FromConfiguration _from = new FromConfiguration(fromState, inputChar);
 19            ToConfiguration _to = new ToConfiguration(toState, tapeAction);
 20            ///TODO: Check determenistic if _from conf already exist
 21            transactionTable.Add(_from, _to);
 22        }
 23
 24        public bool IsFromConfigurationExist(State fromState, char inputChar)
 25        {
 26            bool _ret = false;
 27            foreach (FromConfiguration _toCheck in transactionTable.Keys)
 28            {
 29                if ((fromState.Name == _toCheck.PairState.Name) && (inputChar == _toCheck.PairChar))
 30                {
 31                    _ret = true;
 32                    break;
 33                }
 34            }
 35            return _ret;
 36        }
 37
 38        public FromConfiguration GetFromConfigurationInstance(State fromState, char inputChar)
 39        {
 40            FromConfiguration _ret = null;
 41            foreach (FromConfiguration _toCheck in transactionTable.Keys)
 42            {
 43                if ((fromState.Name == _toCheck.PairState.Name) && (inputChar == _toCheck.PairChar))
 44                {
 45                    _ret = _toCheck;
 46                    break;
 47                }
 48            }
 49            return _ret;
 50        }
 51
 52        public ToConfiguration GetToConfiguration(FromConfiguration fromConfiguration)
 53        {
 54            ToConfiguration _ret = null;
 55            if (transactionTable.ContainsKey(fromConfiguration))
 56            {
 57                _ret = transactionTable[fromConfiguration];
 58            }
 59            return _ret;
 60        }
 61    }
 62
 63    class FromConfiguration : IPair
 64    {
 65        private State fromState;
 66        private char inputChar;
 67
 68        public FromConfiguration(State fromState, char inputChar)
 69        {
 70            this.PairState = fromState;
 71            this.PairChar = inputChar;
 72        }
 73        #region IPair Members
 74
 75        public State PairState
 76        {
 77            get
 78            {
 79                return fromState;
 80            }
 81            set
 82            {
 83                fromState = value;
 84            }
 85        }
 86        public char PairChar
 87        {
 88            get
 89            {
 90                return inputChar;
 91            }
 92            set
 93            {
 94                inputChar = value;
 95            }
 96        }
 97
 98        #endregion
 99    }
100
101    class ToConfiguration : IPair
102    {
103        private State toState;
104        private char tapeAction;
105        public ToConfiguration(State toState, char tapeAction)
106        {
107            this.PairState = toState;
108            this.PairChar = tapeAction;
109        }
110        #region IPair Members
111
112        public State PairState
113        {
114            get
115            {
116                return  toState;
117            }
118            set
119            {
120                toState = value;
121            }
122        }
123
124        public char PairChar
125        {
126            get
127            {
128                return tapeAction;
129            }
130            set
131            {
132                tapeAction = value;
133            }
134        }
135
136        #endregion
137    }
138}