使用规则引擎进行验证

发布于 2024-09-24 23:56:25 字数 824 浏览 4 评论 0原文

我们处理基于文本(无 XML)的消息。我们的目标是验证消息,如果内容正确,则消息有效。我们开发了自己的 XML 定义语言来表达消息规则。我们需要添加更复杂的规则,我们认为现在是时候考虑其他替代方案并使用真正的规则引擎了。我们支持这些类型的规则:

  • 值列表中的名称或常规名称 表达式 ex {SMITH, MOORE, A*}
  • 消息中存在名称 -
  • 消息中不存在名称
  • if condition then name = John else name = Jane 请注意,条件很简单,不包含任何逻辑运算符。

我们需要支持这些类型的规则:

  • if then else 但条件包含逻辑运算符
  • for ...loop :
    • 对于消息中的所有客户,我们希望至少有一位来自美国,至少一位来自法国
    • 对于消息中的所有客户,我们希望至少有五个来自美国且每年购买超过 1000 美元的客户
    • 对于任何名为 John 的客户,姓氏必须为 Doe
  • Total 客户,名为 John << 15
  • 公司名称与消息中其他位置的公司名称相同

规则将取决于我们处理的消息类型。因此,我们正在研究几个现有的解决方案,例如:

  • JESS
  • OWL(一致性检查)
  • Schematron(通过转换 XML 中的消息)

考虑到我们用 Java 开发,最好的替代方案是什么?另一件需要考虑的事情是我们应该能够进行错误报告,例如错误描述、错误位置(行号和列号)。

We work with messages that are text-based (no XML). Our goal is to validate the messages, a message is valid if the content is correct. We developed our own language defined in XML to express rules on the message. We need to add more complex rules and we think that it’s now time to look at other alternative and use real rules engine. We support these types of rules:

  • name in a list of values or a regular
    expression ex {SMITH, MOORE, A*}
  • name is present in the message-
  • name is not present in the message
  • if condition then name = John else name = Jane
    Note that the condition is simple and does not contain any logical operators.

We need to support these types of rules:

  • if then else but the condition contains logical operators
  • for ... loop :
    • For all the customers in the message we want at least one from the USA and at least one from France
    • For all the customers in the message we want at least five that are from the USA and are buying more than $1000 a year
    • For any customer with name John, the last name must be Doe
  • Total customers with name John < 15
  • The name of the company is equal to the name of the company in another location in the message

The rules will depend on the type of messages we process. So we were investigating several existing solutions like:

  • JESS
  • OWL (Consistency checking)
  • Schematron (by transforming the message in XML)

What would be the best alternatives considering that we develop in Java? Another thing to consider is that we should be able to do error reporting like error description, error location (line and column number).

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

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

发布评论

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

评论(2

鹿! 2024-10-01 23:56:25

在我看来,你已经走上了正确的道路;我的建议是:

  1. 使用解析器/解释器直接检查基于文本的消息,并对生成的对象应用规则。 @Kdeveloper 建议使用 JavaCC 来生成解析器/解释器,我可以通过个人担保 < a href="http://www.antlr.org/1.1。" rel="nofollow">ANTLRv3 这是一个用 Java(以及其他语言)生成解析器/解释器/转换器的优秀环境。从那里,您可以使用 Jess 或其他一些 Java 规则引擎来验证您生成的对象。您也可以尝试直接将规则编码到解析器/解释器中,但我建议不要这样做,而是选择将规则分开,以将解析和语义验证步骤分开。
  2. 将基于文本的消息转换为 XML 以应用 Schematron 也是另一个可行的选择,但您显然需要解析文本消息以将其转换为 XML。为此,我仍然建议查看 JavaCC 或 ANTLRv3,也许填充一个可以编组为 XML 的预先确定的对象模型(例如可以由 CastorJAXB 来自 W3C XML 架构)。从那里,您可以对生成的 XML 应用 Schematron。
  3. 我认为转换为 OWL 是您建议中最棘手的选择,但可能是最强大的。首先,您可能需要一个本体术语 (TBox)(类、属性、 ETC)。将您的实例数据 (ABox) 映射到。从那时起,一致性检查只能让你走到这一步。您概述的想要捕获的许多类型的约束根本无法用 OWL 表示,也无法单独使用 DL 推理器进行验证。但是,如果您将 OWL 本体与 SWRL 规则(例如)结合起来,您就有机会捕获您概述的大部分规则类型。查看规则类型和SWRL 中可用的内置规则,看看这是否是对你来说足够表达。如果是,您可以使用支持 SWRL 的 DL-Reasoners,例如 PelletHermiT。请注意,诸如此类的 OWL/SWRL 推理器的单独实现可能会或多或少地实现 W3C 规范,因此您需要检查每一个以确定它们的适用性。

It sounds to me like you're on the right track already; my suggestions are:

  1. Inspect your text-based messages directly with a parser/interpreter and apply rules over the generated objects. @Kdeveloper has suggested JavaCC for generating parser/interpreters, and I can add to this by personally vouching for ANTLRv3 which is an excellent environment for generating parser/interpreter/transformers in Java (amongst other languages). From there, you could use Jess or some other Java rules engine to validate the objects you generate. You could possibly also try encoding your rules into a parser/interpreter directly, but I'd advise against this and instead opt for separating the rules out to keep the parsing and semantic validation steps separate.
  2. Transforming your text-based messages to XML to apply Schematron is also another viable option, but you'll obviously need to parse your text messages to get them into XML anyway. For this, I'd still suggest looking at JavaCC or ANTLRv3, and perhaps populating a pre-determined object model which can be marshaled to XML (such as that which can be generated by Castor or JAXB from a W3C XML Schema). From there, you can apply Schematron over the resulting XML.
  3. I'd argue that transforming to OWL the trickiest option of your suggestions, but could be the most powerful. To start with, you'll probably want an ontology terminology (TBox) (the classes, properties, etc). to map your instance data (ABox) into. From there, consistency checking will only get you so far; many of the kinds of constraints you've outlined as wanting to capture simply can't be represented in OWL and validated using a DL-reasoner alone. However, if you couple your OWL ontology with SWRL rules (for example), you have a chance of capturing much of the types of rules you've outlined. Look at the types of rules and built-ins available in SWRL to see if this is expressive enough for you. If it is, you can employ the use of DL-Reasoners with SWRL support such as Pellet or HermiT. Note that individual implementations of OWL/SWRL reasoners such as these may implement more or less of the W3C specification, so you'll need to inspect each to determine their applicability.
时光沙漏 2024-10-01 23:56:25

如果您的规则是静态的(即在编译时已知),您可以使用众所周知的 Java 解析器生成器来实现: JavaCC

If you're rules are static (i.e. known at compile time) you could make this with well known Java parser generator: JavaCC.

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