More on Using the Nools DSL and Engine…

JavaScript

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.

[code language=”javascript”]
define Message {
text : ”,
constructor : function(message){
this.text = message;
}
}
[/code]

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.

[sourcecode language=”javascript”]
rule <name> {
when {<constraints>}
then {<action>}
}
[/sourcecode]

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.

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

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.

[sourcecode language=”javascript”]
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”));

session.match();
[/sourcecode]

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.

[code language=”javascript”]
$ node server.js
goodbye
hello world goodbye
hello or goodbye
hello goodbye
[/code]

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.

[sourcecode language=”javascript”]
rule Goodbye {
when {
m : Message m.text =~ /.*goodbye$/;
}
then {
console.log(m.text);
}
}
[/sourcecode]

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

    Thanks,

    Chris

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