非同期ルール・セット実行を設定するには、コードでステートレス・セッションと実行要求を作成し、正準ルール・セット・パスを指定して、入力パラメーターを渡し、オブザーバーを作成する必要があります。
このタスクについて
実行イベントに応じて、ルール・セットを非同期に実行するように、ステートレス・セッションを設定できます。実行を非同期に設定すると、コードの残りの部分は、実行の結果を待機する必要がなくなります。これは、例えば、グラフィカル・ユーザー・インターフェース (GUI) で処理を行う場合などに便利です。そのような場合は、実行が進行している間も、GUI のアクションを実行できるようにする必要があるからです。これを実現するために、マルチスレッド化を手動でコーディングする必要はありません。実行を非同期に設定すれば、後は API が自動的に処理します。
API について詳しくは、ilog.rules.res.session.async パッケージを参照してください。
手順
非同期実行を設定するには、以下のようにします。
- ファクトリーを使用してステートレス・セッションを作成します。
IlrStatelessSession statelessSession = getSessionFactory().createStatelessSession();
- ファクトリーを使用して実行要求を作成します。
IlrSessionRequest request = sessionFactory.createRequest();
- ルール・セット・パスを指定します。
request.setRulesetPath(IlrPath.parsePath("/bigsequential/1.0/bigsequentialruleset/1.0"));
- 入力パラメーターを渡します。
request.setInputParameter("param1", "ruoa")
- オブザーバーを作成します。
オブザーバーは、実行の失敗時または終了時に呼び出されます。
IlrAsyncExecutionObserver myObserver = new IlrAsyncExecutionObserver() {
public void update(IlrAsyncExecutionEvent event) {
if (event instanceof IlrAsyncExecutionEndedEvent) {
IlrAsyncExecutionEndedEvent endedEvent = (IlrAsyncExecutionEndedEvent)event;
IlrSessionResponse response = endedEvent.getResponse();
assertNotNull("ended exec event references null response", response);
assertNotNull("expected param not found in response", response.getOutputParameters().get("param1"));
hasExecuted = true;
} else if (event instanceof IlrAsyncExecutionFailedEvent) {
IlrAsyncExecutionFailedEvent failedEvent = (IlrAsyncExecutionFailedEvent)event;
String msg = "observer got notified of an error, e=" + failedEvent.getException().getMessage();
fail(msg);
}
}
};
- オブザーバーを指定して、非同期で実行を開始します。
statelessSession.executeAsynchronous(request, myObserver, -1);
タスクの結果
ルール・セットを実行しても、コードはブロックされません。