使用insert方法,我可以将值从Java传递给Drool文件,但是在操作之后我想要从Drool文件返回值。
答案 0 :(得分:0)
您无法从drool文件中返回任何内容。它的工作原理是参考。 以下3个活动来自您的java类:
在规则被触发后使用事实数据
class TdpFactData{
private int age ....
getter , setters ....
}
----------------------------------------
TdpFactData factData=new TdpFactData();
kieSession.insert(factData); Fact data is inserted in kiesession for the drool to use
kieSession.fireAllRules(); // Here the drool file executes
// Fact data got changed based on the rules
//Utilize the fact data again in java code .
答案 1 :(得分:0)
您可以使用GetObjectsCommand对象从Drool会话中获取值。为此,请首先从kiecontainer创建一个会话对象。
KieServices ks = KieServices.Factory.get();
KieContainer kieContainer = ks.getKieClassPathContainer() ;
String session = "session" //session name from kmodeule.xml
StatelessKieSession session = kieContainer.newStatelessKieSession(sessionName);
List<Command<?>> cmds = new ArrayList<Command<?>>();
KieCommands commands = KieServices.Factory.get().getCommands();
cmds.add(commands.newInsert("hi)); //insert all POJO here.
GetObjectsCommand getObjectsCommand = new GetObjectsCommand();
getObjectsCommand.setOutIdentifier("objects");
cmds.add(commands.newFireAllRules());
cmds.add(getObjectsCommand);
BatchExecutionCommand myCommands = CommandFactory.newBatchExecution(cmds);
ServiceResponse<ExecutionResults> response = session.executeCommandsWithResults( myCommands);
List responseList = (List) response.getResult().getValue("objects");
//the response list will contain the objects present plus inserted via drool engine.