我正在 OptaPlanner 中实现一种类似于 NurseRoster 的算法。我需要在 drools 中实施一条规则来检查是否Employee
工作天数不能超过他的工作天数contract
。由于我不知道如何在 drools 中实现这一点,所以我决定将其编写为类中的方法,然后在 drools 中使用它来检查约束是否已被打破。因为我需要一个List
of ShiftAssignments
in the Employee
类,我需要使用@InverseRelationShadowVariable
自动更新该列表Employee
被分配到一个Shift
。自从我的Employee
现在必须是PlanningEntity
, 错误The entity was never added to this ScoreDirector
出现了。我相信该错误是由我造成的ShiftAssignment
实体,它有一个@ValueRangeProvider
of employees
可以在那方面发挥作用Shift
。我认为这是因为ScoreDirector.beforeEntityAdded
and ScoreDirector.afterEntityAdded
从未被调用,因此出现错误。由于某种原因,当我从中删除该范围提供者时ShiftAssignment
并穿上它NurseRoster
哪一个是@PlanningSolution
, 有效。
这是代码:
员工:
@InverseRelationShadowVariable(sourceVariableName = "employee")
public List<ShiftAssignment> getEmployeeAssignedToShiftAssignments() {
return employeeAssignedToShiftAssignments;
}
班次分配:
@PlanningVariable(valueRangeProviderRefs = {
"employeeRange" }, strengthComparatorClass = EmployeeStrengthComparator.class,nullable = true)
public Employee getEmployee() {
return employee;
}
// the value range for this planning entity
@ValueRangeProvider(id = "employeeRange")
public List<Employee> getPossibleEmployees() {
return getShift().getEmployeesThatCanWorkThisShift();
}
护士名册:
@ValueRangeProvider(id = "employeeRange")
@PlanningEntityCollectionProperty
public List<Employee> getEmployeeList() {
return employeeList;
}
这就是我用来更新的方法listOfEmployeesThatCanWorkThisShift
:
public static void checkIfAnEmployeeCanBelongInGivenShiftAssignmentValueRange(NurseRoster nurseRoster) {
List<Shift> shiftList = nurseRoster.getShiftList();
List<Employee> employeeList = nurseRoster.getEmployeeList();
for (Shift shift : shiftList) {
List<Employee> employeesThatCanWorkThisShift = new ArrayList<>();
String shiftDate = shift.getShiftDate().getDateString();
ShiftTypeDefinition shiftTypeDefinitionForShift = shift.getShiftType().getShiftTypeDefinition();
for (Employee employee : employeeList) {
AgentDailySettings agentDailySetting = SearchThroughSolution.findAgentDailySetting(employee, shiftDate);
List<ShiftTypeDefinition> shiftTypeDefinitions = agentDailySetting.getShiftTypeDefinitions();
if (shiftTypeDefinitions.contains(shiftTypeDefinitionForShift)) {
employeesThatCanWorkThisShift.add(employee);
}
}
shift.setEmployeesThatCanWorkThisShift(employeesThatCanWorkThisShift);
}
}
我使用的规则:
rule "maxDaysInPeriod"
when
$shiftAssignment : ShiftAssignment(employee != null)
then
int differentDaysInPeriod = MethodsUsedInScoreCalculation.employeeMaxDaysPerPeriod($shiftAssignment.getEmployee());
int maxDaysInPeriod = $shiftAssignment.getEmployee().getAgentPeriodSettings().getMaxDaysInPeriod();
if(differentDaysInPeriod > maxDaysInPeriod)
{
scoreHolder.addHardConstraintMatch(kcontext, differentDaysInPeriod - maxDaysInPeriod);
}
end
我该如何修复这个错误?