Skip to Content
Author's profile photo Arti Gopalan

Invoking Business Rules – Part 1

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.


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 = “”;

    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( 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();



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



                    ret_payloadSeparator = trimmedLine;

                } else





            if(sb.length() != 0)


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



            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();


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






            return op.toString();



      Assigned Tags

          Be the first to leave a comment
          You must be Logged on to comment or reply to a post.