自定义克隆过程中从未将实体添加到此scoreDirector异常中 [英] The entity was never added to this scoreDirector exception during custom cloning

查看:86
本文介绍了自定义克隆过程中从未将实体添加到此scoreDirector异常中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在解决方案中实现自定义克隆,我按照文档中的说明进行操作,并且遇到了以下异常形式的障碍:The entity was never added to this ScoreDirector. Maybe that specific instance is not in the return values of the PlanningSolution's entity members.我知道这是不对的,因为在自定义克隆之前,没有引发此异常.

I'm trying to implement custom cloning in my solution, i followed the instructions as in the documentation, and i encountered a roadblock in the form of this exception : The entity was never added to this ScoreDirector. Maybe that specific instance is not in the return values of the PlanningSolution's entity members. I know that this is not true because before the custom cloning, this exception wasn't thrown.

我的planningClone方法是这样设置的:

My planningClone method is setup like this :

@Override
public Solution planningClone() {
    Solution clonedSolution = new Solution();
    clonedSolution.id = id;
    clonedSolution.code = code;
    clonedSolution.score = score;
    clonedSolution.field1 = field1;
    clonedSolution.field2 = field2;
    ...............
    clonedSolution.fieldN = fieldN;

    List<PlanningEntity1> clonedPlanningEntity1List= new ArrayList<PlanningEntity1>(planningEntity1List.size());
    List<PlanningEntity2> clonedPlanningEntity2List= new ArrayList<PlanningEntity2>(planningEntity1List.size());

    for (PlanningEntity1 planningEntity: planningEntity1List) {
        clonedPlanningEntity1List.add(planningEntity.clone());
    }
    for (PlanningEntity2 planningEntity: planningEntity2List) {
        clonedPlanningEntity1List.add(planningEntity.clone());
    }
    clonedSolution.planningEntity1List = clonedPlanningEntity1List;
    clonedSolution.planningEntity2List = clonedPlanningEntity2List;
    return clonedSolution;
{

我的计划实体的克隆方法是通过Java接口Cloneable实现的:

The clone method for my planning entities is implemented through the Java interface Cloneable:

protected PlanningEntity clone() {
    try {
        return (PlanningEntity) super.clone();
    } catch (CloneNotSupportedException e) {
        e.printStackTrace();
    }
    return null;
}

请确定,我检查了每个实体实例及其集合,以确保克隆工作正常,并且确实如此.

Just to be sure, i checked every entity instance and their collections to make sure my cloning was working correctly, and it in fact is.

我在这里错过了哪一步?

What step am i missing here?

推荐答案

如果有一个计划实体指向另一个类中的另一个计划实体,或者指向一个列表,则克隆过程需要照顾引用.对于这些计划实体,它们将指向已克隆的对象.这是默认克隆过程可以毫无问题地完成的工作,从而使解决方案保持一致的状态.它甚至可以正确地更新父计划实体中的计划实体实例列表(由OptaPlanner核心的"FieldAccessingSolutionCloner"类的"cloneCollectionsElementIfNeeded"方法覆盖).

If there is one planning entity pointing to another planning entity from a different class or maybe pointing to a list, than the cloning process needs to take care of the references for those planning entities so they point to the cloned objects.This is something that the default cloning process is doing without a problem, and thus leaving the solution in a consistent state. It even updates the Lists of planning entity instances in the parent planning entities correctly (covered by the method "cloneCollectionsElementIfNeeded" from the class "FieldAccessingSolutionCloner" from the OptaPlanner core).

例如,如果我们有接下来的两个计划实体类

So for example if we have the next two planning entity classes

@PlanningEntity
public class ParentPlanningEntityClass{
    List<ChildPlanningEntityClass> childPlanningEntityClassList;
}

@PlanningEntity
public class ChildPlanningEntityClass{
    ParentPlanningEntityClass parentPlanningEntityClass;
}

需要将"parentPlanningEntityClass"变量设置为指向克隆的对象.当涉及到"childPlanningEntityClassList"列表时,首先需要使用"new ArrayList();"从头开始创建它.这样一来,无论是可行的解决方案还是新的最佳解决方案(当前正在克隆的解决方案)都不会指向同一列表.最后,需要使用克隆的对象填充新创建的列表.

The "parentPlanningEntityClass" variable needs to be set to point to the cloned object. When it comes to the list "childPlanningEntityClassList" it first needs to be created from scratch with "new ArrayList();" so that both the working and new best solution (the one that is currently getting cloned) don't point to the same list. At the end the newly created list needs to be filled with the cloned objects.

这篇关于自定义克隆过程中从未将实体添加到此scoreDirector异常中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆