0
votes

I am new to Kie Workbench. I am using Java Rest calls to fire rules in kie workbench. Please find the code below:

public class RuleEngineConnector {

    @Value("${brms.execution.server.url}")
    private String SERVER_URL;

    @Value("${brms.execution.server.username}")
    private String USER;


    @Value("${brms.execution.server.password}")
    private String PASSWORD;

    @Value("${brms.containerId}")
    private String CONTAINER_ID;

    private static final MarshallingFormat FORMAT = MarshallingFormat.JAXB;


    public String getAdapter(AdapterRuleDO adapterRule) {
        KieServicesConfiguration cfg = KieServicesFactory.newRestConfiguration(SERVER_URL, USER, PASSWORD);
        cfg.setMarshallingFormat(FORMAT);
        Set<Class<?>> extraClasses = new HashSet<Class<?>>();
        extraClasses.add(AdapterRuleDO.class);
        cfg.addJaxbClasses(extraClasses);
        KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(cfg);
        ServiceResponse<ExecutionResults> response = getRulesResponse(adapterRule, kieServicesClient);
        List<AdapterRuleDO> listOfObjects = (List<AdapterRuleDO>) response.getResult().getValue("get-adapter");//to be changed
        return listOfObjects.get(0).getAdapterName();
    }

    private ServiceResponse<ExecutionResults> getRulesResponse(AdapterRuleDO adapterRule, KieServicesClient kieServicesClient) {
        List<Command<?>> commands = new ArrayList<Command<?>>();
        KieCommands commandsFactory = KieServices.Factory.get().getCommands();
        commands.add(commandsFactory.newInsert(adapterRule, "adapterRule"));
        commands.add(commandsFactory.newFireAllRules());
        commands.add(commandsFactory.newGetObjects("get-adapter"));
        BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands);
        RuleServicesClient ruleServicesClient = kieServicesClient.getServicesClient(RuleServicesClient.class);
        ServiceResponse<ExecutionResults> response = ruleServicesClient.executeCommandsWithResults(CONTAINER_ID, batchExecution);
        return response;
    }

}

I am getting the rules fired correctly and the values are getting properly updated in AdapterRuleDO class after the rule is fired. One problem is when I again call this method to execute rules for the second time, I receive two AdapterRuleDO object and for each subsequent calls I get one additional object. It seems that the objects in the session are stored and not getting cleared for each call. How can I achieve that for every call I will get only one AdapterRuleDO object in return?

Please note I have only one such decision table where this fact has been used.

3

3 Answers

1
votes

After searching different blogs and the user forums got a solution which worked fine.The above problem can be resolved by the following steps:

1) Use the "adapterRule" to get the result instead of "get-adapter".

2) In KIE Workbench, search for deployment descriptor and make this following change: <runtime-strategy>PER_REQUEST</runtime-strategy> By default, runtime strategy is SINGLETON.

Hope this makes sense and help somebody out.

0
votes

If you are interested in stateless evaluation try to configure your session as stateless. This will create a new session for each request. You should be able to do this in the kie-workbench.

Hope it helps,

0
votes

In place of below line:

 BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands);

Use this line:

 BatchExecutionCommand batchExecution = commandsFactory.newBatchExecution(commands,Ksession_name);