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);
}
}
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);
}
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);
}
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()));
}
Aggregations