Search in sources :

Example 11 with SolverInstance

use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.

the class OptaplannerIntegrationTest method testExecuteRunningSolver.

@Test
public void testExecuteRunningSolver() throws Exception {
    SolverInstance solverInstance = solverClient.createSolver(CONTAINER_1_ID, SOLVER_1_ID, SOLVER_1_CONFIG);
    assertNotNull(solverInstance);
    assertEquals(SolverInstance.SolverStatus.NOT_SOLVING, solverInstance.getStatus());
    // start solver
    Object planningProblem = loadPlanningProblem(5, 15);
    solverClient.solvePlanningProblem(CONTAINER_1_ID, SOLVER_1_ID, planningProblem);
    // start solver again
    try {
        solverClient.solvePlanningProblem(CONTAINER_1_ID, SOLVER_1_ID, planningProblem);
        fail("A KieServicesException should have been thrown by now.");
    } catch (KieServicesException e) {
        KieServerAssert.assertResultContainsStringRegex(e.getMessage(), ".*Solver .* on container .* is already executing.*");
    }
    solverClient.disposeSolver(CONTAINER_1_ID, SOLVER_1_ID);
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) KieServicesException(org.kie.server.api.exception.KieServicesException) Test(org.junit.Test)

Example 12 with SolverInstance

use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.

the class OptaplannerIntegrationTest method testExecuteRealtimePlanningSolverBulkItemSubmit.

private void testExecuteRealtimePlanningSolverBulkItemSubmit(boolean daemon) throws Exception {
    SolverInstance solverInstance = solverClient.createSolver(CONTAINER_1_ID, SOLVER_1_ID, daemon ? SOLVER_1_REALTIME_DAEMON_CONFIG : SOLVER_1_REALTIME_CONFIG);
    assertNotNull(solverInstance);
    assertEquals(SolverInstance.SolverStatus.NOT_SOLVING, solverInstance.getStatus());
    final int initialComputerCount = 5;
    final int numberOfComputersToAdd = 5;
    final Object planningProblem = loadPlanningProblem(initialComputerCount, 15);
    solverClient.solvePlanningProblem(CONTAINER_1_ID, SOLVER_1_ID, planningProblem);
    SolverInstance solver = solverClient.getSolver(CONTAINER_1_ID, SOLVER_1_ID);
    assertEquals(SolverInstance.SolverStatus.SOLVING, solver.getStatus());
    Thread.sleep(3000);
    List<ProblemFactChange> problemFactChangeList = new ArrayList<>(numberOfComputersToAdd);
    for (int i = 0; i < numberOfComputersToAdd; i++) {
        problemFactChangeList.add(loadAddProblemFactChange(initialComputerCount + i));
    }
    solverClient.addProblemFactChanges(CONTAINER_1_ID, SOLVER_1_ID, problemFactChangeList);
    List computerList = null;
    do {
        final Object bestSolution = verifySolverAndGetBestSolution();
        computerList = getCloudBalanceComputerList(bestSolution);
    } while (initialComputerCount + numberOfComputersToAdd != computerList.size());
    assertNotNull(computerList);
    assertTrue(solverClient.isEveryProblemFactChangeProcessed(CONTAINER_1_ID, SOLVER_1_ID));
    Thread.sleep(3000);
    problemFactChangeList.clear();
    for (int i = 0; i < numberOfComputersToAdd; i++) {
        problemFactChangeList.add(loadDeleteProblemFactChange(computerList.get(i)));
    }
    solverClient.addProblemFactChanges(CONTAINER_1_ID, SOLVER_1_ID, problemFactChangeList);
    do {
        final Object bestSolution = verifySolverAndGetBestSolution();
        computerList = getCloudBalanceComputerList(bestSolution);
    } while (initialComputerCount != computerList.size());
    assertTrue(solverClient.isEveryProblemFactChangeProcessed(CONTAINER_1_ID, SOLVER_1_ID));
    solverClient.disposeSolver(CONTAINER_1_ID, SOLVER_1_ID);
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ProblemFactChange(org.optaplanner.core.impl.solver.ProblemFactChange)

Example 13 with SolverInstance

use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.

the class OptaplannerIntegrationTest method testGetSolvers.

@Test
public void testGetSolvers() {
    List<SolverInstance> solverInstanceList = solverClient.getSolvers(CONTAINER_1_ID);
    assertNotNull(solverInstanceList);
    assertEquals(0, solverInstanceList.size());
    solverClient.createSolver(CONTAINER_1_ID, SOLVER_1_ID, SOLVER_1_CONFIG);
    solverInstanceList = solverClient.getSolvers(CONTAINER_1_ID);
    assertNotNull(solverInstanceList);
    assertEquals(1, solverInstanceList.size());
    SolverInstance returnedInstance = solverInstanceList.get(0);
    assertEquals(CONTAINER_1_ID, returnedInstance.getContainerId());
    assertEquals(SOLVER_1_CONFIG, returnedInstance.getSolverConfigFile());
    assertEquals(SOLVER_1_ID, returnedInstance.getSolverId());
    assertEquals(SolverInstance.getSolverInstanceKey(CONTAINER_1_ID, SOLVER_1_ID), returnedInstance.getSolverInstanceKey());
    assertEquals(SolverInstance.SolverStatus.NOT_SOLVING, returnedInstance.getStatus());
    assertNotNull(returnedInstance.getScoreWrapper());
    assertNull(returnedInstance.getScoreWrapper().toScore());
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) Test(org.junit.Test)

Example 14 with SolverInstance

use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.

the class OptaplannerCommandServiceImpl method executeScript.

@Override
public ServiceResponsesList executeScript(CommandScript commands, MarshallingFormat marshallingFormat, String classType) {
    List<ServiceResponse<?>> responses = new ArrayList<>();
    for (KieServerCommand command : commands.getCommands()) {
        try {
            ServiceResponse<?> response;
            logger.debug("About to execute command: {}", command);
            if (command instanceof CreateSolverCommand) {
                CreateSolverCommand createSolverCommand = (CreateSolverCommand) command;
                String containerId = context.getContainerId(createSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                SolverInstance instance = new SolverInstance();
                instance.setContainerId(containerId);
                instance.setSolverId(createSolverCommand.getSolverId());
                instance.setSolverConfigFile(createSolverCommand.getSolverConfigFile());
                response = solverService.createSolver(containerId, createSolverCommand.getSolverId(), instance);
            } else if (command instanceof GetSolversCommand) {
                GetSolversCommand getSolversCommand = (GetSolversCommand) command;
                String containerId = context.getContainerId(getSolversCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolvers(containerId);
            } else if (command instanceof GetSolverCommand) {
                GetSolverCommand getSolverCommand = (GetSolverCommand) command;
                String containerId = context.getContainerId(getSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolver(containerId, getSolverCommand.getSolverId());
            } else if (command instanceof GetSolverWithBestSolutionCommand) {
                GetSolverWithBestSolutionCommand getSolverWithBestSolutionCommand = (GetSolverWithBestSolutionCommand) command;
                String containerId = context.getContainerId(getSolverWithBestSolutionCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.getSolverWithBestSolution(containerId, getSolverWithBestSolutionCommand.getSolverId());
            } else if (command instanceof SolvePlanningProblemCommand) {
                SolvePlanningProblemCommand solvePlanningProblemCommand = (SolvePlanningProblemCommand) command;
                String containerId = context.getContainerId(solvePlanningProblemCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                KieContainerInstanceImpl kc = context.getContainer(containerId);
                Marshaller marshaller = kc.getMarshaller(marshallingFormat);
                Object planningProblem = marshaller.unmarshall(solvePlanningProblemCommand.getPlanningProblem(), Object.class);
                response = solverService.solvePlanningProblem(containerId, solvePlanningProblemCommand.getSolverId(), planningProblem);
            } else if (command instanceof TerminateSolverEarlyCommand) {
                TerminateSolverEarlyCommand terminateSolverEarlyCommand = (TerminateSolverEarlyCommand) command;
                String containerId = context.getContainerId(terminateSolverEarlyCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.terminateSolverEarly(containerId, terminateSolverEarlyCommand.getSolverId());
            } else if (command instanceof AddProblemFactChangeCommand) {
                AddProblemFactChangeCommand addProblemFactChangeCommand = (AddProblemFactChangeCommand) command;
                String containerId = context.getContainerId(addProblemFactChangeCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangeCommand.getSolverId(), addProblemFactChangeCommand.getProblemFactChange());
            } else if (command instanceof AddProblemFactChangesCommand) {
                AddProblemFactChangesCommand addProblemFactChangesCommand = (AddProblemFactChangesCommand) command;
                String containerId = context.getContainerId(addProblemFactChangesCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.addProblemFactChanges(containerId, addProblemFactChangesCommand.getSolverId(), addProblemFactChangesCommand.getProblemFactChanges());
            } else if (command instanceof IsEveryProblemFactChangeProcessedCommand) {
                IsEveryProblemFactChangeProcessedCommand isEveryProblemFactChangeProcessedCommand = (IsEveryProblemFactChangeProcessedCommand) command;
                String containerId = context.getContainerId(isEveryProblemFactChangeProcessedCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                ServiceResponse<Boolean> everyProblemFactChangeProcessedResponse = solverService.isEveryProblemFactChangeProcessed(containerId, isEveryProblemFactChangeProcessedCommand.getSolverId());
                if (marshallingFormat.equals(MarshallingFormat.JAXB)) {
                    Object wrappedResult = ModelWrapper.wrap(everyProblemFactChangeProcessedResponse.getResult());
                    response = new ServiceResponse<>(everyProblemFactChangeProcessedResponse.getType(), everyProblemFactChangeProcessedResponse.getMsg(), wrappedResult);
                } else {
                    response = everyProblemFactChangeProcessedResponse;
                }
            } else if (command instanceof DisposeSolverCommand) {
                DisposeSolverCommand disposeSolverCommand = (DisposeSolverCommand) command;
                String containerId = context.getContainerId(disposeSolverCommand.getContainerId(), ContainerLocatorProvider.get().getLocator());
                response = solverService.disposeSolver(containerId, disposeSolverCommand.getSolverId());
            } else {
                throw new IllegalStateException("Unsupported command: " + command);
            }
            logger.debug("Service returned response {}", response);
            // return successful result
            responses.add(response);
        } catch (Throwable e) {
            logger.error("Error while processing {} command", command, e);
            // return failure result
            responses.add(new ServiceResponse<>(ServiceResponse.ResponseType.FAILURE, e.getMessage()));
        }
    }
    logger.debug("About to return responses '{}'", responses);
    return new ServiceResponsesList(responses);
}
Also used : ServiceResponsesList(org.kie.server.api.model.ServiceResponsesList) KieServerCommand(org.kie.server.api.model.KieServerCommand) ArrayList(java.util.ArrayList) TerminateSolverEarlyCommand(org.kie.server.api.commands.optaplanner.TerminateSolverEarlyCommand) SolverInstance(org.kie.server.api.model.instance.SolverInstance) ServiceResponse(org.kie.server.api.model.ServiceResponse) AddProblemFactChangesCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangesCommand) IsEveryProblemFactChangeProcessedCommand(org.kie.server.api.commands.optaplanner.IsEveryProblemFactChangeProcessedCommand) GetSolverCommand(org.kie.server.api.commands.optaplanner.GetSolverCommand) Marshaller(org.kie.server.api.marshalling.Marshaller) GetSolversCommand(org.kie.server.api.commands.optaplanner.GetSolversCommand) SolvePlanningProblemCommand(org.kie.server.api.commands.optaplanner.SolvePlanningProblemCommand) GetSolverWithBestSolutionCommand(org.kie.server.api.commands.optaplanner.GetSolverWithBestSolutionCommand) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) CreateSolverCommand(org.kie.server.api.commands.optaplanner.CreateSolverCommand) AddProblemFactChangeCommand(org.kie.server.api.commands.optaplanner.AddProblemFactChangeCommand) DisposeSolverCommand(org.kie.server.api.commands.optaplanner.DisposeSolverCommand)

Example 15 with SolverInstance

use of org.kie.server.api.model.instance.SolverInstance in project droolsjbpm-integration by kiegroup.

the class SolverServiceBaseTest method setUp.

@Before
public void setUp() {
    solverInstance = new SolverInstance();
    solverInstance.setContainerId(CONTAINER_ID);
    solverInstance.setSolverId(SOLVER_ID);
    solverInstance.setSolverConfigFile(SOLVER_CONFIG);
    doReturn(internalKieContainer).when(containerInstance).getKieContainer();
    doReturn(getClass().getClassLoader()).when(internalKieContainer).getClassLoader();
    doReturn(containerInstance).when(context).getContainer(CONTAINER_ID);
    serviceBase = spy(new SolverServiceBase(context, executorService));
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) Before(org.junit.Before)

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