2017-05-08 46 views
0

我是Kie Workbench和Execution Server的新手。我正在使用Java Rest调用来在kie工作台中运行规则。请找到以下代码:Drools - 检索输出对象

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; 
} 

规则在规则期间被正确执行并且输出对象被立即执行。一个问题是当我再次调用这个方法来第二次执行规则时,我收到两个Output对象,并且对于每个后续调用,我都会得到一个额外的对象。看来会话中的对象被存储并且不会被每个调用清除。我怎样才能达到这一点,每一个电话我只会得到一个输出对象?

回答

1

由于您是Drools的新手,您可能不知道Drools有两种会话类型,无状态和有状态。验证KIE执行服务器会话配置是无状态的,因为有状态保留事先请求处理的事实。

验证它是无状态由它在项目编辑器设置:

Open Project Editor -> Knowledge bases and sessions 

检讨现有的或创建一个具有:

Add Knowledge Sessions -> and set the State to Stateless 
+0

如何验证KIE执行服务器会话配置是无状态的? –

+0

我更新了如何验证。完成后请接受答案。 – Jeff