【问题标题】:Get expression value from drools从drools获取表达式值
【发布时间】:2022-01-25 09:34:04
【问题描述】:

我想从drools中获取表达式值。这样的drl。

global java.util.Map $globalVar;
global java.lang.Object $result;

import java.util.Map;

function myFunc(Map map){
    return "hello";
}

rule "onlyone"
when
    $factMap: Map($tmpResult:(myFunc($globalVar)), eval(true))
then
    $result = $tmpResult;
end

当规则出现异常时,像这样

原因:org.mvel2.PropertyAccessException:[无法解析方法:java.util.Map.$globalVar() [argslength=0]]

全局变量 $globarVar 与事实无关。

很可能drools无法识别全局变量。我的drl有语法错误吗?

【问题讨论】:

  • 您的函数缺少返回类型。而且你不能像那样使用eval(true)
  • 其实你到底想做什么,Map( $tmpResults: ... ) 的事情完全没有意义。

标签: drools


【解决方案1】:

您的语法太错误了,我什至无法弄清楚您要做什么。

函数非常简单,它们就像 Java 中的方法。它们甚至看起来像 Java 中的方法——你的方法缺少返回类型。

function String myFunc(Map map) {
  return "hello";
}

function String greeting(String name) {
  return "Hello, " + name + ". How are you?"
}

您可以像调用任何其他函数一样在规则中调用它们,无论是在左侧(“when”)还是在右侧(“then”)。

rule "Apply Greeting and Send Memo"
when
  $memo: Memo( $recipient: recipient, sent == false )
  $greeting: String() from greeting($recipient)
then
  MemoSender.send($greeting, $memo);
  modify( $memo ) {
    setSent(true)
  };
end

rule "Apply Greeting and Send Memo - v2"
when
  $memo: Memo( $recipient: recipient, sent == false )
then
  String $greeting = greeting($recipient);
  MemoSender.send( $greeting, $memo );
  modify( $memo ) {
    setSent(true)
  }
end

您的问题中带有全局变量的部分是红鲱鱼。是的,它们没有工作并抛出错误,但语法非常错误,以至于我不确定它应该如何工作。

可能与函数中的全局交互的一种方式可能类似于...

global Float taxRate; // passed in as a global because it's an external constant value
global Float freeShippingThreshold;

function float calculateTotal(float subtotal) {
  // some math here that references taxRate
  return calculatedTotal
}

rule "Apply free shipping"
when
  Cart( $subtotal: subtotal )
  Float( this >= freeShippingThreshold) from calculateTotal($subtotal)
then
  // you get free shipping
end

【讨论】:

  • 对不起,我是新来的流口水。我想做的只是从 lhs 获取温度值。例如 。当 Float(calculateTotal(subtotal) > 3) 时,我想得到calculateTotal(subtotal) 的值。并将其赋予全局变量。
  • 所以在右手边做。但是您不能依赖左侧更新的全局变量。但如果你只是想让它成为你的“输出”,那很好——这是从规则中获取数据的老式方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-30
  • 2011-04-18
相关资源
最近更新 更多