Skip to content
🚀 Play in Aletyx Sandbox to start building your Business Processes and Decisions today! ×

Creating the Data Model

In this section, we'll walk through the process of creating the Java classes that define the data model for our loan application decision service. These classes, known as POJOs (Plain Old Java Objects), will represent the entities involved in the loan application process.

Step 1: Create the Package Directory

First, let's create the directory structure for our Java classes. Open a terminal in your IDE (e.g., IntelliJ IDEA, Eclipse, or VSCode) and navigate to the src/main/java directory of your project.

Run the following command to create the package directory:

mkdir -p src/main/java/org/drools/sample/loan
  1. Open a terminal in VSCode and create a directory for Java classes to go. For the Java components, we're going to put them in src/main/java and more specifically since we're going to write a quick rule about if a person is an adult or not, we will put the packages at org.drools.sample.adult, the command to do this in one line is mkdir src/main/java/org/drools/sample/loan to create the required path. After the path is created, you will see it reflected in the VSCode workspace's file explorer.

Step 2: Create the Applicant Class

  1. Now, let's create the Applicant class, which represents the person applying for the loan. Right-click on the loan package in your IDE and create a new Java class named Applicant.java.

  2. Open the Applicant.java file and add the following code:

Applicant.java
package org.drools.sample.loan;

public class Applicant {
    private String name; // (1)
    private int age; // (2)

    public Applicant() { // (3)
    }

    public Applicant(String name, int age) { // (4)
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
  1. Name field is used for identification in reports and documentation
  2. Age is a critical field used in multiple rules for determining eligibility
  3. Default constructor required for JSON serialization/deserialization
  4. Constructor used when programmatically creating applicants, typically in test cases

The Applicant class has two properties: name (String) and age (int). It includes a default constructor, a parameterized constructor, and getter and setter methods for each property.

Step 3: Create the LoanApplication Class

  1. Next, let's create the LoanApplication class, which represents the loan application submitted by an applicant. Right-click on the loan package and create a new Java class named LoanApplication.java.
  2. Open the LoanApplication.java file and add the following code:

    LoanApplication.java
    package org.drools.sample.loan;
    
    public class LoanApplication {
        private String id; // (1)
        private Applicant applicant; // (2)
        private int amount; // (3)
        private int deposit; // (4)
        private boolean approved = false; // (5)
    
        public LoanApplication() { // (6)
        }
    
        public LoanApplication(String id, Applicant applicant, int amount, int deposit) {
            this.id = id;
            this.applicant = applicant;
            this.amount = amount;
            this.deposit = deposit;
        }
    
        public String getId() {
            return id;
        }
    
        public void setId(String id) {
            this.id = id;
        }
    
        public Applicant getApplicant() {
            return applicant;
        }
    
        public void setApplicant(Applicant applicant) {
            this.applicant = applicant;
        }
    
        public boolean isApproved() {
            return approved;
        }
    
        public void setApproved(boolean approved) {
            this.approved = approved;
        }
    
        public int getAmount() {
            return amount;
        }
    
        public void setAmount(int amount) {
            this.amount = amount;
        }
    
        public int getDeposit() {
            return deposit;
        }
    
        public void setDeposit(int deposit) {
            this.deposit = deposit;
        }
    }
    
    1. Unique identifier following ABC##### pattern
    2. Reference to the applicant object containing personal details
    3. Requested loan amount in base currency units
    4. Initial deposit amount used for risk assessment
    5. Approval status, initialized as false and modified by rules
    6. Default constructor needed for REST endpoint deserialization

The LoanApplication class has five properties:

  • id (String): Unique identifier for the loan application.
  • applicant (Applicant): The applicant associated with the loan application.
  • amount (int): The requested loan amount.
  • deposit (int): The deposit amount provided by the applicant.
  • approved (boolean): Indicates whether the loan application is approved or not.

The class includes a default constructor, a parameterized constructor, and getter and setter methods for each property.

Step 4: Create the LoanUnit Class

  1. Finally, let's create the LoanUnit class, which serves as the RuleUnit for the loan application decision service. The RuleUnit encapsulates the data and rules related to loan processing.
  2. Right-click on the loan package and create a new Java class named LoanUnit.java.
  3. Open the LoanUnit.java file and add the following code:
LoanUnit.java
package org.drools.sample.loan;

import org.drools.ruleunits.api.DataSource;
import org.drools.ruleunits.api.DataStore;
import org.drools.ruleunits.api.RuleUnitData;

public class LoanUnit implements RuleUnitData { // (1)
    private int maxAmount; // (2)
    private DataStore<LoanApplication> loanApplications; // (3)
    private DataStore<AllAmounts> allAmounts; // (4)

    public LoanUnit() { // (5)
        this(DataSource.createStore(), DataSource.createStore());
    }

    public LoanUnit(DataStore<LoanApplication> loanApplications, DataStore<AllAmounts> allAmounts) {
        this.loanApplications = loanApplications;
        this.allAmounts = allAmounts;
    }

    public DataStore<LoanApplication> getLoanApplications() {
        return loanApplications;
    }

    public void setLoanApplications(DataStore<LoanApplication> loanApplications) {
        this.loanApplications = loanApplications;
    }

    public DataStore<AllAmounts> getAllAmounts() {
        return allAmounts;
    }

    public void setAllAmounts(DataStore<AllAmounts> allAmounts) {
        this.allAmounts = allAmounts;
    }

    public int getMaxAmount() {
        return maxAmount;
    }

    public void setMaxAmount(int maxAmount) {
        this.maxAmount = maxAmount;
    }
}
  1. RuleUnitData interface enables REST endpoint generation and provides execution context
  2. Global threshold used across rules for maximum loan amount
  3. Primary DataStore containing all loan applications being processed
  4. Secondary DataStore for tracking aggregate amounts and exposure
  5. Default constructor creates empty DataStores for fresh rule sessions

The LoanUnit class implements the RuleUnitData interface, which marks it as a Kogito RuleUnit. It has three properties:

  • loanApplications (DataStore): Stores individual loan applications.
  • allAmounts (DataStore): Stores aggregate loan amount information.
  • maxAmount (int): Represents the maximum loan amount allowed.

The class includes two constructors:

  • Default constructor: Creates empty DataStores for loanApplications and allAmounts.
  • Parameterized constructor: Allows injection of pre-populated DataStores.

The LoanUnit class uses Kogito's DataStore for fact management. Having separate DataStores for individual loan applications and aggregate amounts provides a separation of concerns and enables flexibility in rule matching and complex scenarios.

Conclusions

In this section, we created the Java classes that define the data model for our loan application decision service. We created the Applicant, LoanApplication, and LoanUnit classes.

The Applicant class represents the person applying for the loan, while the LoanApplication class represents the loan application itself. The LoanUnit class serves as the RuleUnit and encapsulates the data and rules related to loan processing.

By using separate DataStores for individual loan applications and aggregate amounts, we achieve a separation of concerns and enable flexibility in rule matching and handling complex scenarios.

With the data model in place, we can now proceed to define the business rules and decision logic using DRL (Drools Rule Language) in the next section.