英文:
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文件中也有一个相同类型的Java类。但我没有将它发送到Drools,因为我不知道在哪里应该为它引用。
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AskedQuestions{
private String question;
private boolean answer;
}
我有许多规则,我试图从定义的类型中设置question
和answer
变量的值,通过以下方法:
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
我必须为定义的类型变量设置值,然后从Java中获取所有插入的对象,并从中创建JSON。
以下是我得到的错误:
Unable to Analyse Expression drools.insert(FlagQuestion.QRF_1_ASK_FLAG);
AskedQuestions $askedQuestions=new AskedQuestions(question:$q1,answer:true);
[Error: unable to resolve method using strict-mode:
org.drools.core.spi.KnowledgeHelper.question()]
[Near : {... =new AskedQuestions(question:$q1,answer:true); ....}]
我可能做错了什么?我在这个仓库和所提供的书籍中都没有找到相关的参考资料。非常感谢您的帮助。
英文:
I have a define type in drools like this
package referee.security.category;
declare AskedQuestions
question : String
answer : Boolean
end
and a Java class for the same type in my java files. It is not sent to drools as I don't know where should I give a reference for this.
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class AskedQuestions{
private String question;
private boolean answer;
}
I have a number of rules and I am trying to set the values to question
and answer
variables from the defined type by following this method.
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);*/
I have to set the values to the defined type variables and then get all the inserted objects in Java and make a JSON from that.
here is the error which I am getting
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); ....}]
What I might be doing wrong? I am not finding any reference to this in this repo and the book provided here. Any help would be really appreciated. Thanks a lot
答案1
得分: 1
我对你提到的那本书不熟悉,但总体而言,我对该出版商的作品质量越来越失望。鉴于你通常使用简单的用例,我无法理解你极其复杂的规则。
对于AskedQuestion类型的声明是正确的,但是你在规则的右侧尝试实例化它的方式是错误的。请参考官方Drools文档。
你试图将“question”属性设置为左侧的$q1
变量,但$q1
可能是某种奇怪的“risk factor”类或类似的东西。它可能是一个枚举值 - 你没有提供规则实际使用的任何模型。根据声明,问题应该是一个字符串。
// 不要导入实际的Java AskedQuestions类
declare AskedQuestions
question : String
answer : Boolean
end
rule "Example rule"
when
$q1: QRiskFactor(this == QRiskFactor.QRF1_S4_PUBLIC_READABLE)
// 其他条件在这里
then
AskedQuestions askedQuestions = new AskedQuestions();
askedQuestions.setQuestion( $q1.toString() ); // 设置问题的值
askedQuestions.setAnswer( false ); // 设置答案的值
// 在这里处理askedQuestions
end
当然,最简单的解决方案就是导入你实际的AskedQuestions Java模型并使用它 - 根据你的问题,不清楚你为什么不这样做。
import com.mycompany.path.to.AskedQuestions;
rule "just using the imported class"
when
// 一些条件
then
AskedQuestions asked = new AskedQuestions( "questions", true );
// 使用AskedQuestions实例进行处理
end
我建议依赖官方文档,而不是一本旧版本Drools的书,尤其是在学习时。官方文档写得非常好,任何书在印刷时都已经极其过时。
英文:
I'm unfamiliar with the book you're referencing, but generally I've been increasingly disappointed with the quality of works from that publisher. Your rules are extremely complicated for reasons I can't fathom given your generally simple use cases.
The declaration of the AskedQuestion type is correct, but the way you're trying to instantiate it on the RHS of your rule is not. Please refer to the official Drools documentation.
You are trying to set your 'question' attribute to the $q1
variable on the left hand side, but $q1
is some weird 'risk factor' class or something like that. It might be an enum -- you haven't provided any models that your rules are actually using. Per the declaration, the question is supposed to be a String.
// 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
Of course, the easiest solution of all would just be to import your actual AskedQuestions Java model and use that -- it's unclear from your question why you're not just doing that.
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
I would recommend relying on the official documentation rather than a book for an old version of Drools, especially when learning. The official docs are extremely well written, and any book is extremely out of date by the time it gets printed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论