PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/TuringMachine/StateAutomatModule/Transaction.cs

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