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

Procedure

  1. 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 project pom.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?.

  2. Use the KieServices API to create a KieModuleModel instance with the desired KIE base and KIE session. The KieServices API enables you to access all KIE building and runtime configurations. The KieModuleModel instance generates the kmodule.xml file for your project.

    For more information about kmodule.xml configuration, see 「Configuring a KIE module descriptor file」.

  3. Convert your KieModuleModel instance into XML and add the XML to KieFileSystem.

    Creating kmodule.xml programmatically and adding it to KieFileSystem

    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());

  4. 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 a Resource instance. You can create the Resource instance using the KieResources factory, also provided by the KieServices instance. The KieResources class provides factory methods to convert InputStream, URL, and File objects, or a String representing a path of your file system to a Resource instance that the KieFileSystem can manage.

    You can also explicitly assign a ResourceType property to a Resource object when you add project artifacts to KieFileSystem:

    import org.kie.api.builder.KieFileSystem;
    
      KieFileSystem kfs = ...
      kfs.write("src/main/resources/myDrl.txt",
        kieServices.getResources().newInputStreamResource(drlStream)
          .setResourceType(ResourceType.DRL));
  5. Use KieBuilder with the buildAll() method to build the content of KieFileSystem, 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, no KieModule was produced, and nothing was added to the KieRepository singleton. A WARNING or an INFO 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」.