More on Using the Nools DSL and Engine…

Nools Objects

In the helloworld.nools object there is a single object defined called Message. This object has two elements defined; the text property and the constructor. The specific object definition is shown below.

define Message {
    text : '',
    constructor : function(message){
        this.text = message;

This object can now be referred to by name throughout the nools file. The other way to reference this object is to call the getDefined function from the flow object that is being used in code processing the business rules. In the nools language any javascript has can be put inside the define blog. By defining the constructor as shown, it overrides the default constructor behavior.

Nools Rules

In the nools DSL writing a rule consists of the following format.

rule <name> {
   when {<constraints>}
   then {<action>}

Let’s take a look at some of the example rules from the previous blog entry Learning “nools” Rules Engine and break those down. The first example is the Hello rule.

rule Hello {
    when {
        m : Message m.text =~ /^hello(\s*world)?$/;
    then {
        modify(m, function(){this.text += " goodbye";});

In this rule when the content starts with the word hello then the rule is set to modify the text of the object to append goodbye. So if I sent a message of ‘hello’ the message would become ‘hello goodbye’. As an example, modify the code in server.js as I’ve done below.

var nools = require ('nools');
var ruleFilePath = __dirname + "/rules/helloworld.nools";
var flow = nools.compile (ruleFilePath);
var session = flow.getSession ();

var Message = flow.getDefined ("message");

session.assert (new Message ("hello"));
session.assert (new Message ("hello or goodbye"));
session.assert (new Message ("hello world"));
session.assert (new Message ("goodbye"));


In the code above I’ve added four new message objects to have the rules process against. In each of these the Hello rules would process these four messages and produce output with a call of the match() function. When executing the code to compile and process the nools rules run node server.js command. The output then returns the following.

$ node server.js
hello world goodbye
hello or goodbye
hello goodbye

Each of the messages, even though entered in a particular order, returns the process results of the objects in reverse order. In this case the message ‘hello’ becomes ‘hello world’, ‘hello or goodbye’ remains ‘hello or goodbye’, ‘hello world’ becomes ‘hello world goodbye’ and last ‘goodbye’ is left untouched since it doesn’t start with ‘hello’. The other rule Goodbye, shown below, is also processed for each match.

rule Goodbye {
    when {
        m : Message m.text =~ /.*goodbye$/;
    then {

But since the first rule Hello processes, rule two actually runs every time (since post the Hello rule every message ends with goodbye) and displays the results based on the Goodbye rule.

That covers nools objects and rules. Stay tuned and there will be more on the nools from me in the near future. Cheers!

3 thoughts on “More on Using the Nools DSL and Engine…

  1. Chris says:

    Hi Adron,

    Thanks for the article. I’m wondering about the getSession method in Nools. How is a session established? Is this ‘state keeping’ going to limit scalability in nools?



    • So far it looks like the session management will give a hit to performance – thus making scaling a bit harder. However I’ve got a few strategies that I’m aiming to implement that will give me the ability to scale the engine. However, it’s narly so far. I’m intending to blog it once I get a few of the implementations working.

      thx for commenting!

Comments are closed.