ai.timefold.solver.spring.boot.autoconfigure.TimefoldSolverAotFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of timefold-solver-spring-boot-autoconfigure Show documentation
Show all versions of timefold-solver-spring-boot-autoconfigure Show documentation
Timefold solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains the Spring Boot autoconfigure.
package ai.timefold.solver.spring.boot.autoconfigure;
import java.io.StringReader;
import ai.timefold.solver.core.api.solver.SolverFactory;
import ai.timefold.solver.core.api.solver.SolverManager;
import ai.timefold.solver.core.config.solver.SolverConfig;
import ai.timefold.solver.core.config.solver.SolverManagerConfig;
import ai.timefold.solver.core.impl.io.jaxb.SolverConfigIO;
import ai.timefold.solver.spring.boot.autoconfigure.config.SolverManagerProperties;
import ai.timefold.solver.spring.boot.autoconfigure.config.TimefoldProperties;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
public class TimefoldSolverAotFactory implements EnvironmentAware {
private TimefoldProperties timefoldProperties;
@Override
public void setEnvironment(Environment environment) {
// We need the environment to set run time properties of SolverFactory and SolverManager
BindResult result = Binder.get(environment).bind("timefold", TimefoldProperties.class);
this.timefoldProperties = result.orElseGet(TimefoldProperties::new);
}
public SolverManager solverManagerSupplier(String solverConfigXml) {
SolverFactory solverFactory = SolverFactory.create(solverConfigSupplier(solverConfigXml));
SolverManagerConfig solverManagerConfig = new SolverManagerConfig();
SolverManagerProperties solverManagerProperties = timefoldProperties.getSolverManager();
if (solverManagerProperties != null && solverManagerProperties.getParallelSolverCount() != null) {
solverManagerConfig.setParallelSolverCount(solverManagerProperties.getParallelSolverCount());
}
return SolverManager.create(solverFactory, solverManagerConfig);
}
public SolverConfig solverConfigSupplier(String solverConfigXml) {
SolverConfigIO solverConfigIO = new SolverConfigIO();
return solverConfigIO.read(new StringReader(solverConfigXml));
}
}