Search in sources :

Example 1 with DMNResultKS

use of org.kie.server.api.model.dmn.DMNResultKS in project droolsjbpm-integration by kiegroup.

the class DecisionMarshallingTest method testMarshalling.

@Test
public void testMarshalling() {
    KieSession kieSession = new KieHelper().addFromClassPath("/FunctionDefinition.dmn").build().newKieSession();
    DMNRuntime dmnRuntime = kieSession.getKieRuntime(DMNRuntime.class);
    DMNModel model = dmnRuntime.getModels().get(0);
    DMNContext realCtx = dmnRuntime.newContext();
    realCtx.set("a", 10);
    realCtx.set("b", 5);
    DMNContextKS dmnClientRequest = new DMNContextKS(realCtx.getAll());
    DMNContextKS mu_dmnClientRequest = marshallUnmarshall(dmnClientRequest);
    assertEquals(dmnClientRequest.getNamespace(), mu_dmnClientRequest.getNamespace());
    assertEquals(dmnClientRequest.getModelName(), mu_dmnClientRequest.getModelName());
    assertThat(dmnClientRequest.getDecisionNames(), is(mu_dmnClientRequest.getDecisionNames()));
    assertEquals(dmnClientRequest.getDmnContext().size(), mu_dmnClientRequest.getDmnContext().size());
    assertEquals(dmnClientRequest.getDmnContext().keySet(), mu_dmnClientRequest.getDmnContext().keySet());
    DMNResult evaluateAll = dmnRuntime.evaluateAll(model, realCtx);
    ServiceResponse<DMNResultKS> dmnClientResponse = new ServiceResponse<DMNResultKS>(ServiceResponse.ResponseType.SUCCESS, "Test case", new DMNResultKS(model.getNamespace(), model.getName(), dmnClientRequest.getDecisionNames(), evaluateAll));
    ServiceResponse<DMNResultKS> mu_dmnClientResponse = marshallUnmarshall(dmnClientResponse);
    assertEquals(dmnClientResponse.getResult().getNamespace(), mu_dmnClientResponse.getResult().getNamespace());
    assertEquals(dmnClientResponse.getResult().getModelName(), mu_dmnClientResponse.getResult().getModelName());
    assertThat(dmnClientResponse.getResult().getDecisionNames(), is(mu_dmnClientResponse.getResult().getDecisionNames()));
    assertEquals(dmnClientResponse.getResult().getDmnContext().size(), mu_dmnClientResponse.getResult().getDmnContext().size());
    assertEquals(dmnClientResponse.getResult().getDmnContext().keySet(), mu_dmnClientResponse.getResult().getDmnContext().keySet());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) ServiceResponse(org.kie.server.api.model.ServiceResponse) DMNContext(org.kie.dmn.api.core.DMNContext) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) DMNContextKS(org.kie.server.api.model.dmn.DMNContextKS) Test(org.junit.Test)

Example 2 with DMNResultKS

use of org.kie.server.api.model.dmn.DMNResultKS in project droolsjbpm-integration by kiegroup.

the class ModelEvaluatorServiceBase method evaluateDecisions.

public ServiceResponse<DMNResultKS> evaluateDecisions(String containerId, String contextPayload, String marshallingType) {
    try {
        KieContainerInstanceImpl kContainer = context.getContainer(containerId, ContainerLocatorProvider.get().getLocator());
        DMNRuntime dmnRuntime = KieRuntimeFactory.of(kContainer.getKieContainer().getKieBase()).get(DMNRuntime.class);
        wirePrometheus(kContainer, dmnRuntime);
        LOG.debug("Will deserialize payload: {}", contextPayload);
        DMNContextKS evalCtx = marshallerHelper.unmarshal(containerId, contextPayload, marshallingType, DMNContextKS.class);
        DMNEvaluationResult evaluationResult = DMNEvaluationUtils.evaluate(dmnRuntime, evalCtx.getNamespace(), evalCtx.getModelName(), evalCtx.getDmnContext(), evalCtx.getDecisionNames(), evalCtx.getDecisionIds(), evalCtx.getDecisionServiceName());
        DMNResultKS res = new DMNResultKS(evaluationResult.model.getNamespace(), evaluationResult.model.getName(), evalCtx.getDecisionNames(), evaluationResult.result);
        return new ServiceResponse<DMNResultKS>(ServiceResponse.ResponseType.SUCCESS, "OK from container '" + containerId + "'", res);
    } catch (Exception e) {
        e.printStackTrace();
        LOG.error("Error from container '" + containerId + "'", e);
        return new ServiceResponse<DMNResultKS>(ServiceResponse.ResponseType.FAILURE, "Error from container '" + containerId + "'" + e.getMessage(), null);
    }
}
Also used : DMNEvaluationResult(org.kie.dmn.core.internal.utils.DMNEvaluationUtils.DMNEvaluationResult) ServiceResponse(org.kie.server.api.model.ServiceResponse) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNContextKS(org.kie.server.api.model.dmn.DMNContextKS)

Example 3 with DMNResultKS

use of org.kie.server.api.model.dmn.DMNResultKS in project droolsjbpm-integration by kiegroup.

the class ModelEvaluatorResource method evaluateDecisions.

@ApiOperation(value = "Evaluates decisions for given input", response = ServiceResponse.class, code = 200, notes = REF_KIESERVER_DMN_API_DOC)
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 404, message = "Container not found"), @ApiResponse(code = 200, response = String.class, message = "Successful response", examples = @Example(value = { @ExampleProperty(mediaType = MediaType.APPLICATION_JSON, value = EXAMPLE_KIESERVER_POST_RESPONSE_JSON), @ExampleProperty(mediaType = MediaType.APPLICATION_XML, value = EXAMPLE_KIESERVER_POST_RESPONSE_XML) })) })
@POST
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response evaluateDecisions(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = PARAM_CONTAINER_ID_EVAL, required = true, example = EXAMPLE_CONTAINER_ID) @PathParam(CONTAINER_ID) String containerId, @ApiParam(value = "DMN context to be used while evaluation decisions as DMNContextKS type", required = true, examples = @Example(value = { @ExampleProperty(mediaType = MediaType.APPLICATION_JSON, value = EXAMPLE_KIESERVER_POST_REQ_JSON), @ExampleProperty(mediaType = MediaType.APPLICATION_XML, value = EXAMPLE_KIESERVER_POST_REQ_XML) })) String payload) {
    LOG.debug("About to evaluateDecisions() on container {}", containerId);
    Variant v = getVariant(headers);
    Header conversationIdHeader = buildConversationIdHeader(containerId, modelEvaluatorService.getKieServerRegistry(), headers);
    try {
        String contentType = getContentType(headers);
        LOG.debug("Payload received: {}", payload);
        ServiceResponse<DMNResultKS> result = modelEvaluatorService.evaluateDecisions(containerId, payload, contentType);
        if (result.getType() == ServiceResponse.ResponseType.SUCCESS) {
            return createCorrectVariant(marshallerHelper, containerId, result, headers, Response.Status.OK, conversationIdHeader);
        }
        return createCorrectVariant(marshallerHelper, containerId, result, headers, Response.Status.NOT_FOUND, conversationIdHeader);
    } catch (Exception e) {
        LOG.error("Unexpected error retrieving models. Message: '{}'", e.getMessage(), e);
        return internalServerError(MessageFormat.format("ERROR", e.getMessage()), v, conversationIdHeader);
    }
}
Also used : RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) Variant(javax.ws.rs.core.Variant) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

DMNResultKS (org.kie.server.api.model.dmn.DMNResultKS)3 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2 ServiceResponse (org.kie.server.api.model.ServiceResponse)2 DMNContextKS (org.kie.server.api.model.dmn.DMNContextKS)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 Variant (javax.ws.rs.core.Variant)1 Test (org.junit.Test)1 KieSession (org.kie.api.runtime.KieSession)1 DMNContext (org.kie.dmn.api.core.DMNContext)1 DMNModel (org.kie.dmn.api.core.DMNModel)1 DMNResult (org.kie.dmn.api.core.DMNResult)1 DMNEvaluationResult (org.kie.dmn.core.internal.utils.DMNEvaluationUtils.DMNEvaluationResult)1 KieHelper (org.kie.internal.utils.KieHelper)1 Header (org.kie.server.remote.rest.common.Header)1 RestUtils.buildConversationIdHeader (org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader)1 RestUtils.createCorrectVariant (org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant)1