Search in sources :

Example 6 with ChainElement

use of org.kie.kogito.taskassigning.core.model.ChainElement in project kogito-apps by kiegroup.

the class RemoveTaskProblemFactChange method doChange.

@Override
public void doChange(ScoreDirector<TaskAssigningSolution> scoreDirector) {
    TaskAssigningSolution solution = scoreDirector.getWorkingSolution();
    TaskAssignment workingTaskAssignment = scoreDirector.lookUpWorkingObjectOrReturnNull(taskAssignment);
    if (workingTaskAssignment != null) {
        ChainElement previousElement = workingTaskAssignment.getPreviousElement();
        unlinkTaskAssignment(workingTaskAssignment, previousElement, scoreDirector);
        scoreDirector.beforeEntityRemoved(workingTaskAssignment);
        // Planning entity lists are already cloned by the SolutionCloner, no need to clone.
        solution.getTaskAssignmentList().remove(workingTaskAssignment);
        scoreDirector.afterEntityRemoved(workingTaskAssignment);
        scoreDirector.triggerVariableListeners();
    }
}
Also used : ChainElement(org.kie.kogito.taskassigning.core.model.ChainElement) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) ProblemFactChangeUtil.unlinkTaskAssignment(org.kie.kogito.taskassigning.core.model.solver.realtime.ProblemFactChangeUtil.unlinkTaskAssignment) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Example 7 with ChainElement

use of org.kie.kogito.taskassigning.core.model.ChainElement in project kogito-apps by kiegroup.

the class TaskHelperTest method hasPinnedTasks.

@Test
void hasPinnedTasks() {
    ChainElement chainElement = buildChainElement();
    assertThat(TaskHelper.hasPinnedTasks(chainElement)).isTrue();
}
Also used : ChainElement(org.kie.kogito.taskassigning.core.model.ChainElement) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with ChainElement

use of org.kie.kogito.taskassigning.core.model.ChainElement in project kogito-apps by kiegroup.

the class AssignTaskProblemFactChangeTest method doChangeForExistingUserAndTaskAssignmentThatBelongsToAnotherUser.

@Test
void doChangeForExistingUserAndTaskAssignmentThatBelongsToAnotherUser() {
    // Initial assignments:
    // User1 <- TASK_0 <- TASK_1 <- TASK_2 <- TASK_3 <- TASK_4
    // User2 <- USER2_TASK_0 <- USER2_TASK_1 <- USER2_TASK_2
    List<TaskAssignment> user1Tasks = buildUserTasks();
    User user1 = buildUser(user1Tasks);
    List<TaskAssignment> user2Tasks = Arrays.asList(mockTaskAssignment(USER2_TASK_0, false), mockTaskAssignment(USER2_TASK_1, false), mockTaskAssignment(USER2_TASK_2, false));
    User user2 = mockUser("USER_2", user2Tasks);
    TaskAssignment taskAssignment = mockTaskAssignment(USER2_TASK_1, false);
    TaskAssignment workingTaskAssignment = user2Tasks.get(1);
    ChainElement originalPreviousElement = workingTaskAssignment.getPreviousElement();
    TaskAssignment expectedPreviousElement = user1Tasks.get(1);
    when(scoreDirector.lookUpWorkingObjectOrReturnNull(user1)).thenReturn(user1);
    when(scoreDirector.lookUpWorkingObjectOrReturnNull(taskAssignment)).thenReturn(workingTaskAssignment);
    change = new AssignTaskProblemFactChange(taskAssignment, user1);
    change.doChange(scoreDirector);
    assertPinning(workingTaskAssignment);
    assertThat(workingTaskAssignment.getPreviousElement()).isSameAs(expectedPreviousElement);
    assertThat(user2Tasks.get(2).getPreviousElement()).isSameAs(originalPreviousElement);
    verify(scoreDirector, never()).beforeEntityAdded(any());
    verify(scoreDirector, never()).afterEntityAdded(any());
    verify(scoreDirector).triggerVariableListeners();
    // Expected assignments:
    // User1 <- TASK_0 <- TASK_1 <- USER2_TASK_1 <- TASK_2 <- TASK_3 <- TASK_4
    // User2 <- USER2_TASK_0 <- USER2_TASK_2
    assertTaskPositions(user1, user1Tasks.get(0), user1Tasks.get(1), workingTaskAssignment, user1Tasks.get(2), user1Tasks.get(3), user1Tasks.get(4));
    assertTaskPositions(user2, user2Tasks.get(0), user2Tasks.get(2));
}
Also used : ChainElement(org.kie.kogito.taskassigning.core.model.ChainElement) User(org.kie.kogito.taskassigning.core.model.User) TestUtil.mockUser(org.kie.kogito.taskassigning.core.model.TestUtil.mockUser) TestUtil.mockTaskAssignment(org.kie.kogito.taskassigning.core.model.TestUtil.mockTaskAssignment) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment) Test(org.junit.jupiter.api.Test)

Example 9 with ChainElement

use of org.kie.kogito.taskassigning.core.model.ChainElement in project kogito-apps by kiegroup.

the class AssignTaskExecutableProblemFactChangeTest method assertAssignTaskProblemFactChangeWasProduced.

/**
 * Given an AssignTaskProblemFactChange and a solution that was produced as the result of applying the change,
 * asserts that the assignment defined by the change is not violated (exists in) by the solution.
 * The assignment defined in the change must also be pinned in the produced solution as well as any other
 * previous assignment for the given user.
 *
 * @param change The change that was executed for producing the solution.
 * @param solution The produced solution.
 */
private void assertAssignTaskProblemFactChangeWasProduced(AssignTaskProblemFactChange change, TaskAssigningSolution solution) throws Exception {
    User internalUser = solution.getUserList().stream().filter(user -> Objects.equals(user.getId(), change.getUser().getId())).findFirst().orElseThrow(() -> new Exception("User: " + change.getUser() + " was not found in solution."));
    TaskAssignment internalTaskAssignment = solution.getTaskAssignmentList().stream().filter(taskAssignment -> Objects.equals(taskAssignment.getId(), change.getTaskAssignment().getId())).findFirst().orElseThrow(() -> new Exception("TaskAssignment: " + change + " was not found in solution."));
    assertThat(internalTaskAssignment.getUser()).isEqualTo(internalUser);
    assertThat(internalTaskAssignment.isPinned()).isTrue();
    // all the previous tasks must be pinned by construction and be assigned to the user
    ChainElement previousElement = internalTaskAssignment.getPreviousElement();
    while (previousElement != null) {
        if (previousElement.isTaskAssignment()) {
            TaskAssignment previousTaskAssignment = (TaskAssignment) previousElement;
            assertThat(previousTaskAssignment.isPinned()).isTrue();
            assertThat(previousTaskAssignment.getUser()).isEqualTo(internalUser);
            previousElement = previousTaskAssignment.getPreviousElement();
        } else {
            assertThat(previousElement).isEqualTo(internalUser);
            previousElement = null;
        }
    }
    // all the next tasks must to the user.
    TaskAssignment nextTaskAssignment = internalTaskAssignment.getNextElement();
    while (nextTaskAssignment != null) {
        assertThat(nextTaskAssignment.getUser()).isEqualTo(internalUser);
        nextTaskAssignment = nextTaskAssignment.getNextElement();
    }
}
Also used : ChainElement(org.kie.kogito.taskassigning.core.model.ChainElement) User(org.kie.kogito.taskassigning.core.model.User) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Example 10 with ChainElement

use of org.kie.kogito.taskassigning.core.model.ChainElement in project kogito-apps by kiegroup.

the class SolutionBuilder method addAssignmentsToUser.

/**
 * Link the list of tasks to the given user. The tasks comes in the expected order.
 *
 * @param user the user that will "own" the tasks in the chained graph.
 * @param taskAssignments the tasks to link.
 */
private static void addAssignmentsToUser(User user, List<TaskAssignment> taskAssignments) {
    ChainElement previousElement = user;
    int previousEndTimeInMinutes = 0;
    TaskAssignment nextElement;
    // they are initialized as part of the solution restoring.
    for (TaskAssignment taskAssignment : taskAssignments) {
        nextElement = taskAssignment;
        previousElement.setNextElement(nextElement);
        nextElement.setStartTimeInMinutes(previousEndTimeInMinutes);
        nextElement.setEndTimeInMinutes(nextElement.getStartTimeInMinutes() + nextElement.getDurationInMinutes());
        nextElement.setPreviousElement(previousElement);
        nextElement.setUser(user);
        previousEndTimeInMinutes = nextElement.getEndTimeInMinutes();
        previousElement = nextElement;
    }
}
Also used : ChainElement(org.kie.kogito.taskassigning.core.model.ChainElement) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Aggregations

ChainElement (org.kie.kogito.taskassigning.core.model.ChainElement)15 TaskAssignment (org.kie.kogito.taskassigning.core.model.TaskAssignment)13 User (org.kie.kogito.taskassigning.core.model.User)5 Test (org.junit.jupiter.api.Test)4 ProblemFactChangeUtil.unlinkTaskAssignment (org.kie.kogito.taskassigning.core.model.solver.realtime.ProblemFactChangeUtil.unlinkTaskAssignment)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)1 TestUtil.mockTaskAssignment (org.kie.kogito.taskassigning.core.model.TestUtil.mockTaskAssignment)1 TestUtil.mockUser (org.kie.kogito.taskassigning.core.model.TestUtil.mockUser)1