0

In order to work on data compiled in my local database, I have created a HashMap with an object as key and a String as value, containing the data that I need.

When I pass this HashMap and the key to the Drools session, I find that I cannot retrieve the required value with that key.

Here's the error that I get:

org.drools.runtime.rule.ConsequenceException: rule: Education
            at org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
            at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:916)
            at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:845)
            at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1056)
            at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:733)
            at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:699)
            at org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:218)
            at com.example.jeanineharb.reasoning.TriggerReceiver.onReceive(TriggerReceiver.java:117)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
            at android.app.ActivityThread.access$1700(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: [Error: unable to resolve method: java.util.HashMap.$k() [arglength=0]]
    [Near : {... this[$k] ....}]
    ^
    [Line: 1, Column: 6]
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1094)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getMethod(ReflectiveAccessorOptimizer.java:1003)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getBeanProperty(ReflectiveAccessorOptimizer.java:693)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:360)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:163)
            at org.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:81)
            at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
            at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
            at org.mvel2.compiler.ExecutableAccessor.getValue(ExecutableAccessor.java:38)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.getCollectionProperty(ReflectiveAccessorOptimizer.java:758)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:366)
            at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:163)
            at org.mvel2.optimizers.dynamic.DynamicOptimizer.optimizeAccessor(DynamicOptimizer.java:81)
            at org.mvel2.ast.ASTNode.optimize(ASTNode.java:159)
            at org.mvel2.ast.ASTNode.getReducedValueAccelerated(ASTNode.java:115)
            at org.mvel2.MVELRuntime.execute(MVELRuntime.java:85)
            at org.mvel2.compiler.CompiledExpression.getDirectValue(CompiledExpression.java:123)
            at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:119)
            at org.mvel2.compiler.CompiledExpression.getValue(CompiledExpression.java:113)
            at org.mvel2.MVEL.executeExpression(MVEL.java:954)
            at org.drools.base.extractors.MVELClassFieldReader.getValue(MVELClassFieldReader.java:153)
            at org.drools.rule.Declaration.getValue(Declaration.java:219)
            at org.drools.base.mvel.MVELCompilationUnit.updateFactory(MVELCompilationUnit.java:358)
            at org.drools.base.mvel.MVELCompilationUnit.getFactory(MVELCompilationUnit.java:282)
            at org.drools.base.mvel.MVELConsequence.evaluate(MVELConsequence.java:79)
            at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:906)
            ... 16 more

Here's the problematic rule:

rule "Education"
    when
        $s : Story( getQuestionContent().equals("") )
        $k : Key( )
        $msgMap : HashMap( $m : this[$k] != null )
    then
        modify ( $s ) {
           setQuestionContent( $m )
        };
end

Does anyone know why do I get this error?

ETA: I'm using Drools v5.2.0

jharb
  • 159
  • 3
  • 16

1 Answers1

0

Looks like this is a tad beyond the scope of the LHS expression syntax (which isn't defined precisely anywhere). Use this:

rule "Education"
when
    $s : Story( questionContent == "" )
    $k : Key()
    $msgMap : HashMap( this[$k] != null )
then
    modify ( $s ) {
       setQuestionContent( $msgMap.get( $k ) )
    };
end

Note that you can use == for string values.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Now the error is gone, but the questionContent is not being set... As for your note, when I use the == operator for strings, I get this error: `java.lang.IncompatibleClassChangeError: The method 'void org.mvel2.asm.MethodVisitor.visitCode()' was expected to be of type interface but instead was found to be of type virtual (declaration of 'java.lang.reflect.ArtMethod' appears in /system/framework/core-libart.jar)` – jharb Aug 19 '15 at 07:39
  • If questionContent isn't being set you have another problem. You should ask another question, adding the Java code to declare, create and insert the facts. – laune Aug 20 '15 at 05:46
  • 1
    As for the IncompatibleClassChangeError, you have yet another problem. Read the javadoc for this error and fix your environment by recompiling everything. – laune Aug 20 '15 at 05:48
  • Thank you for all your help @laune. I will look into it right away. – jharb Aug 20 '15 at 07:48