<br><font size=2 face="sans-serif">Thanks Edson, worked like a charm!</font>
<br><font size=2 face="sans-serif"><br>
</font><font size=3>Steve Ronderos</font>
<br>
<br><tt><font size=2>rules-users-bounces@lists.jboss.org wrote on 07/16/2009
09:09:31 AM:<br>
<br>
&gt; [image removed] </font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Re: [rules-users] Using variables as map keys in MVEL in LHS</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Edson Tirelli </font></tt>
<br><tt><font size=2>&gt; <br>
&gt; to:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Rules Users List</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; 07/16/2009 09:11 AM</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Sent by:</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; rules-users-bounces@lists.jboss.org</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Please respond to Rules Users List</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; <br>
&gt; &nbsp;&nbsp; This was a bug in one of the MVEL versions that is fixed.
Can you<br>
&gt; update your MVEL jar to one of the latest releases? I think it is
2.0.12.<br>
&gt; <br>
&gt; &nbsp;&nbsp; []s<br>
&gt; &nbsp;&nbsp; Edson<br>
</font></tt>
<br><tt><font size=2>&gt; 2009/7/16 Steve Ronderos &lt;steve.ronderos@ni.com&gt;</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; Hello, <br>
&gt; <br>
&gt; Again, I'm working on converting my project that used Drools 4.0.7
to 5.0.1. <br>
&gt; <br>
&gt; I've run across a problem with a rule that accesses a map in the LHS<br>
&gt; using a variable as the key. &nbsp;When MVEL tries to resolve this,
it <br>
&gt; appears that it is not evaluating the variable before accessing the
<br>
&gt; map. &nbsp;I've modified the Shopping example from the Drools 5.0.1
<br>
&gt; Examples to recreate the issue in a simpler setting. <br>
&gt; <br>
&gt; This is the rule (I removed all the other rules for this example)
<br>
&gt; rule &quot;Cart notification&quot; <br>
&gt; &nbsp; &nbsp; salience 10 <br>
&gt; <br>
&gt; &nbsp; &nbsp; when <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; $p : Product( $productName : name) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; $c : Customer( cart[$productName] == $p)
&nbsp; &nbsp; &nbsp; <br>
&gt; &nbsp; &nbsp; then <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println( &quot;Customer &quot;
+ $c.name + &quot; has &quot; + $p.name<br>
&gt; + &quot; in cart&quot;); <br>
&gt; end &nbsp; <br>
&gt; <br>
&gt; I added the following to the Customer class: <br>
&gt; <br>
&gt; private Map&lt;String,Product&gt; cart = new HashMap&lt;String, Product&gt;();
<br>
&gt; public Map&lt;String, Product&gt; getCart() { <br>
&gt; &nbsp; &nbsp; return cart; <br>
&gt; } <br>
&gt; <br>
&gt; and I modified the main method to be: <br>
&gt; <br>
&gt; public static final void main(String[] args) throws Exception { <br>
&gt; &nbsp; &nbsp; final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.<br>
&gt; newKnowledgeBuilder(); <br>
&gt; &nbsp; &nbsp; kbuilder.add( ResourceFactory.newClassPathResource(
<br>
&gt; &quot;Shopping.drl&quot;, ShoppingExample.class ), <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; ResourceType.DRL ); <br>
&gt; <br>
&gt; &nbsp; &nbsp; final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
<br>
&gt; &nbsp; &nbsp; kbase.addKnowledgePackages( kbuilder.getKnowledgePackages()
); <br>
&gt; <br>
&gt; &nbsp; &nbsp; final StatefulKnowledgeSession ksession = <br>
&gt; kbase.newStatefulKnowledgeSession(); <br>
&gt; <br>
&gt; &nbsp; &nbsp; Customer mark = new Customer( &quot;mark&quot;, <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 ); <br>
&gt; &nbsp; &nbsp; ksession.insert( mark ); <br>
&gt; <br>
&gt; &nbsp; &nbsp; Product shoes = new Product( &quot;shoes&quot;, <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;60 ); <br>
&gt; &nbsp; &nbsp; ksession.insert( shoes ); <br>
&gt; &nbsp; &nbsp; <br>
&gt; &nbsp; &nbsp; mark.getCart().put(&quot;shoes&quot;, shoes); <br>
&gt; &nbsp; &nbsp; <br>
&gt; &nbsp; &nbsp; ksession.fireAllRules(); <br>
&gt; } <br>
&gt; <br>
&gt; So basically, I added a map to Customer, inserted one item and made
<br>
&gt; a rule that will use a variable as a key for that map. &nbsp; <br>
&gt; What I would expect to happen is: <br>
&gt; &nbsp; the Product pattern would match the shoes Product that is inserted<br>
&gt; into the ksession, the $productName variable would contain the value
&quot;shoes&quot; <br>
&gt; &nbsp; the Customer fact, mark, would be matched with the second pattern
<br>
&gt; in the rule because cart[&quot;shoes&quot;] should resolve to the
shoes fact <br>
&gt; which is $p in the rule <br>
&gt; &nbsp; The message should print <br>
&gt; <br>
&gt; What actually happens is an error message: <br>
&gt; <br>
&gt; Exception in thread &quot;main&quot; org.drools.RuntimeDroolsException:
<br>
&gt; Exception executing predicate cart[$productName] == $p <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.rule.PredicateConstraint.isAllowedCachedLeft(<br>
&gt; PredicateConstraint.java:302) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.common.SingleBetaConstraints.isAllowedCachedLeft(<br>
&gt; SingleBetaConstraints.java:138) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.reteoo.JoinNode.assertLeftTuple(JoinNode.java:114)
<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.drools.reteoo.SingleLeftTupleSinkAdapter.doPropagateAssertLeftTuple(<br>
&gt; SingleLeftTupleSinkAdapter.java:117) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.drools.reteoo.SingleLeftTupleSinkAdapter.createAndPropagateAssertLeftTuple<br>
&gt; (SingleLeftTupleSinkAdapter.java:78) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.reteoo.LeftInputAdapterNode.assertObject(<br>
&gt; LeftInputAdapterNode.java:142) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(<br>
&gt; SingleObjectSinkAdapter.java:42) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.reteoo.ObjectTypeNode.assertObject(<br>
&gt; ObjectTypeNode.java:185) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.reteoo.EntryPointNode.assertObject(<br>
&gt; EntryPointNode.java:146) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.common.AbstractWorkingMemory.insert(<br>
&gt; AbstractWorkingMemory.java:1046) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.common.AbstractWorkingMemory.insert(<br>
&gt; AbstractWorkingMemory.java:1001) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.common.AbstractWorkingMemory.insert(<br>
&gt; AbstractWorkingMemory.java:788) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.impl.StatefulKnowledgeSessionImpl.insert(<br>
&gt; StatefulKnowledgeSessionImpl.java:216) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.examples.ShoppingExample.main(ShoppingExample.java:32)
<br>
&gt; Caused by: [Error: unable to resolve method: java.util.HashMap.<br>
&gt; $productName() [arglength=0]] <br>
&gt; [Near : {... Unknown ....}] <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^ <br>
&gt; [Line: 1, Column: 0] <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(<br>
&gt; ReflectiveAccessorOptimizer.java:906) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getBeanProperty(<br>
&gt; ReflectiveAccessorOptimizer.java:585) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(<br>
&gt; ReflectiveAccessorOptimizer.java:313) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(<br>
&gt; ReflectiveAccessorOptimizer.java:138) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:133)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.compiler.ExecutableAccessor.getValue(<br>
&gt; ExecutableAccessor.java:37) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getCollectionProperty<br>
&gt; (ReflectiveAccessorOptimizer.java:633) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(<br>
&gt; ReflectiveAccessorOptimizer.java:319) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at <br>
&gt; org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(<br>
&gt; ReflectiveAccessorOptimizer.java:138) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:133)<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.ast.BinaryOperation.getReducedValueAccelerated(<br>
&gt; BinaryOperation.java:102) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.compiler.CompiledExpression.getValue(<br>
&gt; CompiledExpression.java:104) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.mvel2.MVEL.executeExpression(MVEL.java:978)
<br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.base.mvel.MVELPredicateExpression.evaluate(<br>
&gt; MVELPredicateExpression.java:75) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; at org.drools.rule.PredicateConstraint.isAllowedCachedLeft(<br>
&gt; PredicateConstraint.java:295) <br>
&gt; &nbsp; &nbsp; &nbsp; &nbsp; ... 13 more <br>
&gt; <br>
&gt; Is this a bug in MVEL or Drools, or am I using the map access incorrectly?
<br>
&gt; <br>
&gt; Thanks, <br>
&gt; <br>
&gt; Steve Ronderos<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; rules-users@lists.jboss.org<br>
&gt; </font></tt><a href="http://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>http://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
</font></tt>
<br><tt><font size=2>&gt; <br>
&gt; <br>
&gt; <br>
&gt; -- <br>
&gt; &nbsp;Edson Tirelli<br>
&gt; &nbsp;JBoss Drools Core Development<br>
&gt; &nbsp;JBoss by Red Hat @ </font></tt><a href=www.jboss.com><tt><font size=2>www.jboss.com</font></tt></a><tt><font size=2><br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; rules-users@lists.jboss.org<br>
&gt; </font></tt><a href="http://lists.jboss.org/mailman/listinfo/rules-users"><tt><font size=2>http://lists.jboss.org/mailman/listinfo/rules-users</font></tt></a><tt><font size=2><br>
</font></tt>