java 在 Drools 6 中从数据库加载和更新规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30243805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading and updating rules from a database in Drools 6
提问by user506069
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? I've found an exampleusing 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 6.2.0 中的同一个表更新它们?我找到了一个使用 Drools 5的示例,我可能可以将它从 Scala 转换为 Java,但看起来 API 已经发生了巨大的变化……例如,我没有看到 RuleBaseFactory 类。
Any sample or documentation would be appreciated.
任何示例或文档将不胜感激。
回答by laune
I'm not sure from where that org.drools.RuleBaseFactory
was taken. Below is how it was done in Drools 5.3 (and possibly earlier) up to 5.6:
我不确定这org.drools.RuleBaseFactory
是从哪里拍摄的。下面是它在 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; a java.lang.String
should be acceptable.
省略号表示插入包含规则文本的数据的位置。检查 API 以获取合适的类型;ajava.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();
回答by Rajvidya Chandele
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. I found a way to do this using ObjectDataCompiler. May be some people might find this useful.
我将通过 HTTP 传输的数据转换为规则。我找到了一种使用 ObjectDataCompiler 做到这一点的方法。可能有些人可能会发现这很有用。
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