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());
}
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);
}
}
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);
}
}
Aggregations