이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 5. Designing the application logic for a Red Hat build of Kogito microservice using DMN
After you create your project, you can create or import Decision Model and Notation (DMN) decision models and Drools Rule Language (DRL) business rules in the src/main/resources folder of your project. You can also include Java classes in the src/main/java folder of your project that act as Java services or provide implementations that you call from your decisions.
The example for this procedure is a basic Red Hat build of Kogito microservice that provides a REST endpoint /persons. This endpoint is automatically generated based on an example PersonDecisions.dmn DMN model to make decisions based on the data being processed.
The business decision contains the decision logic of the Red Hat Decision Manager service. You can define business rules and decisions in different ways, such as with DMN models or DRL rules. The example for this procedure uses a DMN model.
Prerequisites
- You have created a project. For more information about creating a Maven project, see Chapter 3, Creating a Maven project for a Red Hat build of Kogito microservice.
Procedure
In the Maven project that you generated for your Red Hat Decision Manager service, navigate to the
src/main/java/org/acmefolder and add the followingPerson.javafile:Example person Java object
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example Java object sets and retrieves a person’s name, age, and adult status.
Navigate to the
src/main/resourcesfolder and add the followingPersonDecisions.dmnDMN decision model:Figure 5.1. Example
PersonDecisionsDMN decision requirements diagram (DRD)Figure 5.2. Example DMN boxed expression for
isAdultdecisionFigure 5.3. Example DMN data types
This example DMN model consists of a basic DMN input node and a decision node defined by a DMN decision table with a custom structured data type.
In VSCode, you can add the Red Hat Business Automation Bundle VSCode extension to design the decision requirements diagram (DRD), boxed expression, and data types with the DMN modeler.
To create this example DMN model quickly, you can copy the following
PersonDecisions.dmnfile content:Example DMN file
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To create this example DMN model in VSCode using the DMN modeler, follow these steps:
-
Open the empty
PersonDecisions.dmnfile, click the Properties icon in the upper-right corner of the DMN modeler, and confirm that the DMN model Name is set toPersonDecisions. -
In the left palette, select DMN Input Data, drag the node to the canvas, and double-click the node to name it
Person. -
In the left palette, drag the DMN Decision node to the canvas, double-click the node to name it
isAdult, and link to it from the input node. - Select the decision node to display the node options and click the Edit icon to open the DMN boxed expression editor to define the decision logic for the node.
- Click the undefined expression field and select Decision Table.
- Click the upper-left corner of the decision table to set the hit policy to Unique.
Set the input and output columns so that the input source
Person.Agewith typenumberdetermines the age limit and the output targetisAdultwith typebooleandetermines adult status:Figure 5.4. Example DMN decision table for
isAdultdecisionIn the upper tab options, select the Data Types tab and add the following
tPersonstructured data type and nested data types:Figure 5.5. Example DMN data types
- After you define the data types, select the Editor tab to return to the DMN modeler canvas.
-
Select the Person input node, click the Properties icon, and under Information item, set the Data type to
tPerson. -
Select the isAdult decision node, click the Properties icon, and under Information item, confirm that the Data type is still set to
boolean. You previously set this data type when you created the decision table. - Save the DMN decision file.
-
Open the empty
5.1. Using DRL rule units as an alternative decision service 링크 복사링크가 클립보드에 복사되었습니다!
You can also use a Drools Rule Language (DRL) file implemented as a rule unit to define this example decision service, as an alternative to using Decision Model and Notation (DMN).
A DRL rule unit is a module for rules and a unit of execution. A rule unit collects a set of rules with the declaration of the type of facts that the rules act on. A rule unit also serves as a unique namespace for each group of rules. A single rule base can contain multiple rule units. You typically store all the rules for a unit in the same file as the unit declaration so that the unit is self-contained. For more information about rule units, see Designing a decision service using DRL rules.
Prerequisites
- You have created a project. For more information about creating a Maven project, see Chapter 3, Creating a Maven project for a Red Hat build of Kogito microservice.
Procedure
In the
src/main/resourcesfolder of your example project, instead of using a DMN file, add the followingPersonRules.drlfile:Example
PersonRulesDRL fileCopy to Clipboard Copied! Toggle word wrap Toggle overflow This example rule determines that any person who is older than 18 is classified as an adult. The rule file also declares that the rule belongs to the rule unit
PersonRules. When you build the project, the rule unit is generated and associated with the DRL file.The rule also defines the condition using OOPath notation. OOPath is an object-oriented syntax extension to XPath for navigating through related elements while handling collections and filtering constraints.
You can also rewrite the same rule condition in a more explicit form using the traditional rule pattern syntax, as shown in the following example:
Example
PersonRulesDRL file using traditional notationCopy to Clipboard Copied! Toggle word wrap Toggle overflow