Search in sources :

Example 1 with ScoreWrapper

use of org.kie.server.api.model.instance.ScoreWrapper 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 ScoreWrapper

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

the class SolverServiceBase method updateSolverInstance.

private void updateSolverInstance(SolverInstanceContext sic) {
    synchronized (sic) {
        // We keep track of the solver status ourselves, so there's no need to call buggy updateSolverStatus( sic );
        Score<?> bestScore = sic.getSolver().getBestScore();
        sic.getInstance().setScoreWrapper(new ScoreWrapper(bestScore));
    }
}
Also used : ScoreWrapper(org.kie.server.api.model.instance.ScoreWrapper)

Example 3 with ScoreWrapper

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

the class OptaplannerIntegrationTest method testGetBestSolution.

@Test(timeout = 60000)
public void testGetBestSolution() throws Exception {
    SolverInstance solverInstance = solverClient.createSolver(CONTAINER_1_ID, SOLVER_1_ID, SOLVER_1_CONFIG);
    // Start the solver
    Object planningProblem = loadPlanningProblem(10, 30);
    solverClient.solvePlanningProblem(CONTAINER_1_ID, SOLVER_1_ID, planningProblem);
    Object solution = null;
    HardSoftScore score = null;
    // The test timeout will interrupt this thread if it takes too long
    while (!Thread.currentThread().isInterrupted()) {
        solverInstance = solverClient.getSolverWithBestSolution(CONTAINER_1_ID, SOLVER_1_ID);
        assertNotNull(solverInstance);
        solution = solverInstance.getBestSolution();
        ScoreWrapper scoreWrapper = solverInstance.getScoreWrapper();
        assertNotNull(scoreWrapper);
        if (scoreWrapper.toScore() != null) {
            assertEquals(HardSoftScore.class, scoreWrapper.getScoreClass());
            score = (HardSoftScore) scoreWrapper.toScore();
        }
        // Wait until the solver finished initializing the solution
        if (solution != null && score != null && score.isSolutionInitialized()) {
            break;
        }
        Thread.sleep(1000);
    }
    assertNotNull(score);
    assertTrue(score.isSolutionInitialized());
    assertTrue(score.getHardScore() <= 0);
    // A soft score of 0 is impossible because we'll always need at least 1 computer
    assertTrue(score.getSoftScore() < 0);
    List<?> computerList = (List<?>) KieServerReflections.valueOf(solution, "computerList");
    assertEquals(10, computerList.size());
    List<?> processList = (List<?>) KieServerReflections.valueOf(solution, "processList");
    assertEquals(30, processList.size());
    for (Object process : processList) {
        Object computer = KieServerReflections.valueOf(process, "computer");
        assertNotNull(computer);
        // TODO: Change to identity comparation after @XmlID is implemented
        assertTrue(computerList.contains(computer));
    }
    solverClient.disposeSolver(CONTAINER_1_ID, SOLVER_1_ID);
}
Also used : SolverInstance(org.kie.server.api.model.instance.SolverInstance) ScoreWrapper(org.kie.server.api.model.instance.ScoreWrapper) ArrayList(java.util.ArrayList) List(java.util.List) HardSoftScore(org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore) Test(org.junit.Test)

Aggregations

ScoreWrapper (org.kie.server.api.model.instance.ScoreWrapper)3 SolverInstance (org.kie.server.api.model.instance.SolverInstance)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Test (org.junit.Test)1 HardSoftScore (org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore)1