在将标志插入Drools之前检查标志值。

huangapple go评论64阅读模式
英文:

Checking flag value before inserting flag in drools

问题

我已经生成了一条规则,它会在会话中插入一个问题。如果问题为真,它会插入一个FLAG,如果问题不为真,则会删除该问题并且不会更新该FLAG。在插入问题到会话之前,我需要检查FLAG的值。我已经尝试了几种方法来实现这一点,但是没有让Drools执行这个操作。以下是我的规则:

插入问题规则

rule "Threat: ATTACK_OTHER_USERS; insert question"
agenda-group "evaluate attack category"
dialect "mvel"

when
    Threat(this == Threat.ATTACK_OTHER_USERS)
//    $FLAGS(this == FLAGS.PUBLIC_READABLE)   // 我需要在这里进行检查,但现有的方式不起作用
then
    insertLogical(QRiskFactor.QRF1_S4_PUBLIC_READABLE);
end

问题为真

rule "Threat: Public Readable  QRF_1.4 [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
    delete($q1);
    insert(FLAGS.PUBLIC_READABLE);
end

问题为假

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
    delete($q1);
end
英文:

I've produced a rule and it inserts a question in session. If the question is true, it inserts a FLAG and if the question is not true, it deletes the question and doesn't update the flag. I need to check the value of the flag before inserting the question in the session. I have tried several ways to do this but not getting the drools thing to do this. Here are my rules:
Inserting question rule

rule "Threat: ATTACK_OTHER_USERS; insert question"
agenda-group "evaluate attack category"
dialect "mvel"

when
    Threat(this == Threat.ATTACK_OTHER_USERS)
//    $FLAGS(this == FLAGS.PUBLIC_READABLE)   // i need the check here, the existing doesn't work
then

    insertLogical(QRiskFactor.QRF1_S4_PUBLIC_READABLE);
end

question is true

rule "Threat: Public Readable  QRF_1.4 [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
    delete($q1);
    insert(FLAGS.PUBLIC_READABLE);

end

question is false

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
   delete($q1);

end

答案1

得分: 0

你需要在工作内存中检查特定标志的存在性。在你的规则中的注释语法基本正确,除了你似乎多出了一个多余的 $

由于你没有分享FLAGS是什么,所以很难对你的问题做出具体回答。根据你的insert语句的构造方式,我假设它是一个类似于以下枚举的枚举类型:

public enum FLAGS {
  PUBLIC_READABLE,
  // 可能还有其他值
}

因此,如果你想要验证FLAGS.PUBLIC_READABLE是否已经插入到工作内存中,你的规则应该包括:

when
  exists(FLAGS( this == FLAGS.PUBLIC_READABLE ))

我使用exists,因为你没有指示你需要对这个标志做任何操作,所以我只是检查它是否存在。

请注意,insert不会重新执行先前评估过的规则。如果你需要重新评估整个工作内存,你应该使用update


根据评论,以下是如何实现一个简单的“测验”应用程序。用户提供问题的答案;如果用户回答正确,他们将得到下一个问题。如果用户回答错误,游戏就结束了,他们将得到他们回答错误的问题的正确答案。

我使用了一些非常简单的模型:

class Question { 
  private int id;
  private String questionText;
  private String correctAnswer;
  // 获取器和设置器
}

class QuizUtils {
  public static Question showNextQuestion();
  public static void doGameOver(Question questionMissed);
}

用户的答案是直接输入到工作内存中的字符串。

rule "用户回答正确"
when
  // 用户的答案
  $userAnswer: String()
  
  // 当前的问题
  Question( correctAnswer == $userAnswer )
then
  QuizUtils.showNextQuestion();
end

rule "用户回答错误"
when
  $userAnswer: String
  $question: Question (correctAnswer != $userAnswer )
then
  QuizUtils.doGameOver($question);
end
英文:

You need to check for the presence of your specific flag in working memory. The commented out syntax in your rule is nearly correct, except you seem to have an extraneous $ present.

Since you've not shared what FLAGS is, it's a little hard to answer your question specifically. I'm going to assume, based on how you've formulated your insert statement, that it's an enum like this:

public enum FLAGS {
  PUBLIC_READABLE,
  // possibly other values
}

So if you want to verify that a FLAGS.PUBLIC_READABLE has been inserted into working memory, your rule would include:

when
  exists(FLAGS( this == FLAGS.PUBLIC_READABLE ))

I use exists because you've not indicated that you need to do anything with the flag, so I'm just checking for its presence.

Note that insert doesn't re-execute previously evaluated rules. If you need to reevaluate all of working memory, you should use update instead.


Based on comments, here is how you'd implement a simple "quiz" application. A user presents an answer to a question; if the user answers correctly, they are presented with the next question. If the user answers incorrectly, it is "game over" for them and they are given the correct answer to the question they did incorrectly.

I'm using some very simple models:

class Question { 
  private int id;
  private String questionText;
  private String correctAnswer;
  // getters and setters
}
class QuizUtils {
  public static Question showNextQuestion();
  public static void doGameOver(Question questionMissed);
}

The user's answer is a String entered directly into working memory.

rule "User got the question right"
when
  // the user's answer
  $userAnswer: String()
  
  // the current question
  Question( correctAnswer == $userAnswer )
then
  QuizUtils.showNextQuestion();
end

rule "User got the question wrong"
when
  $userAnswer: String
  $question: Question (correctAnswer != $userAnswer )
then
  QuizUtils.doGameOver($question);
end

huangapple
  • 本文由 发表于 2020年8月27日 17:44:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定