3 Replies Latest reply on Jan 20, 2012 6:34 AM by Mahi M

    fail to fire a rule task~~ frustrated

    Guo-Guang Chiou Newbie

      testrule-image.png

      I defined a process as the above.

      between 2 script task there is a rule task.

       

      here is my rule content. it's very simple.

      ============================================================

      #created on: 2011/10/11

      #list any import classes here.

      import org.drools.runtime.process.WorkflowProcessInstance;

      #declare any global variables here 

      rule rule1

                ruleflow-group "validate"

          when              

          then

               System.out.println( "TEST RULE ^^^" );

      end

      =============================================================

       

      and process content is as following.

       

      ========================================================================================================

      <?xml version="1.0" encoding="UTF-8"?>

      <definitions id="Definition"

                   targetNamespace="http://www.jboss.org/drools"

                   typeLanguage="http://www.java.com/javaTypes"

                   expressionLanguage="http://www.mvel.org/2.0"

                   xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"

                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                   xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"

                   xmlns:g="http://www.jboss.org/drools/flow/gpd"

                   xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"

                   xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"

                   xmlns:di="http://www.omg.org/spec/DD/20100524/DI"

                   xmlns:tns="http://www.jboss.org/drools">

        <process processType="Private" isExecutable="true" id="testrule" name="TestRule" tns:packageName="defaultPackage" >

          <!-- nodes -->

          <startEvent id="_1" name="StartProcess" />

          <scriptTask id="_2" name="Script" scriptFormat="http://www.java.com/java" >

            <script>System.out.println("Before rule\n");

                          org.drools.runtime.StatefulKnowledgeSession ksession =

                               (org.drools.runtime.StatefulKnowledgeSession)

                                kcontext.getKnowledgeRuntime();

              ksession.fireAllRules();

      System.out.println("After rule\n");</script>

          </scriptTask>

          <businessRuleTask id="_3" name="Rule" g:ruleFlowGroup="validate" >

          </businessRuleTask>

          <endEvent id="_4" name="End" >

              <terminateEventDefinition/>

          </endEvent>

          <scriptTask id="_5" name="Script" scriptFormat="http://www.java.com/java" >

            <script>System.out.println("DONE");</script>

          </scriptTask>

          <!-- connections -->

          <sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2" />

          <sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3" />

          <sequenceFlow id="_5-_4" sourceRef="_5" targetRef="_4" />

          <sequenceFlow id="_3-_5" sourceRef="_3" targetRef="_5" />

        </process>

        <bpmndi:BPMNDiagram>

          <bpmndi:BPMNPlane bpmnElement="testrule" >

            <bpmndi:BPMNShape bpmnElement="_1" >

              <dc:Bounds x="16" y="16" width="48" height="48" />

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="_2" >

              <dc:Bounds x="96" y="16" width="80" height="48" />

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="_3" >

              <dc:Bounds x="208" y="16" width="80" height="48" />

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="_4" >

              <dc:Bounds x="432" y="16" width="48" height="48" />

            </bpmndi:BPMNShape>

            <bpmndi:BPMNShape bpmnElement="_5" >

              <dc:Bounds x="320" y="16" width="80" height="48" />

            </bpmndi:BPMNShape>

            <bpmndi:BPMNEdge bpmnElement="_1-_2" >

              <di:waypoint x="40" y="40" />

              <di:waypoint x="136" y="40" />

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="_2-_3" >

              <di:waypoint x="136" y="40" />

              <di:waypoint x="248" y="40" />

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="_5-_4" >

              <di:waypoint x="360" y="40" />

              <di:waypoint x="456" y="40" />

            </bpmndi:BPMNEdge>

            <bpmndi:BPMNEdge bpmnElement="_3-_5" >

              <di:waypoint x="248" y="40" />

              <di:waypoint x="360" y="40" />

            </bpmndi:BPMNEdge>

          </bpmndi:BPMNPlane>

        </bpmndi:BPMNDiagram>

      </definitions>


      ========================================================================================================

       

      in the first script task.

      I have a code snippet to call fireAllRules in order to trigger rule task to work.

      ==================================================================

      System.out.println("Before rule\n");

                          org.drools.runtime.StatefulKnowledgeSession ksession =

                               (org.drools.runtime.StatefulKnowledgeSession)

                                kcontext.getKnowledgeRuntime();

              ksession.fireAllRules();

      System.out.println("After rule\n");


      ===================================================================

       

      and then I deployed all these artifacts to Guvnor repository by using Gnvnor plugin in eclipse.

      the I open a browser to http://localhost:8080/jbpm-console to start a process.

       

      i have been trying many ways to try to fire a rule task to work.

      however,

      It always stops at rule task and can not mve to next script task.

      I don't know why it goes this way.

       

      Screenshot.png

      log file is attached.

       

      any suggest and help will be appreciated.

       

      Guo-Guang

        • 1. Re: fail to fire a rule task~~ frustrated
          Francesco Pietrobelli Newbie

          The behavior that you describe is correct, that is you invoke fireAllRules() before the process reached the business rule task and so at the time you invoke fireAllRules() there aren't any active rules.

           

          A simple solution is to add an EventProcessListener to the StatefulKnowledgeSession and then for every ProcessNodeTriggeredEvent, if the node is a business rule task, call the fireAllRules() method. But if you using jbpm-console to run the process I don't have any suggestion because i don't know how to manage the StatefulKnowledgeSession incapsulated in jbpm-console.

          • 2. Re: fail to fire a rule task~~ frustrated
            Guo-Guang Chiou Newbie

            hi, Francesco. thanks for your reply.

            Finally, I found a way to run rule task on jbpm-console.

            here is my process definition..

            =====================================================================================================================

            <?xml version="1.0" encoding="UTF-8"?>

            <definitions id="Definition"

                         targetNamespace="http://www.jboss.org/drools"

                         typeLanguage="http://www.java.com/javaTypes"

                         expressionLanguage="http://www.mvel.org/2.0"

                         xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"

                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                         xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"

                         xmlns:g="http://www.jboss.org/drools/flow/gpd"

                         xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"

                         xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"

                         xmlns:di="http://www.omg.org/spec/DD/20100524/DI"

                         xmlns:tns="http://www.jboss.org/drools">

             

             

              <process processType="Private" isExecutable="true" id="testrule" name="TestRule" tns:packageName="defaultPackage" tns:version="16" >

             

             

                <extensionElements>

                 <tns:import name="org.drools.event.*" />

                </extensionElements>

                <!-- nodes -->

                <startEvent id="_1" name="StartProcess" />

                <scriptTask id="_2" name="Script" scriptFormat="http://www.java.com/java" >

                  <script>System.out.println("Before rule\n");

              org.drools.event.AgendaEventListener lis = new org.drools.event.AgendaEventListener(){

                                                    public void activationCancelled(org.drools.event.ActivationCancelledEvent arg0,

                                                                        org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void activationCreated(org.drools.event.ActivationCreatedEvent arg0,

                                                                        org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void afterActivationFired(

                                                                        org.drools.event.AfterActivationFiredEvent arg0, org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void afterRuleFlowGroupActivated(

                                                                        org.drools.event.RuleFlowGroupActivatedEvent arg0, org.drools.WorkingMemory arg1) {

                                                              System.out.println("firing all rules");

                                                              arg1.fireAllRules();

                                                    }

                                                    public void afterRuleFlowGroupDeactivated(

                                                                        org.drools.event.RuleFlowGroupDeactivatedEvent arg0, org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void agendaGroupPopped(org.drools.event.AgendaGroupPoppedEvent arg0,

                                                                        org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void agendaGroupPushed(org.drools.event.AgendaGroupPushedEvent arg0,

                                                                        org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void beforeActivationFired(

                                                                        org.drools.event.BeforeActivationFiredEvent arg0, org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void beforeRuleFlowGroupActivated(

                                                                        org.drools.event.RuleFlowGroupActivatedEvent arg0, org.drools.WorkingMemory arg1) {

                                                    }

                                                    public void beforeRuleFlowGroupDeactivated(

                                                                        org.drools.event.RuleFlowGroupDeactivatedEvent arg0, org.drools.WorkingMemory arg1) {

                                                    }

                                          };

             

                       System.out.println("casting..");

                       org.drools.runtime.StatefulKnowledgeSession ksession = (org.drools.runtime.StatefulKnowledgeSession) kcontext.getKnowledgeRuntime();

                      

                        try{

                       org.drools.impl.StatefulKnowledgeSessionImpl s = (org.drools.impl.StatefulKnowledgeSessionImpl)ksession;

                       s.session.addEventListener(lis)  ;

                       System.out.println("listener is added");

                 }

                 catch(Exception ex)

                 {

                         System.out.println("================================================================================================================");

                           System.out.println(ex.getMessage());    

                                   System.out.println("================================================================================================================");

                 }</script>

                </scriptTask>

                <businessRuleTask id="_3" name="Rule" g:ruleFlowGroup="validate" >

                </businessRuleTask>

                <endEvent id="_4" name="End" >

                    <terminateEventDefinition/>

                </endEvent>

                <scriptTask id="_5" name="Script" scriptFormat="http://www.java.com/java" >

                  <script>

            System.out.println("Rule Task is done"); </script>

                </scriptTask>

             

             

                <!-- connections -->

                <sequenceFlow id="_1-_2" sourceRef="_1" targetRef="_2" />

                <sequenceFlow id="_2-_3" sourceRef="_2" targetRef="_3" />

                <sequenceFlow id="_5-_4" sourceRef="_5" targetRef="_4" />

                <sequenceFlow id="_3-_5" sourceRef="_3" targetRef="_5" />

             

             

              </process>

             

             

              <bpmndi:BPMNDiagram>

                <bpmndi:BPMNPlane bpmnElement="testrule" >

                  <bpmndi:BPMNShape bpmnElement="_1" >

                    <dc:Bounds x="16" y="16" width="48" height="48" />

                  </bpmndi:BPMNShape>

                  <bpmndi:BPMNShape bpmnElement="_2" >

                    <dc:Bounds x="96" y="16" width="80" height="48" />

                  </bpmndi:BPMNShape>

                  <bpmndi:BPMNShape bpmnElement="_3" >

                    <dc:Bounds x="208" y="16" width="80" height="48" />

                  </bpmndi:BPMNShape>

                  <bpmndi:BPMNShape bpmnElement="_4" >

                    <dc:Bounds x="432" y="16" width="48" height="48" />

                  </bpmndi:BPMNShape>

                  <bpmndi:BPMNShape bpmnElement="_5" >

                    <dc:Bounds x="320" y="16" width="80" height="48" />

                  </bpmndi:BPMNShape>

                  <bpmndi:BPMNEdge bpmnElement="_1-_2" >

                    <di:waypoint x="40" y="40" />

                    <di:waypoint x="136" y="40" />

                  </bpmndi:BPMNEdge>

                  <bpmndi:BPMNEdge bpmnElement="_2-_3" >

                    <di:waypoint x="136" y="40" />

                    <di:waypoint x="248" y="40" />

                  </bpmndi:BPMNEdge>

                  <bpmndi:BPMNEdge bpmnElement="_5-_4" >

                    <di:waypoint x="360" y="40" />

                    <di:waypoint x="456" y="40" />

                  </bpmndi:BPMNEdge>

                  <bpmndi:BPMNEdge bpmnElement="_3-_5" >

                    <di:waypoint x="248" y="40" />

                    <di:waypoint x="360" y="40" />

                  </bpmndi:BPMNEdge>

                </bpmndi:BPMNPlane>

              </bpmndi:BPMNDiagram>

             

             

            </definitions>

             

            ====================================================================================================================

            • 3. Re: fail to fire a rule task~~ frustrated
              Mahi M Newbie

              Hi Francesco,

               

              Do have any example of a firing the Rules from Script Task before the Business Rule Task ?