package
package定义在规则文件的首行,是规则文件的三大模块之一。
package表示规则的逻辑路径。建议在定义的时候和物理路径相同。但这并不是必须的。可以不同。不过强烈建议。
package包含 import、global、funcation、query、rule、EOF。
rule是规则文件的核心。前面学习的规则属性就是rule中的内容。
在前面的示例中,我们配置过kmodule.xml这样的一个文件,在这个文件中KieBase元素设置了packages路径,来表示当前路径下的所有规则文件,像规则文件、决策表、领域语言文件等等都会被加入到规则库中。但是当前路径下子文件夹的规则文件并没有包含在当前的规则库中。
package参数实际上是一个命名空间,没有去关联文件或者文件夹。因此,可以有多个规则目录为规则库构建源组合规则。有一个顶级的package配置。所有的规则都在其控制之下。
虽然生命在不同名称下的资源不可能合并成一个包,但是单个规则库可以用多个包来构建它。
packages可以设置多个路径,通过逗号来分割。
创建规则内容:
1 package rules.isPackage 2 rule "testRuleNameOnly" 3 when 4 eval(true); 5 then 6 System.out.println("testRuleNameOnly say hello"); 7 end 8 rule "testRuleNameOnly" 9 when 10 eval(true); 11 then 12 System.out.println("testRuleNameOnly say hello"); 13 end
在配置文件中继续添加:
<kbase name="isPackage" packages="rules.isPackage"> <ksession name="isPackge"/> </kbase>
看下运行结果:
2019-08-07 22:58:12 [ERROR]org.drools.....KieProject - Unable to build KieBaseModel:isPackage [8,0]: Duplicate rule name: testRuleNameOnly Rule Compilation error : [Rule name=‘testRuleNameOnly‘] rules/isPackage/Rule_testRuleNameOnly670538843.java (3:109) : The type Rule_testRuleNameOnly670538843 is already defined java.lang.RuntimeException: Error while creating KieBase[Message [id=1, kieBase=isPackage, level=ERROR, path=D:\JavaDev\workspace\DroolsProject\BaseProject\target\classes\rules.isPackage\package.drl, line=8, column=0 text=Duplicate rule name: testRuleNameOnly], Message [id=2, kieBase=isPackage, level=ERROR, path=D:\JavaDev\workspace\DroolsProject\BaseProject\target\classes\rules.isPackage\package.drl, line=8, column=0 text=Rule Compilation error The type Rule_testRuleNameOnly670538843 is already defined]] at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:363) at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:519) at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:487) at com.packages.RulePackage.RulePackage(RulePackage.java:14) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
很是明显的一个错误,提示规则文件代码rule名称重复了。修改名称后就可以正常运行。这个时候再添加一个规则文件,内容如下:
package rules.isPackage2 rule "testRuleNameOnly" when eval(true); then System.out.println("testRuleNameOnly say hello"); end
和之前的例子中区别就是package参数是不相同的。继续看下结果:
2019-08-07 23:02:41 [DEBUG]org.drools...DefaultAgenda - State was INACTIVE is now FIRING_ALL_RULES testRuleNameOnly say hello testRuleNameOnly say hello testRuleNameOnly say hello 2019-08-07 23:02:41 [DEBUG]org.drools...DefaultAgenda - State was FIRING_ALL_RULES is now HALTING 2019-08-07 23:02:41 [DEBUG]org.drools...DefaultAgenda - State was HALTING is now INACTIVE 总共执行了3条规则 2019-08-07 23:02:41 [DEBUG]org.drools...DefaultAgenda - State was INACTIVE is now DISPOSED
从结果中可以看出,同一个物理路径下的规则相关文件都会被加载到规则苦衷,不同规则文件中不同的package会影响规则名称的定义。
继续创建一个规则文件,测试一下子目录下的规则文件是不是会被加载。
创建规则内容:
package rules.isPackage.package2; rule "testRuleNameOnly" when eval(true); then System.out.println("testRuleNameOnly say hello"); end
看下结果:
2019-08-07 23:06:32 [DEBUG]org.drools...DefaultAgenda - State was INACTIVE is now FIRING_ALL_RULES testRuleNameOnly say hello testRuleNameOnly say hello testRuleNameOnly say hello 2019-08-07 23:06:32 [DEBUG]org.drools...DefaultAgenda - State was FIRING_ALL_RULES is now HALTING 2019-08-07 23:06:32 [DEBUG]org.drools...DefaultAgenda - State was HALTING is now INACTIVE 总共执行了3条规则 2019-08-07 23:06:32 [DEBUG]org.drools...DefaultAgenda - State was INACTIVE is now DISPOSED
结果是没有任何变化的。说明了规则库不会去加载子目录下的规则相关文件。
原文:https://www.cnblogs.com/sunl123/p/11318304.html