布尔逻辑规则评估器

发布于 2024-07-08 18:48:28 字数 838 浏览 7 评论 0原文

我基本上展示了一项调查,人们回答问题就像测试一样, 并且有不同的路径,到目前为止非常容易,但我想让它更加动态,这样我就可以有一个用于所有路径测试的通用规则,使评估器更容易使用当前我只需允许 AND,每个 OR 本质上成为集合中的另一个规则

QuestionID,然后我形成一堆 AND 规则,如下所示 <代码>

<rule id="1">
<true>
 <question ID=123>
 <question ID=124>
</true>
<false>
 <question ID=127>
 <question ID=128>
</false>
</rule>
<rule id="2"><true>
 <question ID=123>
 <question ID=125>
</true>
<false>
 <question ID=127>
</false>
</rule>

该规则 1 规定,如果问题 123 和 124 的回答为真,而问题 127、128 的回答为假,则通过。 OR(规则 2)是,如果 123 和 125 为真且 127 为假,它们也会通过。 如果有很多组合,这会变得很乏味,所以我想在逻辑中实现 OR,我只是不确定解决这个问题的最佳方法是什么。

我认为规则引擎太复杂了,必须有一种更简单的方法,也许可以像 LINQ 一样构建一个图,然后评估它们是否通过,

谢谢!

——不是计算机科学专业。

I have essentially a survey that is shown, and people answer questions a lot like a test,
and there are different paths, it is pretty easy so far, but i wanted to make it more dynamic, so that i can have a generic rule that is for the test with all the paths, to make the evaluator easier to work with currently i just allow AND's, and each OR essentially becomes another Rule in the set,

QuestionID, then i form a bunch of AND rules like so

<rule id="1">
<true>
 <question ID=123>
 <question ID=124>
</true>
<false>
 <question ID=127>
 <question ID=128>
</false>
</rule>
<rule id="2"><true>
 <question ID=123>
 <question ID=125>
</true>
<false>
 <question ID=127>
</false>
</rule>

this rule 1 says if question 123, and 124 are answered true, and 127, 128 are false, they pass. OR (rule 2) is if 123 and 125 are true and 127 is false, they pass as well.
This gets tedious if there are many combinations, so i want to implement OR in the logic, I am just not sure what best approach is for this problem.

I think rules engine is too complicated, there must be an easier way, perhaps constructing a graph like in LINQ and then evaluating to see if they pass,

thanks!

--not an compsci major.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-07-15 18:48:28

这不必很复杂:您已经完成了大部分工作,因为您的 and 元素有效地实现了 AND 类型的规则。 我会介绍一个可以容纳 和 元素的元素。

在您的能力范围内,您可以拥有:

  • 一个 RuleBase 类,带有“公共抽象 bool Evaluate()”方法
  • TrueRule、FalseRule 和 OrRule 类,其中包含 RuleBase 对象列表
  • 一个 QuestionRule 类,它引用一个特定问题

您将实现对其中每一个的评估方法如下:

  • TrueRule: 仅当所有包含的规则从 评估中返回 true 时才返回 true
  • FalseRule: 仅当所有包含的规则从 中返回 false 时才返回 true Evaluate
  • 如果至少有一个包含的规则从 Evaluate
  • QuestionRule: 返回原始问题的答案,则

OrRule: 返回 true该类层次结构实现了一个简单的抽象语法树 (谷草转氨酶)。 LINQ 以 System.Expressions.Expression 类的形式执行几乎相同的操作,但如果所有内容如何组合在一起并不明显,那么编写自己的代码会很有帮助。

This doesn't have to be complicated: you're most of the way already, since your and elements effectively implement an AND-type rule. I would introduce an element that can hold and elements.

In your could, you could have:

  • A RuleBase class, with a "public abstract bool Evaluate()" method
  • TrueRule, FalseRule and OrRule classes, which contain lists of RuleBase objects
  • A QuestionRule class, which refers to a specific question

You would implement the Evaluate method on each of these as follows:

  • TrueRule: returns true only if all the contained rules return true from Evaluate
  • FalseRule: returns true only if all the contained rules return false from Evaluate
  • OrRule: returns true if at least one of the contained rules returns true from Evaluate
  • QuestionRule: returns the answer to the original question

This class hierarchy implements a simple abstract syntax tree (AST). LINQ, in the form of the System.Expressions.Expression class, does pretty much the same thing, but it's helpful to write your own if it's not obvious how everything fits together.

眼趣 2024-07-15 18:48:28

如果您使用支持推理的适当规则引擎,它将更加高效且可扩展。

看看http://www.flexrule.com,它是一个灵活的、可扩展的规则引擎,支持三种类型的规则。 程序、推理和规则流规则可以从您的应用程序外部化,并使用此框架执行。

If you use a proper Rule Engine that supports inferencing it would be more efficient and extensible.

Take a look at http://www.flexrule.com which is a flexible, extensible rule engine that supports three types of rule. Procedural, Inference and Rule-Flow rules can be externalized from your application and get executed using this framework.

我很OK 2024-07-15 18:48:28

我不确定我完全理解您要解决的问题,但您可以使用简单的 XPath 来获取 ID:

这将为您提供规则 ID = 1 的所有“真实”ID:
/rule[@id="1"]/true//@ID

与上面相同,只是它为您提供了错误的 ID:
/rule[@id="1"]/false//@ID

最后一个 .NET 中 XPath 简介的链接
http://www.developer.com/xml/article.php/3383961

祝你好运

I'm not sure I totally understand the problem you are trying to solve but you could use a simple XPath to get at the ID's:

This would give you all of the "true" ID's where the rule ID = 1:
/rule[@id="1"]/true//@ID

Same as above only it gives you the false ID's:
/rule[@id="1"]/false//@ID

Lastly a link to an introduction to XPath in .NET
http://www.developer.com/xml/article.php/3383961

Good Luck

岁月打碎记忆 2024-07-15 18:48:28

我建议将答案放在问题上,而不是使用 truefalse 对问题进行分组。 我认为它使 XML 更易于阅读,但这一点值得商榷。 无可争议的是,它使得独立评估 question 元素成为可能,即无需了解您尝试评估它的上下文。 这使得代码更简单。

我还会从 XML 架构中获取一个页面,并将 OR 逻辑实现为 choice 元素。 如果 choice 元素的任何子元素为 true,则该元素为 true。 当然,您可以嵌套它们:

<rule id="1">
   <question id="123" answer="true" />
   <question id="124" answer="false" />
   <choice id="1">
      <question id="125" answer='true' />
      <choice id="2">
         <question id="126" answer='false' />
         <question id="127" answer='false' />
      </choice>
   </choice>
</rule>

这样您就可以实现四个非常简单的方法,每个方法都由前面的方法使用:

  • bool GetProvidedAnswer(int QuestionID)
  • bool IsQuestionCorrect( XmlElement 问题)
  • bool IsChoiceCorrect(XmlElement choice)
  • bool IsRuleSatisfied(XmlElement规则)

XML 的结构使这些方法实现起来非常简单:

 bool IsRuleSatisfied(XmlElement rule)
 {
    bool satisfied = true;
    foreach (XmlElement child in rule.SelectNodes("*"))
    {
       if (child.Name == "question")
       {
          satisfied = satisfied && IsQuestionCorrect(child);
       }
       if (child.Name == "choice")
       {
          satisfed = satisfied && IsChoiceCorrect(child);
       }
       if (!satisfied)
       {
          return false;
       }
   }
   return true;
}

可能值得将 List 添加到 IsFooCorrect 方法的参数中。 (如果规则引擎位于类中,则可以将其设为类字段。)“当答案错误时,所有方法都将当前元素添加到列表中。” 然后,您可以检查该列表的内容,以准确了解规则失败的原因。

I'd suggest putting the answers on the questions, rather than using true and false to group the questions. I think that it makes for XML that's easier to read, which is debatable. What's not debatable is that it makes it possible to evaluate a question element independently, i.e. without any knowledge of the context in which you're trying to evaluate it. That makes for simpler code.

I'd also take a page from XML Schema and implement your OR logic as a choice element. A choice element is true if any of its children are true. You can, of course, nest them:

<rule id="1">
   <question id="123" answer="true" />
   <question id="124" answer="false" />
   <choice id="1">
      <question id="125" answer='true' />
      <choice id="2">
         <question id="126" answer='false' />
         <question id="127" answer='false' />
      </choice>
   </choice>
</rule>

This leaves you with four pretty simple methods to implement, each of which is used by the one preceding it:

  • bool GetProvidedAnswer(int questionID)
  • bool IsQuestionCorrect(XmlElement question)
  • bool IsChoiceCorrect(XmlElement choice)
  • bool IsRuleSatisfied(XmlElement rule)

The structure of the XML makes these methods pretty simple to implement:

 bool IsRuleSatisfied(XmlElement rule)
 {
    bool satisfied = true;
    foreach (XmlElement child in rule.SelectNodes("*"))
    {
       if (child.Name == "question")
       {
          satisfied = satisfied && IsQuestionCorrect(child);
       }
       if (child.Name == "choice")
       {
          satisfed = satisfied && IsChoiceCorrect(child);
       }
       if (!satisfied)
       {
          return false;
       }
   }
   return true;
}

It might be worth adding a List<XmlElement> to the parameters of the IsFooCorrect methods. (If the rule engine is in a class, you could make it a class field.) Make `all of the methods add the current element to the list when an answer's wrong. You can then examine the contents of that list to know exactly why a rule failed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文