我是 Kie Workbench 和执行服务器的新手。我正在使用 Java Rest 调用在 kie 工作台中运行规则。请找到下面的代码:
private String kieServerUrl;
private String kieServerContainerId;
private String KieServerUsername;
private String kieServerPassword;
private RuleServicesClient ruleClient;
private static final String INPUT_OUT_IDENTIFIER = "Input";
private static final String SESSION_OBJECTS = "SessionObjects";
private static final String RUN_ALL_RULES = "RunAllRules";
public void init() {
final KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(kieServerUrl, KieServerUsername, kieServerPassword);
config.setMarshallingFormat(MarshallingFormat.XSTREAM);
KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(config);
ruleClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
}
@Override
public Output process(final Input input) {
Output output = null;
logger.debug("Running rules ..");
BatchExecutionCommandImpl executionCommand = new BatchExecutionCommandImpl();
executionCommand.getCommands().add(new InsertObjectCommand(input, INPUT_OUT_IDENTIFIER));
executionCommand.getCommands().add(new FireAllRulesCommand(RUN_ALL_RULES));
executionCommand.getCommands().add(new GetObjectsCommand(null, SESSION_OBJECTS));
logger.debug("Sending commands to the server");
ServiceResponse<ExecutionResults> response = ruleClient.executeCommandsWithResults(kieServerContainerId, executionCommand);
if(response.getType().equals(ServiceResponse.ResponseType.SUCCESS)){
logger.debug("Commands executed with success! Response: ");
final ExecutionResultImpl result = (ExecutionResultImpl) response.getResult();
ArrayList<Object> values = (ArrayList<Object>)result.getValue(SESSION_OBJECTS);
}else{
logger.error("Error executing rules. Message: {}", response.getMsg());
}
logger.debug("...finished running rules.");
return output;
}
规则被正确执行,并且输出对象在规则期间被实例化。一个问题是,当我再次调用此方法第二次执行规则时,我收到两个输出对象,并且对于每个后续调用,我都会获得一个附加对象。 session 中的对象似乎已被存储,并且每次调用都不会被清除。如何才能实现每次调用都只得到一个输出对象作为返回?
最佳答案
由于您是 Drools 的新手,您可能不知道 Drools 有两种 session 类型:无状态和有状态。验证 KIE 执行服务器 session 配置是否无状态,因为有状态会保留先前请求处理中的事实。
通过项目编辑器中的设置验证其是否为无状态:
Open Project Editor -> Knowledge bases and sessions
查看现有的或创建一个:
Add Knowledge Sessions -> and set the State to Stateless
关于java - Drools - 检索输出对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43844064/