Just to report back. Downloading the mvel2-2.0.14-SNAPSHOT.jar and renaming it to mvel2-2.0.10.jar in my Drools runtime folder (top level and lib) while renaming the old mvel2-2.0.10.jars to something else. got this to work: $m: Map( this[$str] == 1 )
However, although the MVEL page at http://mvel.codehaus.org/MVEL+2.0+Property+Navigation says "For Maps that use a String as a key, you may use another special syntax: user.foobar" I found that $m: Map( this.$str == 1 ) still produces the same error as before. I don't know if this is only because that particular special MVEL syntax is not supported in Drools (obviously it's not a major thing as [$str] works), or because there's still a bug there. Just thought I ought to mention it in case it's a bug. Cheers. 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-tp25031348p25066578.html Sent from the drools - user mailing list archive at Nabble.com. _______________________________________________ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users