NodeBrain Module Reference
Release 0.6.4 - April 2006

< Prev Table of Contents Next >

6         Simple Module

 

The Simple module is only included in the distribution as an example for programmers writing their own skill modules.  It is simple in function to keep the sample code simple, making it of little value for application.

 

This module provides three trivial skills: Sum, Add, and Count.  Actually, Add is just an alias for Sum, illustrating how multiple skills can share methods.  The following two files should have come with your distribution for experimenting with the Simple skills.

 

          #!/usr/local/bin/nb

          # simpleRules.nb

          declare simple module /usr/local/lib/nb_mod_simple.so;     

          declare sum skill simple.sum;

          declare count skill simple.count;

 

          define sum expert sum;

          define add expert simple.add;

          # Try uncommenting the following line if rule r2 does not file

          # using the events in simpleEvents.nb

          # assert a=0;

          define aisoneCounter expert count(a=1);

          define aistrueCounter expert count(a);

 

          define r1 on(sum(a,b)>5 and add(a,b,c)<20);

          define r2 on(aisoneCounter>2);

          define r3 on(aistrueCounter>2);

          ----------------------------------------------------------------------------

          # simpleEvents.nb

          assert a=1,b=2,c=3;

          assert a=0;

          assert a=10;  # r1 should fire

          assert a=0;

          assert a=1;    # r3 should fire

          assert a=0;

          assert a=1;    # r2 should fire

          assert a=5;    # r1 should fire

          show aisoneCounter;

          assert aisoneCounter()=0;      # reset counter */

          show aisoneCounter;

 


You can run a test with either of the following commands.

 

          ./simpleRules.nb simpleEvents.nb

              -or-

          nb simpleRules.nb simpleEvents.nb

 

Now what’s going on here?  The Sum and Add skills compute the sum of numeric arguments plus the sum of the lengths of string arguments.  When an argument changes, the expert conditions sum(a,b) and add(a,b,c) are re-evaluated to adjust their values.  This changes the rule condition for r1, causing it to fire each time the condition transitions to true.

 

The Count skill increments a counter every time the condition specified in the expert definition transitions to true.  As the aisoneCounter in our sample rules increments the r2 rule condition is re-evaluated.  When it transitions to True, r2 fires.  The strange looking assertion aisoneCounter()=0 is used to reset the counter.  This skill ignores arguments to an assertion, using only the assigned value to set the counter.

The following strange assertions are also possible.

 

          assert aisoneCounter();       # set counter to 1  (True)

          assert !aisoneCounter();      # set counter to 0  (False)

          assert aisoneCounter()=10; # set counter to 10

 

These sample skills are useful for illustrating the way a skill module can add functionality to NodeBrain without modifying nb.  However, they are not very impressive skills, and when you look at the source code you might wonder if the effort to create a skill module is worth the benefit.  But keep in mind, the complexity of the API calls is not proportional to the complexity of the skill you are implementing.  Trivial examples like these have a much greater percent of overhead in the API.  You can start with one of these examples and create a very useful expert skill module without having to touch the API calls much---depending on your requirements.  In most cases, the bulk of the complexity should be in the code you write to perform the internal function of the module, not in API calls to NodeBrain. 

              



Copyright © 2003-2006 The Boeing Company