Drools Decision Table CSV Example - Java @ Desk

Monday, March 19, 2018

Drools Decision Table CSV Example

Drools Decision Table CSV Example

Drools rules can be defined in various formats one of which is in Decision Tables. Decision Tables rules are configured in an excel sheet in .xls format.

Decision Table is primarily used when there are rules that need to be configured for similar properties. For example, there is a rule implementation on age property of a user. Below are the examples:
If age > 18 && age < 22 then Do X
If age > 22 && age < 28 then Do Y
If age > 28 && age < 32 then Do Z
If age > 32 && age < 36 then Do A
If age > 36 && age < 48 then Do B
If age > 48 && age < 56 then Do C

If such rules are implemented in DRL files, then there will be lot of redundant code. Instead the same are configured in a decision table.

There are different components of a Decision table as mentioned below. The structure is similar to DRL file itself
1) Ruleset - Its similar to package from DRL file.
2) Import - To import comma separated Java packages that are used in rules.
3) Sequential - If set to true, rules will be fired in sequence from top to bottom.
4) Variables - One of more Global variables.
5) Functions - Functions used if any in rules.
6) Queries - Queries used if any in rules.

Rules configuration consists of below :
1) Rule Name
2) Description
3) CONDITION
4) ACTION

In the below example, we will see the usage of decision tables to create rules.

Here is the snapshot of Decision Table that is used in the example.




User.java - Pojo used for rules
package com.pojo;

public class User {

 private String name;

 private String address;

 private Integer age;

 private boolean adult;

 private Integer rateOfInterest;
 
 private String bankAccounts;
 
 public String getBankAccounts() {
  return bankAccounts;
 }

 public void setBankAccounts(String bankAccounts) {
  this.bankAccounts = bankAccounts;
 }

 @Override
 public String toString() {
  return "Name - " + this.name + "\nAddress -" + this.address + "\nAge" + this.age + "\nAdult - " + this.adult
    + "\nRate Of Interest - " + this.rateOfInterest;
 }

 public Integer getRateOfInterest() {
  return rateOfInterest;
 }

 public void setRateOfInterest(Integer rateOfInterest) {
  this.rateOfInterest = rateOfInterest;
 }

 public Integer getAge() {
  return age;
 }

 public void setAge(Integer age) {
  this.age = age;
 }

 public String getName() {
  return name;
 }

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

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public boolean isAdult() {
  return adult;
 }

 public void setAdult(boolean adult) {
  this.adult = adult;
 }
}


Client File
package com.drools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.pojo.User;

public class DecisionTableClient {

 public static final void main(final String[] args) throws InterruptedException {
  KieSession kieSession = new DecisionTableClient().execute();

  User user = new User();
  user.setName("Andy");
  user.setAge(62);
  user.setAddress("Japan");
  user.setBankAccounts("Axis");
  kieSession.insert(user);
  kieSession.fireAllRules();
  System.out.println("\n");
  System.out.println(
    "User Object After Firing Rules - \n"
      + user.toString());
  kieSession.dispose();
 }

 public KieSession execute() {
  KieServices ks = KieServices.Factory.get();
  KieContainer kc = ks.getKieClasspathContainer();
  KieSession kieSession = kc.newKieSession("DecisionTableKIESession");
  return kieSession;
 }

}






No comments:

Post a Comment