54

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.

Philippe Monnet
  • 1,152
  • 1
  • 9
  • 13
  • @Philippe - Are you talking validation rules? – TheCloudlessSky Aug 07 '10 at 16:57
  • No, more of a series of if-then rules to narrow down a context in terms of criteria, leading eventually to a way to look up decision values in a matrix. So this would be like a simple forward chaining engine. – Philippe Monnet Aug 07 '10 at 17:58
  • 5
    You have got a fully capable programming language, what feature does it lack? – aaaaaaaaaaaa Oct 21 '10 at 23:48
  • 2
    Sounds like it would be faster to write one, rather than find, learn and adapt an existing rule engine. – Pēteris Caune Oct 26 '10 at 11:36
  • Since I started my research on this I have found something somewhat related but not a rule engine per se. Brain (see http://harthur.github.com/brain/ ) is neural network library implemented in Javascript. I have built a prototype with it and it is pretty nice. That said I still have not found a rule engine framework. – Philippe Monnet Oct 28 '10 at 15:42
  • 1
    Define "rule engine" better, perhaps. –  Dec 26 '10 at 19:52
  • 2
    By "rule engine" I mean the traditional definition - Wikipedia http://en.wikipedia.org/wiki/Rules_engine#Types_of_rule_engines – Philippe Monnet Dec 27 '10 at 18:06
  • Have a look at http://www.flexrule.com/archives/javascript-rules-engine/ it's a commercial product that supports forward chaining, backward chaining and you can use Natural Language and Decision table for execution and modelling rules in your JS application. (I work for FlexRule) – Arash Aghlara Apr 14 '18 at 07:40
  • C# World : You have a very capable programming language. Create a rule engine using Linq and lambda. => Enter FluentValidation – int-i Jul 27 '18 at 20:31

6 Answers6

15

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

dearwish
  • 181
  • 1
  • 6
  • How can one make this work for IE 8-9 ? – niceman Nov 14 '15 at 09:28
  • 7
    nools is no longer maintained by C2FO, expert from their github repo: `C2FO is no longer maintaining this project. Please use accordingly. If you would like to help maintain or take over the project please let us know.` – Maulik Soneji Mar 06 '17 at 10:47
15

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.

Tim M.
  • 53,671
  • 14
  • 120
  • 163
  • 1
    That's not really what I had in mind but since you're on this topic I have found a great framework to model dependency chains: KnockoutJS http://knockoutjs.com/. In fact I'm currently using it in conjunction with the Brain (http://harthur.github.com/brain/) neural network library to create a dynamic simulation. But for the original problem I am trying to solve I can't use a neural net as I need to model a suite of rules (condition/action pairs) that I can evaluate either based on a goal (prove goal X i.e. backward chaining) or based on a series of facts (i.e. forward chaining). – Philippe Monnet Dec 27 '10 at 18:12
  • @Phillipe - agreed, a neural network doesn't address the problem you described. I may have phrased my answer poorly; in brief, I don't know of an existing framework that would do what you asked, but based on my experience, you could easily knock out a basic but flexible rules engine in a few days, even in JS. The bells/whistles/caveats is where time is lost (which is why I pointed out things I had learned that had been pitfalls). I don't know if writing your own is an option or not. – Tim M. Dec 27 '10 at 20:25
6

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.

AnyWhichWay
  • 716
  • 8
  • 11
3

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

Dan D.
  • 73,243
  • 15
  • 104
  • 123
John
  • 864
  • 1
  • 11
  • 26
1

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.

1

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.

Alwin
  • 11
  • 1