Search in sources :

Example 36 with TaskAssigningSolution

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

the class AddUserProblemFactChange method doChange.

@Override
public void doChange(ScoreDirector<TaskAssigningSolution> scoreDirector) {
    final TaskAssigningSolution workingSolution = scoreDirector.getWorkingSolution();
    final User workingUser = scoreDirector.lookUpWorkingObjectOrReturnNull(user);
    if (workingUser != null) {
        throw new TaskAssigningRuntimeException(String.format("A user with the given identifier id: %s already exists", user.getId()));
    }
    // Shallow clone the user list so only workingSolution is affected, not bestSolution
    workingSolution.setUserList(new ArrayList<>(workingSolution.getUserList()));
    // Add the problem fact itself
    scoreDirector.beforeProblemFactAdded(user);
    workingSolution.getUserList().add(user);
    scoreDirector.afterProblemFactAdded(user);
    scoreDirector.triggerVariableListeners();
}
Also used : User(org.kie.kogito.taskassigning.core.model.User) TaskAssigningRuntimeException(org.kie.kogito.taskassigning.core.TaskAssigningRuntimeException) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)

Example 37 with TaskAssigningSolution

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

the class AssignTaskProblemFactChange method lookupOrAddWorkingUser.

private static User lookupOrAddWorkingUser(User user, ScoreDirector<TaskAssigningSolution> scoreDirector, boolean addIfNotExists) {
    TaskAssigningSolution solution = scoreDirector.getWorkingSolution();
    User workingUser = scoreDirector.lookUpWorkingObjectOrReturnNull(user);
    if (workingUser == null) {
        if (!addIfNotExists) {
            throw new TaskAssigningRuntimeException(String.format("Expected user: %s was not found in current working solution", user));
        } else {
            // Shallow clone the user list so only workingSolution is affected, not bestSolution
            solution.setUserList(new ArrayList<>(solution.getUserList()));
            // Ensure that the nextElement value calculated by OptaPlanner doesn't have any out-side manually
            // assigned value.
            user.setNextElement(null);
            scoreDirector.beforeProblemFactAdded(user);
            solution.getUserList().add(user);
            scoreDirector.afterProblemFactAdded(user);
            workingUser = user;
        }
    }
    return workingUser;
}
Also used : User(org.kie.kogito.taskassigning.core.model.User) TaskAssigningRuntimeException(org.kie.kogito.taskassigning.core.TaskAssigningRuntimeException) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)

Example 38 with TaskAssigningSolution

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

the class TaskAssigningServiceTest method preparePlanningExecutionWithNoPinningChanges.

private void preparePlanningExecutionWithNoPinningChanges(int queuedEvents) throws Exception {
    prepareStart();
    TaskAssigningSolution solution = new TaskAssigningSolution("1", Arrays.asList(mockUser(USER_1_ID, Collections.emptyList()), mockUser(USER_2_ID, Collections.emptyList())), Collections.emptyList());
    taskAssigningService.onBestSolutionChange(mockEvent(solution, true, true));
    verify(managedExecutor).runAsync(managedExecutorCaptor.capture());
    managedExecutorCaptor.getValue().run();
    context.setTaskPublished(TASK_1_ID, false);
    context.setTaskPublished(TASK_2_ID, false);
    PlanningItem planningItem1 = new PlanningItem(Task.newBuilder().id(TASK_1_ID).build(), USER_1_ID);
    PlanningItem planningItem2 = new PlanningItem(Task.newBuilder().id(TASK_2_ID).build(), USER_2_ID);
    PlanningExecutionResult executionResult = new PlanningExecutionResult(Arrays.asList(new PlanningExecutionResultItem(planningItem1, new RuntimeException("planningItem1 failed")), new PlanningExecutionResultItem(planningItem2, new RuntimeException("planningItem2 failed"))));
    doReturn(queuedEvents).when(serviceEventConsumer).queuedEvents();
    taskAssigningService.onPlanningExecuted(executionResult);
}
Also used : TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)

Example 39 with TaskAssigningSolution

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

the class TaskAssigningServiceTest method onSolutionUpdatedOnBackgroundWithScoreImprovement.

@Test
void onSolutionUpdatedOnBackgroundWithScoreImprovement() throws Exception {
    TaskAssigningSolution newBestSolution = buildSolutionWithScore(IMPROVED_SCORE);
    prepareOnSolutionUpdatedOnBackground(INITIAL_SCORE, newBestSolution);
    verify(taskAssigningService).executeSolutionChange(newBestSolution);
}
Also used : TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) Test(org.junit.jupiter.api.Test)

Example 40 with TaskAssigningSolution

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

the class TaskAssigningServiceTest method onPlanningExecutedWithPinningChanges.

@Test
void onPlanningExecutedWithPinningChanges() throws Exception {
    prepareStart();
    TaskAssigningSolution solution = new TaskAssigningSolution("1", Arrays.asList(mockUser(USER_1_ID, Collections.emptyList()), mockUser(USER_2_ID, Collections.emptyList())), Collections.emptyList());
    taskAssigningService.onBestSolutionChange(mockEvent(solution, true, true));
    verify(managedExecutor).runAsync(managedExecutorCaptor.capture());
    managedExecutorCaptor.getValue().run();
    context.setTaskPublished(TASK_1_ID, false);
    context.setTaskPublished(TASK_2_ID, false);
    PlanningItem planningItem1 = new PlanningItem(Task.newBuilder().id(TASK_1_ID).build(), USER_1_ID);
    PlanningItem planningItem2 = new PlanningItem(Task.newBuilder().id(TASK_2_ID).build(), USER_2_ID);
    PlanningExecutionResult executionResult = new PlanningExecutionResult(Arrays.asList(new PlanningExecutionResultItem(planningItem1), new PlanningExecutionResultItem(planningItem2, new RuntimeException("planningItem2 failed"))));
    taskAssigningService.onPlanningExecuted(executionResult);
    assertThat(context.isTaskPublished(TASK_1_ID)).isTrue();
    assertThat(context.isTaskPublished(TASK_2_ID)).isFalse();
    verify(solverExecutor).addProblemFactChanges(problemFactChangesCaptor.capture());
    List<ProblemFactChange<TaskAssigningSolution>> changes = problemFactChangesCaptor.getValue();
    assertThat(changes).isNotNull().hasSize(2);
    assertHasAssignTaskChangeForTask(changes, TASK_1_ID, USER_1_ID);
}
Also used : TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) 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) Test(org.junit.jupiter.api.Test)

Aggregations

TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)65 Test (org.junit.jupiter.api.Test)30 User (org.kie.kogito.taskassigning.core.model.User)24 TaskAssignment (org.kie.kogito.taskassigning.core.model.TaskAssignment)23 ArrayList (java.util.ArrayList)15 AssignTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)12 TaskUtil.fromTaskData (org.kie.kogito.taskassigning.service.util.TaskUtil.fromTaskData)12 AddTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange)11 TestUtil.mockTaskData (org.kie.kogito.taskassigning.service.TestUtil.mockTaskData)11 ProblemFactChange (org.optaplanner.core.api.solver.ProblemFactChange)11 RemoveTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange)10 AddUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange)9 DisableUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange)7 ReleaseTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange)7 RemoveUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange)7 UserPropertyChangeProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange)7 List (java.util.List)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Task (org.kie.kogito.taskassigning.core.model.Task)5 UserDataEvent (org.kie.kogito.taskassigning.service.event.UserDataEvent)5