0
我想了解用xText和xPand生成dsl代碼。。但我有一個主要方法
我在eclipse中打開了statemachine xText例子,並作爲新的eclipse應用程序運行。然後,我在src中創建了一個包含test.statemachine文件的java,並將提供的代碼複製到其中。
下面的java文件,然後在src根文件夾中生成:
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 "";
}
}
}
然而,這將不好玩與錯誤「的編輯器不包含的主要類型」但從我可以看到這個存在
'testing'包含main。不知道「編輯器」是什麼,因爲你沒有包含它...... – John3136 2013-04-30 11:51:58
它是否也在Java項目的Java構建路徑中?你能否聲明一個包(否則它不會在現代JRE上運行)? – nitind 2013-04-30 11:57:20