Drools retrieves information from a database at runtime

I need a little help with assigning Drools and a variable.

rule "check that no previously submitted requests exist" when $user : UserFormField( name == 'employeeId', value != null ) $repository : Repository( ) $activeRequests : List( ) from $repository.findActiveRequestsByEmployee( $user.getValue() ) # call to repository eval( $activeRequests.size() > 0 ) then System.err.println(' You have active requests: ' + ((Request)$activeRequests.get(0)).getTitle); insert(Boolean.TRUE); end 

In this rule, I try to access the repository and receive active requests for the current user. The rule is compiled and executed without any exceptions or warnings. In debug mode, you can see that the repository does not return an empty list, and I expect to see the console message "You have active requests", but this does not happen. I think the problem is in this line

 $activeRequests : List( ) from $repository.findActiveRequestsByEmployee( $user.getValue() ) 

because this rule works great

 rule "check that no previously submitted requests exist" when $user : UserFormField( name == 'employeeId', value != null ) $repository : Repository( ) eval( $repository.findActiveRequestsByEmployee($user.getValue()).size() > 0 ) then System.err.println(' You have active requests !' ); insert(Boolean.TRUE); end 

So can someone tell me how to solve this problem?

Thanks!

+8
drools
source share
2 answers

They helped me find a solution. I should use the from collect expression instead of the simple from to combine the facts into a collection:

 $activeRequests : ArrayList() from collect ( Request() from $repository.findActiveRequestsByEmployee( $user.getValue() ) ) 
+4
source share

You must distinguish (ie read the fine print in the documentation) between “from” and “from collection”. If you want a rule for individual collection for each element of the collection created by the expression after "from", then use "from". If, however, you want it all bundled in a collection that you should use “from collection”.

 $activeRequests : ArrayList() from collect ( Request() from $repository.findActiveRequestsByEmployee( $user.getValue() ) ) 

Please note that a separate eval is not required. You can put this restriction in an ArrayList template:

 ArrayList( size > 0 ) 
+2
source share

All Articles