【Drools学习笔记】02-动态加载规则
程序员文章站
2022-05-28 11:17:17
...
上一篇Drools简介中的例子将规则写在了drl文件中,我们还可以动态的拼凑我们想要的规则,代码如下:
@Test
public void testKieRuntimeAccess() throws IOException, ClassNotFoundException {
String str = "";
str += "package org.drools.compiler.test\n";
str += "import " + Message.class.getName() + "\n";
str += "rule \"Hello World\"\n";
str += "when\n";
str += " Message( )\n";
str += "then\n";
str += " System.out.println( drools.getKieRuntime() );\n";
str += "end\n";
KieBase kbase = KnowledgeBuilderFactory.newKnowledgeBuilder().add(ResourceFactory.newByteArrayResource(str).getBytes()), ResourceType.DRL);
kbase = SerializationHelper.serializeObject( kbase );
final KieSession ksession = kbase.newKieSession();
ksession.insert( new Message( "help" ) );
ksession.fireAllRules();
ksession.dispose();
}
上一篇: 规则引擎 Drools 使用解析
下一篇: java规则引擎 -- Drools