OptaPlanner求解器能在运行时重新加载ConstraintProvider吗?

huangapple go评论91阅读模式
英文:

Can OptaPlanner solver reload the ConstraintProvider in runtime?

问题

让我们假设我构建了一个ConstraintProvider,并将其标记为Spring bean。我需要从数据库加载一些参数,然后将其用于构建约束。

@Component
public class SomeConstraintProvider implements ConstraintProvider {

  private final SomeRepository someRepository;

  public SomeConstraintProvider(SomeRepository someRepository) {
    this.someRepository = someRepository;
  }

  @Override
  public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
    return new Constraint[]{
        someConstraint(someRepository.findSomething()),
    };
  }
}

根据我的理解,Optaplanner只在构建求解器时加载约束提供程序,所以即使我更改数据库中的参数值,它也不会影响当前的求解器。因此,我想知道是否有一种方法可以请求求解器重新加载约束提供程序,或者是否有任何方法可以实现动态从数据库加载参数、构建约束提供程序并更新求解器?

甚至进一步说,我是否可以从Spring上下文中删除当前的求解器,然后重新构建一个新的求解器?

英文:

Let's say I build a ConstraintProvider and also mark it as a Spring bean. I somehow need to load some parameters from the database and use it to build the constraints.

@Component
public class SomeConstraintProvider implements ConstraintProvider {

  private final SomeRepository someRepository;

  public SomeConstraintProvider(SomeRepository someRepository) {
    this.someRepository = someRepository;
  }

  @Override
  public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
    return new Constraint[]{
        someConstraint(someRepository.findSomething()),
    };
  }
}

From my understanding, Optaplanner only load the constraint provider when the solver was built, so even if I change the parameter value in the database, it will not effect the current solver anymore. So I was wondering if there is a way I can somehow ask the solver to reload the constraint provider or if there is any approach that can use to achieve loading parameters from database dynamically and build the constraint provider and update the solver?

or even further I can remove the current solver from spring context and rebuild a new one?

答案1

得分: 0

OptaPlanner和Timefold都不支持重新加载约束提供者。事实上,将任何状态放入约束提供者都是不鼓励的;这仅仅是因为我们还没有找到一种完全不可能的方法才有可能。

英文:

Neither OptaPlanner nor Timefold support reloading the constraint provider. In fact, putting any state in the constraint provider is discouraged; it is only possible because we haven't found a way to make it entirely impossible.

huangapple
  • 本文由 发表于 2023年7月28日 00:19:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76781685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定