简体   繁体   English

从 Drools 6 中的数据库加载和更新规则

[英]Loading and updating rules from a database in Drools 6

How would one go about loading rules from a database table at startup and updating them from the same table in Drools 6.2.0?如何在启动时从数据库表加载规则并从 Drools 6.2.0 中的同一个表更新它们? I've found an example using Drools 5 that I could probably convert from Scala to Java but it looks like the API has changed pretty drastically... I don't see the RuleBaseFactory class, for example.我找到了一个使用 Drools 5 的示例,我可以将它从 Scala 转换为 Java,但看起来 API 已经发生了巨大的变化……例如,我没有看到 RuleBaseFactory 类。

Any sample or documentation would be appreciated.任何示例或文档将不胜感激。

I'm not sure from where that org.drools.RuleBaseFactory was taken.我不确定org.drools.RuleBaseFactory是从哪里获取的。 Below is how it was done in Drools 5.3 (and possibly earlier) up to 5.6:下面是它在 Drools 5.3(可能更早)到 5.6 中是如何完成的:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ..., ResourceType.DRL);
if( kbuilder.hasErrors() ){
    System.err.println( "### compilation errors ###" );
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    for( KnowledgeBuilderError err: errors ){
        System.err.println( err.toString() );
    }
    throw new IllegalStateException( "compile errors" );
}
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();

The ellipsis indicates the place for inserting the data holding the rule text.省略号表示插入包含规则文本的数据的位置。 Check the API for suitable types;检查 API 以获取合适的类型; a java.lang.String should be acceptable. java.lang.String应该是可以接受的。

This is the way I use for 6.2:这是我用于 6.2 的方式:

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
kfs.write( "src/main/resources/simple.drl", ... );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
    System.out.println( results.getMessages() );
    throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
    kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
KieBase kieBase = kieContainer.getKieBase();
kieSession = kieContainer.newKieSession();

drools-templates has ResultSetGenerator.java which has method compile(resultSet, template) to do the job. drools-templates 有 ResultSetGenerator.java,它有方法 compile(resultSet, template) 来完成这项工作。

I had data coming over HTTP to be converted to rule.我将通过 HTTP 传输的数据转换为规则。 I found a way to do this using ObjectDataCompiler.我找到了一种使用 ObjectDataCompiler 做到这一点的方法。 May be some people might find this useful.可能有些人可能会发现这很有用。

ObjectDataCompiler compiler = new ObjectDataCompiler();
String generatedDRL = compiler.compile(ruleAttributes, new FileInputStream(REGULATION_TEMPLATE_FILE));

where ruleAttributes is其中 ruleAttributes 是

List<Map<String, String>> ruleAttributes = new ArrayList<>();
Map<String, String> rule1 = new HashMap<>();
rule1.put("ruleid", "2");
rule1.put("ifcondition", "abc: Abc(xyz.getId() == 2);");
rule1.put("thencondition", "myGlobal.setPqr(200.1D);");
ruleAttributes.add(rule1);

KieBase can then be created like this:然后可以像这样创建 KieBase:

KieServices kieServices = KieServices.Factory.get();

KieHelper kieHelper = new KieHelper();

//multiple such resoures/rules can be added
byte[] b1 = generatedDRL.getBytes();
Resource resource1 = kieServices.getResources().newByteArrayResource(b1);
kieHelper.addResource(resource1, ResourceType.DRL);

KieBase kieBase = kieHelper.build();

Rules can be applied like this:规则可以这样应用:

KieSession kieSession = kieBase.newKieSession();
kieSession.setGlobal("myGlobal", myGlobal);
kieSession.insert(abc);
int numberOfRulesFired = kieSession.fireAllRules();
kieSession.dispose();

Template file looks like this:模板文件如下所示:

template header
ruleid
ifcondition
thencondition

import fk.sp.seldon.msku.MSKU

global com.something.blah.MyGlobal myGlobal

template "tmp1"

rule "@{ruleid}"
  dialect "mvel"
  when
    @{ifcondition}
  then
    @{thencondition};
end
end template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM