布尔逻辑规则评估器
我基本上展示了一项调查,人们回答问题就像测试一样, 并且有不同的路径,到目前为止非常容易,但我想让它更加动态,这样我就可以有一个用于所有路径测试的通用规则,使评估器更容易使用当前我只需允许 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不必很复杂:您已经完成了大部分工作,因为您的 and 元素有效地实现了 AND 类型的规则。 我会介绍一个可以容纳 和 元素的元素。
在您的能力范围内,您可以拥有:
您将实现对其中每一个的评估方法如下:
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:
You would implement the Evaluate method on each of these as follows:
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.
如果您使用支持推理的适当规则引擎,它将更加高效且可扩展。
看看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.
我不确定我完全理解您要解决的问题,但您可以使用简单的 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
我建议将答案放在问题上,而不是使用
true
和false
对问题进行分组。 我认为它使 XML 更易于阅读,但这一点值得商榷。 无可争议的是,它使得独立评估question
元素成为可能,即无需了解您尝试评估它的上下文。 这使得代码更简单。我还会从 XML 架构中获取一个页面,并将 OR 逻辑实现为
choice
元素。 如果choice
元素的任何子元素为 true,则该元素为 true。 当然,您可以嵌套它们:这样您就可以实现四个非常简单的方法,每个方法都由前面的方法使用:
bool IsChoiceCorrect(XmlElement choice)
bool IsRuleSatisfied(XmlElement规则)
XML 的结构使这些方法实现起来非常简单:
可能值得将
List
添加到IsFooCorrect
方法的参数中。 (如果规则引擎位于类中,则可以将其设为类字段。)“当答案错误时,所有方法都将当前元素添加到列表中。” 然后,您可以检查该列表的内容,以准确了解规则失败的原因。I'd suggest putting the answers on the questions, rather than using
true
andfalse
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 aquestion
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. Achoice
element is true if any of its children are true. You can, of course, nest them: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:
It might be worth adding a
List<XmlElement>
to the parameters of theIsFooCorrect
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.