0

I'm trying to create a rule on Drools. My current code is:

rule "Test Rule #1"
when
 obj1 : MyObject ( )
 map : ArrayList (size < 1) from collect ( HashMap ( values ( House ( windows = 3) ) ) ) from obj1.getHouses()
then
  // Do something
end

What I am trying to do is: check if there are any Houses with 3 windows in the HashMap. If there aren't // Do something. To do that I'm trying to access obj1.getHouses() which is an HashMap < String, List < Houses>>. Then I'm checking this HashMap values, and filter it for Houses that have 3 windows.

But there is something wrong with this code. I'm getting this error:

Unable to Analyse Expression values ( House ( windows = 3) ): java.lang.Class cannot be cast to org.mvel2.util.MethodStub]

Can someone help? Thanks in advance.

  • How is you HashMap is look like? The map values is accessed by this syntax [http://stackoverflow.com/a/9095266/3710490](http://stackoverflow.com/a/9095266/3710490) – Valijon Jan 06 '16 at 15:25
  • My Map is: HashMap < String, LinkedList < Houses > >. But I don't want to search for a specific key. I need to search on all the LinkedLists of the Map. That link shows me how to search with a key. – Maria Pedro Sales Jan 06 '16 at 15:29
  • Can you post full drools `.drl` file? Seems some class imports is wrong... – Valijon Jan 06 '16 at 15:43

1 Answers1

0
rule "find houses"
when
  obj1: MyObject()
  $fl: List() 
    from accumulate( $l: List(), 
        init( List flat = new ArrayList(); ),
        action( flat.addAll( $l ); ),
        result( flat ) )
      from obj1.getHouses().values()
  not House( windows == 3 ) from $fl
then
  //... $h is a house with three windows
end

You need to unravel the Lists that are values in your hash map. Then you can inspect the resulting list for houses with three windows.

As you didn't provide all the code to set up a demo, this rule is untested.

laune
  • 31,114
  • 3
  • 29
  • 42