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