运行Drools时出错 - 我该如何调试?

时间:2014-04-15 20:09:24

标签: debugging drools optaplanner

我在Java程序中有一个Drools文件(在本例中是Optaplanner的变体),我在Netbeans 8上构建。当我运行该程序时,我收到一条错误消息:

Exception in thread "main" java.lang.IllegalArgumentException: The scoreDrl (taskassignment/solver/taskAssignmentScoreRules.drl) does not exist as a classpath resource.
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildKieBase(ScoreDirectorFactoryConfig.java:304)
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:279)
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:186)
at org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:174)
at org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:145)
at org.optaplanner.core.config.solver.XmlSolverFactory.buildSolver(XmlSolverFactory.java:101)
at taskassignment.app.TaskAssignmentHelloWorld.main(TaskAssignmentHelloWorld.java:32)

使用Google搜索错误信息时,似乎在drools代码中出现错误时,Optaplanner会抛出does not exist as a classpath resource

我的问题是,没有关于drools文件中错误位置的进一步信息,调试这段代码的好方法是什么?是否有任何插件可以帮助我缩小Netbeans通常使用Java的问题?

以下是我的Drools代码:

package taskassignment.solver;
dialect "java"

import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScoreHolder;

import taskassignment.domain.TaskAssignment;
import taskassignment.domain.Task;
import taskassignment.domain.Dev;

global HardSoftScoreHolder scoreHolder;

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

rule "DevCanOnlyDoOneTask"
when
    $T1:Task
    $T2:Task
    $D:Dev
    (($T1.getAssignedDev()=$D)&&($T2.getAssignedDev()=$D)&&(($T2.getAllottedStartTime()<=$T1.getAllottedStartTime())&&($T1.getAllottedStartTime()<$T2.getAllottedStartTime()+$T2.getDuration()))||(($T1.getAllottedStartTime()<=$T2.getAllottedStartTime())&&($T2.getAllottedStartTime()<$T1.getAllottedStartTime()+$T1.getDuration()))

then
    scoreHolder.addHardConstraintMatch(kcontext,-1000);
end

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

rule "MaximiseEarliestFinishTime"
when
    $TA: TaskAssignment
    $EFT: $TA.getEFT()

then
    scoreHolder.addSoftConstraintMatch(kcontext,+$EFT);

谢谢你们

1 个答案:

答案 0 :(得分:3)

当drools(DRL)代码出现错误时,它会抛出一个不同的异常,来自drools并说明错误消息中DRL中的哪一行。

消息

The scoreDrl (taskassignment/solver/taskAssignmentScoreRules.drl) does not exist as a classpath resource.

表示您已在求解器配置

中对此进行了配置
<scoreDrl>taskassignment/solver/taskAssignmentScoreRules.drl</scoreDrl>

并且它不是有效的类路径资源位置。

在具有maven目录结构的典型项目中,类路径资源位于src/main/resources下。

陷阱:在6.0中,OptaPlanner使用Class.getResource(String)来查找它。在6.1.0.Beta3 +中,OptaPlanner使用ClassLoader.getResource(String)来查找它。这意味着目前在6.0中,它需要以/开头,而在6.1中,它不能以/开头。

因此,如果您的项目源中有一个资源文件(假设您正在使用maven目录结构):

PROJECT_DIR/src/main/resources/taskassignment/solver/taskAssignmentScoreRules.drl

在6.0中,您需要配置:

<scoreDrl>/taskassignment/solver/taskAssignmentScoreRules.drl</scoreDrl>

在6.1中你需要配置:

<scoreDrl>taskassignment/solver/taskAssignmentScoreRules.drl</scoreDrl>

注意:您可能希望遵循java约定并使用反向url命名空间。因此,我使用taskassignment/solver/...而不是com/mycompany/taskassignment/solver/...