此内容没有您所选择的语言版本。
Chapter 4. DMN model execution
You can create or import DMN files in your Red Hat Process Automation Manager project using Business Central or package the DMN files as part of your project knowledge JAR (KJAR) file without Business Central. After you implement your DMN files in your Red Hat Process Automation Manager project, you can execute the DMN decision service by deploying the KIE container that contains it to Process Server for remote access or by manipulating the KIE container directly as a dependency of the calling application. Other options for creating and deploying DMN knowledge packages are also available, and most are similar for all types of knowledge assets, such as DRL files or process definitions.
For information about including external DMN assets with your project packaging and deployment method, see Packaging and deploying a Red Hat Process Automation Manager project.
4.1. Embedding a DMN call directly in a Java application 复制链接链接已复制到粘贴板!
A KIE container is local when the knowledge assets are either embedded directly into the calling program or are physically pulled in using Maven dependencies for the KJAR. You typically embed knowledge assets directly into a project if there is a tight relationship between the version of the code and the version of the DMN definition. Any changes to the decision take effect after you have intentionally updated and redeployed the application. A benefit of this approach is that proper operation does not rely on any external dependencies to the run time, which can be a limitation of locked-down environments.
Using Maven dependencies enables further flexibility because the specific version of the decision can dynamically change, (for example, by using a system property), and it can be periodically scanned for updates and automatically updated. This introduces an external dependency on the deploy time of the service, but executes the decision locally, reducing reliance on an external service being available during run time.
Prerequisites
-
Process Server is installed and configured, including a known user name and credentials for a user with the
kie-server
role. For installation options, see Planning a Red Hat Process Automation Manager installation. You have built the DMN project as a KJAR artifact and deployed it to Process Server. Ideally, you have built the DMN project as an executable model for more efficient execution:
mvn clean install -DgenerateDMNModel=yes
mvn clean install -DgenerateDMNModel=yes
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about project packaging and deployment and executable models, see Packaging and deploying a Red Hat Process Automation Manager project.
Procedure
In your client application, add the following dependencies to the relevant classpath of your Java project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
<version>
is the Maven artifact version for Red Hat Process Automation Manager currently used in your project (for example, 7.30.0.Final-redhat-00003).NoteInstead of specifying a Red Hat Process Automation 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the Red Hat Business Automation BOM, see What is the mapping between RHPAM product and maven library version?.
Create a KIE container from
classpath
orReleaseId
:KieServices kieServices = KieServices.Factory.get(); ReleaseId releaseId = kieServices.newReleaseId( "org.acme", "my-kjar", "1.0.0" ); KieContainer kieContainer = kieServices.newKieContainer( releaseId );
KieServices kieServices = KieServices.Factory.get(); ReleaseId releaseId = kieServices.newReleaseId( "org.acme", "my-kjar", "1.0.0" ); KieContainer kieContainer = kieServices.newKieContainer( releaseId );
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Alternative option:
KieServices kieServices = KieServices.Factory.get(); KieContainer kieContainer = kieServices.getKieClasspathContainer();
KieServices kieServices = KieServices.Factory.get(); KieContainer kieContainer = kieServices.getKieClasspathContainer();
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Obtain
DMNRuntime
from the KIE container and a reference to the DMN model to be evaluated, by using the modelnamespace
andmodelName
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Execute the decision services for the desired model:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Instantiate a new DMN Context to be the input for the model evaluation. Note that this example is looping through the Age Classification decision multiple times.
- 2
- Assign input variables for the input DMN context.
- 3
- Evaluate all DMN decisions defined in the DMN model.
- 4
- Each evaluation may result in one or more results, creating the loop.
This example prints the following output:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the DMN model was not previously compiled as an executable model for more efficient execution, you can enable the following property when you execute your DMN models:
-Dorg.kie.dmn.compiler.execmodel=true
-Dorg.kie.dmn.compiler.execmodel=true
Copy to Clipboard Copied! Toggle word wrap Toggle overflow
The Process Server Java client API provides a lightweight approach to invoking a remote DMN service either through the REST or JMS interfaces of Process Server. This approach reduces the number of runtime dependencies necessary to interact with a KIE base. Decoupling the calling code from the decision definition also increases flexibility by enabling them to iterate independently at the appropriate pace.
For more information about the Process Server Java client API, see Interacting with Red Hat Process Automation Manager using KIE APIs.
Prerequisites
-
Process Server is installed and configured, including a known user name and credentials for a user with the
kie-server
role. For installation options, see Planning a Red Hat Process Automation Manager installation. You have built the DMN project as a KJAR artifact and deployed it to Process Server. Ideally, you have built the DMN project as an executable model for more efficient execution:
mvn clean install -DgenerateDMNModel=yes
mvn clean install -DgenerateDMNModel=yes
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about project packaging and deployment and executable models, see Packaging and deploying a Red Hat Process Automation Manager project.
- You have the ID of the KIE container containing the DMN model. If more than one model is present, you must also know the model namespace and model name of the relevant model.
Procedure
In your client application, add the following dependency to the relevant classpath of your Java project:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
<version>
is the Maven artifact version for Red Hat Process Automation Manager currently used in your project (for example, 7.30.0.Final-redhat-00003).NoteInstead of specifying a Red Hat Process Automation 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:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about the Red Hat Business Automation BOM, see What is the mapping between RHPAM product and maven library version?.
Instantiate a
KieServicesClient
instance with the appropriate connection information.Example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- The connection information:
-
Example URL:
http://localhost:8080/kie-server/services/rest/server
-
The credentials should reference a user with the
kie-server
role.
-
Example URL:
- 2
- The Marshalling format is an instance of
org.kie.server.api.marshalling.MarshallingFormat
. It controls whether the messages will be JSON or XML. Options for Marshalling format are JSON, JAXB, or XSTREAM.
Obtain a
DMNServicesClient
from the KIE server Java client connected to the related Process Server by invoking the methodgetServicesClient()
on the KIE server Java client instance:DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class );
DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class );
Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
dmnClient
can now execute decision services on Process Server.Execute the decision services for the desired model.
Example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - 1
- Instantiate a new DMN Context to be the input for the model evaluation. Note that this example is looping through the Age Classification decision multiple times.
- 2
- Assign input variables for the input DMN Context.
- 3
- Evaluate all the DMN Decisions defined in the DMN model:
-
$kieContainerId
is the ID of the container where the KJAR containing the DMN model is deployed -
$modelNamespace
is the namespace for the model. -
$modelName
is the name for the model.
-
- 4
- The DMN Result object is available from the server response.
At this point, the
dmnResult
contains all the decision results from the evaluated DMN model.You can also execute only a specific DMN decision in the model by using alternative methods of the
DMNServicesClient
.NoteIf the KIE container only contains one DMN model, you can omit
$modelNamespace
and$modelName
because the Process Server API selects it by default.
Directly interacting with the REST endpoints of Process Server provides the most separation between the calling code and the decision logic definition. The calling code is completely free of direct dependencies, and you can implement it in an entirely different development platform such as Node.js
or .NET
. The examples in this section demonstrate Nix-style curl commands but provide relevant information to adapt to any REST client.
For more information about the Process Server REST API, see Interacting with Red Hat Process Automation Manager using KIE APIs.
Prerequisites
-
Process Server is installed and configured, including a known user name and credentials for a user with the
kie-server
role. For installation options, see Planning a Red Hat Process Automation Manager installation. You have built the DMN project as a KJAR artifact and deployed it to Process Server. Ideally, you have built the DMN project as an executable model for more efficient execution:
mvn clean install -DgenerateDMNModel=yes
mvn clean install -DgenerateDMNModel=yes
Copy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about project packaging and deployment and executable models, see Packaging and deploying a Red Hat Process Automation Manager project.
- You have the ID of the KIE container containing the DMN model. If more than one model is present, you must also know the model namespace and model name of the relevant model.
Procedure
Determine the base URL for accessing the Process Server REST API endpoints. This requires knowing the following values (with the default local deployment values as an example):
-
Host (
localhost
) -
Port (
8080
) -
Root context (
kie-server
) -
Base REST path (
services/rest/
)
Example base URL in local deployment:
http://localhost:8080/kie-server/services/rest/
-
Host (
Determine user authentication requirements.
When users are defined directly in the Process Server configuration, HTTP Basic authentication is used and requires the user name and password. Successful requests require that the user have the
kie-server
role.The following example demonstrates how to add credentials to a curl request:
curl -u username:password <request>
curl -u username:password <request>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow If Process Server is configured with Red Hat Single Sign-On, the request must include a bearer token:
curl -H "Authorization: bearer $TOKEN" <request>
curl -H "Authorization: bearer $TOKEN" <request>
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Specify the format of the request and response. The REST API endpoints work with both JSON and XML formats and are set using request headers:
JSON
curl -H "accept: application/json" -H "content-type: application/json"
curl -H "accept: application/json" -H "content-type: application/json"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow XML
curl -H "accept: application/xml" -H "content-type: application/xml"
curl -H "accept: application/xml" -H "content-type: application/xml"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow (Optional) Query the container for a list of deployed decision models:
[GET]
server/containers/{containerId}/dmn
Example curl request:
curl -u krisv:krisv -H "accept: application/xml" -X GET "http://localhost:8080/kie-server/services/rest/server/containers/MovieDMNContainer/dmn"
curl -u krisv:krisv -H "accept: application/xml" -X GET "http://localhost:8080/kie-server/services/rest/server/containers/MovieDMNContainer/dmn"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Sample XML output:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Sample JSON output:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Execute the model:
[POST]
server/containers/{containerId}/dmn
Example curl request:
curl -u krisv:krisv -H "accept: application/json" -H "content-type: application/json" -X POST "http://localhost:8080/kie-server/services/rest/server/containers/MovieDMNContainer/dmn" -d "{ \"model-namespace\" : \"http://www.redhat.com/_c7328033-c355-43cd-b616-0aceef80e52a\", \"model-name\" : \"dmn-movieticket-ageclassification\", \"decision-name\" : [ ], \"decision-id\" : [ ], \"dmn-context\" : {\"Age\" : 66}}"
curl -u krisv:krisv -H "accept: application/json" -H "content-type: application/json" -X POST "http://localhost:8080/kie-server/services/rest/server/containers/MovieDMNContainer/dmn" -d "{ \"model-namespace\" : \"http://www.redhat.com/_c7328033-c355-43cd-b616-0aceef80e52a\", \"model-name\" : \"dmn-movieticket-ageclassification\", \"decision-name\" : [ ], \"decision-id\" : [ ], \"dmn-context\" : {\"Age\" : 66}}"
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example JSON request:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example XML request (JAXB format):
Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteRegardless of the request format, the request requires the following elements:
- Model namespace
- Model name
- Context object containing input values
Example JSON response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Example XML (JAXB format) response:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow