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 KIE Server, you can use a KieModuleModel
instance to programmatically 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.
-
KIE Server is installed and
kie-server
user access is configured. For installation options, see Planning a Red Hat Decision Manager installation.
Procedure
Optional: If your project contains Decision Model and Notation (DMN) assets, add the following dependency to the relevant class path of your Java project to enable DMN executable models. DMN executable models enable DMN decision table logic in DMN projects to be evaluated more efficiently.
<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.48.0.Final-redhat-00004).注記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.xml
file. 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.10.0.redhat-00004</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
KieServices
API to create aKieModuleModel
instance with the desired KIE base and KIE session. TheKieServices
API enables you to access all KIE building and runtime configurations. TheKieModuleModel
instance generates thekmodule.xml
file for your project.For more information about
kmodule.xml
configuration, see 「Configuring a KIE module descriptor file」.Convert your
KieModuleModel
instance into XML and add the XML toKieFileSystem
.Creating
kmodule.xml
programmatically and adding it toKieFileSystem
import 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
KieFileSystem
instance. 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
String
variable and as aResource
instance. You can create theResource
instance using theKieResources
factory, also provided by theKieServices
instance. TheKieResources
class provides factory methods to convertInputStream
,URL
, andFile
objects, or aString
representing a path of your file system to aResource
instance that theKieFileSystem
can manage.You can also explicitly assign a
ResourceType
property to aResource
object 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
KieBuilder
with thebuildAll()
method to build the content ofKieFileSystem
, 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 ); kieBuilder.buildAll() assertEquals(0, kieBuilder.getResults().getMessages(Message.Level.ERROR).size()); KieContainer kieContainer = kieServices .newKieContainer(kieServices.getRepository().getDefaultReleaseId());
A build
ERROR
indicates that the project compilation failed, noKieModule
was produced, and nothing was added to theKieRepository
singleton. AWARNING
or anINFO
result indicates that the compilation of the project was successful, with information about the build process.注記To build the rule assets in your Java application project from an executable rule model, verify that the following dependency is in the
pom.xml
file of your project:<dependency> <groupId>org.drools</groupId> <artifactId>drools-model-compiler</artifactId> <version>${rhdm.version}</version> </dependency>
This dependency is required for rule assets in Red Hat Decision Manager to be built from executable rule models. This dependency is included as part of the Red Hat Decision Manager core packaging, but depending on your Red Hat Decision Manager upgrade history, you may need to manually add this dependency to enable the executable rule model behavior.
After you verify the dependency, use the following modified
buildAll()
option to enable the executable model:kieBuilder.buildAll(ExecutableModelProject.class)
For more information about executable rule models, see 「Executable rule models」.