Chapter 19. Migration of a DRL service to a Red Hat build of Kogito microservice
You can build and deploy a sample project in Red Hat build of Kogito to expose a stateless rules evaluation of the decision engine in a Red Hat build of Quarkus REST endpoint, and migrate the REST endpoint to Red Hat build of Kogito.
The stateless rule evaluation is a single execution of a rule set in Red Hat Process Automation Manager and can be identified as a function invocation. In the invoked function, the output values are determined using the input values. Also, the invoked function uses the decision engine to perform the jobs. Therefore, in such cases, a function is exposed using a REST endpoint and converted into a microservice. After converting into a microservice, a function is deployed into a Function as a Service environment to eliminate the cost of JVM startup time.
19.1. Major changes and migration considerations Copy linkLink copied to clipboard!
The following table describes the major changes and features that affect migration from the KIE Server API and KJAR to Red Hat build of Kogito deployments:
| Feature | In KIE Server API | In Red Hat build of Kogito with legacy API support | In Red Hat build of Kogito artifact |
|---|---|---|---|
| DRL files |
stored in |
copy as is to | rewrite using the rule units and OOPath. |
|
|
configured using a system property or |
replaced by | not required. |
|
|
configured using a system property or |
configured using a system property or | replaced by rule units. |
19.2. Migration strategy Copy linkLink copied to clipboard!
In Red Hat Process Automation Manager, you can migrate a rule evaluation to a Red Hat build of Kogito deployment in the following two ways:
- Using legacy API in Red Hat build of Kogito
-
In Red Hat build of Kogito, the
kogito-legacy-apimodule makes the legacy API of Red Hat Process Automation Manager available; therefore, the DRL files remain unchanged. This approach of migrating rule evaluation requires minimal changes and enables you to use major Red Hat build of Quarkus features, such as hot reload and native image creation. - Migrating to Red Hat build of Kogito rule units
Migrating to Red Hat build of Kogito rule units include the programming model of Red Hat build of Kogito, which is based on the concept of rule units.
A rule unit in Red Hat build of Kogito includes both a set of rules and the facts, against which the rules are matched. Rule units in Red Hat build of Kogito also come with data sources. A rule unit data source is a source of the data processed by a given rule unit and represents the entry point, which is used to evaluate the rule unit. Rule units use two types of data sources:
-
DataStream: This is an append-only data source and the facts added into theDataStreamcannot be updated or removed. -
DataStore: This data source is for modifiable data. You can update or remove an object using theFactHandlethat is returned when the object is added into theDataStore.
Overall, a rule unit contains two parts: The definition of the fact to be evaluated and the set of rules evaluating the facts.
-
19.3. Example loan application project Copy linkLink copied to clipboard!
In the following sections, a loan application project is used as an example to migrate a DRL project to Red Hat build of Kogito deployments. The domain model of the loan application project is made of two classes, the LoanApplication class and the Applicant class:
Example LoanApplication class
Example Applicant class
The rule set is created using business decisions to approve or reject an application, along with the last rule of collecting all the approved applications in a list.
Example rule set in loan application
19.3.1. Exposing rule evaluation with a REST endpoint using Red Hat build of Quarkus Copy linkLink copied to clipboard!
You can expose the rule evaluation that is developed in Business Central with a REST endpoint using Red Hat build of Quarkus.
Procedure
Create a new module based on the module that contains the rules and Quarkus libraries, providing the REST support:
Example dependencies for creating a new module
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create a REST endpoint.
The following is an example setup for creating a REST endpoint:
Example
FindApprovedLoansEndpointendpoint setupCopy to Clipboard Copied! Toggle word wrap Toggle overflow In the previous example, a
KieContainercontaining the rules is created and added into a static field. The rules in theKieContainerare obtained from the other module in the class path. Using this approach, you can reuse the sameKieContainerfor subsequent invocations related to theFindApprovedLoansEndpointendpoint without recompiling the rules.NoteThe two modules are consolidated in the next process of migrating rule units to a Red Hat build of Kogito microservice using legacy API. For more information, see Migrating DRL rules units to Red Hat build of Kogito microservice using legacy API.
When the
FindApprovedLoansEndpointendpoint is invoked, a newKieSessionis created from theKieContainer. TheKieSessionis populated with the objects fromLoanAppDtoresulting from the unmarshalling of a JSON request.Example
LoanAppDtoclassCopy to Clipboard Copied! Toggle word wrap Toggle overflow When the
fireAllRules()method is called,KieSessionis fired and the business logic is evaluated against the input data. After business logic evaluation, the last rule collects all the approved applications in a list and the same list is returned as an output.- Start the Red Hat build of Quarkus application.
Invoke the
FindApprovedLoansEndpointendpoint with a JSON request that contains the loan applications to be checked.The value of the
maxAmountis used in the rules as shown in the following example:Example curl request
curl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"maxAmount":5000, "loanApplications":[ {"id":"ABC10001","amount":2000,"deposit":1000,"applicant":{"age":45,"name":"John"}}, {"id":"ABC10002","amount":5000,"deposit":100,"applicant":{"age":25,"name":"Paul"}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George"}} ]}' http://localhost:8080/find-approvedcurl -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -d '{"maxAmount":5000, "loanApplications":[ {"id":"ABC10001","amount":2000,"deposit":1000,"applicant":{"age":45,"name":"John"}}, {"id":"ABC10002","amount":5000,"deposit":100,"applicant":{"age":25,"name":"Paul"}}, {"id":"ABC10015","amount":1000,"deposit":100,"applicant":{"age":12,"name":"George"}} ]}' http://localhost:8080/find-approvedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example JSON response
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Using this approach, you cannot use the hot reload feature and cannot create a native image of the project. In the next steps, the missing Quarkus features are provided by the Kogito extension that enables Quarkus aware of the DRL files and implement the hot reload feature in a similar way.
19.3.2. Migrating a rule evaluation to a Red Hat build of Kogito microservice using legacy API Copy linkLink copied to clipboard!
After exposing a rule evaluation with a REST endpoint, you can migrate the rule evaluation to a Red Hat build of Kogito microservice using legacy API.
Procedure
Add the following dependencies to the project
pom.xmlfile to enable the use of Red Hat build of Quarkus and legacy API:Example dependencies for using Quarkus and legacy API
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Rewrite the REST endpoint implementation:
Example REST endpoint implementation
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the rewritten REST endpoint implementation, instead of creating the
KieSessionfrom theKieContainer, theKieSessionis created automatically using an integratedKieRuntimeBuilder.The
KieRuntimeBuilderis an interface provided by thekogito-legacy-apimodule that replaces theKieContainer. UsingKieRuntimeBuilder, you can createKieBasesandKieSessionsin a similar way you create inKieContainer. Red Hat build of Kogito automatically generates an implementation ofKieRuntimeBuilderinterface at compile time and integrates theKieRuntimeBuilderinto a class, which implements theFindApprovedLoansEndpointREST endpoint.Start your Red Hat build of Quarkus application in development mode.
You can also use the hot reload to make the changes to the rules files that are applied to the running application. Also, you can create a native image of your rule based application.
19.3.3. Implementing rule units and automatic REST endpoint generation Copy linkLink copied to clipboard!
After migrating rule units to a Red Hat build of Kogito microservice, you can implement the rule units and automatic generation of the REST endpoint.
In Red Hat build of Kogito, a rule unit contains a set of rules and the facts, against which the rules are matched. Rule units in Red Hat build of Kogito also come with data sources. A rule unit data source is a source of the data processed by a given rule unit and represents the entry point, which is used to evaluate the rule unit. Rule units use two types of data sources:
-
DataStream: This is an append-only data source. InDataStream, subscribers receive new and past messages, stream can be hot or cold in the reactive streams. Also, the facts added into theDataStreamcannot be updated or removed. -
DataStore: This data source is for modifiable data. You can update or remove an object using theFactHandlethat is returned when the object is added into theDataStore.
Overall, a rule unit contains two parts: the definition of the fact to be evaluated and the set of rules evaluating the facts.
Procedure
Implement a fact definition using POJO:
Example implementation of a fact definition using POJO
Copy to Clipboard Copied! Toggle word wrap Toggle overflow In the previous example, instead of using
LoanAppDtotheLoanUnitclass is bound directly.LoanAppDtois used to marshall or unmarshall JSON requests. Also, the previous example implements theorg.kie.kogito.rules.RuleUnitDatainterface and uses aDataStoreto contain the loan applications to be approved.The
org.kie.kogito.rules.RuleUnitDatais a marker interface to notify the decision engine thatLoanUnitclass is part of a rule unit definition. In addition, theDataStoreis responsible to allow the rule engine to react on the changes by firing new rules and triggering other rules.Additionally, the consequences of the rules modify the
approvedproperty in the previous example. On the contrary, themaxAmountvalue is considered as a configuration parameter for the rule unit, which is not modified. ThemaxAmountis processed automatically during the rules evaluation and automatically set from the value passed in the JSON requests.Implement a DRL file:
Example implementation of a DRL file
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The DRL file that you create must declare the same package as fact definition implementation and a unit with the same name of the Java class. The Java class implements the
RuleUnitDatainterface to state that the interface belongs to the same rule unit.Also, the DRL file in the previous example is rewritten using the OOPath expressions. In the DRL file, the data source acts as an entry point and the OOPath expression contains the data source name as root. However, the constraints are added in square brackets as follows:
$l: /loanApplications[ applicant.age >= 20, deposit >= 1000, amount ⇐ maxAmount ]Alternatively, you can use the standard DRL syntax, in which you can specify the data source name as an entry point. However, you need to specify the type of the matched object again as shown in the following example, even if the decision engine can infer the type from the data source:
$l: LoanApplication( applicant.age >= 20, deposit >= 1000, amount ⇐ maxAmount ) from entry-point loanApplicationsIn the previous example, the last rule that collects all the approved loan applications is replaced by a query that retrieves the list. A rule unit defines the facts to be passed in input to evaluate the rules, and the query defines the expected output from the rule evaluation. Using this approach, Red Hat build of Kogito can automatically generate a class that executes the query and returns the output as shown in the following example:
Example
LoanUnitQueryFindApprovedclassCopy to Clipboard Copied! Toggle word wrap Toggle overflow The following is an example of a REST endpoint that takes a rule unit as input and passing the input to a query executor to return the output:
Example
LoanUnitQueryFindApprovedEndpointendpointCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteYou can also add multiple queries and for each query, a different REST endpoint is generated. For example, the
FindApprovedREST endpoint is generated for find-approved.