Javascript中的轻量级规则引擎 [英] Lightweight Rules Engine in Javascript

查看:402
本文介绍了Javascript中的轻量级规则引擎的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

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

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.

此类规则引擎的一般观察(无特定顺序):


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

  1. Non-linear lookups are your friend. In JavaScript, this would be easy using the obj[key] = val syntax. 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.

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

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).

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

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.

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

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

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

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.

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

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."

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

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

这篇关于Javascript中的轻量级规则引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆