Thursday, March 24, 2016

Employee salary calculation using Drool

below example demonstrate employee salary calculation based on employee type
before start this example you need to have basic understanding about Drools and this example shows  how to calculate salary based on different business logic if you have any issues regarding this example you can contact me.

following component are used to calculate salary

  • basic Salary
  • vehicle Rental Allowance
  • traveling Allowance
  • tax Percentage
  • salary Amount
for my example there are three employee category based on those salary should be calculated

  •  SKU_H("daily", "Daily Workers")
  •  SKU_B("monthly", "Monthly paid workers")
  •  BHL("contract", "Contract worker") 
refer my package structure as follows






and .drl files






my example i have integrate Drools with Spring  please refer drools-context.xml


need to import this xml though applicationContext.xml file as follows
 


refer all .drl file below

basic_salary.drl

 vehicleRental.drl


tax.drl

 salary_calculation.drl


please find maven dependence details below



EmployeeType.java
package com.goodhope.fas.drools.enums;

/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
public enum EmployeeType {
    SKU_H("daily", "Daily Workers"),
    SKU_B("monthly", "Monthly paid workers"),
    BHL("contract", "Contract worker");


    private final String type;
    private final String description;

    private EmployeeType(String type, String description) {
        this.type = type;
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }
}


Employee.java

package com.goodhope.fas.drools.model;


import com.goodhope.fas.drools.enums.EmployeeType;

/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
public class Employee {
    private final EmployeeType employeeType;
    private String employeeName;
    private String employeeTypeDescription;
    private double baseSalary;
    private double vehicleRental;
    private double travellingAllowance;
    private double taxPercentage;
    private double salaryAmount;

    public Employee(EmployeeType employeeType) {
        this.employeeType = employeeType;
        employeeTypeDescription = employeeType.getDescription();
    }

    public EmployeeType getEmployeeType() {
        return employeeType;
    }

    public String getEmployeeName() {
        return employeeName;
    }

    public void setEmployeeName(String employeeName) {
        this.employeeName = employeeName;
    }

    public String getEmployeeTypeDescription() {
        return employeeTypeDescription;
    }

    public void setEmployeeTypeDescription(String employeeTypeDescription) {
        this.employeeTypeDescription = employeeTypeDescription;
    }

    public double getBaseSalary() {
        return baseSalary;
    }

    public void setBaseSalary(double baseSalary) {
        this.baseSalary = baseSalary;
    }

    public double getVehicleRental() {
        return vehicleRental;
    }

    public void setVehicleRental(double vehicleRental) {
        this.vehicleRental = vehicleRental;
    }

    public double getTravellingAllowance() {
        return travellingAllowance;
    }

    public void setTravellingAllowance(double travellingAllowance) {
        this.travellingAllowance = travellingAllowance;
    }

    public double getTaxPercentage() {
        return taxPercentage;
    }

    public void setTaxPercentage(double taxPercentage) {
        this.taxPercentage = taxPercentage;
    }

    public double getSalaryAmount() {
        return salaryAmount;
    }

    public void setSalaryAmount(double salaryAmount) {
        this.salaryAmount = salaryAmount;
    }
}

ProcessingFactory.java

package com.goodhope.fas.drools.factory;
/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
public interface ProcessingFactory {
    T createProcessingObject(V inputObject);
}


ProcessingFactoryImpl.java

package com.goodhope.fas.drools.factory.drools;

import com.goodhope.fas.drools.factory.ProcessingFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;


/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
@Component("ProcessingFactoryImpl")
public class ProcessingFactoryImpl implements ProcessingFactory {

    @Autowired
    private ApplicationContext applicationContext;

    @Override
    public StatefulKnowledgeSession createProcessingObject(String beanId) {
        return (StatefulKnowledgeSession)applicationContext.getBean(beanId);
    }
}



SalaryService.java

package com.goodhope.fas.drools.service;


/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
public interface SalaryService {
    void calculateSalary(T input);
}
 


SalaryServiceImpl.java


package com.goodhope.fas.drools.service.drools;

import com.goodhope.fas.drools.factory.ProcessingFactory;
import com.goodhope.fas.drools.model.Employee;
import com.goodhope.fas.drools.service.SalaryService;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.runtime.StatelessKnowledgeSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;


import java.util.List;



/**
 * Created with IntelliJ IDEA.
 * User: Gayan Dissanayake
 * Date: 02.11.15
 */
@Component("SalaryServiceImpl")
public class SalaryServiceImpl implements SalaryService> {

    private static final Logger LOGGER = LoggerFactory.getLogger(SalaryServiceImpl.class);

    private static final String SALARY_K_SESSION="salaryKSession";

    @Autowired
    @Qualifier("ProcessingFactoryImpl")
    ProcessingFactory processingFactory;

    @Override
    public void calculateSalary(List input) {
        LOGGER.debug("Running employee Salary logic");
        Logger logger = LoggerFactory.getLogger(SalaryServiceImpl.class);
        StatefulKnowledgeSession statefulKnowledgeSession = processingFactory.createProcessingObject(SALARY_K_SESSION);
        statefulKnowledgeSession.setGlobal("logger", logger);
        LOGGER.debug("Running rules for employees...");
        if(input!=null && !input.isEmpty()){
            for(Employee employee :input){
                statefulKnowledgeSession.insert(employee);
            }
        }
        statefulKnowledgeSession.fireAllRules();
        LOGGER.debug("...finished running employees.");
    }



}















calling class


setter injection by spring


    @Autowired
    @Qualifier("SalaryServiceImpl")
    SalaryService processingService;

 salary calculation method

public void calculateSalary() throws SalaryCalculationException {
        List list=new ArrayList<>();
        for(int i=0;i<100 br="" i="">            Employee emp1 = new Employee(EmployeeType.SKU_H);
            emp1.setEmployeeName("Emp 1 Name "+i);
            Employee emp2 = new Employee(EmployeeType.SKU_B);
            emp2.setEmployeeName("Emp 2 Name "+i);
            Employee emp3 = new Employee(EmployeeType.BHL);
            emp3.setEmployeeName("Emp 3 Name "+i);
            list.add(emp1);
            list.add(emp2);
            list.add(emp3);
            System.out.println("Adding "+i);
            processingService.calculateSalary(list);
        }
        for(Employee employee:list){
            System.out.println(employee.getEmployeeName()+"-"+employee.getSalaryAmount());
        }
    }