arti.gopalan

Previous post Next post
Currently Being Moderated
Arti Gopalan

Invoking Business Rules - Part 1

Posted by Arti Gopalan in arti.gopalan on Sep 25, 2008 11:28:52 PM

This blog is going to be about how business rules can be evaluated. The scenario considered is that, you have already created a rules project.  The rules project could either be using XSD or a  java resource as its business object on which the rules are defined. Let us consider in this blog the case that the rules project created here makes use of a XSD as the business object.

Rules can be invoked by either –

I.   Using the code to Look-up the rule engine EJB in your application.

(or)

2.  Creating a web service containing a method to invoke the rule engine. 

 

We are going to understand how business rules, created in the rules composer perspective of the NWDS, can be invoked inside an application. Steps to do this -

    The rules engine which invokes/evaluates/ executes business rules is available as an EJB. We need to look up this EJB and create an instance of the engine for use, in our application. To do this we make use of the following lines of code –

    String jndiName = "com.sap.brms.RuleEngine";

    InitialContext context = new InitialContext();

    Object obj = context.lookup(jndiName);

    RuleEngineHome engineObj = (RuleEngineHome)  PortableRemoteObject.narrow(obj, RuleEngineHome.class);

      

    1. Passing the required information to the rules engine.

    To invoke rules we need to pass information such as – the project name; the ruleset name and the business objects which are to be processed – to the rules engine. To do this we make use of the method – invoke Ruleset.

    The method syntax is  -

    RulesetContext  invokeRuleset(com.sap.brms.qrules.engine.RulesetContext arg0)

    List  invokeRuleset(java.lang.String arg0, java.lang.String arg1, java.util.List arg2)

     

    Here, arg0 is the Project Name ; arg1 is the Ruleset Name; and the arg2 is the business objects list. i.e,  we need to pass the business object, in this case as a XML as an object in a List to the rules engine.

    To do this we make use of the following lines of code –

    An xml string is created by making use of the inputted information from the web form, by the user. This xml string is then passed to the processPayload method.If more than one xml needs to be passed to the rules engine a payload saperator - # - needs to be used between each set of xml string.  The  processPayload method then creates an xml object for each set of xml string passed to it and adds the objects to a List. This list is then returned.
      1. This  returned list is then passed to the rule engine for invocation.
    The returned list is then passed to the processReturnPayload method which creates a xml string from each object returned. This xml string can then be parsed to get the required/processed element value.

               

       

      List xmlObjList = processPayload(inputXML);

      List opXMLObjects = ruleEngine.invokeRuleset(projectName, rsName,xmlObjList);

      output = processReturnPayload(opXMLObjects);

       


      private static List processPayload(String inputXML)

            throws Exception

        {

            List inputList = new ArrayList();

            StringReader strReader = new StringReader(inputXML);

            BufferedReader in = new BufferedReader(strReader);

            String line = null;

            StringBuffer sb = new StringBuffer();

            XMLObject xmlObj = null;

            while((line = in.readLine()) != null)

            {

                String trimmedLine = line.trim();

                if(trimmedLine.startsWith(payloadSeparator))

                {

                    xmlObj = XMLObjectFactory.createXMLObject(sb.toString());

                    inputList.add(xmlObj);

                    sb.setLength(0);

                    ret_payloadSeparator = trimmedLine;

                } else

                {

                    sb.append(line);

                }

            }

            if(sb.length() != 0)

            {

                xmlObj = XMLObjectFactory.createXMLObject(sb.toString());

                inputList.add(xmlObj);

            }

            return inputList;

      }

       

        private static String processReturnPayload(List opXMLObjects)

        {

            StringBuffer op = new StringBuffer();

            int size = opXMLObjects.size();

            for(int i = 0; i < size; i++)

            {

                XMLObject obj = (XMLObject)opXMLObjects.get(i);

                String opS = obj.getXmlString();

                op.append(opS);

                if(size != 1 && i != size - 1)

                {

                    op.append(ret_payloadSeparator);

                    op.append(System.getProperty("line.separator"));

                }

            }       

            return op.toString();

        }

       

      Comments