KDR schrieb:
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!)?
In exactly this thread Marc answered to that problem.
http://www.mail-archive.com/rules-users@lists.jboss.org/msg09837.html
His first idea is that this is a bug in the mvel dialect.
I tried exactly that. I had a global var and put the string "a" into it.
Then I wanted to check if Map( this[myVar] == 1 ) but this didn't work.
I'd also need to test for null i.e. whether a key/value pair
exists for a
given String as the key.
This seems to be only true when you go the route that I go, namely using
the default rule syntax, i.e., eval.
Sunny greetings,
André
--
Lisp is not dead. It’s just the URL that has changed:
http://clojure.org/