语法写作初学者指南
我正在工作的应用程序从文件导入中输入大量数据,并相应地更新数据库列。我需要提出一个自定义规则引擎,该引擎将根据验证处理所有输入值并相应地执行数据转换。 Ex
我们应用程序中的字段之一是产品名称。因此,我们需要实现的规则之一是,如果文件中的输入值是小写的,则将产品名称从小写转换为大写。同样,还有许多文本/数学转换需要完成。由于这些原因,我们需要提出自定义规则引擎,在其中为每个属性定义规则,解析它们,然后应用规则。
我确实知道 ANTLR 是 Java
的解析器生成器之一。我正在寻求有关以下问题的建议:
1>有关解析器生成器工作的一般信息以及实现语法的最佳实践。
2>由于我需要完全设计这个规则引擎,任何人都可以向我指出一个可供我参考的示例规则引擎吗?从 UI 到数据库设计。我使用 GWT 进行 UI,使用 Java 进行核心逻辑,使用 Oracle 进行数据库
3>是否还有其他适用于 Java
4> 的 解析器生成器虽然我确实想遵循定义自己的语法并使用解析器生成器来构建此规则引擎的路径,但我还应该考虑其他方法吗?
The application I am working inputs lot of data from file import and updates the database column accordingly. I need to come up with a custom Rule engine that would process all the input values based on validation and perform transformation of data accordingly. E.x.
One of the fields in our application is Product Name. So one of the rules we need to implement is to convert Product name from lower case to upper case, if the input value from the file is in lower case. Similarly, there are many text/mathematical transformations that need to be done. For these reasons, we need to come up with custom rule engine where we define the rules for each attribute, parse them and then apply the rules.
I do know that ANTLR is one of the parser generators around for Java
. I am seeking advice on following queries:
1> General information on working of a parser generator and best practices for implementing grammar.
2> Since I need to design this rule engine completely, can anyone point me to a sample rule engine out there that I can refer to? right from UI to database design. I am using GWT for UI, Java for core logic and oracle for database
3> Are there any other parser generators around for Java
4> Though I do want to follow the path of defining my own grammar and using parser generator to build this rule engine, is there any other approach I should consider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要考虑仅使用 JbossRules(以前称为 Drools),它是一个基于 Java 的规则引擎。或者,脚本引擎可能是实现规则的另一种方式(例如 Apache Rhino(Java 中的 Javascript))。
在这种情况下编写自己的规则似乎有点矫枉过正,但如果最终用户要创建规则/脚本,它可能会让您提供更好的安全保证。
编辑以解决评论中的问题:
我建议使用现有的规则引擎(ala JbossRules/Drools),而不是编写自己的解析器和语法(用于规则组件)。例如,看看这里:Drools。
对于规则可能需要使用的专门逻辑(数据库访问或计算库),您应该编写规则使用的单个 Java API(这样规则就不会深入访问您的其他代码,因为如果/当您重构时,这可能会导致错误) 。无论您使用哪种规则引擎(您自己的或现有的),此建议都适用。
我假设您已经解决了数据输入文件的数据格式,并且您只是在寻找规则格式和规则解析的解决方案。
You might want to consider just using JbossRules (formerly Drools) which is a Java based rules engine. Alternatively, a scripting engine may be another way to implement your rules (e.g. Apache Rhino (Javascript in Java)).
Writing your own in this situation seems like overkill, but it may allow you to provide better security guarantees if end users are going to be creating the rules / scripts.
EDIT to address questions in comments:
I suggest using an existing rules engine (ala JbossRules/Drools) instead of writing your own parser and grammar (for the rule component). Take a look here for instance: Drools.
For specialized logic that rules may need to use (db access or computation libraries) you should write a single Java API used by your rules (so that rules are not deeply accessing your other code since that can lead to bugs if/when you refactor). This advice applies regardless of which rules engine you use (your own or an existing one).
I assume that you already have the data format of your data input files solved and that you are only looking for a solution to the rule format and rule parsing.
有 JavaCC,它是一个解析器生成器,还有用于评估规则的 groovy。是否要使用脚本引擎取决于语法。如果规则无法用 javascript、java、python 等表达,并且您想用新语言编写它们,那么您必须使用解析器生成器。但是您始终可以在您创建的方法中执行任何您想要的操作,然后从规则中调用它们。这些规则将由脚本引擎评估。
There is JavaCC, which is a Parser generator and there is groovy for evaluating rules. If you are going to use a script engine or not depends on the grammar. If the rules can't be expressed in javascript, java, python, etc, and you want to write them in a new language, well then you have to use a parser generator. But you can always do anything you want inside methods that you create and then call them from the rules. The rules will be evaluated by the script engine.