3 Replies Latest reply on Jan 3, 2014 2:01 PM by jjsantoro

    Invoking a business rule from a process

    jjsantoro

      I have a simple process with a simple rule, but I cannot get it to trigger.  The process does this:

       

      Start -> Human Task -> Rule -> Gateway ...

       

      How can I get the process data to the rule?  I know via a script activity that my process data (e.g., ProcessDataType) has values.  For the rule task, I mapped my process data to an input and output variable of type ProcessDataType, but in my rule my when does not get triggered even as:

       

      when

              $data : ProcessDataType()

      then

              System.out.println("this rule has been triggered");

       

       

      I can invoke the rule only if my when is "eval(true)", so I know that the rule is there.  What step am I missing?

        • 1. Re: Invoking a business rule from a process
          ahmedza

          Hi Santoro,

           

          You need to follow these steps if u want process data in rules.

                                                       IN - Code

          Apporach 1

          U need to put process instance in working memory so that the rule can evaluate the conditions in when part.

               a. Do this In a script task just after start node     kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance())

          Approach 2

          Or Register a Process event listener, and in afterProcessStarted() event add this line session.insert(event.getProcessInstance())

           

                                                            IN - RULES

          Sample Rule 1 :

          import org.drools.runtime.process.WorkflowProcessInstance;

          when

                   $process: WorkflowProcessInstance()

                   eval(Integer.parseInt((String)$process.getVariable("piAgeVariable")) > 30)

               then .........

                                                                                                                                       

           

           

           

          Notes :

          1.      I am not sure about JBPM-6, but in JBPM 5.4 if u don't fire the rule when u enter the node, then rule is not fired. Resolution to this is Be sure that u have placed this line just before u enter           the rules node. ksession.fireAllRules(); .

          2.      Keep one thing in mind to retract the process instaces properly after rule node is executed using the kcontext.getKnowledgeRuntime().retract()  or the eventListener approach. Else this process instance will remain in working memory and will be part of rules evaluation when anyother JBPM process instance triggers fireAllRules().

          • 2. Re: Invoking a business rule from a process
            swiderski.maciej

            on top of what was already mentioned you can take advantage of data input and output (and their associations) on business rule task to insert and the retract the data from working memory. Take a look at screen cast 3 on this blog post. It guides you through using rule task in process in jbpm6.

             

            HTH

            • 3. Re: Invoking a business rule from a process
              jjsantoro

              Maciej,

               

              Thank you for the blog.  I was able to reproduce your process and make my own.  My problem was that I named a project with mixed case, but the forms and process did not behave properly unless I used all lower case (e.g., my package was "MixedCase" but I had to refer to it as "org.jbpm.mixedcase".