如何实例化.drl文件中的对象

时间:2012-11-11 14:11:42

标签: java instantiation drools

我对这个流口水的工作人员来说是全新的。因此,我在这里遇到了一些麻烦。

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)

如果你们帮助我,我会很感激:))

1 个答案:

答案 0 :(得分: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 = new警报();