Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package turingMachine.io;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.LineNumberReader;
- import java.io.Reader;
- import turingMachine.*;
- import turingMachine.tape.Direction;
- import turingMachine.tape.MultiTapeReadWriteData;
- import finiteStateMachine.state.State;
- public class TuringMachineReader {
- public static TuringMachine<Character> read(InputStream input) {
- return null;
- }
- public static TuringMachine<Character> read(Reader input) throws IOException {
- int part = 0;
- int tapeCount = 0;
- String transition[];
- MultiTapeReadWriteData<Character> in = null;
- MultiTapeReadWriteData<Character> out = null;
- TuringTransitionOutput<Character> output = null;
- Direction[] directions = null;
- Direction direction = null;
- LineNumberReader reader = new LineNumberReader(input);
- TuringMachine<Character> turi = null;
- for(;;) {
- String line = reader.readLine();
- if(line.matches("^#") || line.matches("")) {
- //nichts, wird ignoriert
- } else if(part == 0) {
- if(!line.matches("[0-9]+") || line.matches("0")) {
- throw new IllegalArgumentException("<Anzahl der Bänder> gefordert!");
- }
- for(int i=0; i<line.length(); i++) {
- tapeCount += Integer.parseInt(line.charAt(i)+"") * (line.length()-i);
- }
- turi = new TuringMachine<Character>(tapeCount);
- in = new MultiTapeReadWriteData<Character>(tapeCount);
- out = new MultiTapeReadWriteData<Character>(tapeCount);
- part++;
- } else if(part == 1) {
- for(int i=1; i<=line.length(); i++) {
- if(!line.matches(i + ":[_0-9]+")) {
- throw new IllegalArgumentException("<Position des Lesekopfes (startend bei 1)>:<Bandinhalt ab Position 1> gefordert!");
- }
- for(int j=2; j<line.length()-2; j++) {
- if(line.charAt(j) == '_') {
- turi.getTapes().getTapes().get(i).write(null);
- } else {
- turi.getTapes().getTapes().get(i).write(line.charAt(j));
- }
- }
- }
- part++;
- } else if(part == 2) {
- if(!line.matches("((.+),)*.+")) {
- throw new IllegalArgumentException("Durch Komma getrennte Namen der Zustände gefordert!");
- }
- String[] states = line.split(",");
- for(int i=0; i<states.length; i++) {
- turi.addState(states[i]);
- }
- part++;
- } else if(part == 3) {
- turi.setCurrentState(line);
- part++;
- } else if(part == 4) {
- if(!line.matches("((.+),)*.+")) {
- throw new IllegalArgumentException("Durch Komma getrennte Namen der akzeptierten Zustände gefordert!");
- }
- String[] accStates = line.split(",");
- for(int i=0; i<accStates.length; i++) {
- turi.getState(accStates[i]).setAccepted(true);
- }
- part++;
- } else if(part == 5) {
- if(!line.matches(".+,[_0-9] -> .+,[_0-9],[RLN]")) {
- 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!");
- }
- transition = line.split(",| -> ");
- for(int i=1; i<transition.length-1; i++) {
- if(i<=(transition.length-1)/2) {
- in.set(i-1, transition[i].charAt(0));
- } else {
- out.set(i-1, transition[i].charAt(0));
- }
- }
- if(transition[transition.length-1].matches("R")) {
- direction = Direction.RIGHT;
- } else if(transition[transition.length-1].matches("L")) {
- direction = Direction.LEFT;
- } else {
- direction = Direction.NON;
- }
- for(int i=0; i<tapeCount; i++) {
- directions[i] = direction;
- }
- output = new TuringTransitionOutput<Character>(out, directions);
- turi.addTransition(transition[0], in, transition[transition.length-2], output);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement