《Drools7.0.0.Final规则引擎教程》注释&错误信息详解编程语言

注释

像Java开发语言一样,Drools文件中也可以添加注释。注释部分Drools引擎是会将其忽略调的。单行注释使用“//”,示例如下:

rule "Testing Comments" when // this is a single line comment 
    eval( true ) // this is a comment in the same line of a pattern 
then // this is a comment inside a semantic code block 
end

注意,使用“#”进行注释已经被移除。
多行注释与Java相同,采用“/注释内容/”,来进行注释,示例如下:

rule "Test Multi-line Comments" when 
    /* this is a multi-line comment 
       in the left hand side of a rule */ 
    eval( true ) 
then 
    /* and this is a multi-line comment 
       in the right hand side of a rule */ 
end

错误信息

Drools 5引入了标准化的错误信息,可以快速的查找和解决问题。本节将介绍如何利用错误信息来进行快速定位问题和解决问题。
错误信息的各式如下图:
这里写图片描述
第一部分:错误编码;
第二部分:错误出现的行列信息;
第三部分:错误信息描述;
第四部分:上下午的第一行信息,通常表示发生错误的规则,功能,模板或查询。此部分并不强制。
第五部分:标识发生错误的pattern(模式)。此部分并不强制。

下面以一组错误实例来分析常见的异常情况,首先用官网提供的例子来执行:

rule one 
  when 
    exists Foo() 
    exits Bar()  // "exits" 
  then end

由于exits是错误的语法,因此会抛出异常,但此处需要注意的事在Drools 7中抛出的异常并非官网提供的异常。异常信息如下:

java.lang.RuntimeException: Error while creating KieBase[Message [id=1, kieBase=rules, level=ERROR, path=conditional1.drl, line=27, column=0 
   text=[ERR 102] Line 27:6 mismatched input 'Bar' in rule "one" in pattern], Message [id=2, kieBase=rules, level=ERROR, path=conditional1.drl, line=0, column=0 
   text=Parser returned a null Package]]

再看一个没有规则名称导致的错误:

rule 
   when Object() 
   then 
     System.out.println("A RHS"); 
end

执行之后异常信息如下:

java.lang.RuntimeException: Error while creating KieBase[Message [id=1, kieBase=rules, level=ERROR, path=conditional1.drl, line=25, column=0 
   text=[ERR 102] Line 25:3 mismatched input 'when' in rule], Message [id=2, kieBase=rules, level=ERROR, path=conditional1.drl, line=0, column=0 
   text=Parser returned a null Package]]

很显然上面的异常是因为规则没有指定名称,而关键字when无法作为名称,因此在此处抛出异常。
格式不正确导致的异常:

rule test 
   when 
    foo3:Object(

异常信息如下:

java.lang.RuntimeException: Error while creating KieBase[Message [id=1, kieBase=rules, level=ERROR, path=conditional1.drl, line=0, column=0 
   text=Line 26:16 unexpected exception at input '<eof>'. Exception: java.lang.NullPointerException. Stack trace: 
 java.lang.NullPointerException

其他异常信息就不在这里赘述了,实际应用中不断的学习总结即可根据错误信息快速定位问题所在。

 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/15194.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论