英文:
How can we use multiple ConstraintProviders, when using the springboot optaplanner-spring-boot-starter
问题
optaplanner-spring-boot-starter会自动发现ContraintProvider,无需solverConfig.xml。因此,如果我们想为不同问题使用多个constrantProvider,**例如** *问题A需要使用problemAContraintProvider,问题B需要使用ProblemBContraintProvider*,是否有类似的限定符注释,我可以使用并告诉solverManger使用特定的ConstraintProvider实现。
public class ProblemAConstraintProvider implements ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[] {
typeMatch(constraintFactory),
timeSlotMatch(constraintFactory),
};
}
}
public class ProblemBConstraintProvider implements ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[] {
checkId(constraintFactory),
};
}
}
英文:
optaplanner-spring-boot-starter discovers the ContraintProvider automatically, without a solverConfig.xml, so if we want to use multiple constrantProviders for different problems, like for example
problem A, needs to use problemAContraintProvider and problem B, needs to use ProblemBContraintProvider, is there any Qualifier kind of annotation which i can use and tell the solverManger to use specific ConstraintProvider implementation
public class ProblemAConstraintProvider implements ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[] {
typeMatch(constraintFactory),
timeSlotMatch(constraintFactory),
};
}
}
public class ProblemBConstraintProvider implements ConstraintProvider {
@Override
public Constraint[] defineConstraints(ConstraintFactory constraintFactory) {
return new Constraint[] {
checkId(constraintFactory),
};
}
}
答案1
得分: 2
你需要添加一个 solverConfig.xml
来覆盖默认行为,以避免查找 ConstraintProvider
类(然后因为找到两个而导致失败)。
在 src/main/resources/solverConfig.xml
中添加以下内容(文件位置可以在 application.properties
中进行定制):
<solver>
<scoreDirectoryFactory>
<constraintProviderClass>...FirstConstraintProvider</constraintProviderClass>
</scoreDirectoryFactory>
</solver>
但正如 Radovan 指出的那样:在同一个 Spring Boot 应用程序中,我们尚不支持多个 SolverManager/solverConfig 注入。这是一个 API 差距。
英文:
You'll need to add a solverConfig.xml
to overrides the default behavior of looking for the ConstraintProvider
class (and then fail fasting because it finds two).
Add src/main/resources/solverConfig.xml
(file location can be tailored with in application.properties) and have it include something like:
<solver>
<scoreDirectoryFactory>
<constraintProviderClass>...FirstConstraintProvider</>
</scoreDirectoryFactory>
</solver>
But as Radovan pointed out: we don't support multiple SolverManager/solverConfig injections yet in the same Spring Boot app. That is an API gap.
答案2
得分: 0
简短的回答是否定的;与 https://stackoverflow.com/questions/63715928/can-we-use-multiple-planningsolution-classes-and-multiple-solvermanager-in-spri/63736367#63736367 中的回答类似,每种类型的规划问题都需要一个新的求解器配置,其中定义了“constraintProviderClass”。
“optaplanner-spring-boot-starter” 仅支持单个求解器配置,因此只支持单一类型的规划问题。
英文:
The short answer is no; similarly to the answer in https://stackoverflow.com/questions/63715928/can-we-use-multiple-planningsolution-classes-and-multiple-solvermanager-in-spri/63736367#63736367, each type of a planning problem requires a new solver configuration which defines the constraintProviderClass
.
The optaplanner-spring-boot-starter
supports only a single solver configuration and thus only a single type of a planning problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论