Developing Web Applications
with OpenRules and Tomcat

Part 3 – Moving Business Logic to OpenRules

This is the third part of a step-by-step account of how to develop web applications using Tomcat and OpenRules. The first part described how to setup a Tomcat-based development environment and create a trivial web application. The second part added a JSP-based GUI and Java classes to implement a HelloCustomer application that greets a customer based on his/her personal data and time of the day. Now we want to move logic of greeting generation from Java to Business Rules using OpenRules Excel tables.
 

Step 9 – Creating a new project with OpenRules jars

Let's copy previously created project 'hello.tomcat' to a new project 'hello.openrules.tomcat'. Before making any serious changes, we have to replace the word  'hello.tomcat' to 'hello.openrules.tomcat' in the files build.properties and run.html. Rebuild the project and make sure that it still works.

Now we will add OpenRules jars and build files to our project. Create a subdirectory 'lib' inside 'war/WEB-INF' and copy to this subdirectory the the following files from 'openrules.config' that is a part of OpenRules 3.0.* standard installation:

Step 10 – Adding Business Rules

In the Part 2, we implemented business logic inside a class JavaEngine. It included a method 'run' with parameters of types Customer and EngineResponse and two other methods to define Greeting and Salutation. Now we will externalize this logic from Java code and will present in tables of an Excel file "HelloCustomer.xls". Create a subdirectory "rules" inside "hello.openrules.tomcat/war".  Open MS Excel and create a new file HelloCustomer.xls inside hello.openrules.tomcat/war/rules.  Create inside this file the following tables:

To make sure that our rules inside this Excel file "understand" Java classes Customer and EngineResponse from the package "hello", we should add the following Environment table:

Now we can delete JavaEngine from the package src/hello and replace it with the standard OpenRuleEngine inside GreetingEngine implementation:

 

hello.tomcat/src/hello/GreetingEngine.java
package hello;

import com.openrules.ruleengine.OpenRulesEngine;

public class GreetingEngine implements IGreetingEngine {

	private OpenRulesEngine engine;

	public GreetingEngine() {
		String fileName = "file:../webapps/hello.openrules.tomcat/rules/HelloCustomer.xls";
		engine = new OpenRulesEngine(fileName);
	}

	public String generateGreeting(Customer customer) {
		EngineResponse response = new EngineResponse();
		String methodName = "helloCustomer";
		Object[] objects = new Object[] { customer, response }; 
		engine.run(methodName,objects);
		
		String result = response.get("greeting") + ", " +
				response.get("salutation") + " " +
				customer.getName() + "!";
		System.out.println("Generated Greeting: " + result);
		return result;
	}
	public static void main(String[] args) {
		GreetingEngine engine = new GreetingEngine();

		Customer customer = new Customer();
		customer.setName("Robinson");
		customer.setGender("Female");
		customer.setMaritalStatus("Married");

		engine.generateGreeting(customer);
	}
}

Please note that there are no changes in other files (like index.jsp) because we continue to use the same interface with GreetingEngine.

Step 11 – Configuring OpenRules

Staring with OpenRules-3 no special configuration is required.
 

Step 12 – Deploy and Execute

Now we are ready to build, deploy and test our hello.openrules.tomcat application. Run Ant target 'deploywar' with a Tomcat running. After running a browser with to http://localhost:8080/hello.openrules.tomcat/index.jsp (or from file run.html) we should see the same web client:

Summary

  1. We externalized business logic form a Java class JavaEngine to Excel-based business rules.

  2. We reconfigured our web application allowing it to work with OpenRules.

Back     Part 4 – Moving Presentation Logic to OpenRules


Copyright © 2005-2006, OpenRules, Inc.