Search in sources :

Example 1 with DMNServicesClient

use of org.kie.server.client.DMNServicesClient in project droolsjbpm-integration by kiegroup.

the class RemoteBusinessRuleTaskHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Map<String, Object> parameters = new HashMap<>(workItem.getParameters());
    String containerId = (String) parameters.remove("ContainerId");
    if (containerId == null || containerId.isEmpty()) {
        throw new IllegalArgumentException("Container ID is required for remote BusinessRuleTask");
    }
    String language = (String) parameters.remove("Language");
    if (language == null) {
        language = DRL_LANG;
    }
    String kieSessionName = (String) parameters.remove("KieSessionName");
    // remove engine specific parameters
    parameters.remove("TaskName");
    parameters.remove("KieSessionType");
    Map<String, Object> results = new HashMap<>();
    logger.debug("Facts to be inserted into working memory {}", parameters);
    if (DRL_LANG.equalsIgnoreCase(language)) {
        RuleServicesClient ruleClient = client.getServicesClient(RuleServicesClient.class);
        List<Command<?>> commands = new ArrayList<Command<?>>();
        BatchExecutionCommand executionCommand = commandsFactory.newBatchExecution(commands, kieSessionName);
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            String inputKey = workItem.getId() + "_" + entry.getKey();
            commands.add(commandsFactory.newInsert(entry.getValue(), inputKey, true, null));
        }
        commands.add(commandsFactory.newFireAllRules("Fired"));
        ServiceResponse<ExecutionResults> reply = ruleClient.executeCommandsWithResults(containerId, executionCommand);
        if (ServiceResponse.ResponseType.FAILURE.equals(reply.getType())) {
            throw new KieServicesException(reply.getMsg());
        }
        ExecutionResults executionResults = reply.getResult();
        logger.debug("{} rules fired", executionResults.getValue("Fired"));
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            String inputKey = workItem.getId() + "_" + entry.getKey();
            String key = entry.getKey().replaceAll(workItem.getId() + "_", "");
            results.put(key, executionResults.getValue(inputKey));
        }
    } else if (DMN_LANG.equalsIgnoreCase(language)) {
        String namespace = (String) parameters.remove("Namespace");
        String model = (String) parameters.remove("Model");
        String decision = (String) parameters.remove("Decision");
        DMNServicesClient dmnClient = client.getServicesClient(DMNServicesClient.class);
        DMNContext dmnContext = dmnClient.newContext();
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            dmnContext.set(entry.getKey(), entry.getValue());
        }
        ServiceResponse<DMNResult> evaluationResult = null;
        if (decision != null) {
            evaluationResult = dmnClient.evaluateDecisionByName(containerId, namespace, model, decision, dmnContext);
        } else {
            evaluationResult = dmnClient.evaluateAll(containerId, namespace, model, dmnContext);
        }
        DMNResult dmnResult = evaluationResult.getResult();
        results.putAll(dmnResult.getContext().getAll());
    } else {
        throw new IllegalArgumentException("Not supported language type " + language);
    }
    logger.debug("Facts retrieved from working memory {}", results);
    workItemManager.completeWorkItem(workItem.getId(), results);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNServicesClient(org.kie.server.client.DMNServicesClient) HashMap(java.util.HashMap) ExecutionResults(org.kie.api.runtime.ExecutionResults) ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) RuleServicesClient(org.kie.server.client.RuleServicesClient) ServiceResponse(org.kie.server.api.model.ServiceResponse) Command(org.kie.api.command.Command) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) BatchExecutionCommand(org.kie.api.command.BatchExecutionCommand) HashMap(java.util.HashMap) Map(java.util.Map) KieServicesException(org.kie.server.api.exception.KieServicesException)

Example 2 with DMNServicesClient

use of org.kie.server.client.DMNServicesClient in project businessautomation-cop by redhat-cop.

the class RulesApiImpl method canOpenAccount.

@Override
public CanOpenAccountResults canOpenAccount(final Customer customer) {
    final DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class);
    final DMNContext dmnContext = dmnClient.newContext();
    dmnContext.set("customer", customer);
    final ServiceResponse<DMNResult> serviceResponse = dmnClient.evaluateAll(CONTAINER_ID, NAMESPACE, MODEL_NAME, dmnContext);
    final DMNResult dmnResult = serviceResponse.getResult();
    return extractResult(dmnResult);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNServicesClient(org.kie.server.client.DMNServicesClient) DMNContext(org.kie.dmn.api.core.DMNContext)

Example 3 with DMNServicesClient

use of org.kie.server.client.DMNServicesClient in project businessautomation-cop by redhat-cop.

the class RulesApiImpl method canOpenAccount.

@Override
public CanOpenAccountResults canOpenAccount(final Customer customer) {
    final DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class);
    final DMNContext dmnContext = dmnClient.newContext();
    dmnContext.set("customer", customer);
    final ServiceResponse<DMNResult> serviceResponse = dmnClient.evaluateAll(CONTAINER_ID, NAMESPACE, MODEL_NAME, dmnContext);
    final DMNResult dmnResult = serviceResponse.getResult();
    return extractResult(dmnResult);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNServicesClient(org.kie.server.client.DMNServicesClient) DMNContext(org.kie.dmn.api.core.DMNContext)

Example 4 with DMNServicesClient

use of org.kie.server.client.DMNServicesClient in project businessautomation-cop by redhat-cop.

the class RulesApiImpl method canOpenAccount.

@Override
public CanOpenAccountResults canOpenAccount(final Customer customer) {
    final DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class);
    final DMNContext dmnContext = dmnClient.newContext();
    dmnContext.set("customer", customer);
    final ServiceResponse<DMNResult> serviceResponse = dmnClient.evaluateAll(CONTAINER_ID, NAMESPACE, MODEL_NAME, dmnContext);
    final DMNResult dmnResult = serviceResponse.getResult();
    return extractResult(dmnResult);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNServicesClient(org.kie.server.client.DMNServicesClient) DMNContext(org.kie.dmn.api.core.DMNContext)

Example 5 with DMNServicesClient

use of org.kie.server.client.DMNServicesClient in project kiegroup-examples by tkobayas.

the class DMNTest method testProcess.

// private static final MarshallingFormat FORMAT = MarshallingFormat.JSON;
// private static final MarshallingFormat FORMAT = MarshallingFormat.JAXB;
@Test
public void testProcess() {
    KieServicesConfiguration config = KieServicesFactory.newRestConfiguration(BASE_URL, USERNAME, PASSWORD);
    // config.setMarshallingFormat(FORMAT);
    // HashSet<Class<?>> classes = new HashSet<Class<?>>();
    // classes.add(Person.class);
    // 
    // config.addExtraClasses(classes);
    KieServicesClient kieServicesClient = KieServicesFactory.newKieServicesClient(config);
    DMNServicesClient dmnClient = kieServicesClient.getServicesClient(DMNServicesClient.class);
    DMNContext dmnContext = dmnClient.newContext();
    Map<String, Object> driverMap = new HashMap<>();
    driverMap.put("Points", 2);
    dmnContext.set("Driver", driverMap);
    Map<String, Object> violationMap = new HashMap<>();
    violationMap.put("Type", "speed");
    violationMap.put("Actual Speed", 120);
    violationMap.put("Speed Limit", 100);
    dmnContext.set("Violation", violationMap);
    ServiceResponse<DMNResult> serverResp = dmnClient.evaluateAll(CONTAINER_ID, "https://github.com/kiegroup/drools/kie-dmn/_A4BCA8B8-CF08-433F-93B2-A2598F19ECFF", "Traffic Violation", dmnContext);
    DMNResult dmnResult = serverResp.getResult();
    for (DMNDecisionResult dr : dmnResult.getDecisionResults()) {
        System.out.println("Decision: '" + dr.getDecisionName() + "', " + "Result: " + dr.getResult());
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNServicesClient(org.kie.server.client.DMNServicesClient) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) KieServicesConfiguration(org.kie.server.client.KieServicesConfiguration) KieServicesClient(org.kie.server.client.KieServicesClient) Test(org.junit.Test)

Aggregations

DMNContext (org.kie.dmn.api.core.DMNContext)5 DMNResult (org.kie.dmn.api.core.DMNResult)5 DMNServicesClient (org.kie.server.client.DMNServicesClient)5 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Test (org.junit.Test)1 BatchExecutionCommand (org.kie.api.command.BatchExecutionCommand)1 Command (org.kie.api.command.Command)1 ExecutionResults (org.kie.api.runtime.ExecutionResults)1 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)1 KieServicesException (org.kie.server.api.exception.KieServicesException)1 ServiceResponse (org.kie.server.api.model.ServiceResponse)1 KieServicesClient (org.kie.server.client.KieServicesClient)1 KieServicesConfiguration (org.kie.server.client.KieServicesConfiguration)1 RuleServicesClient (org.kie.server.client.RuleServicesClient)1