control
Class Controller

java.lang.Object
  extended by control.Controller
All Implemented Interfaces:
java.io.Serializable

public class Controller
extends java.lang.Object
implements java.io.Serializable

The controller of the Turing Machine. It implements the state, symbol and movement domains as String Accepted movements are "Left", "Right" and "Stop" Copyright (C) 2011 Federico "MrModd" Cosentino (http://mrmodd.it/)

See Also:
Serialized Form

Field Summary
private static Controller controller
           
private  java.lang.String currentState
           
private  java.lang.Integer indexOfT
           
private  java.lang.String initialState
           
private static boolean instanced
           
private  InstructionSet<java.lang.String,java.lang.String> instructions
           
private  java.lang.String nullSymbol
           
private static long serialVersionUID
           
private  java.lang.Integer steps
           
private  tape<java.lang.String> T
           
 
Constructor Summary
private Controller()
          Use the getController() method to instance this object
 
Method Summary
 void clearProgram()
          Delete all the instructions on the program set
 void clearTape()
          Delete all the symbol on the tape
static void deserialize(java.lang.String file)
          Load the state of the controller from a file
 int getActualPosition()
          Get the position of the head on the tape
static Controller getController()
          Because this class is a singleton this method should be called to get an instance of it.
 java.lang.String getNullSymbol()
          Get the symbol that in the actual program represent the null symbol of the tape
 java.lang.String[] getProgram()
          Get a formatted array of String representing all the instruction of the program loaded
 int getSteps()
          Get how many steps are executed from the program starts
 java.lang.String getTapeSymbol(int index)
          Get the symbol written on the specified position
 void loadProgram(java.lang.String file)
          Read the program from a file
 void nextStep()
          Execute a single step according with the current state of the machine and the symbol on the tape
 void resetSteps()
          Reset the steps count
 void rewindState()
          Return on the initial state of the program
static void serialize(java.lang.String file)
          Save the state of the controller on a file
 void setTape(int index, java.lang.String s)
          Set a symbol on the selected position
 void startGUI()
          Start the graphic interface
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

instanced

private static boolean instanced

controller

private static Controller controller

T

private tape<java.lang.String> T

initialState

private java.lang.String initialState

currentState

private java.lang.String currentState

indexOfT

private java.lang.Integer indexOfT

steps

private java.lang.Integer steps

nullSymbol

private java.lang.String nullSymbol

instructions

private InstructionSet<java.lang.String,java.lang.String> instructions
Constructor Detail

Controller

private Controller()
Use the getController() method to instance this object

Method Detail

getController

public static Controller getController()
Because this class is a singleton this method should be called to get an instance of it.

Returns:
the instanced object

startGUI

public void startGUI()
Start the graphic interface


loadProgram

public void loadProgram(java.lang.String file)
                 throws java.io.IOException,
                        ProgramFileSyntaxException
Read the program from a file

Parameters:
file - the path of the file to read
Throws:
java.io.IOException - if something goes wrong with the reading of file
ProgramFileSyntaxException - if the file does not contain a valid program

getNullSymbol

public java.lang.String getNullSymbol()
Get the symbol that in the actual program represent the null symbol of the tape

Returns:
the null symbol

getSteps

public int getSteps()
Get how many steps are executed from the program starts

Returns:
an int representing the number of steps

getActualPosition

public int getActualPosition()
Get the position of the head on the tape

Returns:
the position of the head (should be negative)

nextStep

public void nextStep()
              throws ProgramHaltedException
Execute a single step according with the current state of the machine and the symbol on the tape

Throws:
ProgramHaltedException - if the execution of the program reach the end

getTapeSymbol

public java.lang.String getTapeSymbol(int index)
Get the symbol written on the specified position

Parameters:
index - the position of the tape
Returns:
the symbol written in or the null symbol if that location isn't written

getProgram

public java.lang.String[] getProgram()
Get a formatted array of String representing all the instruction of the program loaded

Returns:
an array of String

setTape

public void setTape(int index,
                    java.lang.String s)
             throws java.lang.IllegalArgumentException
Set a symbol on the selected position

Parameters:
index - the position of the tape
s - the symbol to write
Throws:
java.lang.IllegalArgumentException - if the position isn't valid

clearTape

public void clearTape()
Delete all the symbol on the tape


rewindState

public void rewindState()
Return on the initial state of the program


resetSteps

public void resetSteps()
Reset the steps count


clearProgram

public void clearProgram()
Delete all the instructions on the program set


serialize

public static void serialize(java.lang.String file)
                      throws java.io.IOException
Save the state of the controller on a file

Parameters:
file - where to save the state
Throws:
java.io.IOException - if something goes wrong while saving

deserialize

public static void deserialize(java.lang.String file)
                        throws java.lang.ClassNotFoundException,
                               java.io.IOException
Load the state of the controller from a file

Parameters:
file - where to find the state
Throws:
java.lang.ClassNotFoundException - if the file isn't valid
java.io.IOException - if something goes wrong while reading the file