13 Replies Latest reply on Sep 26, 2007 4:48 AM by David Roberts

    How to take a decision in java code

    jean-luct Newbie

      I'm new to jBPM 3.2.1 but I didn't find anything on this subject either in doc/examples and this forum : still my question seems to me very basic.

      I want to be able to decide the transition I will follow in a piece of Java Code according to the data previously entered.

      I'm using a Decision node for this with two transition (Oui and Non).
      I've added an action/"Node Enter". This action map a java class using the field setup.

      My java code :

      public class DecideIfCustomer implements ActionHandler {
       private static final Logger logger = Logger.getLogger(IsCustomer.class);
      
       public DecideIfCustomer() {
       super();
       }
      
       public void execute(ExecutionContext executionContext) throws Exception {
       Node node = executionContext.getNode();
       ContextInstance contextInstance = executionContext.getContextInstance();
       if (contextInstance.getVariable("Nom du contact").equals("Non")) {
       node.leave(executionContext, "Non");
       }
       }
      
      }
      


      Notice that my class is an instance of ActionHandler because I've not been able to use a DecisionHandler. When using a DecisionHandler I got a ClassCastException.

      With this, I'm able to go through the transition Non when my variable is correctly set BUT : I noticed that in fact it takes the Non transition twice and then go to the Oui transition.

      I guess I missed something, anyhelp ?

      My process-definition for the decision node :
      <decision name="Client actif ?">
       <event type="node-enter">
       <action name="isCustomerAcitf" class="fr.jl2tho.tutorialjbpm.action.DecideIfCustomer"></action>
       </event>
       <transition name="Oui" to="Enregistrer la Question"></transition>
       <transition name="Non" to="Prevenir le commercial"></transition>
       </decision>
      


        • 1. Re: How to take a decision in java code
          Ronald van Kuijk Master

          look at the testcases in the source.. clear examples of this.

          • 2. Re: How to take a decision in java code
            jean-luct Newbie

            Can you point to a specific example please, because I've seen this comment on some other topic about decision but I didn't find anything in the examples, docs or javadoc.
            I've only seen so far samples with either rules our simple expression and I haven't been able to adapt it to java code.
            Thanks

            • 3. Re: How to take a decision in java code
              Edward Staub Expert

              You can't leave a node from the node-enter event.
              Instead, use a instead of , and put the actionhandler in the "body" of the node.

              You probably want to figure out how to use a Decision node without a ClassCastException, though - you don't want to write custom actionhandlers for every decision!

              -Ed Staub

              • 4. Re: How to take a decision in java code
                jean-luct Newbie

                Hello Ed, I've tryed moving it to the leave node. My new decision node XML is now :

                <decision name="Client actif ?">
                 <event type="node-leave">
                 <action name="decisionWhenLeaving" class="fr.jl2tho.tutorialjbpm.action.DecideIfCustomer"></action>
                 </event>
                 <transition name="Oui" to="Enregistrer la Question"></transition>
                 <transition name="Non" to="Prevenir le commercial"></transition>
                 </decision>
                


                Now, I got a java.lang.StackOverflowError when I use the Non transition (Non default one). It looks like as if it was looping on the instruction : node.leave(executionContext, "Non");

                Any idea ? Am I using the correct instruction ? I'm lost with all this method that seems to be able to change a transition.

                • 5. Re: How to take a decision in java code
                  Ronald van Kuijk Master

                  you decide in a leave either.... and the TESTCASES are in the SOURCE

                  tip: use a handler, which IS described in the docs http://docs.jboss.com/jbpm/v3.2/userguide/html/processmodelling.html#nodetypedecision

                  • 6. Re: How to take a decision in java code
                    jean-luct Newbie

                    Sorry Ronald but I still don't get your point I went one more time on the Node decision without more understanding on the subject.
                    They talk about DecisionHandler but I've got ClassCastException when using it.

                    From documentation about graph flow, I've understood that event Node-Leave is triggered when doing a node.leave, thus now I understand why I've got a loop.

                    From my understanding about the javadoc for token.leaveNode, it is not possible to change process flow from an ActionHandler attached to an event.
                    I've noticed that when I add an action either Enter, Leave or Before Signal, my ActionHandler is always attached on an event.
                    I don't see any way to add an ActionHandler not attached to an event.

                    You're talking about testcase in Source, I've not seen any testcase in my directory C:\jbpm-jpdl-3.2.1\src. I'm sure I missed something, but you will be very helpful to me if you can provide me with the path of the testcase you are talking about.

                    Thanks

                    • 7. Re: How to take a decision in java code
                      Ronald van Kuijk Master

                      The file is called DecisionHandlerTest.java

                      • 8. Re: How to take a decision in java code
                        jean-luct Newbie

                        Sorry but I didn't find this file on my jbpm-jpdl-suite-3.2.1.zip file. I may have to look for another download.

                        But I made some progress : in fact I've now define my process like this :

                        <decision name="Client actif ?">
                        <handler class="fr.jl2tho.tutorialjbpm.action.IsCustomer" />
                         <transition name="Oui" to="Enregistrer la Question"></transition>
                         <transition name="Non" to="Prevenir le commercial"></transition>
                         </decision>
                        


                        There is no way in using the GUI to do so and the code assist doesn't provide any clue to add the handler tag.

                        My class is now a DecisionHandler :
                        public class IsCustomer implements DecisionHandler {
                         private static final Logger logger = Logger.getLogger(IsCustomer.class);
                         public IsCustomer(){
                         super();
                         }
                        
                         public String decide(ExecutionContext executionContext) throws Exception {
                         String maSortie="Oui";
                         ContextInstance contextInstance = executionContext.getContextInstance();
                         if (contextInstance.getVariable("Nom du contact").equals("Non")) {
                         maSortie="Non";
                         }
                        
                         return maSortie;
                         }
                        
                         public void setConfiguration(String Config){
                        
                         }
                        }
                        


                        Im' now able to go either through the Non or Oui transition.
                        My last problem is that it seems to me that it goes twice through the Non transition.

                        • 9. Solved
                          jean-luct Newbie

                          It was the correct way (the transition Non was followed only once in fact). I've made a coding error later that made my trace be triggered twice.

                          So the correct way of doing a Decision in Java code is to code directly in the Source tab of your processdefinition.xml the following code :


                          <decision name="Client actif ?">
                          <handler class="fr.jl2tho.tutorialjbpm.action.IsCustomer" />
                           <transition name="Oui" to="Enregistrer la Question"></transition>
                           <transition name="Non" to="Prevenir le commercial"></transition>
                           </decision>
                          
                          



                          Where IsCustomer implements org.jbpm.graph.node.DecisionHandler.

                          It seems to me impossible to do it through the GUI ProcessDesigner.

                          • 10. Re: How to take a decision in java code
                            Ronald van Kuijk Master

                            no, this is possible... I wanted to try first, but should have trusted Koens words: "All jpdl language constructs are supported in the gui, no need to use the source tab anymore"

                            Select open the properties tab, select your decisionhandler, select handler in the left tab, choose delegation as configuration type and fill in your class.

                            • 11. Re: How to take a decision in java code
                              jean-luct Newbie

                              Ok I found a way using the Design mode.
                              When on my Decision Node, I can right click on it. I then select in the menu Add Child and then Handler.
                              i can then do on the added handler a Add Attribute -> class.
                              Then I can type my class.

                              Ronald, if you were talking about the Diagram mode : The only proprerties available to me just allow me to enter a new name to my Decision Node.
                              I don't understand what you call "Select open the properties tab" neither "select your decision handler".

                              • 12. Re: How to take a decision in java code
                                David Roberts Apprentice

                                Ronald,

                                What version of the GPD are you using to allow this functionality?
                                I'm using 3.0.13.1, and don't see the decision handler option.

                                • 13. Re: How to take a decision in java code
                                  David Roberts Apprentice

                                  Nevermind, I've read the notes on the 3.1 release, and busy downloading 3.1.0.sp1 now.