Lightweight Rules Engine in Javascript

JavascriptRule Engine

Javascript Problem Overview


I am looking for suggestions for a lightweight rules engine implemented in 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.

Javascript Solutions


Solution 1 - 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.

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] = 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.

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

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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.

Solution 4 - Javascript

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

Solution 5 - Javascript

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"

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

Solution 6 - Javascript

Please check out (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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilippe MonnetView Question on Stackoverflow
Solution 1 - JavascriptTim M.View Answer on Stackoverflow
Solution 2 - JavascriptdearwishView Answer on Stackoverflow
Solution 3 - JavascriptAnyWhichWayView Answer on Stackoverflow
Solution 4 - JavascriptJohnView Answer on Stackoverflow
Solution 5 - JavascriptAlwinView Answer on Stackoverflow
Solution 6 - JavascriptRuchir WaliaView Answer on Stackoverflow