7 Replies Latest reply on Apr 29, 2013 2:36 PM by cold_gin

    Difficulties Invoking Rule Task

    tob1as

      Hi,

       

      I was trying to invoke a rule task within my process.

       

      First, I took the example that was part of the unit test examples:

       

      Capture5.PNG

      The execution works fine.

       

      I then added a human task node and a script task:

       

      Capture7.PNG

      Here, the script task prints a hello world, but then the process instance stops at the rule task.

       

      Does anyone know, why it is not running into the rule task? As I estimate, the rule task node has to be active at the time the fireAllRules function is executed ?! But do I have to manually fire that event everytime a rule has to be executed in a process?

       

       

      The java looks as follows.

       

      System.out.println("Loading process BPMN2-RuleTask.bpmn2");

      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

      kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-RuleTask.bpmn2"), ResourceType.BPMN2);

      kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-RuleTask.drl"), ResourceType.DRL);

      KnowledgeBase kbase = kbuilder.newKnowledgeBase();

      StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

             

      ksession.getWorkItemManager().registerWorkItemHandler(

          "Human Task",

          new WSHumanTaskHandler()

      );

       

              List<String> list = new ArrayList<String>();

      ksession.setGlobal("list", list);

      ProcessInstance processInstance = ksession.startProcess("RuleTask");

       

      ksession.fireAllRules();

      Best regards,

       

      Tobias

        • 1. Re: Difficulties Invoking Rule Task
          salaboy21

          Hi Tobias,

          Yes you are right, when you use Rule Tasks now you need to call fireAllRules. The behavior of the Rule Tasks Nodes in the current version works as follow:

          The rule task node will activate the ruleflow group that contains your rules. At that point all the activated rules inside that group are elegible to fire. For that reason you need to call fire all rules in order to fire the active rules. I'm trying to change that behavior to call it automatically.

           

          Cheers

          • 2. Re: Difficulties Invoking Rule Task
            eaa

            The behavior of Rule Task is the one defined by Mauricio.

            You have 2 options:

            1. execute fireAllRules() manually. This could be very tricky because maybe you don't know when or where you need to invoke it
            2. have a separate thread running fireUntilHalt() in the ksession. For this, you need to manually start a thread invoking fileUntilRun() in its run() method.

             

            But the question is, why does the first example work but the second doesn't?

            And the "problem" is because of the inclusion of an asynchornous node in your process: the Human Task.

             

            So, when you start your process, the execution reaches the Human Task, the task is created in the HT Server and the execution control gets back to your code. At that point you are executing the fireAllRules(). This fireAllRules() is not doing anything because there is nothing to be fired. The Rule Task was never executed yet.

            When you complete the HT, the process continues its execution and the Rule Task gets invoked. Here you need to call fireAllRules() to continue with the execution of the process.

             

            Best Regards,

            • 3. Re: Difficulties Invoking Rule Task
              tob1as

              Thanks Esteban and Mauricio,

               

              is there a simple example for case 2 somewhere? Since the rule engine is a key selling point for jBPM, I would like to be able to present a working business rule task in my scenario. This invocation of a rule task within a process flow however seems a bit awkward.

              I would expect the process to just simply run in a rule task, do something and go on with the process.

               

              Another thing -  not that I am trying to do it - but could the rules engine theoretically be exchanged / how tightly coupled is jBPM with the rules engine?

               

              Best regards,

              Tobias

              • 4. Re: Difficulties Invoking Rule Task
                tsurdilovic

                Hi Tobias, there is a third options  which is to attach an AgendaEventListener to your ksession and fire the rules upon an RuleFlowGroupActivatedEvent. Here is an example:

                 

                final org.drools.event.AgendaEventListener agendaEventListener = new org.drools.event.AgendaEventListener() {

                                public void activationCreated(ActivationCreatedEvent event,

                                        WorkingMemory workingMemory){

                                }

                                public void activationCancelled(ActivationCancelledEvent event,

                                          WorkingMemory workingMemory){

                                }

                                public void beforeActivationFired(BeforeActivationFiredEvent event,

                                            WorkingMemory workingMemory) {

                                }

                                public void afterActivationFired(AfterActivationFiredEvent event,

                                           WorkingMemory workingMemory) {

                                }

                                public void agendaGroupPopped(AgendaGroupPoppedEvent event,

                                        WorkingMemory workingMemory) {

                                }

                 

                                public void agendaGroupPushed(AgendaGroupPushedEvent event,

                                        WorkingMemory workingMemory) {

                                }

                                public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event,

                                                   WorkingMemory workingMemory) {

                                }

                                public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event,

                                        WorkingMemory workingMemory) {

                                    workingMemory.fireAllRules();

                                }

                                public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event,

                                                     WorkingMemory workingMemory) {

                                }

                                public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event,

                                                    WorkingMemory workingMemory) {

                                }

                            };

                            ((StatefulKnowledgeSessionImpl)  ((KnowledgeCommandContext) ((CommandBasedStatefulKnowledgeSession) ksession)

                                    .getCommandService().getContext()).getStatefulKnowledgesession() )

                                    .session.addEventListener(agendaEventListener);

                 

                Regarding >> but could the rules engine theoretically be exchanged / how tightly coupled is jBPM with the rules engine? <<

                This is where the beauty of custom work items comes in You could write your work item to interact with any rule engine or service.

                 

                Hope this helps.

                • 5. Re: Difficulties Invoking Rule Task
                  tob1as

                  Thanks, that looks a lot more convenient.

                  • 6. Re: Difficulties Invoking Rule Task
                    ashu_akg21

                    Hi Tihomir,

                     

                    We are using JBPM 5.3 and we are facing the same problem [getting Rule task to fire after Human Task]. When we tried to follow the method suggested, we are getting only the following events within AgendaEventListener:

                     

                    AgendaEventListener a=new AgendaEventListener() {

                       

                        @Override

                        public void beforeActivationFired(

                                org.drools.event.rule.BeforeActivationFiredEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                       

                        @Override

                        public void agendaGroupPushed(

                                org.drools.event.rule.AgendaGroupPushedEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                       

                        @Override

                        public void agendaGroupPopped(

                                org.drools.event.rule.AgendaGroupPoppedEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                       

                        @Override

                        public void afterActivationFired(

                                org.drools.event.rule.AfterActivationFiredEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                       

                        @Override

                        public void activationCreated(

                                org.drools.event.rule.ActivationCreatedEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                       

                        @Override

                        public void activationCancelled(

                                org.drools.event.rule.ActivationCancelledEvent arg0) {

                            // TODO Auto-generated method stub

                           

                        }

                    };

                     

                    We cant find afterRuleFlowGroupActivated event here.  Could you please advise how we can move forward?

                    • 7. Re: Difficulties Invoking Rule Task
                      cold_gin

                      Here is my solution to this problem in case it helps someone:

                       

                      https://community.jboss.org/thread/224241