当前位置:  开发笔记 > 编程语言 > 正文

Drools 6从String加载并执行规则

如何解决《Drools6从String加载并执行规则》经验,为你挑选了1个好方法。

我试图从Drools 6中的String加载规则,如下所示:

// the rule
def drl = '''
dialect "mvel"

rule "Person is over 18"
    when
        $person : Person(age > 18)
    then
        System.out.println("Person is "+$person.name);
end
'''
// setup for rule
KieServices kieServices = KieServices.Factory.get()
KieFileSystem kfs = kieServices.newKieFileSystem()
kfs.write( "src/main/resources/simple.drl",
    kieServices.getResources().newReaderResource( new StringReader(drl) ) )
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll()

// check there have been no errors for rule setup
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
    println( results.getMessages() )
    throw new IllegalStateException( "### errors ###" )
}
KieContainer kieContainer =
    kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() )
KieSession kieSession = kieContainer.newKieSession()

// insert facts and fire rules
kieSession.insert(new Person("Jon Doe", 21))
kieSession.insert(new Person("Jon Darcy", 1))
kieSession.fireAllRules()

kieSession.dispose()

@Immutable
class Person {
    String name
    int age
}

我想要发生的是打印出人名.通过附加eventlistener和logger,我可以看到事实被添加和断言.通过在drl中出错,我可以确定看到和编译规则.但规则永远不会发生.

我很确定在代码中某处有一个愚蠢的小错误.有人能帮助我吗?



1> laune..:

从KieContainer获取KieBase并从中创建KieSession对我有用:

KieContainer kieContainer =
   kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId() );
KieBase kieBase = kieContainer.getKieBase();
KieSession kieSession = kieBase.newKieSession();

但是你的代码也在起作用 - 至少在用Java完成所有代码之后,并确保Person和DRL文件在同一个包中.

String drl = "package drlstring;\n" +
    "dialect 'mvel'\n" +
    "rule Person_is_over_18\n" +
    "when\n" +
    "$person : Person(age > 18)\n" +
    "then\n" +
    "System.out.println(\"Person is \"+$person.getName());\n" +
    "end";
// setup for rule
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write( "src/main/resources/simple.drl",
       kieServices.getResources().newReaderResource( new StringReader(drl) ) );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();

// check there have been no errors for rule setup
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
    System.out.println( results.getMessages() );
    throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
    kieServices.newKieContainer( kieBuilder.getKieModule().getReleaseId() );
KieSession kieSession = kieContainer.newKieSession();

// insert facts and fire rules
kieSession.insert(new Person("Jon Doe", 21));
kieSession.insert(new Person("Jon Darcy", 1));
kieSession.fireAllRules();
kieSession.dispose();

如果要继续使用Scala设置,请通过添加具有空条件的规则来缩小导致失败的可能原因:

rule hello
when then 
  System.out.println( "Hello!" );
end

认为这是Drools引擎无法识别的类Person的Scala定义.

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有