3.3. Packaging and deploying a Red Hat Decision Manager project in a Java application
If you want to deploy a project from within your own Java application to a configured Decision Server, you can use a KieModuleModel instance to programatically create a kmodule.xml file that defines the KIE base and a KIE session, and then add all resources in your project to the KIE virtual file system KieFileSystem.
Prerequisites
- You have a Java application that contains Red Hat Decision Manager business assets.
-
Decision Server is installed and
kie-serveruser access is configured. For installation options, see Planning a Red Hat Decision Manager installation.
Procedure
In your client application, add the following dependencies to the relevant classpath of your Java project to generate an executable rule model from your rule assets:
-
drools-canonical-model: Enables an executable canonical representation of a rule set model that is independent from Red Hat Decision Manager -
drools-model-compiler: Compiles the executable model into Red Hat Decision Manager internal data structures so that it can be executed by the decision engine
<dependency> <groupId>org.drools</groupId> <artifactId>drools-canonical-model</artifactId> <version>${rhdm.version}</version> </dependency> <dependency> <groupId>org.drools</groupId> <artifactId>drools-model-compiler</artifactId> <version>${rhdm.version}</version> </dependency>Executable rule models are embeddable models that provide a Java-based representation of a rule set for execution at build time. The executable model is a more efficient alternative to the standard asset packaging in Red Hat Decision Manager and enables KIE containers and KIE bases to be created more quickly, especially when you have large lists of DRL (Drools Rule Language) files and other Red Hat Decision Manager assets.
For more information about executable rule models, see Designing a decision service using DRL rules.
To enable Decision Model and Notation (DMN) executable models for DMN assets in your project (if applicable), also add the
kie-dmn-coredependency in thepom.xmlfile:<dependency> <groupId>org.kie</groupId> <artifactId>kie-dmn-core</artifactId> <scope>provided</scope> <version>${rhdm.version}</version> </dependency>The
<version>is the Maven artifact version for Red Hat Decision Manager currently used in your project (for example, 7.26.0.Final-redhat-00005).注記Instead of specifying a Red Hat Decision Manager
<version>for individual dependencies, consider adding the Red Hat Business Automation bill of materials (BOM) dependency to your projectpom.xmlfile. The Red Hat Business Automation BOM applies to both Red Hat Decision Manager and Red Hat Process Automation Manager. When you add the BOM files, the correct versions of transitive dependencies from the provided Maven repositories are included in the project.Example BOM dependency:
<dependency> <groupId>com.redhat.ba</groupId> <artifactId>ba-platform-bom</artifactId> <version>7.5.1.redhat-00001</version> <scope>import</scope> <type>pom</type> </dependency>For more information about the Red Hat Business Automation BOM, see What is the mapping between Red Hat Decision Manager and the Maven library version?.
-
Use the
KieServicesAPI to create aKieModuleModelinstance with the desired KIE base and KIE session. TheKieServicesAPI enables you to access all KIE building and runtime configurations. TheKieModuleModelinstance generates thekmodule.xmlfile for your project.For more information about
kmodule.xmlconfiguration, see 「Configuring a KIE module descriptor file」.Convert your
KieModuleModelinstance into XML and add the XML toKieFileSystem.Creating
kmodule.xmlprogrammatically and adding it toKieFileSystemimport org.kie.api.KieServices; import org.kie.api.builder.model.KieModuleModel; import org.kie.api.builder.model.KieBaseModel; import org.kie.api.builder.model.KieSessionModel; import org.kie.api.builder.KieFileSystem; KieServices kieServices = KieServices.Factory.get(); KieModuleModel kieModuleModel = kieServices.newKieModuleModel(); KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("KBase1") .setDefault(true) .setEqualsBehavior(EqualityBehaviorOption.EQUALITY) .setEventProcessingMode(EventProcessingOption.STREAM); KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel("KSession1") .setDefault(true) .setType(KieSessionModel.KieSessionType.STATEFUL) .setClockType(ClockTypeOption.get("realtime")); KieFileSystem kfs = kieServices.newKieFileSystem(); kfs.writeKModuleXML(kieModuleModel.toXML());Add any remaining Red Hat Decision Manager assets that you use in your project to your
KieFileSysteminstance. The artifacts must be in a Maven project file structure.import org.kie.api.builder.KieFileSystem; KieFileSystem kfs = ... kfs.write("src/main/resources/KBase1/ruleSet1.drl", stringContainingAValidDRL) .write("src/main/resources/dtable.xls", kieServices.getResources().newInputStreamResource(dtableFileStream));In this example, the project assets are added both as a
Stringvariable and as aResourceinstance. You can create theResourceinstance using theKieResourcesfactory, also provided by theKieServicesinstance. TheKieResourcesclass provides factory methods to convertInputStream,URL, andFileobjects, or aStringrepresenting a path of your file system to aResourceinstance that theKieFileSystemcan manage.You can also explicitly assign a
ResourceTypeproperty to aResourceobject when you add project artifacts toKieFileSystem:import org.kie.api.builder.KieFileSystem; KieFileSystem kfs = ... kfs.write("src/main/resources/myDrl.txt", kieServices.getResources().newInputStreamResource(drlStream) .setResourceType(ResourceType.DRL));Use
KieBuilderwithbuildAll( ExecutableModelProject.class )specified to build the content ofKieFileSystemfrom an executable model, and create a KIE container to deploy it:import org.kie.api.KieServices; import org.kie.api.KieServices.Factory; import org.kie.api.builder.KieFileSystem; import org.kie.api.builder.KieBuilder; import org.kie.api.runtime.KieContainer; KieServices kieServices = KieServices.Factory.get(); KieFileSystem kfs = ... KieBuilder kieBuilder = ks.newKieBuilder( kfs ); // Build from an executable model kieBuilder.buildAll( ExecutableModelProject.class ) assertEquals(0, kieBuilder.getResults().getMessages(Message.Level.ERROR).size()); KieContainer kieContainer = kieServices .newKieContainer(kieServices.getRepository().getDefaultReleaseId());After
KieFileSystemis built from the executable model, the resultingKieSessionuses constraints based on lambda expressions instead of less-efficientmvelexpressions. To build the project in the standard method without an executable model, do not specify any argument inbuildAll().A build
ERRORindicates that the project compilation failed, noKieModulewas produced, and nothing was added to theKieRepositorysingleton. AWARNINGor anINFOresult indicates that the compilation of the project was successful, with information about the build process.