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