I am trying to understand dsl code generation with xText and xPand.
I have opened the statemachine xText example in eclipse and ran as a new eclipse application. I then made a java contaning a test.statemachine file in the src and copied the supplied code into it.
The following .java file is then generated in the src-gen folder:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class testing {
public static void main(String[] args) {
new testing().run();
}
protected void doUnlockPanel() {
System.out.println("Executing command unlockPanel (PNUL)");
}
protected void doLockPanel() {
System.out.println("Executing command lockPanel (PNLK)");
}
protected void doLockDoor() {
System.out.println("Executing command lockDoor (D1LK)");
}
protected void doUnlockDoor() {
System.out.println("Executing command unlockDoor (D1UL)");
}
protected void run() {
boolean executeActions = true;
String currentState = "idle";
String lastEvent = null;
while (true) {
if (currentState.equals("idle")) {
if (executeActions) {
doUnlockDoor();
doLockPanel();
executeActions = false;
}
System.out.println("Your are now in state 'idle'. Possible events are [doorClosed].");
lastEvent = receiveEvent();
if ("doorClosed".equals(lastEvent)) {
currentState = "active";
executeActions = true;
}
}
if (currentState.equals("active")) {
if (executeActions) {
executeActions = false;
}
System.out.println("Your are now in state 'active'. Possible events are [drawOpened, lightOn].");
lastEvent = receiveEvent();
if ("drawOpened".equals(lastEvent)) {
currentState = "waitingForLight";
executeActions = true;
}
if ("lightOn".equals(lastEvent)) {
currentState = "waitingForDraw";
executeActions = true;
}
}
if (currentState.equals("waitingForLight")) {
if (executeActions) {
executeActions = false;
}
System.out.println("Your are now in state 'waitingForLight'. Possible events are [lightOn].");
lastEvent = receiveEvent();
if ("lightOn".equals(lastEvent)) {
currentState = "unlockedPanel";
executeActions = true;
}
}
if (currentState.equals("waitingForDraw")) {
if (executeActions) {
executeActions = false;
}
System.out.println("Your are now in state 'waitingForDraw'. Possible events are [drawOpened].");
lastEvent = receiveEvent();
if ("drawOpened".equals(lastEvent)) {
currentState = "unlockedPanel";
executeActions = true;
}
}
if (currentState.equals("unlockedPanel")) {
if (executeActions) {
doUnlockPanel();
doLockDoor();
executeActions = false;
}
System.out.println("Your are now in state 'unlockedPanel'. Possible events are [panelClosed].");
lastEvent = receiveEvent();
if ("panelClosed".equals(lastEvent)) {
currentState = "idle";
executeActions = true;
}
}
if ("doorClosed".equals(lastEvent)) {
System.out.println("Resetting state machine.");
currentState = "idle";
executeActions = true;
}
}
}
private String receiveEvent() {
System.out.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
return br.readLine();
} catch (IOException ioe) {
System.out.println("Problem reading input");
return "";
}
}
}
However this will not fun with the error 'editor does not contain a main type' but from what I can see this exists
The folder
src-gen
which contains the generatedtesting.java
is by default a plain folder , not a "source folder". So so must open the context menu on that folder and select "Build Path" -> "Use as source-folder". After that you can execute the Java program.