DROOLS - NeverGiveUp143/Drools GitHub Wiki

Introduction to Drools

Overview :

Drools is a Business Rule Management System (BRMS) solution. It provides a rule engine which processes facts and produces output as a result of rules and facts processing. Centralization of business logic makes it possible to introduce changes fast and cheap. It also bridges the gap between the Business and Technical teams by providing a facility for writing the rules in a format which is easy to understand.

Maven Dependencies:

<dependency>
    <groupId>org.kie</groupId>
    <artifactId>kie-ci</artifactId>
    <version>8.32.0.Final</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-decisiontables</artifactId>
    <version>8.32.0.Final</version>
</dependency>

Drools Components

  1. Facts Facts represent the data that serves as input for rules. These are typically Java objects that are inserted into the Drools working memory, where they are evaluated against the rules.

  2. Working Memory The working memory is a dynamic storage area where facts are stored. Facts in the working memory are used for pattern matching by the rules engine. They can be:

    Inserted: Adding new facts.

    Updated: Modifying existing facts.

    Retracted: Removing facts.

  3. Rule A rule in Drools defines the logic for pattern matching and the corresponding actions to be taken. Rules are typically written in the Drools Rule Language (DRL), which allows for a clear, declarative expression of conditions and actions. Alternatively, rules can be represented in decision tables (e.g., Excel spreadsheets) for a more business-friendly format. A rule typically consists of:

    Conditions (LHS - Left-Hand Side): The "if" part that specifies the patterns to be matched.

    Consequences (RHS - Right-Hand Side): The "then" part that specifies the actions to be performed when the conditions are met.

  4. Knowledge Session A knowledge session is an instance of the Drools runtime environment where all the necessary resources for executing rules are loaded. There are two main types of sessions:

    Stateless Knowledge Session: Best for scenarios where a single execution of rules on a set of facts is required.

    Stateful Knowledge Session: Maintains state across multiple invocations and is useful for scenarios where rules need to be executed on an evolving set of facts.

  5. Knowledge Base The knowledge base is a repository for storing all the Drools resources, such as rules, processes, and functions. It acts as a container for knowledge definitions and is responsible for creating knowledge sessions. The knowledge base ensures that the rules and other resources are consistently managed and can be reused across different sessions.

  6. Module A module in Drools is a logical grouping that can hold multiple knowledge bases. Each knowledge base within a module can contain different sets of rules and sessions. Modules help in organizing and managing large sets of rules and resources, facilitating better modularity and scalability.

Architecture of Drools:

image

Here is the working system of Drools architecture:

Step 1) The rules are loaded into Rule Base, which are available all the times.

Step 2) Facts are asserted into the Working Memory where they may then be modified or retracted.

Step 3) The process of matching the new or existing facts against production rules is called pattern matching, which is performed by the Rule engine.

Step 4) The agenda allows you to manage the execution order of the conflicting rules with the help of a conflict resolution strategy.

Project Structure:

Drools Project Structure

project_root/
    pom.xml        # Maven project configuration file
    src/
        main/
            java/       # Java source code for rule classes
            resources/  # Optional resources directory
            rules/      # Drools rule files (.drl extension)
        test/
            java/       # Java source code for tests
            resources/  # Optional resources directory
    META-INF/
        kmodule.xml  # Optional KIE project configuration (Knowledge Base and KieSessions)

Configuring the Project

1.pom.xml

  <dependencies>
    <dependency>
        <groupId>org.kie</groupId>
        <artifactId>kie-api</artifactId>
        <version>7.57.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-core</artifactId>
        <version>7.57.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.drools</groupId>
        <artifactId>drools-compiler</artifactId>
        <version>7.57.0.Final</version>
    </dependency>
    <!-- Add other dependencies as needed -->
</dependencies>

2.kmodule.xml

<kmodule xmlns="http://www.drools.org/xsd/kmodule">
    <kbase name="rulesKBase" packages="rules">
        <ksession name="rulesKSession"/>
    </kbase>
</kmodule>

3.SampleRule.drl

package rules

rule "Sample Rule"
when
    Fact(attribute == "value")
then
    // Actions to perform
end

⚠️ **GitHub.com Fallback** ⚠️