简体   繁体   English

能否在jbpm6脚本任务中访问Drools规则引擎的工作内存事实?

[英]can we access working memory fact of drools rule engine in jbpm6 script task?

is there a way to access working memory fact of drools rule engine in jbpm6 script task? 有没有办法在jbpm6脚本任务中访问Drools规则引擎的工作内存事实?

I have a model class : Application.java Rule : check if salary is > 10000 (part of rule group : salaryCheck ) 我有一个模型类:Application.java规则:检查薪水是否大于10000(规则组的一部分:SalaryCheck)

jbpm flow : start -> salaryCheck(rule task, associated with rule group : salaryCheck) -> updateScore(script task) -> end jbpm流程:开始->薪金检查(规则任务,与规则组关联:薪水检查)-> updateScore(脚本任务)->结束

updateScore - script rask code: updateScore-脚本rask代码:

System.out.println(System.out.println((Application)(kcontext.getKieRuntime().getFactHandles().toArray()[0]));

Error: 错误:

java.lang.ClassCastException: org.drools.core.common.DefaultFactHandle cannot be cast to org.model.Application

Updated script task: 更新的脚本任务:

import org.model.Application
import org.drools.runtime.rule.QueryResults
import org.drools.runtime.rule.QueryResultsRow

QueryResults results = kcontext.getKieRuntime().getQueryResults( "getObjectsOfApplication" ); 
for ( QueryResultsRow row : results ) {
    Application applicantion = ( Application ) row.get( "$result" ); 
    application.setScore(700);
    System.out.println("Application object :: "+ application);
}

Added Query to rule drl file 向规则drl文件添加了查询

query "getObjectsOfApplication"
    $result: Application()
end

getFactHandles() is definitely not the method you are looking for. getFactHandles()绝对不是您要查找的方法。 The method think you are looking for is getObjects() . 认为您正在寻找的方法是getObjects() Either way, getting the first element of the returned collection without any validation seems dangerous to me. 无论哪种方式,在没有任何验证的情况下获取返回集合的第一个元素对我来说都是危险的。 You can't even guarantee that order of the elements in the returned collection will remain the same between different invocations. 您甚至不能保证在不同的调用之间,返回的集合中元素的顺序将保持不变。

A better approach would be to use the version of getObjects() that accepts an ObjectFilter parameter. 更好的方法是使用接受ObjectFilter参数的getObjects()版本。 An even better and more 'declarative' approach would be to define a query in that returns the exact object you are looking for. 更好,更“声明式”的方法是定义一个查询,以返回您要查找的确切对象。 You can then execute the query using kcontext.getKieRuntime().getQueryResults() . 然后,您可以使用kcontext.getKieRuntime().getQueryResults()执行查询。

You can get a better understanding on any of these 2 approaches (using an ObjectFilter or a query) in this thread: Retrieving facts of a specific type from working memory 您可以在此线程中对这两种方法(使用ObjectFilter或查询)中的任何一种进行更好的理解: 从工作内存中检索特定类型的事实


EDIT: 编辑:

The post I suggested about using a query or an ObjectFilter is Drools 5 code. 我建议有关使用查询或ObjectFilter的帖子是Drools 5代码。 In Drools 6, the API classes were moved to a different package. 在Drools 6中,API类被移到了另一个包中。 These are the imports you should use if you want to invoke a query in your code: 如果要在代码中调用查询,则应使用这些导入:

  • org.kie.api.runtime.rule.QueryResults
  • org.kie.api.runtime.rule.QueryResultsRow

These classes are both part of the kie-api project. 这些类都是kie-api项目的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM