Skip to Content

This guide provides instructions on how to consume or use SAP BRMS in EJB.

Applies to:


This Document Holds good for all CE 7.3 SPXX. This method can also be used for Older version of CE (CE7.1 SP01 on-wards) with some difference in contend .


Summary:

This guide describes step-by-step how to return single value and multiple values from Decision Table.To Explain this we are using 2 Examples to explain how to read SAP BRMS in EJB.


Example 1 : Single set of values Returned.

Example 2 : Multiple List of values Returned.(How to consume or use SAP BRMS in EJB Part 2.)


We will be discussing following points in detail in this document –


  1. How to create & use EJB DTO inside BRMS.
  2. How to use “EngineInvoker” class file.
  3. How to call the rules from EJB.
  4. How to read the response received from BRMS.
  5. How to test the rules service.


Special Thanks to Samudra Gupta Samudra Gupta for his guidance to Document the process.


Example : Based on Entered Plant & product code delivery plant is to be defined.


Step 1 : Create an DTO called “Plant” as shown below:


import java.io.Serializable;
public class Plant implements Serializable{
  private static final long serialVersionUID = -3420223731187536617L;
  private String plant ;
  private String productCode ;
  private String deliveryPlantList ;
  public String getPlant() {
  return plant;
  }
  public void setPlant(String plant) {
  this.plant = plant;
  }
  public String getProductCode() {
  return productCode;
  }
  public void setProductCode(String productCode) {
  this.productCode = productCode;
  }
  public String getDeliveryPlantList() {
  return deliveryPlantList;
  }
  public void setDeliveryPlantList(String deliveryPlantList) {
  this.deliveryPlantList = deliveryPlantList;
  }
}





Step 2: Expose the Class file in public Parts as shown below:

  1. Right click on the EJB project. Click on “Development Component” . Click on “Show In” . Select “Component Properties”./wp-content/uploads/2013/12/1_355464.png
  2. On Selecting “Component Properties” The tab Shown below opens up. Click on “Public Parts”. Right Click on “Client” and Select “Manage Entities”./wp-content/uploads/2013/12/1_355464.png
  3. On Selecting “Manage Entities” the below Pop up opens up. Click on the “Java Class”. Click on the package “dto” . Select Plant and click on Next or Finished./wp-content/uploads/2013/12/1_355464.png
  4. On Click of finish the plant is added to public Part as shown below:/wp-content/uploads/2013/12/1_355464.png
  5. Repeat 2 & 3 for “ejbjar” and the plant will be added to the public part as shown below:/wp-content/uploads/2013/12/1_355464.png

Step 3 : Build the test/ejb/module and add it in “Dependencies” of “test/rules”. Only “Design Time” is required./wp-content/uploads/2013/12/1_355464.png

Step 4 : Go to the rules project and add the dto class.


  1. Open “Project Resource”. Click on the “Aliases” tab. Click on Add./wp-content/uploads/2013/12/1_355464.png
  2. On Click of “Add” the below pop Up opens up. Select the “Class” Option. /wp-content/uploads/2013/12/1_355464.png
  3. On Selecting Class another pop up opens up which looks as shown below. Open the Package select the “Plant” . Click on the arrow to add in the Selected Class. It will appear as shown below. Finally click on Finish./wp-content/uploads/2013/12/1_355464.png
  4. On Clicking of “Finish” the Plant gets added in the “Alias Name” List. Expand it and rename “Plant.getPlant” with “EnteredPlant” & “Plant.getProductCode” with “ProductCode”. /wp-content/uploads/2013/12/1_355464.png

Step 5 : Now Right Click on “Rules Modeling” & Select “New Ruleset”. /wp-content/uploads/2013/12/1_355464.png

Setp 6 : On Selecting “New Ruleset” a pop up opens up as shown below. Enter the name to the rule set like “DeliveryPlantRuleset”./wp-content/uploads/2013/12/1_355464.png

Step 7 :  On Clicking on “OK” We get a new Rule Set created as shown below: /wp-content/uploads/2013/12/1_355464.png

Step 8 : Creation of Rules & Decision Table.


  1. To create a “Decision Table” we have 2 process to create new “Decision Table”/wp-content/uploads/2013/12/1_355464.png
    1. Right Click on “Decision Table” and select “New Decision Tables…”.
    2. Open “DeliveryPlantRuleset”. Click on “Decision Table” Tab. Click on “New” button.
  2. On selecting either of the method we get the below pop up. Enter the “Decision Table Name” & “Comments”. Click on “Next”./wp-content/uploads/2013/12/1_355464.png
  3. On Click of “Next” The pop up changes and looks as shown below. Select the “EnteredPlant” & “ProductCode” from the Available Condition and Add them in the selected Condition. (In our scenario we do not have any horizontal condition or other condition). Click on “Next”./wp-content/uploads/2013/12/1_355464.png
  4. On Click of “Next” The pop up changes and looks as shown below. Select the “Plant.setDeliveryPlantList({String})” from the Available Action and Add them in the selected Action. (In our scenario we do not have any other Action). Click on “Finish”./wp-content/uploads/2013/12/1_355464.png
  5. On Click Of Finish the “Decision Table” is created and looks as shown below. Add the values in build time or you can add values directly in Rules Manager later on. To know how to click here. (Note : This Upload and download of Decision Table is not available in older version)./wp-content/uploads/2013/12/1_355464.png
  6. To create a “Rule” we have 2 process to create new “Rule”/wp-content/uploads/2013/12/1_355464.png
    1. Right Click on “Rules” and select “New Rule…”.
    2. Open “DeliveryPlantRuleset”. Click on “Rules” Tab. Click on “New” button.
  7. On selecting either of the method we get the below pop up. Enter the “Rules Name”. Click on “OK”./wp-content/uploads/2013/12/1_355464.png
  8. On Click Of “OK” the following is visible. In Our example we only have one rule and one decision table so we set a default condition and a default action as shown in the below./wp-content/uploads/2013/12/1_355464.png

Step 9 : Enter some dummy values in the Decision Table as shown below. Save it build and deploy “test/ejb/app” & “test/rules” in sequence./wp-content/uploads/2013/12/1_355464.png

Step 10 : Calling the Rule from “test/ejb/module”.


  1. Create a Java Class Called “EngineInvoker” and copy past the code in Appendix 1
  2. Create a “Session Bean” in “test/ejb/module” with the name “DeliveryPlantRules”.
  3. Copy past the code in Appendix 2 in the “Session Bean” in “test/ejb/module” with the name “DeliveryPlantRules”.
  4. The Code in line 24 in Appendix 2 is displayed below. there are 3 inputs to the method/wp-content/uploads/2013/12/1_355464.png
    1. The first one is the “Application Name” which can be found inside “test/rules” dc’s “Project Resources” as shown below:/wp-content/uploads/2013/12/1_355464.png
    2. The Second one is “Ruleset Name” which can be found inside “test/rules” dc’s “DeliveryPlantRuleset” as shown below:/wp-content/uploads/2013/12/1_355464.png
    3. The third and last one is the dto sent inside a list.
  5. Right Click on the “Session Bean” “DeliveryPlantRules” and Create Web service by following the steps shown in the sequence of screen shot./wp-content/uploads/2013/12/1_355464.png/wp-content/uploads/2013/12/1_355464.png/wp-content/uploads/2013/12/1_355464.png/wp-content/uploads/2013/12/1_355464.png/wp-content/uploads/2013/12/1_355464.png/wp-content/uploads/2013/12/1_355464.png

Step 11 : Build the dc’s “test/ejb/module” & “test/ejb/app”. Deploy the dc “test/ejb/app“.

Step 12 : Go to http://<Server>:<port>/wsnavigator and test the service be entering the “Plant” & “Product Code” you will get back the Plant DTO with the entered values along with “Delivery Plant”.


Appendix 1 : EngineInvoker.java Class file code.


import java.io.Serializable;
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.tc.logging.Location;
public class EngineInvoker {
static Location logger = Location.getLocation(EngineInvoker.class);
    private static String jndiName = "com.sap.brms.RuleEngine";
    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();
    }
    @SuppressWarnings("unchecked")
    public static List<String> invokeRuleset(String projectName, String rsName,List<Serializable> input) {
    logger.debugT("start:invokeRuleset");
            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) {
            logger.errorT("error occured:"+e.getMessage());            
                e.printStackTrace();
            }
            logger.debugT("start:invokeRuleset");
            return output;
    }
}






Appendix 2 : DeliveryPlantRules.java Class file code.


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import util.EngineInvoker;
import com.sap.tc.logging.Location;
import dto.Plant;
@Stateless
public class DeliveryPlantRules implements DeliveryPlantRulesLocal {
  static Location logger = Location.getLocation(DeliveryPlantRules.class);
  @SuppressWarnings("unchecked")
  public Plant getDeliveringPlantForProductsRules(Plant plant){
  logger.debugT("start:getRullesData");
  List<Serializable> ilist = new ArrayList<Serializable>();
  ilist.add(plant);
  Object object = new Object();
  if (ilist.size() != 0) {
  List output = EngineInvoker.invokeRuleset("demo.sap.com~test~rules", "DeliveryPlantRuleset", ilist);
  if (output.size() != 0) {
  object = output.get(0);
  }
  } else {
  object = "Input has not been set.";
  }
  logger.debugT("end:getRullesData");
  if (object instanceof Plant) {
  return (Plant) object;
  }
  return null;
  }
}



To report this post you need to login first.

8 Comments

You must be Logged on to comment or reply to a post.

Leave a Reply