Show TOC

Procedure documentationExecuting the Rules Locate this document in the navigation structure

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   Window   Open Perspective   Other  .

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

    Note Note

    • If the Component Browser view does not open, choose   Window   Show View   Other  .

    • In the dialog box that appears, expand the Development Infrastructure node and choose Component Browser. Choose OK.

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

    Note Note

    If the Component Properties view does not open, choose Window -> Show View -> Other. In the dialog box that appears, expand the Development Infrastructure node and choose Component Properties. Choose OK.

    End of the note.
  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.

Note Note

  • In the context menu of the buyer_wm node, choose   Sync/Create Project   Sync Used DCs  

  • In the dialog box that appears, choose OK.

End of the note.
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   New   Other  

  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   New   Other  

  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:

    Syntax Syntax

    1. 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[]){
          }
      }
    End of the code.
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   New   Other  

  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:

    Syntax Syntax

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>Insert title here</title>
    8. <head>
    9. <body>
    10. </body>
    11. </html>
    End of the code.
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. Copy the following lines after <body>

    Syntax Syntax

    1. 
      <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>
    End of the code.
  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   New   Other  

  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:

    Syntax Syntax

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>Insert the title here</title>
    8. <head>
    9. <body>
    10. </body>
    11. </html>
    End of the code.
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. Copy the following line after <body>:

    Syntax Syntax

    1. <jsp:forward page="BuyerDemo.jsp"/>
    End of the code.
  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   New   Other  

  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:

    Syntax Syntax

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"
    2. pageEncoding="UTF-8"%>
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    4. <html>
    5. <head>
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    7. <title>Insert the title here</title>
    8. <head>
    9. <body>
    10. </body>
    11. </html>
    End of the code.
  5. Replace <title>Insert title here </title> with <title>Buyer Demo</title> .

  6. Copy the following lines before <html>

    Syntax Syntax

    1. <%@page import="com.sap.helper.*"%>
      <%@page import="com.sap.brms.qrules.engine.*" %>
      <%@page import="java.util.*"%>
      <%@page import="com.sap.buyer.*"%>
    End of the code.
  7. Copy the following line after <body>:

    Syntax Syntax

    1. <%
      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"/>
      
    End of the code.
  8. Save the changes.

Creating the Enterprise Application
  1. In the SAP NetWeaver Developer Studio, choose   File   New   Project  .

  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.

Note Note

  • In the context menu of the buyer_ear node, choose   Sync/Create Project   Sync Used DCs  

  • In the dialog box that appears, choose OK.

End of the note.
Creating the application.xml

Make sure that you are in the Java EE perspective.

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

    Note Note

    Expand the enterprise application: buyer_ear node, META-INF node double click the application.xml node.

    End of the note.

    You should see the application.xml window with the following lines:

    Syntax Syntax

    1. <?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>
    End of the code.
  2. Replace <context-root>LocalDevelopment~LocalDevelopment~buyer_wm~demo.sap.com</context-root> with <context-root>BuyerDemo</context-root> .

    Note Note

    Instead of LocalDevelopment~LocalDevelopment~ buyer_wm ~demo.sap.com, you need to enter the customized application name i.e in this tutorial, the name of the application is BuyerDemo.

    End of the note.
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   Build.  .

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

  3. In the context menu of the buyer_ear node, choose   Deploy  .

  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.

Note Note

You can also build and deploy buyer_wm and buyer_ear in the Java EE perspective.

  1. In the Project Explorer view, in the context menu of the buyer_wm and buyer_ear nodes, choose Development Component > Build.

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

  3. In the context menu of the buyer_ear node, choose Development Component > Build.

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

End of the note.
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.

    Example Example

    1. Enter the following data in the respective fields:

      Field

      User Entry

      Name of the Buyer

      John

      Spending Habit of the Buyer

      good

      Buyer's Credit

      good

    2. Choose Submit.

    You should get the Discount Set as 20 because in the BigBuyer rule, if the Name of the Buyer is *, Spending Habit of the Buyer is good and Buyer's Credit is good then the discount set is 20.

    End of the example.

    Here is the snapshot of the web module:

    This graphic is explained in the accompanying text.