DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How To Handle 100k Rows Decision Table in Drools (Part 3)
  • How To Handle 100k Rows Decision Table in Drools (Part 2)
  • Using Maven and Without Drools Eclipse Plugin

Trending

  • Unlocking the Potential of Apache Iceberg: A Comprehensive Analysis
  • Create Your Own AI-Powered Virtual Tutor: An Easy Tutorial
  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)

Learn Drools: Part II (Cross Product)

If Drools seems foreign, try thinking of it as an RDBMS. See how their structures compare and why you can invoke one Drools Rule from another.

By 
Shamik Mitra user avatar
Shamik Mitra
·
Nov. 11, 16 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
20.4K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous article, we got a basic idea what Drools is and how it works. In this article, we'll pay attention to the details of how Rules work.

When we write Rules, we often face the question, "How can I invoke one Rule from another like we call a method?"

Well, the answer is we can’t call a Rule from another Rule. Drools matches the Rules with incoming data/facts, and if the data satisfies the Rule condition, it stores the data in an Agenda. It might be possible for the same data or facts to be matched by different rules, so it stores matching facts in an Agenda. After firing all the rules, then it takes the action on said Agenda.

So, as the rules apply to data/facts, it is not possible to call a rule from another, as we can’t pass any arguments/facts beforehand — the facts are coming at runtime.

So, rather than confusing the Rules with functional/OOP programming, consider it with a RDBMS.

Here's how they compare:

Drools Component RDBMS Component
Data/Facts/Domain Object data structure Table
Facts/Domain Objects Rows
Fact /Domain Object attributes Column
Domain Objects HAS-A relationship Joining/Cartesian Product
Drools operators Filter Criterias
Drools When When in SQL
Drools Then Trigger
Drools Rule SQL Query


Let's try to understand the above relationship with a simple example. Suppose we have two domain objects, say Department and Employee, and each Employee is associated with a Department.

Step 1: Creating the Data Structure for Department and Employee

This is nothing but two Simple POJOs.

Department.java

package com.example.droolsExample.pojo;
public class Department {
   String name;
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}


Employee.java

package com.example.droolsExample.pojo;
public class Employee {
   String name;
   boolean manager;
   String message;
   Department dept;
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public boolean isManager() {
      return manager;
   }
   public void setManager(boolean manager) {
      this.manager = manager;
   }
   public String getMessage() {
      return message;
   }

   public void setMessage(String message) {
      this.message = message;
   }

   public Department getDept() {
      return dept;
   }

   public void setDept(Department dept) {
      this.dept = dept;
   }
}


Step 2: Create a Simple Rule

Employee.drl

package com.rules
import com.example.droolsExample.pojo.Employee
import com.example.droolsExample.pojo.Department

rule "print cross product"
   when
      emp: Employee();
      dept: Department();
   then
   System.out.println("Fire print cross product Rule");
   System.out.println(dept.getName() + "::" + emp.getName()+ ": "+emp.getDept().getName());
end


Here, we create a simple rule. It tells us that for every instance of Employee and Department in working memory, print the Department name and Employee name and the Employee's Department name.

It is same as the following SQL

 Select * from Employee emp Department dept where emp.deptId=dept.deptId 

So it will create all Cartesian products in working memory. If we have two departments and two employees in working memory, it creates four combinations and then fires the rules on these combinations.

Step 3: Create Working Memory and Insert Objects to Show the Result

package com.example.droolsExample;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.WorkingMemory;
import com.example.droolsExample.pojo.Department;
import com.example.droolsExample.pojo.Employee;

public class DroolsTest {
   public static void main(String[] args) throws DroolsParserException,
          IOException {
      DroolsTest droolsTest = new DroolsTest();
      droolsTest.executeDroolsEmployee();
   }
   
   public void executeDroolsEmployee() throws DroolsParserException, IOException {
      PackageBuilder packageBuilder = new PackageBuilder();
      String ruleFile = "/com/rules/employee.drl";
      InputStream resourceAsStream = getClass().getResourceAsStream(ruleFile);
      Reader reader = new InputStreamReader(resourceAsStream);
      packageBuilder.addPackageFromDrl(reader);
      org.drools.core.rule.Package rulesPackage = packageBuilder.getPackage();
      RuleBase ruleBase = RuleBaseFactory.newRuleBase();
      ruleBase.addPackage(rulesPackage);
      WorkingMemory workingMemory = ruleBase.newStatefulSession();
      Department dep = new Department();
      dep.setName("Civil");
      Department dep1 = new Department();
      dep1.setName("IT");
      Employee emp = new Employee();
      emp.setName("Shamik Mitra");
      emp.setManager(true);
      emp.setDept(dep1);    
      Employee emp1 = new Employee();
      emp1.setName("Samir Mitra");
      emp1.setManager(true);
      emp1.setDept(dep);    
      workingMemory.insert(dep);
      workingMemory.insert(dep1);
      workingMemory.insert(emp);
      workingMemory.insert(emp1);
      workingMemory.fireAllRules();
   }
}


Step 4: Checking the Output

Civil::Shamik Mitra: IT
IT::Shamik Mitra: IT
Civil::Samir Mitra: Civil
IT::Samir Mitra: Civil


Please note that as we have inserted two Employees objects and two Department objects in working memory, so it creates four different combinations. In the next article, we will discuss  how to filter the resultset using Drools.

Drools

Published at DZone with permission of Shamik Mitra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Handle 100k Rows Decision Table in Drools (Part 3)
  • How To Handle 100k Rows Decision Table in Drools (Part 2)
  • Using Maven and Without Drools Eclipse Plugin

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!