7 Replies Latest reply on Dec 21, 2011 11:29 AM by zoikks

    Rules Accessing ArrayList as Process Parameters

    zoikks

      Hello,

       

      I am using jBPM 5.1.0.Final to execute some processes.  I started a process with parameters where one of the parameters is an ArrayList<?> named "itemList".  I'm trying to access the "itemList" from within a rule using the following syntax:

       

      when

           $process : WorkflowProcessInstance()

           $a : ArrayList() from ((ArrayList)$process.getVariable("arrayList"))

      then

           System.out.println("Size: " + $a.size());

       

      However, the condition never seems to be true when I use an ArrayList (I assume the same thing would happen using any object that implements Collection).  I have used other singular objects such as String without a problem and read it properly from the process in the condition portion.  Additionally, the arrayList object is accessible in the consequence as long I don't check for it in the condition. 

       

      Can anyone tell me why the ArrayList cannot be read from the process parameters in the condition portion of a rule?

       

      Are there any options to this scenario other than loading the elements from the ArrayList into working memory and running a "from collect" on them?

       

      I have attached code that demonstrates the issue.

       

      Thanks.

        • 1. Re: Rules Accessing ArrayList as Process Parameters
          cross.cy

          You may instead insert the objects in the arraylist into the knowledge session to be evaluated by rules.

           

          Cheers

           

          Yu

          • 2. Re: Rules Accessing ArrayList as Process Parameters
            zoikks

            Hi Yu,

             

            Thanks for the comment.  It is possible to write the objects into working memory and iterate over them.  The real intent of the question was why can't the ArrayList be accessed directly from the process as a parameter while other objects types can be?

             

            - Darin

            • 3. Re: Rules Accessing ArrayList as Process Parameters
              zoikks

              After a little more thought about this, I'm beginning to think it has to do with the method naming.  For instance, the code I've written calls the size() method.  This isn't a typical property accessor (no "get").  Therefore, it might be the code is executing before the rule fires and checking for size() of an ArrayList (Collection) may simply not be possible.  Can anyone confirm this theory?

               

              Thanks.

              • 4. Re: Rules Accessing ArrayList as Process Parameters
                swiderski.maciej

                Darin, after spending some time on this issue I came up with following (that seems to be working fine):

                 

                package org.zoikks.drools
                
                import java.util.ArrayList;
                import java.util.List;
                import org.drools.runtime.process.WorkflowProcessInstance;
                
                rule "Sample Rule"
                ruleflow-group "testgroup"
                  
                  when
                        $process : WorkflowProcessInstance()
                        $a:  ArrayList()
                              from collect( String() from $process.getVariable("arrayList"))
                  then 
                                    System.out.println("Sample Rule run successfully.");
                                    System.out.println("Count: " + $a.size());
                end
                
                

                 

                Somehow $process.getVariable() returns strings instead of entire array list.

                 

                HTH

                • 5. Re: Rules Accessing ArrayList as Process Parameters
                  zoikks

                  Many thanks - seemed to work great.  I find it strange that $process.getVariable("arrayList") returns strings instead - I assume it would return the data type contained by the ArrayList?

                   

                  I would think the $process.getVariable("xxx") would return an instance of the data type matching the variable.  Is this "working as designed" or is this a bug?

                   

                  Thanks.

                  • 6. Re: Rules Accessing ArrayList as Process Parameters
                    swiderski.maciej

                    I do believe it works as designed. "from clause" is to reason over collection elements so it returns data type that is inside the collection.

                     

                    Alternatively you could try to just check if such process variable exists, for instance with "eval" and in consequence section access the array list (I think that works properly).

                     

                    Cheers

                    • 7. Re: Rules Accessing ArrayList as Process Parameters
                      zoikks

                      Thanks again for the info.  I found it very helpful.