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

问题描述

在这样的流口水中有一个定义类型

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); ....}]

我可能做错了什么?我在此存储库中未找到与此相关的任何参考,该书提供了here。任何帮助将非常感激。非常感谢

解决方法

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

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

您试图将'question'属性设置为左侧的$q1变量,但是$q1是一些奇怪的'risk factor'类或类似的东西。可能是一个枚举-您尚未提供规则实际使用的任何模型。根据声明,问题应该是字符串。

// 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的旧版本,尤其是在学习时。官方文档写得非常好,任何一本书出版之时都已经过时了。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...