Client-Side Rules Engine


The above is a demo of a client-side rules engine called MochaBlox. The rules are written in a declarative way based on Datalog, and the company I built this for encoded many of its business rules in this language. By running the code in a browser, the particles can update at the full refresh rate of the user’s monitor, and whenever the user touches the slider bar, the UI can update instantly, with no user perceptible pauses.

Some notable aspects of this project:

  • I use the front end of the company’s rule engine and built a JavaScript back end to go with it.
  • The full semantics of the backend language are supported, including view maintenance and transactions.
  • Compile times are fast, with typical compiles of a full program running in under one second.
  • For communication between the rule engine and the browser, I developed a way to mark a predicate as a “port”. An input port can receive information into the rules engine from the browser, and an output port supports the opposite.
  • I integrated with the company UI framework, allowing a gradual migration path for our legacy UIs that ran all of their rules on the server.
  • I integrated with the Chrome and Firefox debugging support, which allows showing the developer the original source code while single-stepping through the compiled JavaScript. I wrote up the details in this blog article.

Here are some files used in the above demo:

  • particles.logic. The full Datalog code for the particle simulation.
  • out.js. The compiled JavaScript output. The code is not minified and is somewhat readable. Each rule from particles.logic is in a comment in out.js, and each such comment is followed by the compiled JavaScript that implements that rule.