Step-by-step guide to use BRM (Business Rules Management) using Java Classes

...Previous

Creating the Rules  

In the Project Explorer view, expand the travelrules node, the Rules Modeling node and in the context menu of the travel_rules node, choose New Rule.  

In the dialog box that appears, enter M1 in the field and choose OK.

You should see the M1 node in the Outline view.

Repeat the steps to create more rules, depending upon the requirement (in our case for M2 and M3).

The rules will be displayed, as shown below:  

Now we have to edit all the rules, as per the given requirements.

In our case the rule for M1 is like the following.

 

Just we need to repeat the above step in order to edit the remaining rules.  

After editing all the rules we need to execute the Rules.

Executing the Rules

We have already created a Web Module named travelrules_web.

Adding Dependencies to the Web Module  

Ø       Open Development Infrastructure.

Ø       In the Component Browser view, expand the Local Development node, MyComponents [demo.sap.com] node and choose the travelrules_web node.

Ø       In the Component Properties view, choose the Dependencies tab.

Ø       Choose the Add button and in the wizard that appears, expand the BRMS-FACADE [sap.com] node and select the tc/brms/facade checkbox. Choose Next.

Ø       In the screen that appears, select the Design Time, Deploy Time, Run Time checkboxes. Choose Finish.

Creating EngineInvoker.java

Open Java EE perspective.  

Expand the web module: travelrules_web node and in the context menu of the Java Resources: source node, choose New à Package.  

Choose Next.

In the screen that appears, enter com.sap.helper in the Name field.

Choose Finish.

In the context menu of com.sap.helper, choose New > Class

Choose Next.

In the screen that appears, enter EngineInvoker in the Name field. Choose Finish.

The code for EngineInvoker is as follows:

package com.sap.helper;
	import java.io.PrintWriter;
	import java.io.StringWriter;
	import java.util.ArrayList;
	import java.util.List;
	import javax.naming.InitialContext;
	import javax.rmi.PortableRemoteObject;
	import com.sap.brms.qrules.ejb.RuleEngineHome;
	import com.sap.brms.qrules.engine.RuleEngine;
	import com.sap.brms.qrules.engine.RulesetContext;
	public class EngineInvoker
	{
	private static String jndiName = "com.sap.brms.RuleEngine";
	private static String payloadSeparator;
	private static String ret_payloadSeparator;
	private static final String PROPS_FILE = "engine.properties";
	public EngineInvoker()
	{
	}
	public static RuleEngine getRuleEngine()
	throws Exception
	{
	InitialContext context = new InitialContext();
	Object obj = context.lookup(jndiName);
	RuleEngineHome home = (RuleEngineHome)PortableRemoteObject.narrow(obj, RuleEngineHome.class);
	return (RuleEngine)home.create();
	}
	public static List invokeRuleset(String projectName, String rsName, List input)
	{
	List output = new ArrayList();
	RuleEngine ruleEngine;
	if(projectName == null || rsName == null || input == null)
	{
	output
	.add("Project Name or Ruleset Name or Payload should not be NULL");
	}
	try
	{
	ruleEngine = getRuleEngine();
	output = ruleEngine.invokeRuleset(projectName, rsName, input);
	}
	catch(Exception e)
	{
	output.add(e.getMessage());
	}
return output;
	}
	public static void main(String args[]){
	}
	}

After this we have to create “index.jsp”, travelrules.jsp and invoker.jsp under the we module travelrules_web.

Creating the Enterprise Application  

Ø       In the SAP NetWeaver Developer Studio, choose File > New > Project.

Ø       In the wizard that appears, expand the Development Infrastructure node and choose Development Component. Choose Next.

Ø       In the screen that appears, expand the J2EE node and choose Enterprise Application. Choose Next.

Ø       In the screen that appears, choose the software component where you want to create the DCs. For example expand the 'Local Development' node and choose MyComponents [demo.sap.com]. Choose Next.

Ø       In the screen that appears, enter travelrules_ear in the Name field. Choose Next.

Ø       Choose Next.  

Ø       In the screen that appears, select the LocalDevelopment~LocalDevelopment~buyer_wm~demo.sap.com checkbox. Choose Finish.

Adding Dependencies to the Enterprise Application

Ø       Open Development Infrastructure perspective.

Ø       In the Component Browser view, expand the Local Development node, MyComponents[demo.sap.com] node and choose the travelrules_ear node.

Ø       In the Component Properties view, choose the Dependencies tab.

Ø       Choose the Add button and in the wizard that appears, expand the BRMS-FACADE[sap.com] node and select the tc/brms/facade checkbox. Choose Next.

Ø       In the screen that appears, select the Design Time, Deploy Time, Run Time checkboxes. Choose Finish.

Creating the application.xml

Open Java EE perspective.  

Expand the enterprise application: travelrules_ear node and in the context menu of the Deployment Descriptor: LocalDevelopment~LocalDevelopment~ex1_ear~demo.sap.com node, choose Create application.xml.  

The default code for the application.xml is  

<?xml version="1.0" encoding="ASCII"?>
<application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd"
	version="5">
	<display-name>
		LocalDevelopment~LocalDevelopment~travelrules_ear~demo.sap.com
	</display-name>
	<module>
		<web>
			<web-uri>demo.sap.com~ travelrules _web.war</web-uri>
           <context-root> LocalDevelopment~LocalDevelopment~ travelrules _wm~demo.sap.com
</context-root>
		</web>
	</module>
</application>

In the above file replace the value of <context-root> tag with a meaningful name, which is relevant to our project.  

Like in the above case I have replaced the content with “TravelRules”, this name is used to invoke the application using the Browser.

So the modified <context-root> will be as follows:  

<context-root>TravelRules</context-root>

 

Building and Deploying  

Ø       Open Development Infrastructure perspective or Java EE perspective.

Ø       In the Component Browser view, expand the Local Development, MyComponents[demo.sap.com] nodes and in the context menu of the travelrules_web and travelrules_ear nodes, choose Build

Ø       In the dialog box that appears, choose OK.

Ø       In the context menu of the travelrules_ear node, choose Deploy.

Ø       In the dialog box that appears, choose OK.

Ø       Open the Infrastructure Console, to check if the build and deploy actions have happened successfully.  

After successful deployment, we can Run our application using the web browser.

Open the browser and enter the Application Server Address followed by the port number and the application name: TravelRules.  

Ex: http://yh1334:50000/TravelRules     

Note: however we can use decision tables instead of individual rules. The overall process is same as above except, while defining the rules, in the context menu of the Ruleset we have to go for new Decision Table instead of New Rule every time.

 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image