Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. package turingMachine.io;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.LineNumberReader;
  8. import java.io.Reader;
  9.  
  10. import turingMachine.*;
  11. import turingMachine.tape.Direction;
  12. import turingMachine.tape.MultiTapeReadWriteData;
  13. import finiteStateMachine.state.State;
  14.  
  15. public class TuringMachineReader {
  16.  
  17.  
  18.  
  19. public static TuringMachine<Character> read(InputStream input) {
  20.  
  21.  
  22.  
  23.  
  24.  
  25. return null;
  26. }
  27.  
  28. public static TuringMachine<Character> read(Reader input) throws IOException {
  29.  
  30. int part = 0;
  31. int tapeCount = 0;
  32. String transition[];
  33.  
  34. MultiTapeReadWriteData<Character> in = null;
  35. MultiTapeReadWriteData<Character> out = null;
  36. TuringTransitionOutput<Character> output = null;
  37. Direction[] directions = null;
  38. Direction direction = null;
  39.  
  40. LineNumberReader reader = new LineNumberReader(input);
  41. TuringMachine<Character> turi = null;
  42.  
  43. for(;;) {
  44.  
  45. String line = reader.readLine();
  46.  
  47. if(line.matches("^#") || line.matches("")) {
  48. //nichts, wird ignoriert
  49.  
  50. } else if(part == 0) {
  51. if(!line.matches("[0-9]+") || line.matches("0")) {
  52. throw new IllegalArgumentException("<Anzahl der Bänder> gefordert!");
  53. }
  54. for(int i=0; i<line.length(); i++) {
  55. tapeCount += Integer.parseInt(line.charAt(i)+"") * (line.length()-i);
  56. }
  57. turi = new TuringMachine<Character>(tapeCount);
  58. in = new MultiTapeReadWriteData<Character>(tapeCount);
  59. out = new MultiTapeReadWriteData<Character>(tapeCount);
  60. part++;
  61.  
  62. } else if(part == 1) {
  63. for(int i=1; i<=line.length(); i++) {
  64. if(!line.matches(i + ":[_0-9]+")) {
  65. throw new IllegalArgumentException("<Position des Lesekopfes (startend bei 1)>:<Bandinhalt ab Position 1> gefordert!");
  66. }
  67. for(int j=2; j<line.length()-2; j++) {
  68. if(line.charAt(j) == '_') {
  69. turi.getTapes().getTapes().get(i).write(null);
  70. } else {
  71. turi.getTapes().getTapes().get(i).write(line.charAt(j));
  72. }
  73. }
  74. }
  75. part++;
  76.  
  77. } else if(part == 2) {
  78. if(!line.matches("((.+),)*.+")) {
  79. throw new IllegalArgumentException("Durch Komma getrennte Namen der Zustände gefordert!");
  80. }
  81. String[] states = line.split(",");
  82. for(int i=0; i<states.length; i++) {
  83. turi.addState(states[i]);
  84. }
  85. part++;
  86.  
  87. } else if(part == 3) {
  88. turi.setCurrentState(line);
  89. part++;
  90.  
  91. } else if(part == 4) {
  92. if(!line.matches("((.+),)*.+")) {
  93. throw new IllegalArgumentException("Durch Komma getrennte Namen der akzeptierten Zustände gefordert!");
  94. }
  95. String[] accStates = line.split(",");
  96. for(int i=0; i<accStates.length; i++) {
  97. turi.getState(accStates[i]).setAccepted(true);
  98. }
  99. part++;
  100.  
  101. } else if(part == 5) {
  102. if(!line.matches(".+,[_0-9] -> .+,[_0-9],[RLN]")) {
  103. throw new IllegalArgumentException("Name des Anfangszustands>,<Inhalt der einzelnen Bänder> -> <Name des Zielzustands>,<Werte, die auf die einzelnen Bänder zu schreiben sind>,<Richtungen für die einzelnen Köpfe: R für Rechts; L für Links, N für keine Bewegung> gefordert!");
  104. }
  105. transition = line.split(",| -> ");
  106. for(int i=1; i<transition.length-1; i++) {
  107. if(i<=(transition.length-1)/2) {
  108. in.set(i-1, transition[i].charAt(0));
  109. } else {
  110. out.set(i-1, transition[i].charAt(0));
  111. }
  112. }
  113. if(transition[transition.length-1].matches("R")) {
  114. direction = Direction.RIGHT;
  115. } else if(transition[transition.length-1].matches("L")) {
  116. direction = Direction.LEFT;
  117. } else {
  118. direction = Direction.NON;
  119. }
  120. for(int i=0; i<tapeCount; i++) {
  121. directions[i] = direction;
  122. }
  123. output = new TuringTransitionOutput<Character>(out, directions);
  124.  
  125. turi.addTransition(transition[0], in, transition[transition.length-2], output);
  126. }
  127.  
  128. }
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement