Search code examples
solveroptaplanner

Can OptaPlanner solver reload the ConstraintProvider in runtime?


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?


Solution

  • 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.