Search in sources :

Example 1 with SolverResponse

use of org.acme.schooltimetabling.message.SolverResponse in project optaplanner-quickstarts by kiegroup.

the class TimeTableMessagingHandlerTest method solve.

@Test
@Timeout(TEST_TIMEOUT_SECONDS)
void solve() {
    long problemId = 1L;
    TimeTable unsolvedTimeTable = createTestTimeTable();
    sendSolverRequest(new SolverRequest(problemId, unsolvedTimeTable));
    SolverResponse solverResponse = receiveSolverResponse(MESSAGE_RECEIVE_TIMEOUT_SECONDS);
    assertThat(SolverResponse.ResponseStatus.SUCCESS == solverResponse.getResponseStatus());
    assertThat(problemId == solverResponse.getProblemId());
    assertThat(solverResponse.getTimeTable().getLessonList()).hasSameSizeAs(unsolvedTimeTable.getLessonList());
    assertThat(solverResponse.getTimeTable().getScore().isFeasible()).isTrue();
}
Also used : SolverRequest(org.acme.schooltimetabling.message.SolverRequest) TimeTable(org.acme.schooltimetabling.domain.TimeTable) SolverResponse(org.acme.schooltimetabling.message.SolverResponse) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 2 with SolverResponse

use of org.acme.schooltimetabling.message.SolverResponse in project optaplanner-quickstarts by kiegroup.

the class TimeTableResource method process.

@Incoming("solver_response")
@Acknowledgment(Acknowledgment.Strategy.POST_PROCESSING)
public void process(String solverResponseMessage) {
    SolverResponse solverResponse;
    try {
        solverResponse = objectMapper.readValue(solverResponseMessage, SolverResponse.class);
    } catch (JsonProcessingException ex) {
        throw new IllegalStateException("Unable to deserialize the solver response.", ex);
    }
    if (solverResponse.isSuccess()) {
        TimeTable timeTable = solverResponse.getTimeTable();
        timeTable.setSolverStatus(SolverStatus.NOT_SOLVING);
        timeTableRepository.update(timeTable);
    } else {
        timeTableRepository.get().setSolverStatus(SolverStatus.NOT_SOLVING);
        throw new IllegalStateException("Solving failed with exception class (" + solverResponse.getErrorInfo().getExceptionClassName() + ") and message (" + solverResponse.getErrorInfo().getExceptionMessage() + ").");
    }
}
Also used : SolverResponse(org.acme.schooltimetabling.message.SolverResponse) TimeTable(org.acme.schooltimetabling.domain.TimeTable) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Incoming(org.eclipse.microprofile.reactive.messaging.Incoming) Acknowledgment(org.eclipse.microprofile.reactive.messaging.Acknowledgment)

Example 3 with SolverResponse

use of org.acme.schooltimetabling.message.SolverResponse in project optaplanner-quickstarts by kiegroup.

the class TimeTableResourceTest method solveAndRetrieve.

@Test
@Timeout(60L)
void solveAndRetrieve() throws JsonProcessingException {
    timeTableResource.solve();
    await().timeout(AWAIT_TIMEOUT).until(solverRequestSink::received, messages -> messages.size() == 1);
    Message<String> solverRequestMessage = solverRequestSink.received().get(0);
    SolverRequest solverRequest = objectMapper.readValue(solverRequestMessage.getPayload(), SolverRequest.class);
    // Assign one lesson to simulate solving.
    TimeTable requestTimeTable = solverRequest.getTimeTable();
    Lesson firstLesson = requestTimeTable.getLessonList().get(0);
    firstLesson.setRoom(requestTimeTable.getRoomList().get(0));
    firstLesson.setTimeslot(requestTimeTable.getTimeslotList().get(0));
    SolverResponse solverResponse = new SolverResponse(solverRequest.getProblemId(), requestTimeTable);
    solverResponseSource.send(objectMapper.writeValueAsString(solverResponse));
    // Wait until the client receives the message and saves the new timetable to a database.
    await().timeout(AWAIT_TIMEOUT).until(timeTableResource::getTimeTable, timeTable -> timeTable.getLessonList().get(0).getRoom() != null);
    Lesson solvedFirstLesson = timeTableResource.getTimeTable().getLessonList().get(0);
    assertThat(solvedFirstLesson.getRoom()).isNotNull();
    assertThat(solvedFirstLesson.getTimeslot()).isNotNull();
}
Also used : SolverRequest(org.acme.schooltimetabling.message.SolverRequest) TimeTable(org.acme.schooltimetabling.domain.TimeTable) SolverResponse(org.acme.schooltimetabling.message.SolverResponse) Lesson(org.acme.schooltimetabling.domain.Lesson) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 4 with SolverResponse

use of org.acme.schooltimetabling.message.SolverResponse in project optaplanner-quickstarts by kiegroup.

the class TimeTableMessagingHandler method replyFailure.

private void replyFailure(Message<String> solverRequestMessage, Long problemId, Throwable throwable) {
    SolverResponse solverResponse = new SolverResponse(problemId, new SolverResponse.ErrorInfo(throwable.getClass().getName(), throwable.getMessage()));
    reply(solverRequestMessage, solverResponse, serializationException -> {
        throw new IllegalStateException("Unable to serialize error response.", serializationException);
    });
}
Also used : SolverResponse(org.acme.schooltimetabling.message.SolverResponse)

Example 5 with SolverResponse

use of org.acme.schooltimetabling.message.SolverResponse in project optaplanner-quickstarts by kiegroup.

the class TimeTableMessagingHandler method replySuccess.

private void replySuccess(Message<String> solverRequestMessage, Long problemId, TimeTable solution) {
    SolverResponse solverResponse = new SolverResponse(problemId, solution);
    reply(solverRequestMessage, solverResponse, exception -> replyFailure(solverRequestMessage, problemId, exception));
}
Also used : SolverResponse(org.acme.schooltimetabling.message.SolverResponse)

Aggregations

SolverResponse (org.acme.schooltimetabling.message.SolverResponse)6 TimeTable (org.acme.schooltimetabling.domain.TimeTable)4 QuarkusTest (io.quarkus.test.junit.QuarkusTest)3 SolverRequest (org.acme.schooltimetabling.message.SolverRequest)3 Test (org.junit.jupiter.api.Test)3 Timeout (org.junit.jupiter.api.Timeout)3 Lesson (org.acme.schooltimetabling.domain.Lesson)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Room (org.acme.schooltimetabling.domain.Room)1 Timeslot (org.acme.schooltimetabling.domain.Timeslot)1 Acknowledgment (org.eclipse.microprofile.reactive.messaging.Acknowledgment)1 Incoming (org.eclipse.microprofile.reactive.messaging.Incoming)1