Skip to content

Instantly share code, notes, and snippets.

@eternnoir
Created March 9, 2017 02:51
Show Gist options
  • Save eternnoir/b990fd8ee3a3d9ca8673906be083e0fd to your computer and use it in GitHub Desktop.
Save eternnoir/b990fd8ee3a3d9ca8673906be083e0fd to your computer and use it in GitHub Desktop.
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
// 這裡我將drl檔案放在 drools/example1.drl 這裡,記得改成你的檔案目錄
kbuilder.add(ResourceFactory.newClassPathResource("drools/example1.drl"), ResourceType.DRL);
// 確認載入是否有錯誤
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
throw new RuntimeException("Unable to compile \"example1.drl\".");
}
// 取得所有Package
Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(pkgs);
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
ksession.addEventListener( new DebugAgendaEventListener() );
// Set up Message.
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
// 跑所有Rule
ksession.fireAllRules();
ksession.dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment