设置值以在规则的一部分中定义 drools 中的类型

setting values to define type in drools in then part of rule

我在 drools 中有一个这样的定义类型

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

和 Java class 用于我的 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。任何帮助将非常感激。非常感谢

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

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

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

// 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,我建议依赖官方文档而不是书籍,尤其是在学习时。官方文档写得非常好,任何一本书在印刷时都已经过时了。