StateJava Pre-Processor

I have been doing some work every now and then over the course of the summer for one of my professors, Prof. Sheng Yu, at The University of Western Ontario. The work is based off of one of his papers, Adding States into Object Types, where he develops the τ-calculus as an extension to the ς-calculus of Abadi and Cardelli.

My work was to develop a pre-processor for the JavaTM programming language that turns classes into state machines. A description of how the pre-processor works can be found here: http://ioreader.com/code/python/state-java-pre-processor.pdf.

package example;

public class Auto {
    states { :Forward, :Neutral, :Reverse, :Off }
    
    public Auto() :Off { 
        System.out.println("AUTO: Off (constructor)"); 
    }
    
    /* the turnOff method shows a motivation for having the 
       ability to remember a previous state. */
      
    public void turnOn() :Off -> :Neutral { 
        System.out.println("AUTO: On (Neutral)."); 
    }
    
    /* the following methods ass a motivation for having 
       the ability to perform set operations, such as 
       difference, on states. */
    
    public void turnOff() :Forward, :Neutral, :Reverse -> :Off { 
        System.out.println("AUTO: Off."); 
    }
    
    /* this will be specialized by Car::reverse() for the 
       transition :Forward -> :Reverse */
    public void reverse() :Forward, :Neutral -> :Reverse { 
        System.out.println("AUTO: Reverse."); 
    }
    
    public void neutral() :Forward, :Reverse -> :Neutral { 
        System.out.println("AUTO: Neutral."); 
    }
    
    /* the following two methods will be merged into one in the
       compiled version. */
    
    public void forward() :Neutral -> :Forward { 
        System.out.println("AUTO: Forward (from Neutral)."); 
    }
    
    public void forward() :Reverse -> :Forward { 
        System.out.println("AUTO: Forward (from Reverse)."); 
    }
}
package example;
import example.Auto;

public class Car extends Auto {
    
    public Car() :Off { 
        System.out.println("CAR: Off (constructor)"); 
    }
      
    /* example of state specialization when overwriting
       parent class methods. */
    
    public void reverse() :Forward -> :Reverse { 
        System.out.println("CAR: Reverse (from Forward)."); 
    }
}
package example;
import example.Car;

public class Test {
    static public void main(String[] args) {
        Car car = new Car();
        car.turnOn();
        car.forward();
        car.reverse(); /* Car::reverse() specialization */
        car.neutral();
        car.forward();
        car.reverse(); /* Auto::reverse() */
        car.turnOff();
    }
}

When the above code is run through the pre-processor actual JavaTM code is produced. The processed code can then be compiled and run as usual. The following is the output from running the Test class above:

AUTO: Off (constructor)
CAR: Off (constructor)
AUTO: On (Neutral).
AUTO: Forward (from Neutral).
CAR: Reverse (from Forward).
AUTO: Neutral.
AUTO: Forward (from Neutral).
CAR: Reverse (from Forward).
AUTO: Off.

The code for the pre-processor can be found here: http://ioreader.com/code/python/state-java/


Comments


Comment