第 13 章 PMML 模型执行
您可以使用 Business Central(Menu ApplyPmmlModelCommand
命令发送到配置的 KIE 服务器来执行 PMML 决策服务。
有关使用项目打包和部署方法包括 PMML 资产的更多信息,请参阅 打包和部署 Red Hat Process Automation Manager 项目。
您还可以将 PMML 模型作为 Business Central 中决策模型和 Notation(DMN)服务的一部分。当您在 DMN 文件中包含 PMML 模型时,您可以将 PMML 模型作为作为 DMN 决策节点或商业知识节点的 box 功能表达式调用。有关在 DMN 服务中包含 PMML 模型的更多信息,请参阅使用 DMN 模型设计决策服务。
13.1. 直接在 Java 应用程序中嵌入 PMML 信任调用
当知识资产直接嵌入到调用程序中,或者被实际拉取为 KJAR 的 Maven 依赖关系时,KIE 容器是本地的。如果代码版本和 PMML 定义版本之间有紧密关系,则您直接将知识资产嵌入到项目中。在有意更新并重新部署应用程序后,对决策的任何更改都会生效。这种方法的一个优点是,正确的操作不依赖于任何外部依赖项来运行,这可能受锁定的环境限制。
先决条件
- 已创建包含要执行的 PMML 模型的 KJAR。有关项目打包的更多信息,请参阅 打包和部署 Red Hat Process Automation Manager 项目。
流程
在客户端应用程序中,将以下依赖项添加到 Java 项目的相关类路径中:
<!-- Required for the PMML compiler --> <dependency> <groupId>org.drools</groupId> <artifactId>kie-pmml-dependencies</artifactId> <version>${rhpam.version}</version> </dependency> <!-- Required for the KIE public API --> <dependency> <groupId>org.kie</groupId> <artifactId>kie-api</artifactId> <version>${rhpam.version}</version> </dependencies> <!-- Required if not using classpath KIE container --> <dependency> <groupId>org.kie</groupId> <artifactId>kie-ci</artifactId> <version>${rhpam.version}</version> </dependency>
&
lt;version
> 是项目中当前使用的 Red Hat Process Automation Manager 的 Maven 工件版本(例如 7.67.0.Final-redhat-00024)。注意考虑将 Red Hat Business Automation 材料清单(BOM)依赖项添加到项目
pom.xml
文件,而不是为每个依赖项指定 Red Hat Process Automation Manager <version
>。Red Hat Business Automation BOM 适用于 Red Hat Decision Manager 和 Red Hat Process Automation Manager。添加 BOM 文件时,项目中包含来自提供的 Maven 存储库的正确依赖项版本。BOM 依赖项示例:
<dependency> <groupId>com.redhat.ba</groupId> <artifactId>ba-platform-bom</artifactId> <version>7.13.5.redhat-00002</version> <scope>import</scope> <type>pom</type> </dependency>
有关 Red Hat Business Automation BOM 的更多信息,请参阅 RHPAM 产品与 maven 库版本之间的映射是什么?
从
类路径
或ReleaseId
创建 KIE 容器: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(); KieContainer kieContainer = kieServices.getKieClasspathContainer();
创建用于执行模型的
PMMLRuntime
实例:PMMLRuntime pmmlRuntime = KieRuntimeFactory.of(kieContainer.getKieBase()).get(PMMLRuntime.class);
创建一个
PMMLRequestData
类实例,将 PMML 模型应用到数据集:PMMLRequestData pmmlRequestData = new PMMLRequestData({correlation_id}, {model_name}); pmmlRequestData.addRequestParam({parameter_name}, {parameter_value}) ...
创建包含输入数据的
PMMLContext
类的实例:PMMLContext pmmlContext = new PMMLContextImpl(pmmlRequestData);
在使用您创建的所需的 PMML 类实例执行 PMML 模型时,检索
PMML4Result
:PMML4Result pmml4Result = pmmlRuntime.evaluate({model_name}, pmmlContext);