第3章 Project deployment without Business Central

As an alternative to developing and deploying projects in the Business Central interface, you can use independent Maven projects or your own Java applications to develop Red Hat Decision Manager projects and deploy them in KIE containers (deployment units) to a configured Decision Server. You can then use the Decision Server REST API to start, stop, or remove the KIE containers that contain the services and their project versions that you have built and deployed. This flexibility enables you to continue to use your existing application workflow to develop business assets using Red Hat Decision Manager features.

Projects in Business Central are packaged automatically when you build and deploy the projects. For projects outside of Business Central, such as independent Maven projects or projects within a Java application, you must configure the KIE module descriptor settings in an appended kmodule.xml file or directly in your Java application in order to build and deploy the projects.

3.1. Configuring a KIE module descriptor file

A KIE module is a Maven project or module with an additional metadata file META-INF/kmodule.xml. All Red Hat Decision Manager projects require a kmodule.xml file in order to be properly packaged and deployed. This kmodule.xml file is a KIE module descriptor that defines the KIE base and KIE session configurations for the assets in a project. A KIE base is a repository that contains all rules and other business assets in Red Hat Decision Manager but does not contain any runtime data. A KIE session stores and executes runtime data and is created from a KIE base or directly from a KIE container if you have defined the KIE session in the kmodule.xml file.

If you create projects outside of Business Central, such as independent Maven projects or projects within a Java application, you must configure the KIE module descriptor settings in an appended kmodule.xml file or directly in your Java application in order to build and deploy the projects.

Procedure

  1. In the ~/resources/META-INF directory of your project, create a kmodule.xml metadata file with at least the following content:

    <?xml version="1.0" encoding="UTF-8"?>
    <kmodule xmlns="http://www.drools.org/xsd/kmodule">
    </kmodule>

    This empty kmodule.xml file is sufficient to produce a single default KIE base that includes all files found under your project resources path. The default KIE base also includes a single default KIE session that is triggered when you create a KIE container in your application at build time.

    The following example is a more advanced kmodule.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <kmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.drools.org/xsd/kmodule">
      <configuration>
          <property key="drools.evaluator.supersetOf" value="org.mycompany.SupersetOfEvaluatorDefinition"/>
      </configuration>
      <kbase name="KBase1" default="true" eventProcessingMode="cloud" equalsBehavior="equality" declarativeAgenda="enabled" packages="org.domain.pkg1">
        <ksession name="KSession1_1" type="stateful" default="true" />
        <ksession name="KSession1_2" type="stateful" default="true" beliefSystem="jtms" />
      </kbase>
      <kbase name="KBase2" default="false" eventProcessingMode="stream" equalsBehavior="equality" declarativeAgenda="enabled" packages="org.domain.pkg2, org.domain.pkg3" includes="KBase1">
        <ksession name="KSession2_1" type="stateless" default="true" clockType="realtime">
          <fileLogger file="debugInfo" threaded="true" interval="10" />
          <workItemHandlers>
            <workItemHandler name="name" type="new org.domain.WorkItemHandler()" />
          </workItemHandlers>
          <listeners>
            <ruleRuntimeEventListener type="org.domain.RuleRuntimeListener" />
            <agendaEventListener type="org.domain.FirstAgendaListener" />
            <agendaEventListener type="org.domain.SecondAgendaListener" />
            <processEventListener type="org.domain.ProcessListener" />
          </listeners>
        </ksession>
      </kbase>
    </kmodule>

    This example defines two KIE bases. Specific packages of rule assets are included with both KIE bases. When you specify packages in this way, you must organize your rule files in a folder structure that reflects the specified packages. Two KIE sessions are instantiated from the KBase1 KIE base, and one KIE session from KBase2. The KIE session from KBase2 is a stateless KIE session, which means that data from a previous invocation of the KIE session (the previous session state) is discarded between session invocations. That KIE session also specifies a file (or a console) logger, a WorkItemHandler, and listeners of the three supported types shown: ruleRuntimeEventListener, agendaEventListener and processEventListener. The <configuration> element defines optional properties that you can use to further customize your kmodule.xml file.

    As an alternative to manually appending a kmodule.xml file to your project, you can use a KieModuleModel instance within your Java application 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.

    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_1")
      .setDefault(true)
      .setType(KieSessionModel.KieSessionType.STATEFUL)
      .setClockType(ClockTypeOption.get("realtime"));
    
    KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.writeKModuleXML(kieModuleModel.toXML());

  2. After you configure the kmodule.xml file either manually or programmatically in your project, retrieve the KIE bases and KIE sessions from the KIE container to verify the configurations:

    KieServices kieServices = KieServices.Factory.get();
    KieContainer kContainer = kieServices.getKieClasspathContainer();
    
    KieBase kBase1 = kContainer.getKieBase("KBase1");
    KieSession kieSession1 = kContainer.newKieSession("KSession1_1"),
        kieSession2 = kContainer.newKieSession("KSession1_2");
    
    KieBase kBase2 = kContainer.getKieBase("KBase2");
    StatelessKieSession kieSession3 = kContainer.newStatelessKieSession("KSession2_1");

    If KieBase or KieSession have been configured as default="true" in the kmodule.xml file, as in the previous kmodule.xml example, you can retrieve them from the KIE container without passing any names:

    KieContainer kContainer = ...
    
    KieBase kBase1 = kContainer.getKieBase();
    KieSession kieSession1 = kContainer.newKieSession(),
        kieSession2 = kContainer.newKieSession();
    
    KieBase kBase2 = kContainer.getKieBase();
    StatelessKieSession kieSession3 = kContainer.newStatelessKieSession();

    To increase or decrease the maximum number of KIE modules or artifact versions that are cached in the decision engine, you can modify the values of the following system properties in your Red Hat Decision Manager distribution:

    • kie.repository.project.cache.size: Maximum number of KIE modules that are cached in the decision engine. Default value: 100
    • kie.repository.project.versions.cache.size: Maximum number of versions of the same artifact that are cached in the decision engine. Default value: 10

    For the full list of KIE repository configurations, download the Red Hat Decision Manager 7.5.1 Source Distribution ZIP file from the Red Hat Customer Portal and navigate to ~/rhdm-7.5.1-sources/src/drools-$VERSION/drools-compiler/src/main/java/org/drools/compiler/kie/builder/impl/KieRepositoryImpl.java.

For more information about the kmodule.xml file, download the Red Hat Decision Manager 7.5.1 Source Distribution ZIP file from the Red Hat Customer Portal (if not downloaded already) and see the kmodule.xsd XML schema located at $FILE_HOME/rhdm-$VERSION-sources/kie-api-parent-$VERSION/kie-api/src/main/resources/org/kie/api/.

注記

KieBase or KiePackage serialization is not supported in Red Hat Decision Manager 7.5. For more information, see Is serialization of kbase/package supported in BRMS 6/BPM Suite 6/RHDM 7?.

3.1.1. KIE module configuration properties

The optional <configuration> element in the KIE module descriptor file (kmodule.xml) of your project defines property key and value pairs that you can use to further customize your kmodule.xml file.

Example configuration property in a kmodule.xml file

<kmodule>
  ...
  <configuration>
    <property key="drools.dialect.default" value="java"/>
    ...
  </configuration>
  ...
</kmodule>

The following are the <configuration> property keys and values supported in the KIE module descriptor file (kmodule.xml) for your project:

drools.dialect.default

Sets the default Drools dialect.

Supported values: java, mvel

<property key="drools.dialect.default"
  value="java"/>
drools.accumulate.function.$FUNCTION

Links a class that implements an accumulate function to a specified function name, which allows you to add custom accumulate functions into the decision engine.

<property key="drools.accumulate.function.hyperMax"
  value="org.drools.custom.HyperMaxAccumulate"/>
drools.evaluator.$EVALUATION

Links a class that implements an evaluator definition to a specified evaluator name so that you can add custom evaluators into the decision engine. An evaluator is similar to a custom operator.

<property key="drools.evaluator.soundslike"
  value="org.drools.core.base.evaluators.SoundslikeEvaluatorsDefinition"/>
drools.dump.dir

Sets a path to the Red Hat Decision Manager dump/log directory.

<property key="drools.dump.dir"
  value="$DIR_PATH/dump/log"/>
drools.defaultPackageName

Sets a default package for the business assets in your project.

<property key="drools.defaultPackageName"
  value="org.domain.pkg1"/>
drools.parser.processStringEscapes

Sets the String escape function. If this property is set to false, the \n character will not be interpreted as the newline character.

Supported values: true (default), false

<property key="drools.parser.processStringEscapes"
  value="true"/>
drools.kbuilder.severity.$DUPLICATE

Sets a severity for instances of duplicate rules, processes, or functions reported when a KIE base is built. For example, if you set duplicateRule to ERROR, then an error is generated for any duplicated rules detected when the KIE base is built.

Supported key suffixes: duplicateRule, duplicateProcess, duplicateFunction

Supported values: INFO, WARNING, ERROR

<property key="drools.kbuilder.severity.duplicateRule"
  value="ERROR"/>
drools.propertySpecific

Sets the property reactivity of the decision engine.

Supported values: DISABLED, ALLOWED, ALWAYS

<property key="drools.propertySpecific"
  value="ALLOWED"/>
drools.lang.level

Sets the DRL language level.

Supported values: DRL5, DRL6, DRL6_STRICT (default)

<property key="drools.lang.level"
  value="DRL_STRICT"/>

3.1.2. KIE base attributes supported in KIE modules

A KIE base is a repository that you define in the KIE module descriptor file (kmodule.xml) for your project and contains all rules and other business assets in Red Hat Decision Manager. When you define KIE bases in the kmodule.xml file, you can specify certain attributes and values to further customize your KIE base configuration.

Example KIE base configuration in a kmodule.xml file

<kmodule>
  ...
  <kbase name="KBase2" default="false" eventProcessingMode="stream" equalsBehavior="equality" declarativeAgenda="enabled" packages="org.domain.pkg2, org.domain.pkg3" includes="KBase1" sequential="false">
    ...
  </kbase>
  ...
</kmodule>

The following are the kbase attributes and values supported in the KIE module descriptor file (kmodule.xml) for your project:

表3.1 KIE base attributes supported in KIE modules

AttributeSupported valuesDescription

name

Any name

Defines the name that retrieves KieBase from KieContainer. This attribute is mandatory.

includes

Comma-separated list of other KIE base objects in the KIE module

Defines other KIE base objects and artifacts to be included in this KIE base. A KIE base can be contained in multiple KIE modules if you declare it as a dependency in the pom.xml file of the modules.

packages

Comma-separated list of packages to include in the KIE base

Default: all

Defines packages of artifacts (such as rules and processes) to be included in this KIE base. By default, all artifacts in the ~/resources directory are included into a KIE base. This attribute enables you to limit the number of compiled artifacts. Only the packages belonging to the list specified in this attribute are compiled.

default

true, false

Default: false

Determines whether a KIE base is the default KIE base for a module so that it can be created from the KIE container without passing any name. Each module can have only one default KIE base.

equalsBehavior

identity, equality

Default: identity

Defines the behavior of Red Hat Decision Manager when a new fact is inserted into the working memory. If set to identity, a new FactHandle is always created unless the same object is already present in the working memory. If set to equality, a new FactHandle is created only if the newly inserted object is not equal to an existing fact, according to the equals() method of the inserted fact. Use equality mode when you want objects to be assessed based on feature equality instead of explicit identity.

eventProcessingMode

cloud, stream

Default: cloud

Determines how events are processed in the KIE base. If this property is set to cloud, the KIE base treats events as normal facts. If this property is set to stream, temporal reasoning on events is allowed.

declarativeAgenda

disabled, enabled

Default: disabled

Determines whether the declarative agenda is enabled or not.

sequential

true, false

Default: false

Determines whether sequential mode is enabled or not. In sequential mode, the decision engine evaluates rules one time in the order that they are listed in the decision engine agenda without regard to changes in the working memory. Enable this property if you use stateless KIE sessions and you do not want the execution of rules to influence subsequent rules in the agenda.

3.1.3. KIE session attributes supported in KIE modules

A KIE session stores and executes runtime data and is created from a KIE base or directly from a KIE container if you have defined the KIE session in the KIE module descriptor file (kmodule.xml) for your project. When you define KIE bases and KIE sessions in the kmodule.xml file, you can specify certain attributes and values to further customize your KIE session configuration.

Example KIE session configuration in a kmodule.xml file

<kmodule>
  ...
  <kbase>
    ...
    <ksession name="KSession2_1" type="stateless" default="true" clockType="realtime">
    ...
  </kbase>
  ...
</kmodule>

The following are the ksession attributes and values supported in the KIE module descriptor file (kmodule.xml) for your project:

表3.2 KIE session attributes supported in KIE modules

AttributeSupported valuesDescription

name

Any name

Defines the name that retrieves KieSession from KieContainer. This attribute is mandatory.

type

stateful, stateless

Default: stateful

Determines whether data is retained (stateful) or discarded (stateless) between invocations of the KIE session. A session set to stateful enables you to iteratively work with the working memory, while a session set to stateless is typically used for one-off execution of assets. A stateless session stores a knowledge state that is changed every time a new fact is added, updated, or deleted, and every time a rule is executed. An execution in a stateless session has no information about previous actions, such rule executions.

default

true, false

Default: false

Determines whether a KIE session is the default session for a module so that it can be created from the KIE container without passing any name. Each module can have only one default KIE session.

clockType

realtime, pseudo

Default: realtime

Determines whether event time stamps are assigned by the system clock or by a pseudo clock controlled by the application. This clock is especially useful for unit testing on temporal rules.

beliefSystem

simple, jtms, defeasible

Default: simple

Defines the type of belief system used by the KIE session. A belief system deduces the truth from knowledge (facts). For example, if a new fact is inserted based on another fact which is later removed from the decision engine, the system can determine that the newly inserted fact should be removed as well.