Search in sources :

Example 1 with AssignTaskProblemFactChange

use of org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange in project kogito-apps by kiegroup.

the class AssignTaskExecutableProblemFactChangeTest method assignTaskProblemFactChange.

private void assignTaskProblemFactChange(TaskAssigningSolution solution, String solutionResource, String testType, List<ProgrammedAssignTaskProblemFactChange> programmedChanges) throws Exception {
    TaskAssigningSolution initialSolution = executeSequentialChanges(solution, programmedChanges);
    if (writeTestFiles()) {
        writeProblemFactChangesTestFiles(initialSolution, solutionResource, "AssignTaskExecutableProblemFactChangeTest.assignTaskProblemFactChangeTest", testType, programmedChanges, ProgrammedAssignTaskProblemFactChange::workingSolutionBeforeChangeAsString, ProgrammedAssignTaskProblemFactChange::solutionAfterChangeAsString);
    }
    // each partial solution must have the change that was applied on it.
    for (ProgrammedAssignTaskProblemFactChange change : programmedChanges) {
        assertAssignTaskProblemFactChangeWasProduced(change.getChange(), change.getSolutionAfterChange());
    }
    // finally the last solution must have the result of all the changes.
    TaskAssigningSolution lastSolution = programmedChanges.get(programmedChanges.size() - 1).getSolutionAfterChange();
    Map<String, AssignTaskProblemFactChange> summarizedChanges = new HashMap<>();
    programmedChanges.forEach(change -> {
        // if task was changed multiple times record only the last change.
        summarizedChanges.put(change.getChange().getTaskAssignment().getId(), change.getChange());
    });
    for (AssignTaskProblemFactChange change : summarizedChanges.values()) {
        assertAssignTaskProblemFactChangeWasProduced(change, lastSolution);
    }
}
Also used : AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) HashMap(java.util.HashMap) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)

Example 2 with AssignTaskProblemFactChange

use of org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange in project kogito-apps by kiegroup.

the class SolutionChangesBuilderTest method addReservedTaskChangeForAnotherUser.

private void addReservedTaskChangeForAnotherUser(TaskAssigningSolution solution, TaskData taskData, User user) {
    List<ProblemFactChange<TaskAssigningSolution>> result = SolutionChangesBuilder.create().withContext(context).withUserServiceConnector(userServiceConnector).withProcessors(processorRegistry).fromTasksData(mockTaskDataList(taskData)).forSolution(solution).build();
    assertChangeIsTheChangeSetId(result, 0);
    assertChange(result, 1, new AssignTaskProblemFactChange(new TaskAssignment(fromTaskData(taskData)), user, true));
    assertTaskPublishStatus(TASK_1_ID, true);
    assertThatTaskProcessorsWhereNotApplied();
}
Also used : AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) 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) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Example 3 with AssignTaskProblemFactChange

use of org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange in project kogito-apps by kiegroup.

the class SolutionChangesBuilderTest method assertChange.

private static void assertChange(List<ProblemFactChange<TaskAssigningSolution>> result, int index, AssignTaskProblemFactChange expected) {
    assertThat(result.get(index)).isInstanceOf(AssignTaskProblemFactChange.class);
    AssignTaskProblemFactChange change = (AssignTaskProblemFactChange) result.get(index);
    assertTaskEquals(expected.getTaskAssignment(), change.getTaskAssignment());
}
Also used : AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)

Example 4 with AssignTaskProblemFactChange

use of org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange in project kogito-apps by kiegroup.

the class SolutionChangesBuilderTest method addNewReservedTaskChangeWithActualOwner.

private void addNewReservedTaskChangeWithActualOwner(TaskAssigningSolution solution, TaskData taskData) {
    List<TaskData> taskDataList = mockTaskDataList(taskData);
    List<ProblemFactChange<TaskAssigningSolution>> result = SolutionChangesBuilder.create().withContext(context).withUserServiceConnector(userServiceConnector).withProcessors(processorRegistry).fromTasksData(taskDataList).forSolution(solution).build();
    AssignTaskProblemFactChange expected = new AssignTaskProblemFactChange(new TaskAssignment(fromTaskData(taskData)), mockUser(USER_1), true);
    assertChangeIsTheChangeSetId(result, 0);
    assertChange(result, 1, expected);
    assertTaskPublishStatus(taskData.getId(), true);
    assertThatTaskProcessorsWereApplied(taskData.getId());
}
Also used : AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) 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) TaskUtil.fromTaskData(org.kie.kogito.taskassigning.service.util.TaskUtil.fromTaskData) TestUtil.mockTaskData(org.kie.kogito.taskassigning.service.TestUtil.mockTaskData) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Example 5 with AssignTaskProblemFactChange

use of org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange in project kogito-apps by kiegroup.

the class TaskAssigningService method onPlanningExecuted.

/**
 * Invoked when the PlanningExecutor finalized the execution of a plan.
 * Three main scenarios might happen:
 * a) There are successful invocations and thus tasks were assigned, the corresponding "pinning changes" must be produced.
 * Create and add them to the Solver.
 *
 * b) No "pinning changes" to execute there and no available events, retry with the planning items that failed.
 *
 * c) No "pinning changes" but there are available events, execute them.
 *
 * @param result a PlanningExecutionResult with results of the planning execution.
 */
synchronized void onPlanningExecuted(PlanningExecutionResult result) {
    if (isNotOperative()) {
        LOGGER.warn(SERVICE_INOPERATIVE_MESSAGE, context.getStatus());
        return;
    }
    try {
        LOGGER.debug("Planning was executed");
        applyingPlanningExecutionResult.set(false);
        TaskAssigningSolution solution = currentSolution.get();
        Map<String, User> usersById = solution.getUserList().stream().collect(Collectors.toMap(User::getId, Function.identity()));
        List<ProblemFactChange<TaskAssigningSolution>> pinningChanges = new ArrayList<>();
        Task task;
        User user;
        boolean published;
        for (PlanningExecutionResultItem resultItem : result.getItems()) {
            task = resultItem.getItem().getTask();
            published = !resultItem.hasError();
            if (published) {
                user = usersById.get(resultItem.getItem().getTargetUser());
                pinningChanges.add(new AssignTaskProblemFactChange(new TaskAssignment(task), user));
            }
            context.setTaskPublished(task.getId(), published);
        }
        if (!pinningChanges.isEmpty()) {
            LOGGER.debug("Pinning changes must be executed for the successful invocations: {}", pinningChanges.size());
            pinningChanges.add(0, scoreDirector -> context.setCurrentChangeSetId(context.nextChangeSetId()));
            applyingPlanningExecutionResult.set(true);
            cancelScheduledImproveSolutionOnBackgroundTimer();
            solverExecutor.addProblemFactChanges(pinningChanges);
        } else if (!hasQueuedEvents()) {
            List<PlanningItem> failingItems = result.getItems().stream().filter(PlanningExecutionResultItem::hasError).map(PlanningExecutionResultItem::getItem).collect(Collectors.toList());
            LOGGER.debug("No new events to process, but some items failed: {}, we must retry", failingItems.size());
            cancelScheduledImproveSolutionOnBackgroundTimer();
            planningExecutor.start(failingItems, this::onPlanningExecuted);
        } else {
            LOGGER.debug("Some items failed but there are events to process, try to adjust the solution accordingly.");
            resumeEvents();
        }
    } catch (Exception e) {
        failFast(e);
    }
}
Also used : Task(org.kie.kogito.taskassigning.core.model.Task) User(org.kie.kogito.taskassigning.core.model.User) TaskAssigningSolution(org.kie.kogito.taskassigning.core.model.TaskAssigningSolution) ArrayList(java.util.ArrayList) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) ProblemFactChange(org.optaplanner.core.api.solver.ProblemFactChange) AssignTaskProblemFactChange(org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange) List(java.util.List) ArrayList(java.util.ArrayList) TaskAssignment(org.kie.kogito.taskassigning.core.model.TaskAssignment)

Aggregations

AssignTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AssignTaskProblemFactChange)8 TaskAssignment (org.kie.kogito.taskassigning.core.model.TaskAssignment)5 User (org.kie.kogito.taskassigning.core.model.User)4 Task (org.kie.kogito.taskassigning.core.model.Task)3 AddTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddTaskProblemFactChange)3 ReleaseTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.ReleaseTaskProblemFactChange)3 RemoveTaskProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveTaskProblemFactChange)3 UserUtil.fromExternalUser (org.kie.kogito.taskassigning.service.util.UserUtil.fromExternalUser)3 ProblemFactChange (org.optaplanner.core.api.solver.ProblemFactChange)3 TaskAssigningSolution (org.kie.kogito.taskassigning.core.model.TaskAssigningSolution)2 AddUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.AddUserProblemFactChange)2 DisableUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.DisableUserProblemFactChange)2 RemoveUserProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.RemoveUserProblemFactChange)2 UserPropertyChangeProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.UserPropertyChangeProblemFactChange)2 TaskUtil.fromTaskData (org.kie.kogito.taskassigning.service.util.TaskUtil.fromTaskData)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 TaskInfoChangeProblemFactChange (org.kie.kogito.taskassigning.core.model.solver.realtime.TaskInfoChangeProblemFactChange)1 TestUtil.mockTaskData (org.kie.kogito.taskassigning.service.TestUtil.mockTaskData)1