8 Replies Latest reply on Apr 5, 2006 12:39 PM by kukeltje

    Q about actions and trigger transitions

    cwad0000

      I am trying to figure out how I should create a transition in my workflow, I would be thankful for any feedback.

      Imagine I have a workflow "TheWalkWorkflow", consisting of someone travelling a route.
      At some point in the process I will have a crossroad, where the user gives some input and
      depending on the user input and earlier decisions (that should be stored in process variables) it is determined which way to go.

      My thought is that I have a task node for the crossroad, which will wait for input from the user and on the "end" event,
      a class will be called that calculates what to do.

      ...
      <task-node name='crossRoad'>
       <task name='decideWhichWayToGo'>
       <event type='task-end'>
       <action class='TheWalkWorkflow$CrossRoadOnEnd'>
       <memberVarInActionClass1>someconstantdata</memberVarInActionClass1>
       </action>
       </event>
       <transition name="go north" to="roadNorth"></transition>
       <transition name="go south" to="roadSouth"></transition>
      </task-node>
      ....
      


      here are my questions:
      1) how can my action class signal which transition to take (e.g. "go north)?
      1b) As I understand it my action class will be called when end() is called, right?
      1c) so how can the action class determine which path to take, can I still call end("go north")?

      2a) the setting of variables, e.g. "memberVarInActionClass1", can they only be contants defined in the xml?
      2b) Or could I for example say in the process definition that I want this member variable to be set to the same value as some variable in the process instance? (so that the action class does not need to fetch the processinstance variables himself...)

      Would be thankful for any comments, or suggestions if I should rather do it in a different way.

      cheers


        • 1. Re: Q about actions and trigger transitions
          kukeltje


          1) how can my action class signal which transition to take (e.g. "go north)?

          It shouldn't, actions in classes should not end the nodes themselves and take a transition

          1b) As I understand it my action class will be called when end() is called, right?

          yes

          1c) so how can the action class determine which path to take, can I still call end("go north")?
          By implementing some logic, use a businessrule, ask a clairvoyant....

          2a) the setting of variables, e.g. "memberVarInActionClass1", can they only be contants defined in the xml?

          No, it could be just a string which you use as a reference to a real procesvariable. Be creative, lost of possibilities and not many wrong ones

          2b) Or could I for example say in the process definition that I want this member variable to be set to the same value as some variable in the process instance? (so that the action class does not need to fetch the processinstance variables himself...)

          No not directly, but as mentioned in the previous question, you could use it as a string and use that to fetch the value of the processvariable in the actionhandler

          • 2. Re: Q about actions and trigger transitions
            cwad0000

            thanks for the quick reply kukeltje, some follow up questions:

            "kukeltje" wrote:

            1) how can my action class signal which transition to take (e.g. "go north)?

            It shouldn't, actions in classes should not end the nodes themselves and take a transition

            this seems to differ with what I read in the User guide:
            "User Guide 3.1: 9.5. Actions" wrote:

            Actions on events have no way to influence the flow of control of the process. It is similar to the observer pattern. On the other hand, an action that is put on a node has the responsibility of propagating the execution.



            So, my plan is now to do the code below in my action class "TheWalkWorkflow$CrossRoadOnEnd"
            if (....)
             executionContext.leaveNode("go north")
            else
             executionContext.leaveNode("go south")
            

            to determine which transition to leave on.

            3) Does this make sense, or I am still on the wrong track? Please suggest how it could be done in another way if this is not correct/ a good way to do it.

            "kukeltje" wrote:

            1c) so how can the action class determine which path to take, can I still call end("go north")?
            By implementing some logic, use a businessrule, ask a clairvoyant....
            I had phrased it badly, what I wanted to know how to tell the application to "go north", which I think the code above should do.

            cheers




            • 3. Re: Q about actions and trigger transitions
              kukeltje

               


              this seems to differ with what I read in the User guide:
              "User Guide 3.1: 9.5. Actions" wrote:

              Actions on events have no way to influence the flow of control of the process. It is similar to the observer pattern. On the other hand, an action that is put on a node has the responsibility of propagating the execution.



              Not quite, there is a difference between actions on events and actions on the default node type. If you do the latter, you create a new custom node type. In that case the actions are indeed responsible for propagating the execution. Actions on events (like task-end) should not influence the flow of te process. That is what these lines in the documentation mean.

              So, my plan is now to do the code below in my action class "TheWalkWorkflow$CrossRoadOnEnd"
              if (....)
               executionContext.leaveNode("go north")
              else
               executionContext.leaveNode("go south")
              

              to determine which transition to leave on.


              You could do this, if it is an action on a node, not an action on an event. But then, you could not use the task node for this and get all the advantages of the tasknode


              3) Does this make sense, or I am still on the wrong track? Please suggest how it could be done in another way if this is not correct/ a good way to do it.


              Yes it does, but you mixed to concepts. What you could do is just have a task node with one transition (like finish) and a decision node below that which has an action to decide which transition to take (decision nodes are the exception to the rule that actions on events should not influence the flow)

              hth.

              Ronald

              • 4. Re: Q about actions and trigger transitions
                cwad0000

                I tried going the way with a task-node with one transition to a decision node,
                but in the decision node my action is ignored and it just continues to the first transition. Is my xml faulty?
                (i tried both with even type task-start and task-end, but my action code is not called)

                <decision name="approved?">
                 <event type='task-end'>
                 <action class='my.stuff.workflows.TestWF$ApprovedOnEnd'>
                 <leaveReject>rejected</leaveReject>
                 <leaveOk>approved</leaveOk>
                 </action>
                 </event>
                 <transition name="approved" to="Manager approve"></transition>
                 <transition name="rejected" to="wasRejected"></transition>
                 </decision>
                


                • 5. Re: Q about actions and trigger transitions
                  cwad0000

                  ok, type should be 'node-leave' i reckon, then my code is executed, but I end up in an infinite loop...even though both transitions goes to nodes that will point to end, so no loops in my workflow.

                  i am very confused by this behaviour....

                  this is my handler code

                  public void execute(ExecutionContext executionContext) throws Exception {
                   log.debug(" +++++++++++++++ action code+++++++++++++++");
                   log.debug(" +++++++++++++++ leaveReject: "+ leaveReject + " +++++++++++++++");
                   log.debug(" +++++++++++++++ leaveOk: "+ leaveOk + " +++++++++++++++");
                   executionContext.leaveNode(leaveReject);
                   }
                  


                  • 6. Re: Q about actions and trigger transitions
                    kukeltje

                    Yes, it is completely fault. There is no task-end event on a decision node. Look at the test cases...

                    It should be something like

                    <decision name='d'>
                    
                    <decision name="approved?">
                     <handler class='my.stuff.workflows.TestWF$ApprovedOnEnd'>
                     <leaveReject>rejected</leaveReject>
                     <leaveOk>approved</leaveOk>
                     </handler>
                     <transition name="approved" to="Manager approve"></transition>
                     <transition name="rejected" to="wasRejected"></transition>
                    </decision>
                    


                    • 7. Re: Q about actions and trigger transitions
                      cwad0000

                      Thanks Ronald, that seems to do the trick.

                      • 8. Re: Q about actions and trigger transitions
                        kukeltje

                        look at the many testcases in the jbpm sourcecode.... it tells you a lot...

                        Ronald