optaplanner项目作业调度示例 - 切换到drl文件时无法成功编译

时间:2017-12-07 03:31:42

标签: java optaplanner

我正在尝试重用7.4.1.Final发布中的项目作业调度示例。我将projectJobSchedulingSolverConfig.xml文件更改为使用

<scoreDrl>org/optaplanner/examples/projectjobscheduling/solver/projectJobSchedulingScoreRules.drl</scoreDrl>

而不是<incrementalScoreCalculatorClass>org.optaplanner.examples.projectjobscheduling.solver.score.ProjectJobSchedulingIncrementalScoreCalculator</incrementalScoreCalculatorClass>

更清楚:

我只更改了projectJobSchedulingSolverConfig.xml文件的这一部分:

<scoreDirectorFactory>
  <!--<incrementalScoreCalculatorClass>org.optaplanner.examples.projectjobscheduling.solver.score.ProjectJobSchedulingIncrementalScoreCalculator</incrementalScoreCalculatorClass>-->
  <scoreDrl>org/optaplanner/examples/projectjobscheduling/solver/projectJobSchedulingScoreRules.drl</scoreDrl>
</scoreDirectorFactory>

但后来我遇到了编译错误:

Exception in thread "main" java.lang.IllegalStateException: There are errors in a score DRL:
Error Messages:
Message [id=1, kieBase=defaultKieBase, level=ERROR, path=org/optaplanner/examples/projectjobscheduling/solver/projectJobSchedulingScoreRules.drl, line=95, column=0
   text=Rule Compilation error The operator - is undefined for the argument type(s) Comparable]
---
Warning Messages:
---
Info Messages:

at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:507)
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:331)
at org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:220)
at org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:61)
at org.optaplanner.examples.common.app.CommonApp.createSolver(CommonApp.java:103)
at org.optaplanner.examples.common.app.CommonApp.createSolutionBusiness(CommonApp.java:97)
at org.optaplanner.examples.common.app.CommonApp.init(CommonApp.java:84)
at org.optaplanner.examples.common.app.CommonApp.init(CommonApp.java:80)
at org.optaplanner.examples.projectjobscheduling.app.ProjectJobSchedulingApp.main(ProjectJobSchedulingApp.java:34)

我没有修改projectJobSchedulingScoreRules.drl文件,但这里是文件内容:

/*
 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.optaplanner.examples.projectjobscheduling.solver;
    dialect "java"

import org.optaplanner.core.api.score.buildin.bendable.BendableScoreHolder;

import org.optaplanner.examples.projectjobscheduling.domain.Allocation;
import org.optaplanner.examples.projectjobscheduling.domain.ExecutionMode;
import org.optaplanner.examples.projectjobscheduling.domain.Job;
import org.optaplanner.examples.projectjobscheduling.domain.JobType;
import org.optaplanner.examples.projectjobscheduling.domain.Project;
import org.optaplanner.examples.projectjobscheduling.domain.ResourceRequirement;
import org.optaplanner.examples.projectjobscheduling.domain.resource.Resource;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.NonrenewableResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.RenewableResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.capacity.ResourceCapacityTracker;
import org.optaplanner.examples.projectjobscheduling.solver.score.drools.RenewableResourceUsedDay;

global BendableScoreHolder scoreHolder;

// ############################################################################
// Hard constraints
// ############################################################################

rule "nonrenewableResourceCapacity"
    when
        $resource : Resource(renewable == false, $capacity : capacity)
        accumulate(
            ResourceRequirement(resource == $resource,
                    $executionMode : executionMode,
                    $requirement : requirement)
            and Allocation(executionMode == $executionMode);
            $used : sum($requirement);
            $used > $capacity
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, 0, $capacity - $used);
end

rule "renewableResourceUsedDay"
        salience 1 // Do these rules first (optional, for performance)
    when
        ResourceRequirement(resourceRenewable == true, $executionMode : executionMode, $resource : resource)
        Allocation(executionMode == $executionMode,
                $startDate : startDate, $endDate : endDate)
    then
        for (int i = $startDate; i < $endDate; i++) {
            insertLogical(new RenewableResourceUsedDay($resource, i));
        }
end

rule "renewableResourceCapacity"
    when
        RenewableResourceUsedDay($resource : resource, $capacity : resourceCapacity, $usedDay : usedDay)
        accumulate(
            ResourceRequirement(resource == $resource,
                    $executionMode : executionMode,
                    $requirement : requirement)
            and Allocation(executionMode == $executionMode, $usedDay >= startDate, $usedDay < endDate);
            $used : sum($requirement);
            $used > $capacity
        )
    then
        scoreHolder.addHardConstraintMatch(kcontext, 0, $capacity - $used);
end

// ############################################################################
// Soft constraints
// ############################################################################

rule "totalProjectDelay"
    when
        Allocation(jobType == JobType.SINK, endDate != null, $endDate : endDate,
               $criticalPathEndDate : projectCriticalPathEndDate)
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 0,  $criticalPathEndDate - $endDate);
end


rule "totalMakespan"
    when
        accumulate(
            Allocation(jobType == JobType.SINK, $endDate : endDate);
            $maxProjectEndDate : max($endDate)
        )
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 1, -$maxProjectEndDate);
end

有没有人遇到过类似的问题?有任何线索如何修复它?提前谢谢。

2 个答案:

答案 0 :(得分:3)

错误消息是:

  

运算符 - 未定义参数类型可比较

这是max() Drools累加器函数的限制的结果,该函数不是通用的,即使用Comparable参数提供它,它的返回类型总是Integer。因此$maxProjectEndDate变量的类型为Comparable(不是数字),因此您无法使用-运算符否定其值。

作为快速解决方法,您可以在Integer规则中否定它之前将其强制转换为totalMakespan

scoreHolder.addSoftConstraintMatch(kcontext, 1, -((Integer) $maxProjectEndDate));

答案 1 :(得分:1)

正如yurloc所解释的那样,max()drools积累函数的结果是一个可比较的对象,这就是为什么它不能使用 - 运算符来否定。这个答案只是yurloc答案的另一种选择:

rule "totalMakespan"
    when
        $maxProjectEndDate : Number() from accumulate(
            Allocation(jobType == JobType.SINK, $endDate : endDate);
            max($endDate)
        )
    then
        scoreHolder.addSoftConstraintMatch(kcontext, 1, -$maxProjectEndDate.intValue());
end