use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.
the class ScoresMarshallingTest method marshallUnmarshallScore.
@SuppressWarnings("unchecked")
private <S extends Score<?>> S marshallUnmarshallScore(S toBeMarshalled) {
ScoreWrapper wrapper = new ScoreWrapper(toBeMarshalled);
SolverInstance instance = new SolverInstance();
instance.setScoreWrapper(wrapper);
String marshalledSolver = marshaller.marshall(instance);
SolverInstance result = marshaller.unmarshall(marshalledSolver, SolverInstance.class);
return (S) result.getScoreWrapper().toScore();
}
use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.
the class SolutionMarshallingTest method testMarshallHardSoftScore.
@Test
public void testMarshallHardSoftScore() {
SolverInstance solverInstance = new SolverInstance();
DateObjectUnannotated dateObject = new DateObjectUnannotated();
dateObject.setLocalDate(LocalDate.of(2018, 8, 18));
dateObject.setLocalDateTime(LocalDateTime.of(2017, 7, 17, 17, 17, 17));
dateObject.setLocalTime(LocalTime.of(12, 34, 56));
dateObject.setOffsetDateTime(OffsetDateTime.of(2019, 2, 4, 20, 57, 11, 0, ZoneOffset.ofHours(1)));
solverInstance.setBestSolution(dateObject);
Marshaller marshaller = MarshallerFactory.getMarshaller(marshallingFormat, Thread.currentThread().getContextClassLoader());
String marshalledSolverInstance = marshaller.marshall(solverInstance);
logger.debug("Marshalled SolverInstance ({}): {}", marshallingFormat, marshalledSolverInstance);
assertThat(marshalledSolverInstance).as("Dates should be formatted").contains("2018-08-18", "2017-07-17T17:17:17", "12:34:56", "2019-02-04T20:57:11+01");
SolverInstance unmarshalledSolverInstance = marshaller.unmarshall(marshalledSolverInstance, SolverInstance.class);
DateObjectUnannotated bestSolution = (DateObjectUnannotated) unmarshalledSolverInstance.getBestSolution();
assertThat(bestSolution.getLocalDate()).isEqualTo(dateObject.getLocalDate());
assertThat(bestSolution.getLocalDateTime()).isEqualTo(dateObject.getLocalDateTime());
assertThat(bestSolution.getLocalTime()).isEqualTo(dateObject.getLocalTime());
assertThat(bestSolution.getOffsetDateTime()).isEqualTo(dateObject.getOffsetDateTime());
}
use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.
the class SolverResource method getSolver.
@ApiOperation(value = "Retrieves solver by its identifier from given container", response = SolverInstance.class, code = 200)
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 404, message = "Container does not exist or failure in creating solver") })
@GET
@Path(RestURI.SOLVER_ID_URI)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getSolver(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "container id where the solver resides", required = true) @PathParam(CONTAINER_ID) String containerId, @ApiParam(value = "identifier of the solver", required = true) @PathParam(SOLVER_ID) String solverId) {
Variant v = getVariant(headers);
Header conversationIdHeader = buildConversationIdHeader(containerId, solverService.getKieServerRegistry(), headers);
try {
ServiceResponse<SolverInstance> result = solverService.getSolver(containerId, solverId);
if (result.getType() == ServiceResponse.ResponseType.SUCCESS) {
return createCorrectVariant(marshallerHelper, containerId, result.getResult(), headers, Response.Status.OK, conversationIdHeader);
}
return createCorrectVariant(marshallerHelper, containerId, result.getMsg(), headers, Response.Status.NOT_FOUND, conversationIdHeader);
} catch (Exception e) {
logger.error("Unexpected error retrieving solver state {}", e.getMessage(), e);
return internalServerError(MessageFormat.format(Messages.UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader);
}
}
use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.
the class SolverResource method getSolverWithBestSolution.
@ApiOperation(value = "Retrieves best solution from solver within container", response = SolverInstance.class, code = 200)
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 404, message = "Container does not exist or failure in creating solver") })
@GET
@Path(SOLVER_ID_URI + "/" + SOLVER_BEST_SOLUTION)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getSolverWithBestSolution(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "container id where the solver resides", required = true) @PathParam(CONTAINER_ID) String containerId, @ApiParam(value = "identifier of the solver", required = true) @PathParam(SOLVER_ID) String solverId) {
Variant v = getVariant(headers);
Header conversationIdHeader = buildConversationIdHeader(containerId, solverService.getKieServerRegistry(), headers);
try {
ServiceResponse<SolverInstance> result = solverService.getSolverWithBestSolution(containerId, solverId);
if (result.getType() == ServiceResponse.ResponseType.SUCCESS) {
return createCorrectVariant(marshallerHelper, containerId, result.getResult(), headers, Response.Status.OK, conversationIdHeader);
}
return createCorrectVariant(marshallerHelper, containerId, result.getMsg(), headers, Response.Status.NOT_FOUND, conversationIdHeader);
} catch (Exception e) {
logger.error("Unexpected error during processing {}", e.getMessage(), e);
return internalServerError(MessageFormat.format(Messages.UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader);
}
}
use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.
the class SolverResource method createSolver.
@ApiOperation(value = "Creates solver within given container", response = SolverInstance.class, code = 200)
@ApiResponses(value = { @ApiResponse(code = 500, message = "Unexpected error"), @ApiResponse(code = 400, message = "Container does not exist or failure in creating solver") })
@PUT
@Path(RestURI.SOLVER_ID_URI)
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createSolver(@javax.ws.rs.core.Context HttpHeaders headers, @ApiParam(value = "container id where the solver config resides", required = true) @PathParam(CONTAINER_ID) String containerId, @ApiParam(value = "identifier of the solver to create", required = true) @PathParam(SOLVER_ID) String solverId, @ApiParam(value = "solver instance details as SolverInstance type", required = true) String payload) {
logger.debug("About to create solver {} on container {}", solverId, containerId);
Variant v = getVariant(headers);
Header conversationIdHeader = buildConversationIdHeader(containerId, solverService.getKieServerRegistry(), headers);
try {
String contentType = getContentType(headers);
if (solverService.getKieServerRegistry().getContainer(containerId, ContainerLocatorProvider.get().getLocator()) == null) {
ServiceResponse<SolverInstance> response = new ServiceResponse<SolverInstance>(ServiceResponse.ResponseType.FAILURE, "Failed to create solver. Container does not exist: " + containerId);
return createCorrectVariant(response, headers, Response.Status.BAD_REQUEST);
}
SolverInstance solverInstance = marshallerHelper.unmarshal(containerId, payload, contentType, SolverInstance.class);
ServiceResponse<SolverInstance> response = solverService.createSolver(containerId, solverId, solverInstance);
if (response.getType() == ServiceResponse.ResponseType.SUCCESS) {
return createCorrectVariant(marshallerHelper, containerId, response.getResult(), headers, Response.Status.CREATED, conversationIdHeader);
}
return createCorrectVariant(marshallerHelper, containerId, response.getMsg(), headers, Response.Status.BAD_REQUEST, conversationIdHeader);
} catch (Exception e) {
logger.error("Unexpected error creating solver '{}' on container '{}': {}", solverId, containerId, e.getMessage(), e);
return internalServerError(MessageFormat.format(Messages.UNEXPECTED_ERROR, e.getMessage()), v, conversationIdHeader);
}
}
Aggregations