ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Building Enterprise Services with Drools Rule Engine
Pages: 1, 2, 3, 4, 5

The test case instantiates the business objects Borrower, Property, and LoanApplication.



public class LoanApplication {

    private double loanAmount;
    private int creditScore;
    private double loanToValueRatio;
    private Flag affordabilityFlag=Flag.AFFORDABLE;
    private int feedbackMsgSize;

    private Borrower borrower;
    private Property prop;

    private Flag status=Flag.PASSED;

    private ArrayList<String> feedbackMessages =
        new ArrayList<String>();

  .../...  // getters and setters
}

public class Borrower {
    int age;
    double setAffordableLoanAmount;
    double grossIncome;
    int creditScore;
    .../...
}

public class Property {
    private Flag type;
    private Flag purpose;
    private String zipCode;
    private int yearBuilt;
    private double value;

    .../...
}

The following enumeration class contains all the flags used in this article:

public enum Flag {
    AFFORDABLE, NOT_AFFORDABLE, PASSED, FAILED, INVESTMENT, OWNER_OCCUPIED;
}

As I mentioned before, the support services CreditScoringService, AffordabilityModelService, and PropertyValuationService populate parts of the application, but for this article, they are dummied out and return hardcoded values.

The other class that is being used by our JUnit test is the UnderwritingService, which contains the call to the Drools rule engine APIs.

public class UnderwritingService {
    public void evaluate(LoanApplication la)
        throws Exception{

        doRun("/Underwriting.drl", la);
        doRun("/Decision.drl", la);

    }

    private void doRun(String ruleFileName, LoanApplication la)
        throws Exception {

        RuleBase ruleBase = readRule(ruleFileName);
        WorkingMemory wm = ruleBase.newWorkingMemory();
        wm.assertObject(la);
        wm.assertObject(la.getBorrower());
        wm.assertObject(la.getProperty());
        wm.fireAllRules();

    }

    private RuleBase readRule(String ruleFileName) 
        throws Exception {

        Reader source = new InputStreamReader(
                UnderwritingService.class.
                    getResourceAsStream(ruleFileName) );

        PackageBuilder builder = new PackageBuilder();
        builder.addPackageFromDrl( source );
        Package pkg = builder.getPackage();

        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage( pkg );
        return ruleBase;
    }

}

Pages: 1, 2, 3, 4, 5

Next Pagearrow