PageRenderTime 115ms CodeModel.GetById 109ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/TuringMachine/StateAutomat/State.cs

#
C# | 82 lines | 78 code | 4 blank | 0 comment | 4 complexity | 8cec93e6deb00e8e429a41724a0def75 MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace TuringMachine.StateAutomat
 6{
 7    public class State 
 8    {
 9        #region Private members
10        private String name;
11        private StateType type;        
12        private Dictionary<char, State> transactionTable = null;
13        #endregion
14
15        #region Properties
16        public StateType Type
17        {
18            get { return type; }
19            set { type = value; }
20        }
21        public String Name
22        {
23            get { return name; }
24            set { name = value; }
25        }
26        #endregion
27
28        #region Constructors
29        public State()
30        {
31            transactionTable = new Dictionary<char, State>();
32            name = string.Empty;
33            type = StateType.Unknown;
34        }
35        public State(String name)
36        {
37            transactionTable = new Dictionary<char, State>();
38            name = name;
39            type = StateType.Unknown;
40        }
41        public State(String name, StateType type)
42        {
43            transactionTable = new Dictionary<char, State>();
44            name = name;
45            type = type;
46        }
47        #endregion
48
49        #region Public methods
50        public Boolean IsStart()
51        {
52            return (type == StateType.StartState);
53        }
54        public Boolean IsHalt()
55        {
56            return (type == StateType.HaltState);
57        }
58        public State GetNextState(char inputChar)
59        {
60            if (transactionTable.ContainsKey(inputChar))
61            {
62                return transactionTable[inputChar];
63            }
64            else
65            {
66                return null;
67            }
68        }
69        public void AddTransition(char inputChar, State nextState)
70        {
71            if (transactionTable.ContainsKey(inputChar))
72            {
73                throw new Exception("Already exist transaction from state '" + this.Name + "' with character '" + inputChar + "'");
74            }
75            else
76            {
77                transactionTable.Add(inputChar, nextState);
78            }
79        }        
80        #endregion
81    }
82}