Creating Drools Knowledgebase, Knowledgesession and Facts

    4 Votes

Before going into Creating Knowledge Base and Knowledge Session example, we will have look at its definitions and use.  

  KnowledgeBase is an interface which manages a collection of knowledge definitions like rules, Internal Types and process. Knowledge packages are created by grouping knowledge definition. Since the creation of Knowledge packages is very expensive, KnowledgeBase stores and reuse them. Knowledge Sessions are created from KnowledgeBase, which consumes less memory. KnowledgeBase keeps track of each Knowledge Session, so that updates to knowledge base can be applied to all the session at run time. Another advantage of KnowledgeBase is that, it is completely serializable. We can restore serializable object of KnowledgeBase rather creating a new one.

There are two types of Knowledge Sessions 

  1. StatelessKnowledgeSession
  2. StatefulKnowledgeSession

Both session implements BatchExecutor interface. In both sessions, we can insert, update and retract facts. Difference between the two is that stateless session automatically executes "FireAllRulesCommand" when facts are inserted/updated. In StatefulKnowledgeSession, after inserting the facts we had to explicitly call "Fire All Rules" Command.

Sample code to create Knowledge Base is given below

public KnowledgeBase CreateKB() {
    KnowledgeBase kbDemo = KnowledgeBaseFactory.newKnowledgeBase();
    // Create Knowledge Builder
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    // Add Rule files
    kbuilder.add(ResourceFactory.newFileResource("D:\\AlarmRule.drl"), ResourceType.DRL);
    // Find errors in rule and work flow file
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error); 
        }
} // Add Knowledge builder to Knowledge Base kbDemo.addKnowledgePackages(kbuilder.getKnowledgePackages()); // return Knowledge Base return kbDemo ; }

Sample code to create Knowledge Session is given below

KnowledgeBase kbase = CreateKB();
StatefulKnowledgeSession kSession = kbase.newStatefulKnowledgeSession();

DROOLS Facts

Facts are nothing but plain old java objects (POJO).  This article provides an overview on "How to create DROOLS Facts" in eclipse with the help of an example. A sample fact about a "Room" is as given below. Room have 4 properties. They are Room Name, Height , Width and Alarm Status. Value of alarm status will be true when alarm occurs due to fire or short circuit. When in normal condition, value of alarm status to be False. 

public class Room 
{
private String roomName;
private String ROOM_WIDTH;
private String ROOM_HEIGHT;
private boolean alarmStatus;

public String getRoomName() {
return roomName;
}

public void setRoomName(String roomName) {
this.roomName = roomName;
}

public String getROOM_WIDTH() {
return ROOM_WIDTH;
}

public void setROOM_WIDTH(String rOOM_WIDTH) {
ROOM_WIDTH = rOOM_WIDTH;
}

public String getROOM_HEIGHT() {
return ROOM_HEIGHT;
}

public void setROOM_HEIGHT(String rOOM_HEIGHT) {
ROOM_HEIGHT = rOOM_HEIGHT;
}

public boolean isAlarmStatus() {
return alarmStatus;
}

public void setAlarmStatus(boolean alarmStatus) {
this.alarmStatus = alarmStatus;
}

}

 If you look at the above fact you can see that , variables "roomName", "alarmStatus" starts with a small letter. Variables ROOM_WIDTH, ROOM_HEIGHT are capital. Reason behind it is that, drools wont allow variables starting with capital letter preceded by small letter. Fully capitalized words are accepted by DROOLS.

For example , If we define alarm variable as "AlarmStatus" , drools throws exception "Unable to create Field Extractor for 'AlarmStatus' "

Another aspect of defining variables is that , access modifier should be private. There should be getters and setters to access the variable. In the above DROOLS example, variable "roomName" is accessed using "getRoomName" and "setRoomName" methods. If Get and Set methods are not available DROOLS throws exception "Unable to create Field Extractor for 'roomName' "

Hope this article on "Drools Facts Example" helps you in writing rules. 

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

Basic Drools Concepts
Steps to install DROOLS plugin and create DROOLS Project in Eclipse
How to call external function from drools drl files
How to debug drools in eclipse with example