Rules Engines can be a very valuable part of an overall business automation and decision making system. In discussing rules engines the simple view is that it is a system that uses rules, in any form, applied to data to produce outcomes. For this tutorial we will be talking about a specific implementation of the Rete algorithm (pronounced as two syllables, reh-te). The Rete algorithm, developed by Charles Forgy in 1974, is able to scale to a large number of rules and facts.
For more detail on the Rete algorithm, here is a paper published by Dr. Forgy in 1982. The simple form of a rule is a pattern matching system; implementing "when condition then action". As data changes and the condition of the system is altered then one or more actions may be triggered. This is a super simplistic view, for an excellent in depth view check out the Drools project (here). This is a complex Business Rules Management System, implemented in Java, and available as open source.
First we need to create a server on the CenturyLink Cloud. Taking the defaults for number of CPU, memory allocation, and storage is fine. Add a public IP and allow HTTP and SSH port access. I am using the Ubuntu 14.04 server image.
SSH into the newly created server. To install Node.js:
$ sudo apt-get update $ sudo apt-get install nodejs
You can validate that Node.js is installed by running the version command:
$ nodejs --version
Due to a naming conflict with the Amateur Packet Radio Node Program package the Ubuntu maintainers have renamed the standard Node.js binary from node to nodejs. This will break some of the standard Nools examples so we will "fix" this by:
$ sudo ln -s /usr/bin/nodejs /usr/bin/node
And now you should be able to re-run the version command using the binary node instead of nodejs. The last part of installing the Node.js support is to install the Node.js package manager: [
$ sudo apt-get install npm
$ npm version
Should return something that looks like:
While we are going to be installing Nools and running it against a Node.js server it is also capable of running in a variety of browsers. Checking the github repository shows the following browser support:
Installing the Nools rules engine
We will use npm to install Nools:
$ sudo npm install nools
Check to see if you now have the _~/node_modules/nools_ directory. If so, congratulations you have successfully installed Nools!
Verifying the install
Great, now what can you do with Nools, or... how can I make it do anything? We are going to first run one of the canned example programs (to make sure we can) and then will build a simple set of rules and execute them. Change to the Nools examples directory:
$ cd $ cd ~/node_modules/nools/examples
And run the following:
$ node helloWorld
If everything is OK you will see output like the following:
Feel free to explore the "helloWorld.js" files to see what we just ran.
A Simple Hello World example
While you should review and study the Nools documentation to understand all of the terms and interactions, here are a few terms that we will use in our example program.
- Rule - Constraints that must be satisfied for an Action to occur.
- Action - Code that executes once all the constraints are satisfied.
- Flow - A collection of Rules.
- Session - An instance of the object type Flow.
Let's create the Node.js file that will drive the rules process. You can create and run this in any directory, for convenience I am running it out of the Nools example directory. Create a file centurylink.js that looks like:
If you run this file it will (correctly) complain that you haven't provided the DSL file centurylink.nools. So lets go ahead and build this file.
The DSL will define one object ("Output") and two rules ("Rule1" and "Rule2"). Output will be defined as a text object so we can see the results of the rules evaluation. The rules will manipulate the inputs and trigger the display actions.
Create a file centurylink.nools that looks like the following:
Now if you run:
$ node centurylink
You will get the output "nine". Why is that? Remember our original centurylink.js file has the line:
session.assert (new Output ("six"));
When we ran the code this input of "six" matched Rule1... the action made "six" become "nine". When Rule2 was evaluated the "nine" triggered the write to console action, hence we saw "nine" printed to the console.
And, just for fun, if we change Rule1 to be:
And add an additional assert to the centurylink.js file:
And then run the file... We now have the Nools Rules Engine answering the question "Why is six afraid of seven?"
You now have a working installation of Nools. This tutorial has not even scratched the surface of the power and flexibility of the system, but simply has given you a starting place. Check out the Nools documentation and have fun exploring the system!