在Drools规则中将对象作为键的Hashmap

时间:2015-08-18 16:11:34

标签: java hashmap drools

为了处理在我的本地数据库中编译的数据,我创建了一个HashMap,其中一个对象作为键,String作为值,包含我需要的数据。

当我将此HashMap和密钥传递给Drools会话时,我发现无法使用该密钥检索所需的值。

这是我得到的错误:

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

这是有问题的规则:

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

有谁知道为什么我会收到此错误?

ETA:我正在使用Drools v5.2.0

1 个答案:

答案 0 :(得分:0)

看起来这有点超出了LHS表达式语法的范围(在任何地方都没有精确定义)。使用此:

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

请注意,您可以将==用于字符串值。