在 Java 上使用 DSL 的第一步?

发布于 2024-09-10 13:40:35 字数 137 浏览 4 评论 0原文

伙计们……女孩们,我正在开发一个项目,我认为可以通过实施领域特定语言来为某种类型的工作流程定义一组规则和/或条件来增强该项目。

我想牢牢掌握主题、基础知识、最佳实践等,特别是如何用 Java 以某种方式实现它们。

你有什么建议?

Guys... Girls, I'm working on a project which I think could be enhanced by implementing a Domain Specific Language for defining a set of rules and/or conditions for some type of work-flow.

I want to get a firm grasp on the subject, fundamentals, best practices, etc. specially how to implement them somehow with Java.

What do you suggest?

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

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

发布评论

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

评论(4

断念 2024-09-17 13:40:35

首先,我建议阅读 Kernighan 的 编程实践 的第 9 章(符号)和派克。

完成此操作后,请带着有关如何将本章中的概念映射到您想要解决的问题的具体设计的具体问题回到这里。

基本模式是编写一个解释器,传递一个“命令”参数,可能还传递一个“环境”参数并执行命令(在环境中)。然后,您可以选择编写一个解析器,它接受“脚本”字符串并将其转换为有效的“命令”对象(即外部 DSL);或者您提供一个库来帮助用户使用您正在使用的相同语言(内部 DSL)显式构建“命令”对象。

克尼根和派克很好地展示了口译员的琐碎和复杂。如果您想要更深入的了解,那么我建议您阅读编程语言的要点丹尼尔·弗里德曼等人。每章至少构建一个不同的解释器,并演示如何实现变量、函数、作用域、对象、类、静态类型和延续等功能。

不过,我建议您先尝试一下简单的 DSL,否则这只是理论——当一本书因您以前的经验而变得相关且实用时,它会更有趣。

First I would recommend reading chapter 9 (Notation) of The Practice of Programming by Kernighan and Pike.

When you have done that, come back here with specific questions on how to map the concepts in that chapter to specific designs for the problems you want to solve.

The basic pattern is to write an interpreter that is passed a 'command' argument, and possibly an 'environment' argument and executes the command (in the environment). You then have the option of writing a parser, that takes a 'script' string and converts it into a valid 'command' object (ie. an external-DSL); or you provide a library to help users build the 'command' object explicitly in the same language you are using (internal-DSL).

Kernighan and Pike do a good job of showing both how trivial and how complex an interpreter can be. If you want more depth, then I would suggest reading The Essentials of Programming Languages by Daniel Friedman et al. Which builds at least one different interpreter per chapter, and demonstrates how to implement features such as variables, functions, scopes, objects, classes, static-typing, and continuations.

However I would suggest trying your hand at a trivial DSL first, otherwise it's all just theory—a book is much more interesting when it is made relevant and practical by your previous experience.

七月上 2024-09-17 13:40:35

正如其他人评论的那样,Java 确实不是创建 DSL 的最佳选择。 Scala、Clojure、Groovy、Ruby/JRuby 都是不错的选择。然而,考虑到您正在考虑使用 Java,我认为 Groovy 或 Scala 似乎是最自然的选择。对于这两种语言,Java 开发人员的学习曲线都是相当渐进的。以下是一些可以帮助您入门的链接:

As others have commented, Java really isn't a great choice for creating a DSL. Scala, Clojure, Groovy, Ruby/JRuby would all be great choices. However, considering you were thinking about using Java, I think Groovy or Scala seem like the most natural choices. The learning curve for java developers is quite gradual for both languages. Here are some links that will get you started:

迟月 2024-09-17 13:40:35

有一次我使用 openArchitectureware 来定义和使用 DSL。 oAW 是 eclipse 的插件,现在是 eclipse 建模框架的一部分,但当然它也可以在 EMF 之外使用。

我喜欢它,因为定义 DSL 相当容易,并且 oAW 会自动生成一个编辑器,具有语法突出显示和 DSL 错误检查功能。

如果您打算使用 DSL 编写的文档来自动生成 Java、XML 或其他文件,它还提供了一个非常方便的模板引擎。

(我已经链接了旧的 oAW URL,因为该页面仍然提供了一些详细信息以及指向 Eclipse 项目页面的所有链接)

Once I used openArchitectureware to define and use a DSL. oAW is a plugin to eclipse and now part of the eclipse modeling framework, but of course it can be used outside EMF too.

I liked it because it was fairly easy to define a DSL and oAW will automatically generate an editor with syntacx highlighting and error checking for the DSL.

And it provides a template engine that is pretty comfortable if you plan to use documents written in your DSL to autogenerate Java, XML or other files.

(I've linked the old oAW URL because the page still provides some details and all links to the eclipse project pages)

我的黑色迷你裙 2024-09-17 13:40:35

只是@Recurse 的补充。我实际上正在做他提到的第 9 章中的正则表达式示例,直到我更改了主函数中的一行:

            if (grep(argv[1], f, argc>3 ? argv[i] : NULL) > 0)

实际上应该是:

            if (grep(argv[1], f, argc>2 ? argv[i] : NULL) > 0)

注意 argc>2。之后它对我有用。书上没有勘误表(考虑到它的年代,这并不奇怪)。

是的,这是一本内容丰富但又蕴藏丰富的书,所以我不得不说我同意@Recurse。老实说,这对于学生来说是一本时间敏感的书(几年前我可能会感到困惑)。

Just an addition to @Recurse. I'm actually doing the regex example in chapter 9 he was referring to and it didn't work until I changed a line in the main function:

            if (grep(argv[1], f, argc>3 ? argv[i] : NULL) > 0)

should really be:

            if (grep(argv[1], f, argc>2 ? argv[i] : NULL) > 0)

notice the argc>2. It worked for me after that. Wasn't on the books errata (which is not surprising given it's age).

Yeah, this is a dense but treasure trove of a book so I have to say I agree with @Recurse. Honestly, it's a time sensitive book for the student (I probably would have been confused a couple of years ago).

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