[rules-users] Maps in Drools

KDR dr.soprano at neverbox.com
Tue Aug 18 15:18:36 EDT 2009


Apologies, I should have given the error message I get with $m: Map(
this[$str] == 1 ) and also with $m: Map( this.$str == 1 ) -
org.drools.RuntimeDroolsException: Exception executing predicate this[$str]
== 1
and lots more lines followed by -
Caused by: [Error: unable to resolve method: java.util.HashMap.$str()
[arglength=0]]


KDR wrote:
> 
> Hi, I'm relatively new to both Java and Drools. I'm trying to figure out
> how to use maps in Drools. I've looked at the thread
> http://www.mail-archive.com/rules-users@lists.jboss.org/msg09802.html
> 
> From what I've read generally it seems best to insert objects directly
> rather than use nested accessors. So I've been experimenting with trying
> to insert a map and then checking stuff in it.
> 
> I set up a simple test map of String to Integer, with just "a" as key and
> 1 as value, and "b" with 2.
> Map<String, Integer> map = new HashMap<String, Integer>();
> map.put("a", 1);
> map.put("b", 2);
> String a = "a";
> 		    
> I then inserted the map and also inserted the String a of value "a".
> 
> Here's the test rule, with various things I tried commented out:
> 
> rule "testing maps"
> 	dialect "mvel"
> 	when
> 		$str: String()
> 		// $m: Map( this[$str] == 1 ) # error
> 		// $m: Map( this.$str == 1 )   # error
> 		// $m: Map( this["$str"] == 1 ) # compiles but rule won't fire
> 		$m: Map( this["a"] == 1 ) # this works however!
> 	then
> 		System.out.println($m[$str]); #also works with String and Map objects &
> no conditions
> end
> 
> It obviously doesn't like it when I try to use the String object as the
> key for the map. But it works when I use a String literal as the key. What
> am I doing wrong?
> 
> Does anyone have any suggestions please, or shall I give up and either use
> eval as mentioned in
> http://www.mail-archive.com/rules-users@lists.jboss.org/msg09716.html or
> use the map as a field of another object which I insert instead of the map
> (in fact that was my original plan!)?
> 
> I'd also need to test for null i.e. whether a key/value pair exists for a
> given String as the key.
> 
> Any thoughts would be very much appreciated.
> 
> Many thanks in advance.
> 

-- 
View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25031731.html
Sent from the drools - user mailing list archive at Nabble.com.




More information about the rules-users mailing list