Search in sources :

Example 1 with UserDataEvent

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

the class SolutionChangesBuilderTest method addNewUserChange.

@Test
void addNewUserChange() {
    org.kie.kogito.taskassigning.user.service.User newExternalUser = mockExternalUser(USER_1);
    User newUser = mockUser(USER_1);
    TaskAssigningSolution solution = mockSolution(Collections.emptyList(), Collections.emptyList());
    UserDataEvent event = new UserDataEvent(Collections.singletonList(newExternalUser), ZonedDateTime.now());
    List<ProblemFactChange<TaskAssigningSolution>> result = SolutionChangesBuilder.create().withContext(context).withUserServiceConnector(userServiceConnector).withProcessors(processorRegistry).fromTasksData(Collections.emptyList()).fromUserDataEvent(event).forSolution(solution).build();
    AddUserProblemFactChange expected = new AddUserProblemFactChange(newUser);
    assertChangeIsTheChangeSetId(result, 0);
    assertChange(result, 1, expected);
    assertThatUserProcessorsWereApplied(newExternalUser);
}
Also used : User(org.kie.kogito.taskassigning.core.model.User) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) AddUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) RemoveUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) DisableUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange) UserPropertyChangeProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange) AddUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange) RemoveTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange) AddTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange) ReleaseTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange) Test(org.junit.jupiter.api.Test)

Example 2 with UserDataEvent

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

the class SolutionChangesBuilderTest method addDisableUserChange.

@Test
void addDisableUserChange() {
    User user = mockUser(USER_1);
    user.setEnabled(true);
    TaskAssigningSolution solution = mockSolution(Collections.singletonList(user), Collections.emptyList());
    UserDataEvent event = new UserDataEvent(Collections.emptyList(), ZonedDateTime.now());
    List<ProblemFactChange<TaskAssigningSolution>> result = SolutionChangesBuilder.create().withContext(context).withUserServiceConnector(userServiceConnector).withProcessors(processorRegistry).fromTasksData(Collections.emptyList()).fromUserDataEvent(event).forSolution(solution).build();
    DisableUserProblemFactChange expected = new DisableUserProblemFactChange(user);
    assertChangeIsTheChangeSetId(result, 0);
    assertChange(result, 1, expected);
    assertThatUserProcessorsWereNotApplied();
}
Also used : User(org.kie.kogito.taskassigning.core.model.User) DisableUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) RemoveUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) DisableUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange) UserPropertyChangeProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange) AddUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange) RemoveTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange) AddTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange) ReleaseTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange) Test(org.junit.jupiter.api.Test)

Example 3 with UserDataEvent

use of org.kie.kogito.taskassigning.service.event.UserDataEvent 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 4 with UserDataEvent

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

the class UserServiceAdapter method onQuerySuccessful.

private void onQuerySuccessful(List<User> users) {
    if (!destroyed.get()) {
        taskAssigningServiceEventConsumer.accept(new UserDataEvent(users, ZonedDateTime.now()));
        programNextExecution(config.getUserServiceSyncInterval());
    }
}
Also used : UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent)

Example 5 with UserDataEvent

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

the class SolutionChangesBuilderTest method addUpdateUserChange.

private void addUpdateUserChange(User user, org.kie.kogito.taskassigning.user.service.User updatedExternalUser) {
    TaskAssigningSolution solution = mockSolution(Collections.singletonList(user), Collections.emptyList());
    UserDataEvent event = new UserDataEvent(Collections.singletonList(updatedExternalUser), ZonedDateTime.now());
    List<ProblemFactChange<TaskAssigningSolution>> result = SolutionChangesBuilder.create().withContext(context).withUserServiceConnector(userServiceConnector).withProcessors(processorRegistry).fromTasksData(Collections.emptyList()).fromUserDataEvent(event).forSolution(solution).build();
    Set<Group> expectedGroups = updatedExternalUser.getGroups().stream().map(externalGroup -> new Group(externalGroup.getId())).collect(Collectors.toSet());
    UserPropertyChangeProblemFactChange expected = new UserPropertyChangeProblemFactChange(user, true, updatedExternalUser.getAttributes(), expectedGroups);
    assertChangeIsTheChangeSetId(result, 0);
    assertChange(result, 1, expected);
    assertThatUserProcessorsWereApplied(updatedExternalUser);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) RemoveUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange) Mock(org.mockito.Mock) TaskUtil.fromTaskData(org.kie.kogito.taskassigning.service.util.TaskUtil.fromTaskData) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) ZonedDateTime(java.time.ZonedDateTime) READY(org.kie.kogito.taskassigning.service.TaskState.READY) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment) HashMap(java.util.HashMap) Group(org.kie.kogito.taskassigning.core.model.Group) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) Captor(org.mockito.Captor) RESERVED(org.kie.kogito.taskassigning.service.TaskState.RESERVED) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) DisableUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange) Task(org.kie.kogito.taskassigning.core.model.Task) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) TestUtil.mockTaskData(org.kie.kogito.taskassigning.service.TestUtil.mockTaskData) UserPropertyChangeProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) AttributesProcessorRegistry(org.kie.kogito.taskassigning.service.processing.AttributesProcessorRegistry) AddUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange) Mockito.doReturn(org.mockito.Mockito.doReturn) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Set(java.util.Set) ScoreDirector(org.optaplanner.core.api.score.director.ScoreDirector) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) Collectors(java.util.stream.Collectors) User(org.kie.kogito.taskassigning.core.model.User) TestUtil.parseZonedDateTime(org.kie.kogito.taskassigning.service.TestUtil.parseZonedDateTime) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) RemoveTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange) AddTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange) ReleaseTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) Group(org.kie.kogito.taskassigning.core.model.Group) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) UserDataEvent(org.kie.kogito.taskassigning.service.event.UserDataEvent) RemoveUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) DisableUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange) UserPropertyChangeProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange) AddUserProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange) RemoveTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange) AddTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange) ReleaseTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange) UserPropertyChangeProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange)

Aggregations

UserDataEvent (org.kie.kogito.taskassigning.service.event.UserDataEvent)7 AssignTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)5 ProblemFactChange (org.optaplanner.core.api.solver.ProblemFactChange)5 Test (org.junit.jupiter.api.Test)4 TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)4 User (org.kie.kogito.taskassigning.core.model.User)3 AddTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange)3 AddUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange)3 DisableUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange)3 ReleaseTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange)3 RemoveTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange)3 RemoveUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange)3 UserPropertyChangeProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange)3 SolutionUpdatedOnBackgroundDataEvent (org.kie.kogito.taskassigning.service.event.SolutionUpdatedOnBackgroundDataEvent)3 TaskDataEvent (org.kie.kogito.taskassigning.service.event.TaskDataEvent)3 DataEvent (org.kie.kogito.taskassigning.service.event.DataEvent)2 ZonedDateTime (java.time.ZonedDateTime)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1