如何通过ProblemChange按规划ID移除规划实体?API查询疑问
ProblemChangeDirector.lookUpWorkingObject 查找方式说明
ProblemChangeDirector.lookUpWorkingObject不支持直接通过ID查找,必须传入完整的规划实体对象,不能仅传ID值。- 如果想通过ID移除规划实体,得先从当前的Solution里根据ID查到对应的实体实例,再把这个实例传入
lookUpWorkingObject拿到工作对象,之后再执行移除操作。 - 简单的代码示例:
// 根据ID从Solution中获取目标实体 YourPlanningEntity targetEntity = solution.getEntityList(YourPlanningEntity.class) .stream() .filter(entity -> entity.getId().equals(targetEntityId)) .findFirst() .orElseThrow(() -> new RuntimeException("找不到ID为" + targetEntityId + "的规划实体")); // 查找对应的工作对象 YourPlanningEntity workingEntity = problemChangeDirector.lookUpWorkingObject(targetEntity); // 执行移除操作 problemChangeDirector.changeProblem(problem -> problem.removeEntity(workingEntity)); - 这个设计的原因是
lookUpWorkingObject依赖实体的equals/hashCode实现来匹配工作内存中的对象,仅靠ID无法完成准确匹配,必须传入完整实体才能完成映射。
内容的提问来源于stack exchange,提问作者Pezetter
相关产品推荐
相关产品推荐

