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 - Rules anatomy and structure - drools example programs

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

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

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.

Wikitechy drools tutorial provide you the details of all the following contents such as define drool , drools meaning , drools rule engine , java rule engine , drools example , jboss drools , java open source , rule engine java , open source bpm , jboss brms , open source rules engine , drools rules , drools java , drools engine , drools guvnor , drooled definition , drools documentation , java drools , drools fusion , drools eclipse plugin , drools decision table , drools net , drools rules examples , drools alternatives , drools rule engine tutorial , drools workbench tutorial , drools definition , drools jboss , drools spring , drools expert , apache drools , drools interview questions , drools wiki , rule engine drools , drools kie , drools ui , kie drools , jboss drools tutorial , drools syntax , drools java example , jbpm drools , drools jbpm , brms drools , drools rule language

Related Searches to Drools Rules Writing