How can we use multiple ConstraintProviders, when using the springboot optaplanner-spring-boot-starter

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

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:

&lt;solver&gt;
  &lt;scoreDirectoryFactory&gt;
    &lt;constraintProviderClass&gt;...FirstConstraintProvider&lt;/&gt;
  &lt;/scoreDirectoryFactory&gt;
&lt;/solver&gt;

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.

huangapple
  • 本文由 发表于 2020年9月3日 04:07:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63712863.html
匿名

发表评论

匿名网友

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

确定