0

xText と xPand を使用した DSL コード生成を理解しようとしています。

Eclipse で statemachine xText の例を開き、新しい Eclipse アプリケーションとして実行しました。次に、src に test.statemachine ファイルを含む Java を作成し、提供されたコードをそこにコピーしました。

次に、次の .java ファイルが src-gen フォルダーに生成されます。

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 に答える