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

  1. Choose Start of the navigation pathWindow Next navigation step Open Perspective Next navigation step OtherEnd of the navigation path.

  2. In the dialog box that appears, choose Development Infrastructure . Choose OK .

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

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

  5. 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 .

  6. 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.

  1. Expand the web module: buyer_wm node and in the context menu of the Java Resources: source node, choose Start of the navigation pathNew Next navigation step OtherEnd of the navigation path

  2. In the wizard that appears, expand the Java node and choose Package . Choose Next .

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

  4. Choose Finish .

  5. In the context menu of com.sap.helper , choose Start of the navigation pathNew Next navigation step OtherEnd of the navigation path

  6. In the wizard that appears, choose Class . Choose Next .

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

    You should see the EngineInvoker.java window.

  8. 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

  1. In the Project Explorer view, expand the Web module: buyer_wm node, and in the context menu of the WebContent node, choose Start of the navigation pathNew Next navigation step OtherEnd of the navigation path

  2. In the dialog box that appears, expand the Web node and choose JSP . Choose Next

  3. In the screen that appears, enter BuyerDemo in the File name field.

  4. 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>
                      
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. 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>
                      
  7. Save the changes.

Creating the index.jsp

  1. Expand the Web module: buyer_wm node and in the context menu of the Web Content node, choose Start of the navigation pathNew Next navigation step OtherEnd of the navigation path

  2. In the dialog box that appears, expand the Web node and choose JSP . Choose Next

  3. In the screen that appears, enter index in the File name field. Choose Next .

  4. 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>
                      
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. Copy the following line after <body> :

    <jsp:forward page="BuyerDemo.jsp"/>
                      
  7. Save the changes.

Creating the invoker.jsp

  1. Expand the web module: buyer_wm node and in the context menu of the Web Content node, choose Start of the navigation pathNew Next navigation step OtherEnd of the navigation path

  2. In the dialog box that appears, expand the Web node, and choose JSP . Choose Next

  3. In the screen that appears, enter invoker in the File name field. Choose Next .

  4. 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>
                      
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. 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.*"%>
                      
  7. 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"/>
    
                      
  8. Save the changes.

Creating the Enterprise Application

  1. In the SAP NetWeaver Developer Studio, choose Start of the navigation pathFile Next navigation step New Next navigation step ProjectEnd of the navigation path.

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

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

  4. 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 .

  5. In the screen that appears, enter buyer_ear in the Name field. Choose Next .

  6. Choose Next .

  7. 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.

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

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

  3. 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 .

  4. 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.

  1. In the Project Explorer view, in the context menu of the enterprise application: buyer_ear node, choose JavaEE Tools.

  2. 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.

  1. 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 Start of the navigation pathBuild.End of the navigation path.

  2. In the dialog box that appears, choose OK .

  3. In the context menu of the buyer_ear node, choose Start of the navigation pathDeployEnd of the navigation path.

  4. In the dialog box that appears, choose OK .

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

Running the Web Module

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

  2. 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: