Search in sources :

Example 1 with SolverInstance

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();
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) ScoreWrapper(org.kie.server.api.model.instance.ScoreWrapper)

Example 2 with SolverInstance

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());
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) DateObjectUnannotated(org.kie.server.api.marshalling.objects.DateObjectUnannotated) Test(org.junit.Test)

Example 3 with SolverInstance

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);
    }
}
Also used : RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) Variant(javax.ws.rs.core.Variant) SolverInstance(org.kie.server.api.model.instance.SolverInstance) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with SolverInstance

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);
    }
}
Also used : RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) Variant(javax.ws.rs.core.Variant) SolverInstance(org.kie.server.api.model.instance.SolverInstance) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 5 with SolverInstance

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);
    }
}
Also used : RestUtils.createCorrectVariant(org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant) RestUtils.getVariant(org.kie.server.remote.rest.common.util.RestUtils.getVariant) Variant(javax.ws.rs.core.Variant) SolverInstance(org.kie.server.api.model.instance.SolverInstance) ServiceResponse(org.kie.server.api.model.ServiceResponse) Header(org.kie.server.remote.rest.common.Header) RestUtils.buildConversationIdHeader(org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) PUT(javax.ws.rs.PUT)

Aggregations

SolverInstance (org.kie.server.api.model.instance.SolverInstance)26 Test (org.junit.Test)11 ServiceResponse (org.kie.server.api.model.ServiceResponse)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 ApiOperation (io.swagger.annotations.ApiOperation)3 ApiResponses (io.swagger.annotations.ApiResponses)3 HashMap (java.util.HashMap)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 Variant (javax.ws.rs.core.Variant)3 ExecutionServerCommand (org.kie.camel.container.api.ExecutionServerCommand)3 CommandScript (org.kie.server.api.commands.CommandScript)3 Header (org.kie.server.remote.rest.common.Header)3 RestUtils.buildConversationIdHeader (org.kie.server.remote.rest.common.util.RestUtils.buildConversationIdHeader)3 RestUtils.createCorrectVariant (org.kie.server.remote.rest.common.util.RestUtils.createCorrectVariant)3 RestUtils.getVariant (org.kie.server.remote.rest.common.util.RestUtils.getVariant)3 GET (javax.ws.rs.GET)2 CreateSolverCommand (org.kie.server.api.commands.optaplanner.CreateSolverCommand)2 GetSolverCommand (org.kie.server.api.commands.optaplanner.GetSolverCommand)2