具有不同实体来源的 CustomVariableListener - Listener 的参数化

CustomVariableListener with sources of different entities - Parameterization of Listener

最近有人问了一个问题here如何在源变量位于不同规划实体classes上的地方实现监听器,例如

@PlanningEntity
class FirstPlanningEntityClass{

  @PlanningVariable
  int a;
}

@PlanningEntity
class SecondPlanningEntityClass{

  @PlanningVariable
  int b;
}

在我的第三个(影子)规划实体中,我想在 ab 发生变化时更新我的​​影子规划变量(即 ab 作为源),例如:

@PlanningEntity //shadow planning entity
class ShadowEntityClass{
  @CustomShadowVariable(variableListenerClass = MyListener.class,
        sources = {@CustomShadowVariable.Source(entityClass = FirstEntityClass.class, variableName = "a"),
                @CustomShadowVariable.Source(entityClass = SecondEntityClass.class, variableName = "b")})
  public local int getMyShadowVariable(){...}

但是,MyListenerClass 必须实现一个只有一个类型参数的参数化 VariableListener,其中类型参数对应于源变量所在的实体 class:

public class MyListenerClass implements VariableListener<Entity_>{...}

但源变量在两个规划实体classes.

为了解决这个问题,我尝试了几种方法:

手册仅提及相同 class 的许多计划变量或不同 class 的一个计划变量(第 4.3.6.4.、6.5.0 节)。

那么我如何实现一个影子变量,它具有来自不同规划实体classes的两个规划变量作为源?给出的答案here主要涵盖一个规划实体class中两个影子变量的变化。

抱歉打扰了大家 - 多么愚蠢的问题。

我需要做的就是在 ShadowEntityClass 上创建两个 interim 影子变量,分别监听 ab。然后,我使用这些临时影子变量在 getMyShadowVariable 上创建了一个侦听器,这些临时影子变量现在作为源位于同一规划实体 class 中。

(不过,我仍然认为注释签名不应允许来自多个规划实体的影子变量class)。

使用通用接口或只是普通的旧 java.lang.Object 作为通用参数类型:

public class MyListenerClass implements VariableListener<Object>{...}

您的实施可能需要 instanceof 检查源实例并相应地转换为第一个或第二个 class。