Executing the Rules
Procedure
Creating the Web Module
We have already created a Web module named buyer_wm.
More information: Adding the Classes
Adding Dependencies to the Web Module
-
Choose .
-
In the dialog box that appears, choose Development Infrastructure . Choose OK .
-
In the Component Browser view, expand the Local Development node, MyComponents[demo.sap.com] node and choose the buyer_wm 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
Make sure that you are in the Java EE perspective.
-
Expand the web module: buyer_wm node and in the context menu of the Java Resources: source node, choose
-
In the wizard that appears, expand the Java node and choose 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
-
In the wizard that appears, choose Class . Choose Next .
-
In the screen that appears, enter EngineInvoker in the Name field. Choose Finish .
You should see the EngineInvoker.java window.
-
Delete all existing lines and copy the following lines into the window:
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 = getInitialContext(); RuleEngineLocalHome home = (RuleEngineLocalHome); context.lookup(RuleEngineLocalHome.JNDI_NAME); RuleEngineLocal local = home.create(); RuleEngine engine = (RuleEngine) local; } 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[]){ } }
Creating the BuyerDemo.jsp
-
In the Project Explorer view, expand the Web module: buyer_wm node, and in the context menu of the WebContent node, choose
-
In the dialog box that appears, expand the Web node and choose JSP . Choose Next
-
In the screen that appears, enter BuyerDemo in the File name field.
-
Choose Finish .
You should see the BuyerDemo.jsp window with the following lines:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <head> <body> </body> </html> -
Replace <title>Insert title here </title> with <title>Buyer Demo</title> .
-
Copy the following lines after <body>
<form name="ui" method="POST" action="invoker.jsp"> <h3>BRMS Invocation Client</h3> <% String buyersname = (String) request.getAttribute("BUYERS_NAME"); String buyersspendinghabit = (String) request.getAttribute("SPENDING_HABIT"); String buyerscredit = (String) request.getAttribute("BUYERS_CREDIT"); String setdiscount = (String) request.getAttribute("DISCOUNT_SET"); if(buyersname == null){ buyersname = ""; } if(buyersspendinghabit == null){ buyersspendinghabit = ""; } if(buyerscredit == null){ buyerscredit = ""; } if(setdiscount == null){ setdiscount = ""; } %> <table cellpadding="0" cellspacing="0" border="0"> <tr> <td>Name of the Buyer:</td> <td><input type="text" name="BUYERS_NAME" value="<%=buyersname %>"></td> </tr> <tr> <td>Spending Habit of the Buyer:</td> <td><input name="SPENDING_HABIT" type="text" value="<%=buyersspendinghabit %>"></td> </tr> <tr> <td>Buyer's Credit:</td> <td><input name="BUYERS_CREDIT" type="text" value="<%=buyerscredit %>"></td> </tr> <tr> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td><input name="INVOKE" type="submit" value="Submit"/></td> </tr> </table> <br/> <strong>Discount Set:</strong><br/> <textarea name="DISCOUNT_SET" style="width: 503px; height: 50px;"><%=setdiscount %></textarea> </form> -
Save the changes.
Creating the index.jsp
-
Expand the Web module: buyer_wm node and in the context menu of the Web Content node, choose
-
In the dialog box that appears, expand the Web node and choose JSP . Choose Next
-
In the screen that appears, enter index in the File name field. Choose Next .
-
Choose Finish .
You should see the index.jsp window with the following lines:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert the title here</title> <head> <body> </body> </html> -
Replace <title>Insert title here </title> with <title>Buyer Demo</title> .
-
Copy the following line after <body> :
<jsp:forward page="BuyerDemo.jsp"/> -
Save the changes.
Creating the invoker.jsp
-
Expand the web module: buyer_wm node and in the context menu of the Web Content node, choose
-
In the dialog box that appears, expand the Web node, and choose JSP . Choose Next
-
In the screen that appears, enter invoker in the File name field. Choose Next .
-
Choose Finish .
You should see the invoker.jsp window with the following lines:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert the title here</title> <head> <body> </body> </html> -
Replace <title>Insert title here </title> with <title>Buyer Demo</title> .
-
Copy the following lines before <html>
<%@page import="com.sap.helper.*"%> <%@page import="com.sap.brms.qrules.engine.*" %> <%@page import="java.util.*"%> <%@page import="com.sap.buyer.*"%> -
Copy the following line after <body> :
<% Buyer buyerObj = new Buyer(); String buyersname = (String) request.getParameter("BUYERS_NAME"); String buyersspendinghabit = (String) request .getParameter("SPENDING_HABIT"); String buyerscredit = (String) request .getParameter("BUYERS_CREDIT"); String setdiscount = (String) request.getParameter("DISCOUNT_SET"); buyerObj.setBuyerscredit(buyerscredit); buyerObj.setBuyersname(buyersname); buyerObj.setBuyersspendinghabit(buyersspendinghabit); // set project name and ruleset name. String projectName = "demo.sap.com~buyerrules"; String rulesetName = "discountrules"; //invoking the rule engine List inputList = new ArrayList(); inputList.add(buyerObj); Object obj = null; if (inputList.size() != 0) { List output = EngineInvoker.invokeRuleset(projectName,rulesetName, inputList); if (output.size() == 0) { obj = "no output returned. input =" + inputList.size(); } else { obj = output.get(0); } } else { obj = "input has not been set"; } //Object obj = output.get(0); //obj = "Test"; if (obj instanceof String) { request.setAttribute("DISCOUNT_SET", obj); } else if (obj instanceof Buyer) { Buyer buyer = (Buyer) obj; request.setAttribute("BUYERS_NAME", buyer.getBuyersname()); request.setAttribute("DISCOUNT_SET", "" + buyer.getSetdiscount()); request.setAttribute("SPENDING_HABIT", buyer .getBuyersspendinghabit()); request.setAttribute("BUYERS_CREDIT", buyer.getBuyerscredit()); } %> <jsp:forward page="BuyerDemo.jsp"/> -
Save the changes.
Creating the Enterprise Application
-
In the SAP NetWeaver Developer Studio, choose .
-
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 buyer_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 .
In the Project Explorer view, you will see the buyer_ear node.
Adding Dependencies to the Enterprise Application
Make sure that you are in the Development Infrastructure perspective.
-
In the Component Browser view, expand the Local Development node, MyComponents[demo.sap.com] node and choose the buyer_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
Make sure that you are in the Java EE perspective.
-
In the Project Explorer view, in the context menu of the enterprise application: buyer_ear node, choose JavaEE Tools.
-
In the menu that appears, choose Generated Deployment Descriptor Stub .
You should see the application.xml window with the following lines:
<?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~buyer_ear~demo.sap.com</display-name> <module> <web> <web-uri>demo.sap.com~buyer_wm.war</web-uri> <context-root>LocalDevelopment~LocalDevelopment~buyer_wm~demo.sap.com</context-root> </web> </module> </application>Replace <context-root>LocalDevelopment~LocalDevelopment~buyer_wm~demo.sap.com</context-root> with <context-root>BuyerDemo</context-root> .
Building and Deploying
Make sure you are in the Development Infrastructure perspective.
-
In the Component Browser view, expand the Local Development , MyComponents[demo.sap.com] nodes and in the context menu of the buyer_wm and buyer_ear nodes, choose .
-
In the dialog box that appears, choose OK .
-
In the context menu of the buyer_ear node, choose .
-
In the dialog box that appears, choose OK .
-
Open the Infrastructure Console , to check if the build and deploy actions have happened successfully.
Running the Web Module
-
Open the browser and enter the Application Server Address followed by the port number and the application name: BuyerDemo .
-
Enter relevant data in all available fields and choose Submit .
The rules gets executed and you should see the discount set based on the data you entered.
Here is the snapshot of the web module:

