Search in sources :

Example 1 with SolutionUpdatedOnBackgroundDataEvent

use of org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent in project kogito-apps by kiegroup.

the class TaskAssigningService method processDataEvents.

/**
 * Invoked when a set of events are received for processing.
 * Three main scenarios might happen:
 * a) A solution already exists and thus the proper problem fact changes are calculated and passed to the solver for
 * execution. If there are no changes to apply, wait for more events.
 *
 * b) No solution exists. Instruct the solution data loader to read the users information and the solver will be
 * started when this information is returned plus the information collected from the events.
 *
 * c) A solution improved on background event arrives and must be processed accordingly.
 *
 * @param events a list of events to process.
 */
synchronized void processDataEvents(List<DataEvent<?>> events) {
    if (isNotOperative()) {
        LOGGER.warn(SERVICE_INOPERATIVE_MESSAGE, context.getStatus());
        return;
    }
    try {
        List<TaskDataEvent> newTaskDataEvents = filterNewestTaskEventsInContext(context, events);
        if (currentSolution.get() == null) {
            List<TaskDataEvent> activeTaskEvents = newTaskDataEvents.stream().filter(IS_ACTIVE_TASK_EVENT).collect(Collectors.toList());
            if (!activeTaskEvents.isEmpty()) {
                // b) no solution exists, store the events and get the users from the external user service.
                startingEvents = activeTaskEvents;
                startingFromEvents.set(true);
                loadSolutionData(false, true, config.getDataLoaderPageSize());
            } else {
                resumeEvents();
            }
        } else {
            // a) a solution exists, calculate and apply the potential changes if any.
            UserDataEvent userDataEvent = filterNewestUserEvent(events);
            List<ProblemFactChange<TaskAssigningSolution>> changes = SolutionChangesBuilder.create().forSolution(currentSolution.get()).withContext(context).withUserServiceConnector(userServiceConnectorDelegate).withProcessors(processorRegistry).fromTasksData(fromTaskDataEvents(newTaskDataEvents)).fromUserDataEvent(userDataEvent).build();
            if (!changes.isEmpty()) {
                LOGGER.debug("processDataEvents - there are changes: {} to apply", changes.size());
                cancelScheduledImproveSolutionOnBackgroundTimer();
                solverExecutor.addProblemFactChanges(changes);
            } else {
                // c) check if an event for the improve solution on background period has arrived and a better
                // solution was produced
                SolutionUpdatedOnBackgroundDataEvent solutionImprovedOnBackgroundEvent = filterNewestSolutionUpdatedOnBackgroundEvent(events);
                TaskAssigningSolution currentLastBestSolution = lastBestSolution.get();
                if (solutionImprovedOnBackgroundEvent != null && hasToApplyImprovedOnBackgroundSolution(solutionImprovedOnBackgroundEvent, currentLastBestSolution)) {
                    // a better solution was produced during the improveSolutionOnBackgroundDuration period
                    LOGGER.debug("processDataEvents - apply the improved on background solution: {}", currentLastBestSolution);
                    executeSolutionChange(currentLastBestSolution);
                } else {
                    executePlanOrResumeEvents(currentSolution.get());
                }
            }
        }
    } catch (Exception e) {
        failFast(e);
    }
}
Also used : TaskDataEvent(org.kie.kogito.taskassigning.service.event.TaskDataEvent) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent)

Example 2 with SolutionUpdatedOnBackgroundDataEvent

use of org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent in project kogito-apps by kiegroup.

the class EventUtilTest method filterNewestSolutionUpdatedOnBackgroundEvent.

@Test
void filterNewestSolutionUpdatedOnBackgroundEvent() {
    List<DataEvent<?>> eventList = Arrays.asList(SOLUTION_UPDATED_DATA_EVENT_2, SOLUTION_UPDATED_DATA_EVENT_3, SOLUTION_UPDATED_DATA_EVENT_1);
    SolutionUpdatedOnBackgroundDataEvent result = EventUtil.filterNewestSolutionUpdatedOnBackgroundEvent(eventList);
    assertThat(result).isSameAs(SOLUTION_UPDATED_DATA_EVENT_3);
}
Also used : TaskDataEvent(org.kie.kogito.taskassigning.service.event.TaskDataEvent) DataEvent(org.kie.kogito.taskassigning.service.event.DataEvent) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent) SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent) Test(org.junit.jupiter.api.Test)

Example 3 with SolutionUpdatedOnBackgroundDataEvent

use of org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent in project kogito-apps by kiegroup.

the class TaskAssigningServiceTest method prepareOnSolutionUpdatedOnBackground.

@SuppressWarnings("unchecked")
private void prepareOnSolutionUpdatedOnBackground(BendableLongScore initialSolutionScore, TaskAssigningSolution newBestSolution) throws Exception {
    prepareStart();
    TaskAssigningSolution initialSolution = buildSolutionWithScore(initialSolutionScore);
    context.setTaskPublished(TASK_1_ID, true);
    context.setTaskPublished(TASK_2_ID, true);
    context.setTaskPublished(TASK_3_ID, true);
    context.setTaskPublished(TASK_4_ID, true);
    doReturn(IMPROVE_SOLUTION_ON_BACKGROUND_DURATION).when(config).getImproveSolutionOnBackgroundDuration();
    doReturn(TIMER_ID).when(vertx).setTimer(eq(IMPROVE_SOLUTION_ON_BACKGROUND_DURATION.toMillis()), any(Handler.class));
    taskAssigningService.onBestSolutionChange(mockEvent(initialSolution));
    verify(managedExecutor).runAsync(managedExecutorCaptor.capture());
    managedExecutorCaptor.getValue().run();
    taskAssigningService.onBestSolutionChange(mockEvent(newBestSolution));
    List<DataEvent<?>> eventList = Collections.singletonList(new SolutionUpdatedOnBackgroundDataEvent(TIMER_ID, ZonedDateTime.now()));
    dataEventsConsumerCaptor.getValue().accept(eventList);
    verify(managedExecutor, times(2)).runAsync(managedExecutorCaptor.capture());
    managedExecutorCaptor.getValue().run();
    verify(taskAssigningService).executeSolutionChange(initialSolution);
}
Also used : TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) Handler(io.vertx.core.Handler) TaskDataEvent(org.kie.kogito.taskassigning.service.event.TaskDataEvent) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent) DataEvent(org.kie.kogito.taskassigning.service.event.DataEvent) SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent)

Example 4 with SolutionUpdatedOnBackgroundDataEvent

use of org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent in project kogito-apps by kiegroup.

the class TaskAssigningService method onSolutionImprovedOnBackgroundEvent.

void onSolutionImprovedOnBackgroundEvent(@Observes SolutionImprovedOnBackgroundEvent event) {
    LOGGER.debug("onSolutionImprovedOnBackgroundEvent: timerId: {}", event.getTimerId());
    serviceEventConsumer.accept(new SolutionUpdatedOnBackgroundDataEvent(event.getTimerId(), ZonedDateTime.now()));
}
Also used : SolutionUpdatedOnBackgroundDataEvent(org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent)

Aggregations

SolutionUpdatedOnBackgroundDataEvent (org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent)4 TaskDataEvent (org.kie.kogito.taskassigning.service.event.TaskDataEvent)3 UserDataEvent (org.kie.kogito.taskassigning.service.event.UserDataEvent)3 TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)2 DataEvent (org.kie.kogito.taskassigning.service.event.DataEvent)2 Handler (io.vertx.core.Handler)1 Test (org.junit.jupiter.api.Test)1 AssignTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)1 ProblemFactChange (org.optaplanner.core.api.solver.ProblemFactChange)1