第2章 チュートリアル

次のチュートリアルを読んで JPDL の基本的なプロセス構築について学びましょう。このチュートリアルでは、API よりランタイム実行を管理する方法も示しています。
チュートリアルにあるサンプルの詳細説明はそれぞれ、src/java.examples サブディレクトリにある jBPM ダウンロードパッケージにあります。

注記

Red Hat はこの時点でプロジェクトの作成を推奨します。自由に各サンプルを実験し、各サンプルを変更しバリエーションを作成することができます。
まず JBPM をダウンロードしインストールしてください。

注記

JBPM には例示されている XML をオーサリングするためのグラフィックデザイナーツールが含まれています。本書の「ダウンロードの概要」のセクションにグラフィックデザイナーのダウンロードに関する説明があります。グラフィックデザイナーを使う必要なく、このチュートリアルを完了することも可能です。

2.1. 「Hello World」 サンプル

プロセス定義有向グラフで、ノードと遷移で構成されています。Hello World プロセス定義には 3 つのノードがあります (Designer Tool を使わずに簡単なプロセスから始めると、まとまりや組み合わせが理解できるはずです)。
以下の図は、Hello World プロセスのグラフ表示です。
Hello World プロセスグラフ

図2.1 Hello World プロセスグラフ

public void testHelloWorldProcess() {
  // This method shows a process definition and one execution
  // of the process definition.  The process definition has
  // 3 nodes: an unnamed start-state, a state 's' and an
  // end-state named 'end'.
  // The next line parses a piece of xml text into a
  // ProcessDefinition.  A ProcessDefinition is the formal
  // description of a process represented as a java object.
  ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='s' />" +
      "  </start-state>" +
      "  <state name='s'>" +
      "    <transition to='end' />" +
      "  </state>" +
      "  <end-state name='end' />" +
      "</process-definition>"
  );

    // The next line creates one execution of the process definition.
    // After construction, the process execution has one main path
    // of execution (=the root token) that is positioned in the
    // start-state.
    ProcessInstance processInstance =
      new ProcessInstance(processDefinition);

    // After construction, the process execution has one main path
    // of execution (=the root token).
    Token token = processInstance.getRootToken();

    // Also after construction, the main path of execution is positioned
    // in the start-state of the process definition.
    assertSame(processDefinition.getStartState(), token.getNode());

    // Let's start the process execution, leaving the start-state
    // over its default transition.
    token.signal();
    // The signal method will block until the process execution
    // enters a wait state.

    // The process execution will have entered the first wait state
    // in state 's'. So the main path of execution is now
    // positioned in state 's'
    assertSame(processDefinition.getNode("s"), token.getNode());

    // Let's send another signal.  This will resume execution by
    // leaving the state 's' over its default transition.
    token.signal();
    // Now the signal method returned because the process instance
    // has arrived in the end-state.

    assertSame(processDefinition.getNode("end"), token.getNode());
}