1 2 Previous Next 21 Replies Latest reply on Nov 25, 2008 12:44 PM by Tom Eugelink

    variable scope from a script

    Tom Eugelink Newbie

      I need to be able to set a variable inside a token and not on the root token from within a script. In the variable-tag I cannot specify the scope to which the variable should be written.

        • 1. Re: variable scope from a script
          Tom Eugelink Newbie

          I found this piece of code

          <expression>executionContext.setVariable("decisionID",new
          Integer(10))</expression>


          But when I add "token" as a third parameter, I get an exception stating that there is no third parameter.

          Using JBPM 3.2.3 and the user guide says it should be there, but when I access this ExecutionContext from Java I also only get a two-parameter method.

          • 2. Re: variable scope from a script
            Ronald van Kuijk Master

            which userguide? Can you give me the url you used or tell me whether it is the one that comes with the 'suite'

            • 3. Re: variable scope from a script
              Tom Eugelink Newbie

               

              "kukeltje" wrote:
              which userguide? Can you give me the url you used or tell me whether it is the one that comes with the 'suite'


              It is the one that comes with the suite, but it's also the online version.
              http://docs.jboss.com/jbpm/v3.2/userguide/html_single/
              Paragraph 11.1.

              However, I do see a difference now, 11.1 says:

              The most basic operations are
              
              void ContextInstance.setVariable(String variableName, Object value);
              void ContextInstance.setVariable(String variableName, Object value, Token token);
              Object ContextInstance.getVariable(String variableName);
              Object ContextInstance.getVariable(String variableName, Token token);


              And the script documentation says:

              Also the following script-variables will be available :
              
              executionContext
              token
              node
              task
              taskInstance


              So there is a ContextInstance and an ExecutionContext, that got me confused. What is this ContextInstance if not an ExecutionContext? Anyhow, I see the methods are statics on the ContextInstance, so this should work.

              • 4. Re: variable scope from a script
                Tom Eugelink Newbie

                 

                "tbeernot" wrote:
                So there is a ContextInstance and an ExecutionContext, that got me confused.


                Oh, and naturally the fact that ExectionContext does have a setVariable method (with two parameters).

                • 5. Re: variable scope from a script
                  Tom Eugelink Newbie

                  Right. The methods are present, but they are not static. So:

                  token.getProcessInstance().getContextInstance().setVariable("var","val", token)
                  


                  But that does not give me the behavior I expect.

                  Again I have the simple fork/join flow (start state forking state1 and state2) and each of these states has a node-enter event containing a setVariable as described above, but each setting a different value.

                   S
                   |
                   F val="a"
                   / \
                   val="b" s1 s2 val="c"
                   \ /
                   J
                   |
                   E
                  


                  When the two childern have just forked, I iterate over ALL tokens in the workflow and do a
                  lList.add( lContextInstance.getVariable("var", lToken) );
                  


                  So at the end I would expect three different values in that list.

                  I do have three values, but they are all the same ("b"). Since I get 3x the value associated with a child, it must actually be 3x the value of the root token. Which again means that the setVariable is not actually setting the variable in the token context, but the root token context.

                  Am I missing something?

                  • 6. Re: variable scope from a script
                    Tom Eugelink Newbie

                     

                    <process-definition>
                     <start-state name='start'>
                     <transition to='fork1'></transition>
                     </start-state>
                     <fork name='fork1'>
                     <event type='node-enter'>
                     <script>
                     <expression>
                     token.getProcessInstance().getContextInstance().setVariable("var","valueOfFork", token);"
                     </expression>
                     </script>
                     </event>
                     <transition to='state1' name='1'></transition> // the name is mandatory, otherwise there will be only one child.
                     <transition to='state2' name='2'></transition> // the name is mandatory, otherwise there will be only one child.
                     </fork>
                     <state name='state1'>
                     <event type='node-enter'>
                     <script>
                     <expression>
                     token.getProcessInstance().getContextInstance().setVariable("var","valueOfState1", token);"
                     </expression>
                     </script>
                     </event>
                     <transition to='join1'></transition>
                     </state>
                     <state name='state2'>
                     <event type='node-enter'>
                     <script>
                     <expression>
                     token.getProcessInstance().getContextInstance().setVariable("var","valueOfState2", token);"
                     </expression>
                     </script>
                     </event>
                     <transition to='join1'></transition>
                     </state>
                     <join name='join1'>
                     <transition to='end'></transition>
                     </join>
                     <end-state name='end'></end-state>
                    </process-definition>"
                    


                    • 7. Re: variable scope from a script
                      Ronald van Kuijk Master

                      not sure and no time to experiment for me here, but I'd expect

                      token.getProcessInstance().getContextInstance().setVariable(...)

                      to indeed set it on the root token. So behaviour is as expected.

                      • 8. Re: variable scope from a script
                        Tom Eugelink Newbie

                         

                        "kukeltje" wrote:
                        not sure and no time to experiment for me here, but I'd expect
                        token.getProcessInstance().getContextInstance().setVariable(...)

                        to indeed set it on the root token. So behaviour is as expected.


                        The actual call specifies the token as the 3rd parameter (see posted XML), according to the documentation this should set the variable on the token. So it is not expected.

                        • 9. Re: variable scope from a script
                          Ronald van Kuijk Master

                          sorry... I was probably half asleep when posting... can you check what actually happens in the sourcecode? and also check if the token is different each time.

                          • 10. Re: variable scope from a script
                            Tom Eugelink Newbie

                             

                            "kukeltje" wrote:
                            sorry... I was probably half asleep when posting... can you check what actually happens in the sourcecode? and also check if the token is different each time.


                            My printlns tell me I have three different tokens. I'll fetch the sources tomorrow or maybe tonight and see what is going on.

                            Tom

                            PS: Aan het slapen om 2 uur 's middags? Tssss. ;-)

                            • 11. Re: variable scope from a script
                              Ronald van Kuijk Master

                              ja... was laat gisterenavond.... Ben zelfs nu nog niet helemaal 'wakker' (beetje hoofdpijn als je begrijpt wat ik bedoel)

                              • 12. Re: variable scope from a script
                                Ronald van Kuijk Master

                                btw, the 3.2 docs online are in more ways different from the 3.2.x docs. The 3.2.3 docs are also online but the links to it not always correct

                                • 13. Re: variable scope from a script
                                  Tom Eugelink Newbie

                                   

                                  "kukeltje" wrote:
                                  btw, the 3.2 docs online are in more ways different from the 3.2.x docs. The 3.2.3 docs are also online but the links to it not always correct


                                  Ok. In this case both the docs provided with the distribution as the online ones are in agreement. Plus, the method actually exists and also is executed without any exceptions thrown. So it seems the method really exists.

                                  Although it does seem the code is somewhat complex without a good reason:

                                  token.getProcessInstance().getContextInstance().setVariable("var","valueOfFork", token);


                                  IMHO should be written as:

                                  token.setVariable("var","valueOfFork");


                                  But to reverse the question: how do people normally assign a value to a variable in the token scope form within a script? I assume more people do this.

                                  • 14. Re: variable scope from a script
                                    Ronald van Kuijk Master

                                     

                                    I assume more people do this.


                                    Don't. I personally think not a lot of people do this. I had a quick look at the ContextInstance code today and was a bit confused with some things in there. If I find the time (remember, I do not work for JBoss/RH and only do this in my free time) I'll try to make a unit test without scripting and see what happens then.




                                    1 2 Previous Next