属性-rule drool文件中的drools.getRule()。getName()

时间:2015-01-27 07:43:50

标签: java jboss drools mvel jboss-rules

我需要根据来自后端的数据启用/禁用drl文件中的某些规则。 即启用迪拜规则1,2,3,新加坡启用1,3 所以,我传递drools.getRule()。getName(),如下所示。

rule "Apply 50% discount to VIP customers"
    enabled (checkenabled(drools.getRule().getName()))
when
    $s : Sale( customer.type == CustomerType.VIP )  
then
$s.setDiscount(0.50);
    System.out.println("VIP discount applied1" + drools.getRule().getName());
end

我在这方面遇到了错误。

Exception in thread "main" [Error: null pointer: drools.rule.name]
[Near : {... checkenabled(drools.rule.name) ....}]
                          ^
[Line: 1, Column: 14]
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:427)
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:140)
    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.getMethod(ReflectiveAccessorOptimizer.java:948)
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:373)
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.optimizeAccessor(ReflectiveAccessorOptimizer.java:140)
    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:86)
    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:930)
    at org.drools.core.base.mvel.MVELEnabledExpression.getValue(MVELEnabledExpression.java:92)
    at org.drools.core.definitions.rule.impl.RuleImpl.isEffective(RuleImpl.java:384)
    at org.drools.core.phreak.RuleExecutor.cancelAndContinue(RuleExecutor.java:326)
    at org.drools.core.phreak.RuleExecutor.fire(RuleExecutor.java:142)
    at org.drools.core.phreak.RuleExecutor.evaluateNetworkAndFire(RuleExecutor.java:94)
    at org.drools.core.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:964)
    at org.drools.core.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1234)
    at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1239)
    at org.drools.core.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:1212)
    at com.emirates.App.testGoodCustomer(App.java:95)
    at com.emirates.App.main(App.java:74)
Caused by: java.lang.NullPointerException
    at org.mvel2.optimizers.impl.refl.ReflectiveAccessorOptimizer.compileGetChain(ReflectiveAccessorOptimizer.java:393)
    ... 25 more

2 个答案:

答案 0 :(得分:0)

为什么不使用 AgendaFilter

非常容易使用,可以在 Java 部分完成,如下所示:

AgendaFilter myFilter = new RuleNameStartsWithAgendaFilter("DUBAI");
mySession.fireAllRules(filter);

myFilter = new RuleNameStartsWithAgendaFilter("SINGAPORE");
mySession.fireAllRules(filter);

AgendaFilter 有多种变体:

  • StartsWith
  • 的endsWith
  • 包含
  • 匹配

我不完全是你所需要的,但它可能是一个提示; - )

答案 1 :(得分:0)

对于"声明性议程来说,这似乎是一个很好的例子。特征。这基于用于阻止规则的规则。这是一个简单的例子:

import org.kie.api.runtime.rule.Match;
rule "Apply 50% discount"
  @Eager                     // required for each rule to be blocked
  @Category("Singapore")
when
  $s : Sale( article matches ".*PX" ) // anything
then
  System.out.println("VIP: " + drools.getRule().getName());
end

rule "CheckBlock"
  @Direct @Eager // required for each rule that blocks
when
  Location( $country: country )
  // $m: Match( Category == "Singapore" )
  $m: Match( rule.name == "Apply 50% discount" )
then
  kcontext.blockMatch( $m );
end

位置是一个简单的bean。如图所示,阻止规则可以访问"匹配" (神奇地)允许您与元数据或规则名称匹配的对象。当然,评估可以访问位置中的某些方法,以便对规则名称进行更复杂的检查。

请注意,您需要设置一个选项:

 KieBaseConfiguration config = ks.newKieBaseConfiguration();
 config.setOption( DeclarativeAgendaOption.ENABLED );

或使用kmodule XML(参见文档)。