尝试在 optaplannercode 的实体上添加哈希码时出错

Error when tried to add hashcode on entity of optaplannercode

我在 optaplanner 上练习一个项目,我正在尝试在我的实体上添加 equals 和 hashcode。首先,我添加了 equals 方法,效果很好!之后我添加了哈希码,结果出了问题。

@Override
public boolean equals(Object o) {
if (this == o) {
        return true;
    } else if (o instanceof CloudBus) {
        CloudBus otherBus = (CloudBus) o;
        return (capacity == otherBus.getCapacity());
    } else {
        return false;
    }
}

@Override
public int hashCode() {
    return new HashCodeBuilder()
            .append(capacity)
            .toHashCode();
}

错误: SEVERE:Servlet.service() for servlet [WebApp] in context with path [/WebApp] throw exception [Exception executing consequence for rule "conflictingStartBusTooSmall" in com.webapp.jersey: java.lang.IllegalStateException: constraintMatchTotal (com.webapp.jersey/conflictingStartBusTooSmall=-97hard/0medium/0soft) 无法添加约束匹配 (com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83435, CloudBus-17]=-1hard/ 0medium/0soft) 到它的 constraintMatchSet ([com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83555, CloudBus-3]=-22hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/ [CloudRoute-83580, CloudBus-3]=-4hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83594, CloudBus-3]=-8hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83753, CloudBus-3]=-1hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83479, CloudBus-4]=-6hard/ 0medium/0soft,com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83435,CloudBus-6]=-1hard/0medium/0soft,com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83429, CloudBus-8]=-26hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83610, CloudBus-8]=-17hard/0medium/0soft, com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83403, CloudBus-10]=-11hard/0medium/0soft]).] 根本原因

我认为您需要在 CloudBus 的 equals()hashCode() 中包含另一个属性,这将在具有相同容量的两个不同的 CloudBus 之间产生差异。公交车的 ID 是一个不错的选择。由于 ID 是唯一的,因此在 equals()hashCode() 中包含其他属性没有多大意义(两条不同的总线将始终不相等,无论它们是否具有相同的容量等)。

仅根据容量进行比较的问题是两个不同的总线可能相等(并且具有相同的 hashCode)。结果,您将有两个不同的约束匹配,它们是相等的,因此不能将后一个添加到 ConstraintMatchTotalconstraintMatchSet,因为 "equal" 约束匹配已经在其中。参见 ConstraintMatchTotal。当给出更好的格式时,从异常消息中可以明显看出这一点:

ERROR: SEVERE: Servlet.service() for servlet [WebApp] in context with path [/WebApp] threw exception [
Exception executing consequence for rule "conflictingStartBusTooSmall" in com.webapp.jersey: java.lang.IllegalStateException:
  The constraintMatchTotal (com.webapp.jersey/conflictingStartBusTooSmall=-97hard/0medium/0soft)
  could not add constraintMatch (com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83435, CloudBus-17]=-1hard/0medium/0soft)
  to its constraintMatchSet ([
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83555, CloudBus-3]=-22hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83580, CloudBus-3]=-4hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83594, CloudBus-3]=-8hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83753, CloudBus-3]=-1hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83479, CloudBus-4]=-6hard/0medium/0soft,
--> com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83435, CloudBus-6]=-1hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83429, CloudBus-8]=-26hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83610, CloudBus-8]=-17hard/0medium/0soft,
    com.webapp.jersey/conflictingStartBusTooSmall/[CloudRoute-83403, CloudBus-10]=-11hard/0medium/0soft
  ]).
] with root cause

假设所有总线具有相同的容量,突出显示的约束匹配与将要插入的约束匹配具有相同的 hashCode,因为两者具有相同的 CloudRoute 和具有相同容量的总线。