0

我试图了解使用 xText 和 xPand 生成 dsl 代码。

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

然后在 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 "";
    }
}
}

然而,这不会因为错误“编辑器不包含主要类型”而有趣,但据我所知,这是存在的

4

1 回答 1

0

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

于 2013-05-01T09:46:18.873 回答