Drools Rules Writing - rules engine - drools tutorial - business rules engine
What is Rules Writing in Drools?
- If you understand the default rule that is written in the Welcome Wikitechy project (Sample.drl), there are a lot of keywords used which we will be explaining now.
learn drools tutorial - drools project - Rules anatomy and structure - drools example programs
com.sample.DroolsTest.Message
learn drools tutorial - code in drools rules writing - drools example
Sample.drl
- Package: Every Rule starts with a package name. The package acts as a namespace for Rules. Rule names within a package must be unique. Packages in Rules are similar to packages in Java.
- Import statement: Whatever facts you want to apply the rule on, those facts needs to be imported. For example, com.sample.DroolsTest.Message; in the above example.
- Rule Definition: It consists of the Rule Name, the condition, and the Consequence. Drools keywords are rule, when, then, and end. In the above example, the rule names are “Hello World” and “GoodBye”. The when part is the condition in both the rules and the then part is the consequence. In rule terminology, the when part is also called as LHS (left hand side) and the then part as the RHS (right hand side) of the rule.
Now let us walk through the terms used in the Java file used to load the Drools and execute the rules.
Knowledge Base
- Knowledge Base is an interface that bring about a collection of rules, processes, and internal types. It is contained inside the package org.drools.KnowledgeBase. In Drools, these are commonly referred to as knowledge definitions or knowledge. Knowledge definitions are grouped into knowledge packages. Knowledge definitions can be added or removed. The main purpose of Knowledge Base is to store and reuse them because their creation is expensive. Knowledge Base provides methods for creating knowledge sessions.
Knowledge Session
- The knowledge session is recovered from the knowledge base. It is the main interface for interacting with the Drools Engine. The knowledge session can be of two types:
- Stateless Knowledge Session
- Stateful Knowledge Session
Stateless Knowledge Session
- Stateless Knowledge Session is a stateless session that forms the simplest use case, not utilizing inference. A stateless session can be called like a function, passing it some data and then receiving some results back. Common examples of a stateless session include:
Validation
- Is this person eligible for a mortgage?
Calculation
- Compute a mortgage premium.
Routing and Filtering
- Filter incoming messages, such as emails, into folders.
- Send incoming messages to a destination
Stateful Knowledge Session
- Stateful sessions are longer survived and allow iterative changes over time. Some common use cases for stateful sessions include:
Monitoring
- Stock market monitoring and analysis for semi-automatic buying.
Diagnostics
- Fault finding, medical diagnostics
- Parcel tracking and delivery provisioning
Knowledge Builder
- The Knowledge Builder interface is responsible for building a Knowledge Package from knowledge definitions (rules, processes, types). It is contained inside the package org.drools.builder.KnowledgeBuilder. The knowledge definitions can be in various formats. If there are any problems with building, the Knowledge Builder will report errors through these two methods: hasErrorsand getError.
The following diagram explains the process
learn drools tutorial - drools wrting process - drools example
- In the above example, as we are taking a simple example of stateless knowledge session, we have inserted the fact in the session, and then fireAllRules() method is called and you see the output.
- In case of a Computing knowledge session, once the rules are fired, the Computing knowledge session object must call the method dispose() to release the session and avoid memory leaks.