Javascript 中的轻量级规则引擎

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3430885/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:35:11  来源:igfitidea点击:

Lightweight Rules Engine in Javascript

javascriptrule-engine

提问by Philippe Monnet

I am looking for suggestions for a lightweight rules engine implemented in Javascript.

我正在寻找有关在 Javascript 中实现的轻量级规则引擎的建议。

The reason for such an implementation is to build a very lightweight but fast browser-based simulation using a small set of rules (less than 20). The simulation would take half a dozen parameters and run the rules and display results in the browser without any need to go back to the server. Think of a UI with a couple radio buttons, checkboxes, text boxes and sliders to control the parameters. The simulation would quickly re-run based on any parameter change.

这种实现的原因是使用一小组规则(少于 20 个)构建一个非常轻量级但快速的基于浏览器的模拟。模拟将需要六个参数并运行规则并在浏览器中显示结果,而无需返回服务器。想象一个带有几个单选按钮、复选框、文本框和滑块来控制参数的 UI。模拟将根据任何参数更改快速重新运行。

回答by dearwish

Checkout the noolsrule engine implemented in pure JavaScript for node.js. It has a pretty straightforward syntax for rules definitions.

查看用纯 JavaScript 为 node.js 实现的nools规则引擎。它有一个非常简单的规则定义语法。

回答by Tim Medora

I've implemented a (more complicated) version of what you are describing in c#, and thinking back through the code, all of it would be doable with JavaScript. I agree with the comments posted that writing your own is a viable option. It can be as simple or complex as you want it to be.

我已经实现了你在 c# 中描述的(更复杂的)版本,并回想一下代码,所有这些都可以用 JavaScript 来实现。我同意发布的评论,即自己编写是一个可行的选择。它可以像您希望的那样简单或复杂。

General observations for this type of rules engine (in no particular order):

对此类规则引擎的一般观察(排名不分先后):

  1. Non-linear lookups are your friend. In JavaScript, this would be easy using the obj[key] = valsyntax. Once you determine the output of a rule for a given set of parameters, cache its results so that you can use it again without executing the rule again.

  2. Determine whether or not you need to process unique combinations of inputs. For example, let's say you allow the user to enter multiple names and ask for suggestions on XYZ. In reality, you now need to run all rules against each input value. This may be irrelevant, simple, or immensely complicated (imagine a hotel reservation system that takes multiple dates, times, locations, and criteria, and makes suggestions).

  3. setTimeout() can be used to smooth out UI behavior, but the rules you describe should execute in a few milliseconds or less, so worry about performance last. Performance is less of a concern than you might think with a basic rules engine.

  4. Rule definitions will be easiest to manipulate if they are objects (or even simple object trees).

  5. Don't tie UI elements to output results; meaning, put the results of the rule execution into a flexible object list so that you can create whatever visual output you want from it.

  6. Customized output messages are very useful to a user. Meaning, rather than triggering a generic message when a condition is met, try inserting a real value into the output message like, "Your credit score is only 550. You need a minimum of a 600 to continue."

  1. 非线性查找是您的朋友。在 JavaScript 中,使用obj[key] = val语法会很容易。一旦您确定了一组给定参数的规则输出,请缓存其结果,以便您可以再次使用它而无需再次执行该规则。

  2. 确定您是否需要处理独特的输入组合。例如,假设您允许用户输入多个名称并询问有关 XYZ 的建议。实际上,您现在需要针对每个输入值运行所有规则。这可能是无关紧要的、简单的或极其复杂的(想象一个需要多个日期、时间、地点和标准并提出建议的酒店预订系统)。

  3. setTimeout() 可用于平滑 UI 行为,但您描述的规则应在几毫秒或更短的时间内执行,因此请最后担心性能。使用基本规则引擎,性能不像您想象的那么重要。

  4. 如果规则定义是对象(甚至是简单的对象树),那么它们将最容易操作。

  5. 不要将 UI 元素绑定到输出结果;意思是,将规则执行的结果放入一个灵活的对象列表中,以便您可以从中创建任何您想要的视觉输出。

  6. 自定义输出消息对用户非常有用。这意味着,与其在满足条件时触发通用消息,不如尝试在输出消息中插入一个真实值,例如“您的信用评分仅为 550。您至少需要 600 才能继续”。

That's it off the top of my head. Good luck.

这就是我的头顶。祝你好运。

回答by AnyWhichWay

Rule Reactor (https://github.com/anywhichway/rule-reactor) is a light weight, fast, expressive forward chaining business rule engine leveraging JavaScript internals, lazy cross-products, and Functions as objects rather than Rete. It can be used in the browser or on the server.

Rule Reactor ( https://github.com/anywhichway/rule-reactor) 是一个轻量级、快速、富有表现力的前向链式业务规则引擎,它利用 JavaScript 内部结构、惰性交叉产品和函数作为对象而不是 Rete。它可以在浏览器或服务器上使用。

回答by John

This is very simple rule engine, which use server side javascript(Mozilla's Rhino engine) (maybe it will be helpfully to you) http://jubyrajan.blogspot.com/2010/04/implementing-simple-deterministic-rule.html

这是一个非常简单的规则引擎,它使用服务器端 javascript(Mozilla 的 Rhino 引擎)(也许它会对你有所帮助) http://jubyrajan.blogspot.com/2010/04/implementing-simple-deterministic-rule.html

回答by Alwin

I've made an example html / javascript rule engine for a product configurator. The rule engine is based on if then statements. The if then statements will be checked with an array. This array is filled with all possible options every time an options is changed. Check out my blog for the example. Link to my blog "Forward chaining javascript rule engine"

我为产品配置器制作了一个示例 html/javascript 规则引擎。规则引擎基于 if then 语句。if then 语句将使用数组进行检查。每次更改选项时,此数组都会填充所有可能的选项。查看我的博客以获取示例。 链接到我的博客“前向链 javascript 规则引擎”

I think the "obj[key] = val" is the key to a javascript rule engine. Jquery helps with javascript handling.

我认为“obj[key] = val”是javascript规则引擎的关键。Jquery 有助于处理 javascript。

回答by Ruchir Walia

Please check out (JSL) https://www.npmjs.com/package/lib-jsl.

请查看(JSL)https://www.npmjs.com/package/lib-jsl

From the overview document, JSL is a JSON based logic programming library meant for embedded use in JS programs. It uses JSON as its syntax as well as I/O method, and provides callbacks into the host environment for performance optimisation.

从概述文档来看,JSL 是一个基于 JSON 的逻辑编程库,旨在嵌入在 JS 程序中使用。它使用 JSON 作为其语法和 I/O 方法,并提供对主机环境的回调以优化性能。