【问题标题】:setting values to define type in drools in then part of rule设置值以在规则的一部分中定义流口水的类型
【发布时间】:2020-08-31 13:00:36
【问题描述】:

我有一个这样的口水定义类型

package referee.security.category;
declare AskedQuestions
question : String
answer : Boolean
end

和我的 java 文件中相同类型的 Java 类。它没有发送给流口水,因为我不知道我应该在哪里提供参考。

    @Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AskedQuestions{
    private String question;
    private boolean answer;
}

我有许多规则,我正在尝试按照此方法将值设置为定义类型中的 questionanswer 变量。

rule "Threat: Public Readable -- QRF_1.4 [not true]"
agenda-group "evaluate attack category"
dialect "mvel"
when
     $q1: QRiskFactor(this == QRiskFactor.QRF1_S4_PUBLIC_READABLE)
     Application($rf : riskFactors[QRiskFactor.QRF1_S4_PUBLIC_READABLE.value], $rf!.factor != "true")
then
    insert(FlagQuestion.QRF_1_ASK_FLAG);
    AskedQuestions $askedQuestions=new AskedQuestions(question:$q1,answer:true);//if i do this way, I get an error defined below
  /* if i follow this approach, it also throws the error.    
 AskedQuestions $askedQuestions=new AskedQuestions();
  $askedQuestions.setQuestion($q1);
          $askedQuestions.setAnswer(false);
          insert($askedQuestions);*/

我必须将值设置为已定义的类型变量,然后在 Java 中获取所有插入的对象并从中生成 JSON。 这是我得到的错误

Unable to Analyse Expression drools.insert(FlagQuestion.QRF_1_ASK_FLAG);
    AskedQuestions $askedQuestions=new AskedQuestions(question:$q1,answer:true);
  /*     $askedQuestions.setQuestion($q1);
          $askedQuestions.setAnswer(false);
          insert($askedQuestions);*/;
 [Error: unable to resolve method using strict-mode: 
 org.drools.core.spi.KnowledgeHelper.question()]
 [Near : {... =new AskedQuestions(question:$q1,answer:true); ....}]

我可能做错了什么?我在这个 repo 中没有找到任何对此的引用,并且本书提供了here。任何帮助将非常感激。非常感谢

【问题讨论】:

  • 为什么要在 Drools 中重新定义模型?为什么不直接导入您的 Java 类并使用它呢?

标签: java drools rules


【解决方案1】:

我不熟悉您所引用的书,但总的来说,我对该出版商的作品质量越来越失望。鉴于您通常很简单的用例,由于我无法理解的原因,您的规则非常复杂。

AskedQuestion 类型的声明是正确的,但您尝试在规则的 RHS 上实例化它的方式不正确。请参考official Drools documentation

您正在尝试将您的“问题”属性设置为左侧的$q1 变量,但$q1 是一些奇怪的“风险因素”类或类似的东西。它可能是一个枚举——您没有提供您的规则实际使用的任何模型。根据声明,问题应该是一个字符串。

// do not import your actual Java AskedQuestions class 

declare AskedQuestions
  question : String
  answer : Boolean
end

rule "Example rule"
when
  $q1: QRiskFactor(this == QRiskFactor.QRF1_S4_PUBLIC_READABLE)
  // other conditions here
then
  AskedQuestions askedQuestions = new AskedQuestions();
  askedQuestions.setQuestion( $q1.toString() ); // set question value
  askedQuestions.setAnswer( false ); // set answer value
  // do something with askedQuestions here
end

当然,最简单的解决方案就是导入您的实际 AskedQuestions Java 模型并使用它——从您的问题中不清楚您为什么不这样做。

import com.mycompany.path.to.AskedQuestions;

rule "just using the imported class"
when
  // some conditions
then
  AskedQuestions asked = new AskedQuestions( "questions", true );
  // do stuff with the AskedQuestions instance
end

我建议依赖官方文档而不是旧版本的 Drools 书籍,尤其是在学习时。官方文档写得非常好,任何一本书在印刷时都已经过时了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多