自定义VariableListener更新多个阴影变量

时间:2016-04-11 01:17:41

标签: optaplanner

我正在阅读有关阴影变量和自定义变量监听器的文档,我想知道我是否正在理解它们是如何工作的。

从OptaPlanner文档的4.3.6.4节中复制。 “自定义VariableListener”

  

如果一个VariableListener更改了两个阴影变量(因为有   两个单独的VariableListener效率低下,然后注释   只有第一个带有variableListenerClass和let的阴影变量   另一个阴影变量引用第一个阴影变量:

@PlanningVariable(...)
public Standstill getPreviousStandstill() {
    return previousStandstill;
}

@CustomShadowVariable(variableListenerClass = TransportTimeAndCapacityUpdatingVariableListener.class,
        sources = {@CustomShadowVariable.Source(variableName = "previousStandstill")})
public Integer getTransportTime() {
    return transportTime;
}

@CustomShadowVariable(variableListenerRef = @PlanningVariableReference(variableName = "transportTime"))
public Integer getCapacity() {
    return capacity;
}

正如我从中理解的那样,当真正的计划变量发生变化并且我们有更多阴影变量需要相应地在另一个计划实体中更新时,我们可以在真正的计划实体的相同变量监听器中执行此操作。

如果是这样,那么这样的事情会有效吗?

以下是影子规划实体中阴影变量的注释方法。

//shadow variables
protected Integer variable;
protected Integer shadowVariable2;

@CustomShadowVariable(variableListenerClass = CustomVariableListener.class,
    sources = {@CustomShadowVariable.Source(variableName = "variable")})
public Integer getVariable() {
return variable;
}

@CustomShadowVariable(variableListenerRef =     @PlanningVariableReference(variableName = "variable"))
public Integer getShadowVariable2() {
return shadowVariable2;
}

以及真正规划实体的VariableListener的代码

public class CustomVariableListener implements VariableListener<GenuinePlanningEntity> {

@Override
public void afterEntityAdded(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void afterEntityRemoved(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void afterVariableChanged(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {
    List<ShadowPlanningEntity> shadowPlanningEntities = genuinePlanningEntity.getShadowPlanningEntities();
    Integer variable = genuinePlanningEntity.getVariable();
    for(ShadowPlanningEntity shadowPlanningEntity : shadowPlanningEntities){
        scoreDirector.beforeVariableChanged(shadowPlanningEntity,"variable");
        shadowPlanningEntity.setVariable(variable);
        scoreDirector.afterVariableChanged(shadowPlanningEntity,"variable");

        scoreDirector.beforeVariableChanged(shadowPlanningEntity,"shadowVariable2");
        shadowPlanningEntity.setshadowVariable2(shadowPlanningEntity.getshadowVariable2() + 1);
        scoreDirector.afterVariableChanged(shadowPlanningEntity,"shadowVariable2");

    }

}

@Override
public void beforeEntityAdded(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void beforeEntityRemoved(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}

@Override
public void beforeVariableChanged(ScoreDirector scoreDirector, GenuinePlanningEntity genuinePlanningEntity) {

}



}

如果不行,那么如何正确更新影子规划实体中的所有影子变量? 是否应该像这样调用阴影变量的beforeVariableChanged和afterVariableChanged方法?

0 个答案:

没有答案