第5章 ルールの実行
Decision Central でルールを作成したら、プロジェクトをビルドしてデプロイし、ローカルまたは Decision Server でルールを実行してテストできます。
手順
-
Decision Central で、Menu
Design Projects に移動して、プロジェクト名をクリックします。 - 右上にある Build をクリックしてから Deploy をクリックして、プロジェクトをビルドして Decision Server にデプロイします。ビルドに失敗したら、画面下部の Alerts パネルに説明されている問題に対処します。プロジェクトをデプロイする方法は「Red Hat Decision Manager プロジェクトのパッケージングおよびデプロイメント」を参照してください。
クライアントアプリケーションの
pom.xmlファイルを開き、以下の依存関係が追加されていない場合は追加します。-
kie-ci: クライアントアプリケーションで、ReleaseIdを使用して、Decision Central プロジェクトデータをローカルにロードします。 -
kie-server-client: クライアントアプリケーションで、Decision Server のアセットを使用してリモートに接続します。 -
slf4j: (オプション) クライアントアプリケーションで、Decision Server に接続したあと、SLF4J (Simple Logging Facade for Java) を使用して、デバッグのログ情報を返します。
クライアントアプリケーションの
pom.xmlファイルにおける、Red Hat Decision Manager 7.1 の依存関係の例// For local execution: <dependency> <groupId>org.kie</groupId> <artifactId>kie-ci</artifactId> <version>7.11.0.Final-redhat-00002</version> </dependency> // For remote execution on Decision Server: <dependency> <groupId>org.kie.server</groupId> <artifactId>kie-server-client</artifactId> <version>7.11.0.Final-redhat-00002</version> </dependency> // For debug logging (optional): <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.25</version> </dependency>このアーティファクトで利用可能なバージョンについては、オンラインの Nexus Repository Manager でグループ ID とアーティファクト ID を検索してください。
注記個別の依存関係に対して Red Hat Decision Manager
<version>を指定するのではなく、Red Hat Business Automation 部品表 (BOM) の依存関係をプロジェクトのpom.xmlファイルに追加することを検討してください。Red Hat Business Automation BOM は、Red Hat Decision Manager と Red Hat Process Automation Manager の両方に適用します。BOM ファイルを追加すると、指定の Maven リポジトリーからの一時的な依存関係の内、正しいバージョンが、このプロジェクトに追加されます。BOM 依存関係の例:
<dependency> <groupId>com.redhat.ba</groupId> <artifactId>ba-platform-bom</artifactId> <version>7.1.0.GA-redhat-00002</version> <scope>import</scope> <type>pom</type> </dependency>Red Hat Business Automation BOM (Bill of Materials) についての詳細情報は、What is the mapping between Red Hat Decision Manager and the Maven library version? を参照してください。
-
モジュールクラスを含むアーティファクトの依存関係が、クライアントアプリケーションの
pom.xmlファイルに定義されていて、デプロイしたプロジェクトのpom.xmlファイルに記載されているのと同じであることを確認します。モデルクラスの依存関係が、クライアントアプリケーションとプロジェクトで異なると、実行エラーが発生します。Decision Central でプロジェクト
pom.xmlを表示するには、プロジェクトで既存のアセットを選択し、画面左側の Project Explorer メニューで Customize View ギアアイコンをクリックし、Repository Viewpom.xml を選択します。 たとえば、以下はクライアントと、デプロイしたプロジェクトの両方の
pom.xmlファイルに表示されるため、Personクラスの依存関係です。<dependency> <groupId>com.sample</groupId> <artifactId>Person</artifactId> <version>1.0.0</version> </dependency>デバッグ向けロギングを行うために、
slf4j依存関係を、クライアントアプリケーションのpom.xmlファイルに追加した場合は、関連するクラスパス (Maven のsrc/main/resources/META-INF内など) にsimplelogger.propertiesファイルを作成し、以下の内容を記載します。org.slf4j.simpleLogger.defaultLogLevel=debugクライアントアプリケーションに、必要なインポートを含む
.javaメインクラスと、KIE ベースをロードするmain()メソッドを作成し、ファクトを挿入し、ルールを実行します。たとえば、プロジェクトの
Personオブジェクトには、名前、苗字、時給、賃金を設定および取得するゲッターメソッドおよびセッターメソッドが含まれます。プロジェクトにある以下のWageルールでは、賃金と時給を計算し、その結果に基づいてメッセージを表示します。package com.sample; import com.sample.Person; dialect "java" rule "Wage" when Person(hourlyRate * wage > 100) Person(name : firstName, surname : lastName) then System.out.println("Hello" + " " + name + " " + surname + "!"); System.out.println("You are rich!"); end(必要に応じて) Decision Server の外でローカルにこのルールをテストするには、
.javaクラスで、KIE サービス、KIE コンテナー、および KIE セッションをインポートするように設定し、その後、main()メソッドを使用して、定義したファクトモデルに対してすべてのルールを実行するようにします。ローカルでのルールの実行
import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; public class RulesTest { public static final void main(String[] args) { try { // Identify the project in the local repository: ReleaseId rid = new ReleaseId(); rid.setGroupId("com.myspace"); rid.setArtifactId("MyProject"); rid.setVersion("1.0.0"); // Load the KIE base: KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.newKieContainer(rid); KieSession kSession = kContainer.newKieSession(); // Set up the fact model: Person p = new Person(); p.setWage(12); p.setFirstName("Tom"); p.setLastName("Summers"); p.setHourlyRate(10); // Insert the person into the session: kSession.insert(p); // Fire all rules: kSession.fireAllRules(); kSession.dispose(); } catch (Throwable t) { t.printStackTrace(); } } }Decision Server でこのルールをテストするには、ローカル例と同じように、インポートとルール実行情報で
.javaクラスを設定し、KIE サービス設定および KIE サービスクライアントの詳細を指定します。Decision Server でのルールの実行
package com.sample; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import org.kie.api.command.BatchExecutionCommand; import org.kie.api.command.Command; import org.kie.api.KieServices; import org.kie.api.runtime.ExecutionResults; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import org.kie.server.api.marshalling.MarshallingFormat; import org.kie.server.api.model.ServiceResponse; import org.kie.server.client.KieServicesClient; import org.kie.server.client.KieServicesConfiguration; import org.kie.server.client.KieServicesFactory; import org.kie.server.client.RuleServicesClient; import com.sample.Person; public class RulesTest { private static final String containerName = "testProject"; private static final String sessionName = "myStatelessSession"; public static final void main(String[] args) { try { // Define KIE services configuration and client: Set<Class<?>> allClasses = new HashSet<Class<?>>(); allClasses.add(Person.class); String serverUrl = "http://$HOST:$PORT/kie-server/services/rest/server"; String username = "$USERNAME"; String password = "$PASSWORD"; KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(serverUrl, username, password); config.setMarshallingFormat(MarshallingFormat.JAXB); config.addExtraClasses(allClasses); KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(config); // Set up the fact model: Person p = new Person(); p.setWage(12); p.setFirstName("Tom"); p.setLastName("Summers"); p.setHourlyRate(10); // Insert Person into the session: KieCommands kieCommands = KieServices.Factory.get().getCommands(); List<Command> commandList = new ArrayList<Command>(); commandList.add(kieCommands.newInsert(p, "personReturnId")); // Fire all rules: commandList.add(kieCommands.newFireAllRules("numberOfFiredRules")); BatchExecutionCommand batch = kieCommands.newBatchExecution(commandList, sessionName); // Use rule services client to send request: RuleServicesClient ruleClient = kieServicesClient.getServicesClient(RuleServicesClient.class); ServiceResponse<ExecutionResults> executeResponse = ruleClient.executeCommandsWithResults(containerName, batch); System.out.println("number of fired rules:" + executeResponse.getResult().getValue("numberOfFiredRules")); } catch (Throwable t) { t.printStackTrace(); } } }設定した
.javaクラスをプロジェクトディレクトリーから実行します。(Red Hat JBoss Developer Studio などの) 開発プラットフォーム、またはコマンドラインでファイルを実行できます。(プロジェクトディレクトリーにおける) Maven の実行例:
mvn clean install exec:java -Dexec.mainClass="com.sample.app.RulesTest"(プロジェクトディレクトリーにおける) Java の実行例:
javac -classpath "./$DEPENDENCIES/*:." RulesTest.java java -classpath "./$DEPENDENCIES/*:." RulesTest- コマンドラインおよびサーバーログで、ルール実行のステータスを確認します。ルールが期待通りに実行しない場合は、プロジェクトに設定したルールと、メインのクラス設定を確認して、提供されるデータの妥当性を確認します。