In this article, you will design a business rules framework using Java and XML. You will learn what constitutes a good business rules framework design and go over an example that demonstrates how to use the framework.
Characteristics of a Business Rules Framework
The dictionary states that a business rule is "a set of rules for entering data in a database that are specific to an enterprise's methods of conducting its operations." This definition is very exact. But in the real world, everybody has their own definition of a business rule. I guarantee that every software architect adheres to his/her own version of the definition. So, what then is a business rule? Some say that a condition that requires modification of a database is a business rule. Others say that a business rule can be any parameterized condition. Therefore, you need to make sure that your business rules framework is designed to be flexible enough to support any type of a business rule definition. For the framework to be adaptable, it needs to encompass these three main characteristics:
- It must adhere to object-oriented design.
- It must be adaptable enough to implement any type of a business rule—be it a simple one line if statement or a very complex enterprise process.
- It must be centralized. You want to make sure that your rules are defined in the same place and are invoked in the same manner.
Design and Implementation of the Business Rules Framework
Your Business Rules Framework will utilize an XML-based properties file that will define business rule groups. Each business rule group defines a list of business rules. Each business rule contains a class name that applies the rule and a list of parameters (see Figure 1).
Look at the above example.The business rule group, CompanyHireDecider, includes two business rule definitions, IQClassification and HRQualifier. The IQClassification rule determines an applicant's classification based on his/her IQ score. There is a total of four classifications. The IQClassification handler takes an IQ score as input and returns a classification type that correlates to the IQ score range. The HRQualifier rule determines whether the applicant is qualified to work for the company based on his/her classification type that is derived from the previous rule.
To manage your business rules properties file, you are going to define a Business Rules Manager (BusinessRulesManagerImpl.java) class. The class reads your properties file and creates a collection of business rule groups that usr the rule group name as a key. Then, the class instantiates all business rule handlers and passes a list of parameters to each handler. All handlers implement the same interface (IbusinessRule.java) that includes two methods: the initialization method and the apply rule method.
The instantiation of handlers is done by using the java.lang.newInstance() method. After the handler is instantiated, it is cast into the common interface (IbusinessRule.java) and the init method is then called. The init method takes the inner Element of the RULE XML tag (refer to Figure 1). Each handler is responsible for storing its parameters in the class scope variables. Figure 2 depicts a class diagram of the framework.
After initialization is complete, you can store the instance of the Business Rules Manager in the application context. The rules are invoked using business rules group names that should be defined as constants to eliminate hard coding. You can invoke the business rule group by calling the applyBusinessRules () method. This method is defined in the Business Rules Manager interface (IBusinessRulesManager.java) and is implemented in the Business Rules Manager implementation class (BusinessRulesManagerImpl.java).
The code snippet shows the initialization code:
//create a new instance of the Business Rules Manager IBusinessRulesManager businessRulesManager = new BusinessRulesManagerImpl(); //put the properties file into the System properties System.setProperty(BusinessConstants.CONFIG_FILE_NAME,args); //initialize the Business Rules Manager usinessRulesManager.init();
Now that the Business Rules Framework is initialized, you can apply business rules from anywhere in the application. The applyBusinessRules() method accepts two parameters: the business rule group name and an IO array of objects. See Figure 3 for the class diagram.
Now, about the IO array in more detail. The array contains a list of objects that the rule handler requires to apply the rule. In this example, the IQClassification handler requires an instance of the HRApplicant class that contains the IQ score and a placeholder for the classification. The HRApplicant instance is then passed to the HRQualifier handler that uses the value in the classification field to produce a HRVerdict object that is returned to the caller via the IO array. Here is a code snippet that shows how the CompanyHireDecider is invoked:
//create applicant HRApplicant applicant1 = new HRApplicant("Alan", "Scudi",40); //create input array Object data  = new Object ; //assign applicant to the first location of the array data = applicant1; //apply business rule group "CompanyHireDecider" and pass input array businessRulesManager.applyBusinessRules("CompanyHireDecider",data); //get a reference to a verdict object HRVerdict hrVerdict = (HRVerdict) data; //get verdictexplanation rVerdict.getVerbalExplanation());
As you have seen, your implementation of the Business Rules Framework adheres to all of the characteristics listed at the beginning of the article. Review them once again:
- The Framework must adhere to Object Oriented design.
The Business Rules Framework is designed using The Liskov Substitution Principle; it states that derived classes must be usable through the base class interface without the need for the user to know the difference. In your example, the IbusinessRule.java interface is implemented by IQClassification.java and HRQualifier.java, which allow the caller (in your case, BusinessRulesManagerImpl.java) to invoke the classes. The framework is also highly abstract: The caller of the API needs to specify the name of the business rule group and pass the references to the input objects. Nothing else is required to use the framework.
- The Framework must be adaptable enough to implement any type of a business rule, be it a simple one line if statement or a very complex enterprise process.
The XML-based Business Rules property file gives the framework the flexibility that it needs to implement any type of a business rule. You can specify a list of parameters or an SQL query.
- The Framework must be centralized. You want to make sure that your rules are defined in the same place and are invoked in the same manner.
Again, the XML-based Business Rules property file ensures that all the rules and their parameters are centralized in one location. Furthermore, the method of evoking the rules is uniform: The API needs a rule name and a list of references to input objects in all cases.
Download the Source Code
Sample Property File: BusinessRules.xml
Source Code: BRF.zip
About the Author
Aleksey Shevchenko has been working with object-oriented languages for over seven years. He has been implementing Enterprise IT solutions for Wall Street and the manufacturing and publishing industries.