1

我对这个流口水的员工完全陌生。因此,我在这里遇到了一些麻烦。

rule "Raise the alarm when we have one or more fires"
    when
exists Fire()
then 
    insert( new Alarm() );  
end

当我有这段代码时,它工作正常。但我想改变一下:

rule "Raise the alarm when we have one or more fires"
    when
exists Fire()
then 
    $alarm = new Alarm();
    $alarm.RingBell();
    insert( $alarm );  
end

可能你明白我在这里想要做什么。我想实例化 Alarm 类并触发它的 RingBell 方法。但它给了我这个错误

Rule Compilation error : [Rule name='Raise the alarm when we have one or more fires']
    com/sample/Rule_Raise_the_alarm_when_we_have_one_or_more_fires_cd7449c70a6a48c78f4e291495d23b05.java (8:436) : alarm cannot be resolved

java.lang.IllegalArgumentException: Could not parse knowledge.
    at com.sample.DroolsTest.readKnowledgeBase(DroolsTest.java:117)
    at com.sample.DroolsTest.main(DroolsTest.java:28)

如果你们在这里帮助我,我将不胜感激:))

4

1 回答 1

5

错误是因为从未声明 $alarm 变量。尝试执行以下操作:

rule "Raise the alarm when we have one or more fires"
    when
exists Fire()
then 
    Alarm $alarm = new Alarm();
    $alarm.RingBell();
    insert( $alarm );  
end

重要的一行是:Alarm $alarm = new Alarm();

于 2012-11-12T08:14:04.710 回答