Java 流口水:DMN 1.2感觉功能的评估不起作用

Java 流口水:DMN 1.2感觉功能的评估不起作用,java,drools,decision-model-notation,Java,Drools,Decision Model Notation,我想在Drools 7.21中评估DMN 1.2中新增的FEEL函数,如sqrt()或modulo(),但方法 dmnRuntime.evaluateAll(dmnModel,上下文) 始终返回值“null”(仅用于新函数),状态为“SUCCESS”。我做错了什么或遗漏了什么 DMN文件如下所示: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <semantic:definitions xmlns:semantic

我想在Drools 7.21中评估DMN 1.2中新增的FEEL函数,如sqrt()或modulo(),但方法

dmnRuntime.evaluateAll(dmnModel,上下文)

始终返回值“null”(仅用于新函数),状态为“SUCCESS”。我做错了什么或遗漏了什么

DMN文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<semantic:definitions xmlns:semantic="http://www.omg.org/spec/DMN/20180521/MODEL/" xmlns="http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436" xmlns:feel="http://www.omg.org/spec/FEEL/20140401" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exporter="DMN Modeler" exporterVersion="6.0.1" id="_56fd6445-ff6a-4c28-8206-71fce7f80436" name="Sqrt-Function" namespace="http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436" >
  <semantic:decision id="_cf6124bd-9907-4ac0-b4fd-59a962dbc502" name="square_root">
    <semantic:variable id="_edaf978e-3634-4e52-8244-5fd4e16fd257" name="square_root" typeRef="feel:number"/>
    <semantic:literalExpression id="_c990c3b2-e322-4ef9-931d-79bcdac99686">
      <semantic:text>sqrt(81)</semantic:text>
    </semantic:literalExpression>
  </semantic:decision>
</semantic:definitions>
我这样称呼Drools评估:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<semantic:definitions xmlns:semantic="http://www.omg.org/spec/DMN/20180521/MODEL/" xmlns="http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436" xmlns:feel="http://www.omg.org/spec/FEEL/20140401" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" exporter="DMN Modeler" exporterVersion="6.0.1" id="_56fd6445-ff6a-4c28-8206-71fce7f80436" name="Sqrt-Function" namespace="http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436" >
  <semantic:decision id="_cf6124bd-9907-4ac0-b4fd-59a962dbc502" name="square_root">
    <semantic:variable id="_edaf978e-3634-4e52-8244-5fd4e16fd257" name="square_root" typeRef="feel:number"/>
    <semantic:literalExpression id="_c990c3b2-e322-4ef9-931d-79bcdac99686">
      <semantic:text>sqrt(81)</semantic:text>
    </semantic:literalExpression>
  </semantic:decision>
</semantic:definitions>
KieContainer KieContainer=kieheloper.getKieContainer(ks.newReleaseId(“org.kie”,“dmn test-”+UUID.randomUUID(),“1.2”);
DMNRuntime DMNRuntime=kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
((DMNRuntimeImpl)dmnRuntime).setOption(新的RuntimeTypeCheckOption(true));
DMNResult result=dmnRuntime.evaluateAll(dmnModel,context);

最好不要(联合国)马歇尔,也不要手动编译DMN文件;取而代之的是使用来自KJAR;的KieContainer标准构建的标准方法

换句话说,这适用于您的DMN文件:

KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
DMNRuntime dmnRuntime = KieRuntimeFactory.of(kieContainer.getKieBase()).get(DMNRuntime.class);
DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName);
DMNContext context = dmnRuntime.newContext();
((DMNRuntimeImpl) dmnRuntime).setOption(new RuntimeTypeCheckOption(true));
DMNResult result = dmnRuntime.evaluateAll(dmnModel, context);
结果产生
9

如果确实要使用KieHelper,最好将DMN文件作为KieResource传递给调用
KieHelper.getKieContainer(…)
,例如:

KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.2"),
                                                      ks.getResources().newFileSystemResource(new File(dmnFile)));
DMNRuntime dmnRuntime = KieRuntimeFactory.of(kieContainer.getKieBase()).get(DMNRuntime.class);
((DMNRuntimeImpl) dmnRuntime).setOption(new RuntimeTypeCheckOption(true));
DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName);
DMNContext context = dmnRuntime.newContext();
DMNResult result = dmnRuntime.evaluateAll(dmnModel, context);
System.out.println(result);
您可以将调用
ks.getResources().newFileSystemResource(…)
更改为URL、类路径、字节等-基于您的用例,根据需要基于资源。这样,助手将负责解组

第二个代码片段也适用于您的DMN文件,因此产生了
9

代码中的问题是,DMNCompiler的初始化实际上并不意味着用户可以手动调用,事实上,文档中没有任何地方要求手动管理;上述两种方法都会将其委托给KieContainer/KieHelper的内部,这将是标准方法

我建议您按照文档中前一个示例中的详细说明进行KieContainer构建,但我希望这个答案可以帮助您解决这两个问题——在本地都为我工作


希望这有帮助

非常感谢您的支持,这对我也很有用!