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