JavaScript 中的规则引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7661322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rule Engine in JavaScript
提问by Pradyumna
Is there any Rule engine in JavaScript?
JavaScript 中有规则引擎吗?
The question is in this context:
问题是在这种情况下:
- Consider a web application having a form that users fill up.
- As a user fills up each field and proceeds to the next, business logic written in JavaScript controls the visibility(and other attributes) of form elements further down the page.
The same business logic is also applied at the server side after the form gets submitted, albeit, in Java to guard against any mishaps/manipulations at the browser side.
Now, Wouldn't it be nice if we have a JSR 94/Drools/JRules like rule engine that would execute rules in both Java and in JavaScript? With such a rule engine, I can avoid hard coding my rules, and I also retain the flexibility of having client-side as well as server-side validation
- 考虑一个具有用户填写的表单的 Web 应用程序。
- 当用户填写每个字段并继续下一个时,用 JavaScript 编写的业务逻辑控制页面下方表单元素的可见性(和其他属性)。
提交表单后,同样的业务逻辑也应用于服务器端,尽管是在 Java 中,以防止浏览器端的任何事故/操作。
现在,如果我们有一个 JSR 94/Drools/JRules 之类的规则引擎,可以在 Java 和 JavaScript 中执行规则,那不是很好吗?使用这样的规则引擎,我可以避免对规则进行硬编码,并且还保留了客户端和服务器端验证的灵活性
(PS: I've tried the AJAX route and seen that the application becomes a lot less responsive, making it hard to sell to users who've been accustomed to a hand-coded, pure-javascript version.)
(PS:我尝试了 AJAX 路线,发现该应用程序的响应速度变慢了,因此很难向习惯了手动编码的纯 JavaScript 版本的用户销售。)
采纳答案by duffymo
JSR-94 is a Java specification, so I don't see what it has to do with the browser.
JSR-94 是 Java 规范,所以我看不出它与浏览器有什么关系。
There's Google JSON rules:
有谷歌 JSON 规则:
http://code.google.com/p/jsonrules/
http://code.google.com/p/jsonrules/
You said you tried the AJAX route. Does that mean a rules engine running on the server and an asynch call to access it?
你说你试过 AJAX 路线。这是否意味着在服务器上运行规则引擎和访问它的异步调用?
回答by Josef.B
This is a valid question. From this article, JSR 94
这是一个有效的问题。从这篇文章,JSR 94
does not standardize the following: The rule engine itself The execution flow for rules The language used to describe the rules The deployment mechanism for Java EE technology
没有标准化以下内容: 规则引擎本身 规则的执行流程 用于描述规则的语言 Java EE 技术的部署机制
Thus, it may be possible to use a DSL that could be executed on the client and server, and this could be developed, executed, and managed as per the JSR 94 architecture. Or not.
因此,可以使用可以在客户端和服务器上执行的 DSL,并且可以按照 JSR 94 架构进行开发、执行和管理。或不。
Another article Creating a simple rules engine using the Java scripting APIemploys JSR-233 plus other stuff to create a rule engine system. This however predates the JSR 94.
另一篇文章使用 Java 脚本 API 创建简单的规则引擎使用JSR-233 和其他东西来创建规则引擎系统。然而,这早于 JSR 94。
I got to this stackoverflow page since I too was looking for a solution. Currently, I have a page to validate on client side where groups of fields can trigger different validation rules, and requirements are changing. To write this in imperative code just creates a mess with high cyclomatic complexity.
我进入了这个 stackoverflow 页面,因为我也在寻找解决方案。目前,我有一个页面可以在客户端进行验证,其中字段组可以触发不同的验证规则,并且需求正在发生变化。用命令式代码编写它只会造成具有高圈复杂度的混乱。
However, the easiest thing to do is use one of the many JavaScript form validation libraries out there. Still looking.
然而,最简单的方法是使用众多 JavaScript 表单验证库之一。还在寻找。
回答by Sebastian
Since the javascript lives in the browser, it's fairly easy for a user to check your source code and bypass any js validation mechanisms. That's why it's usually done server-side.
由于 javascript 存在于浏览器中,因此用户可以很容易地检查您的源代码并绕过任何 js 验证机制。这就是为什么它通常在服务器端完成。
It's a pain but I usually implement validation both in javascript and on the server, that way "normal" users will have a quick response, and "hackers" will be kept out of the system. Unfortunately I think that's the way you need to go if you want both good user experience and good security.
这很痛苦,但我通常在 javascript 和服务器上实现验证,这样“普通”用户将有快速响应,而“黑客”将被排除在系统之外。不幸的是,如果您想要良好的用户体验和良好的安全性,我认为这就是您需要走的路。
To answer your question, to my knowledge there's no common library that can be used both on the client side and the server side.
要回答您的问题,据我所知,没有可以在客户端和服务器端使用的通用库。
PS. remember JavaScript is not Java ! :-) http://en.wikipedia.org/wiki/JavaScript#JavaScript_and_Java
附注。记住 JavaScript 不是 Java!:-) http://en.wikipedia.org/wiki/JavaScript#JavaScript_and_Java