To fire a rules in a DRL, we need to create the knowledge base and knowledge session. The facts are inserted in a session and rules are fired on them. Before moving to how to create knowledge base and session let us first understand the basic terms
1) KnowledgeBuilder - This class belongs to org.drools.builder.KnowledgeBuilder package. KnowledgeBuilder class is responsible for compilation of the rules. The rules can be loaded into Knowledge Builder in the following formats :
a) DRL
b) BRL
c) DSL
d) PKG
KnowledgeBuilder takes the input rules and perform compilation. In case errors are found, getErrors() method of this class returns the list of org.drools.builder.KnowledgeBuilderError class. If no errors are found, the size of the list is 0.
2) KnowledgeBase - This class belongs to org.drools.KnowledgeBase package. Once the KnowledgeBuilder perform its task and if no errors are found, we load the Knowledge Packages from the builder and add them to KnowledgeBase. KnowledgeBase consists of all the rules, functions and other components of a rules file. KnowledgeBase allows the creation of KnowledgeSession either StatefulKnowledgeSession or StatelessKnowledgeSession.
Once the Knowledge Session gets created, the facts or java objects are inserted into the session and rules are fired on them. In case of StatefulKnowledgeSession, dispose() method needs to be called in order to flush and release the session object.
Below implementation shows what all components are there to run a rules.
1) DRL File - This file holds the rules
2) Fact - Java POJO to hold the bean properties
2) DroolsStatefulSession.java - Client File to load the Drl file, create knowledge base, load the session and fire rules on them.
DroolsStatefulSession.java
package com.client; import org.drools.KnowledgeBase; import org.drools.KnowledgeBaseFactory; import org.drools.builder.KnowledgeBuilder; import org.drools.builder.KnowledgeBuilderError; import org.drools.builder.KnowledgeBuilderErrors; import org.drools.builder.KnowledgeBuilderFactory; import org.drools.builder.ResourceType; import org.drools.io.ResourceFactory; import org.drools.runtime.StatefulKnowledgeSession; import com.pojo.Fact; public class DroolsStatefulSession { public static void main(String args[]) throws Exception { StatefulKnowledgeSession knowledgeSession = readKnowledgeBase().newStatefulKnowledgeSession(); try { Fact fact = new Fact(); fact.setName("First Name"); // Insert a fact in the session knowledgeSession.insert(fact); System.out.println("Fact Inserted into Knowledge Session"); knowledgeSession.fireAllRules(); System.out.println("Rules Fired"); } finally { knowledgeSession.dispose(); System.out.println("Knowledge Session Disposed"); } } private static KnowledgeBase readKnowledgeBase() throws Exception { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add(ResourceFactory.newClassPathResource("DroolsStatefulSession.drl"), ResourceType.DRL); KnowledgeBuilderErrors errors = kbuilder.getErrors(); if (errors.size() > 0) { for (KnowledgeBuilderError error : errors) { System.err.println(error); } throw new IllegalArgumentException("Could not parse knowledge."); } KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); kbase.addKnowledgePackages(kbuilder.getKnowledgePackages()); return kbase; } }
package com.pojo; public class Fact { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
import com.pojo.Fact; rule "This is a rule" when fact : Fact() then System.out.println("In DRL Rule Name - " + drools.getRule().getName()); end
Fact Inserted into Knowledge Session
In DRL Rule Name - This is a rule
Rules Fired
Knowledge Session Disposed
No comments:
Post a Comment