在OptaPlanner Nurse Rostering示例中,有SkillProficiency类:
public class SkillProficiency extends AbstractPersistable {
private Employee employee;
private Skill skill;
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
@Override
public String toString() {
return employee + "-" + skill;
}
}
这是Employee类:
public class Employee extends AbstractPersistable {
private String code;
private String name;
private Contract contract;
private Map<ShiftDate, DayOffRequest> dayOffRequestMap;
private Map<ShiftDate, DayOnRequest> dayOnRequestMap;
private Map<Shift, ShiftOffRequest> shiftOffRequestMap;
private Map<Shift, ShiftOnRequest> shiftOnRequestMap;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Contract getContract() {
return contract;
}
public void setContract(Contract contract) {
this.contract = contract;
}
public int getWeekendLength() {
return getContract().getWeekendLength();
}
public Map<ShiftDate, DayOffRequest> getDayOffRequestMap() {
return dayOffRequestMap;
}
public void setDayOffRequestMap(Map<ShiftDate, DayOffRequest> dayOffRequestMap) {
this.dayOffRequestMap = dayOffRequestMap;
}
public Map<ShiftDate, DayOnRequest> getDayOnRequestMap() {
return dayOnRequestMap;
}
public void setDayOnRequestMap(Map<ShiftDate, DayOnRequest> dayOnRequestMap) {
this.dayOnRequestMap = dayOnRequestMap;
}
public Map<Shift, ShiftOffRequest> getShiftOffRequestMap() {
return shiftOffRequestMap;
}
public void setShiftOffRequestMap(Map<Shift, ShiftOffRequest> shiftOffRequestMap) {
this.shiftOffRequestMap = shiftOffRequestMap;
}
public Map<Shift, ShiftOnRequest> getShiftOnRequestMap() {
return shiftOnRequestMap;
}
public void setShiftOnRequestMap(Map<Shift, ShiftOnRequest> shiftOnRequestMap) {
this.shiftOnRequestMap = shiftOnRequestMap;
}
public String getLabel() {
return "Employee " + name;
}
@Override
public String toString() {
return code + "(" + name + ")";
}
}
还有技能课程:
public class Skill extends AbstractPersistable {
private String code;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Override
public String toString() {
return code;
}
}
我想知道为什么它选择使用SkillProficiency类而不是在Employee类中嵌入技能变量?如果嵌入它会更简单吗? 这个决定背后有充分的理由,但我无法弄明白。如果有人知道,请与我分享。 而且,如果我在Employee类中嵌入技能变量也没关系?这样做有什么不好的影响? 谢谢和问候。
答案 0 :(得分:2)
这真是一个类图设计选择。甚至可能是品味问题。
Employee to Skill是一个ManyToMany关系(另见grudolf的评论)。在这种情况下,我更喜欢将ManyToMany关系设计为一个类。这有一些好处:
int rating
字段添加到SkillProficiency
。您可以轻松扩展此模型,以便在课程List<SkillProficiency> proficiencyList
(和/或课程Employee
上)添加Skill
。我不这样做的唯一原因是因为我还没有必要。
替代设计(我不喜欢)是让课程Employee
有List<Skill>
而课程Skill
有List<Employee>
。 OptaPlanner中没有任何内容阻止您使用该模型:我们不想强迫您做出设计决策。但我确实担心用该模型写一个有效的得分DRL可能会更难......