生成的Xpand代码'编辑器不包含主类型'。但是我有一个主要的方法

时间:2013-04-30 11:48:39

标签: java eclipse dsl xtext xpand

我试图用xText和xPand来理解dsl代码的生成。

我在eclipse中打开了statemachine xText示例并作为新的eclipse应用程序运行。然后我创建了一个java,在src中包含一个test.statemachine文件,并将提供的代码复制到其中。

然后在src-gen文件夹中生成以下.java文件:

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 "";
    }
}
}

然而,如果错误'编辑器不包含主要类型',那么这将不会很有趣,但我可以看到它存在

1 个答案:

答案 0 :(得分:0)

包含生成的src-gen的文件夹testing.java默认为普通文件夹,而不是“源文件夹”。所以必须打开该文件夹上下文菜单并选择“Build Path” - > “用作源文件夹”。之后,您可以执行Java程序。