Caller Rules – Speed Up the Execution of Rules

Basics

The main purpose of caller rules and sequences is to execute other rules by using the exec statement or for loops. Using this code structure can speed up your analysis, because matches on the LHS of the rule are found more efficiently.
For instance, this could be useful if you compare an important number of nodes.

Variant 1 is the “slow version”, with no exec statements and for loops. It first stores all defined nodes and then compares them, respectively to the pre-defined condition. This takes long since the same nodes might be stored and compared several times.

Variant 2 (with exec statements and for loops) stores one node, keeps it and compares it in a second rule to all other possible nodes, which is cheaper in terms of storage and time. The different procedures are illustrated in the graphics below, and examples for codes are given.

 

Variant 1 (without exec or for loop)

Concept

Caller rule

Example Code

sequence copyDataFromSameID {
     [copyData1]				//[...] stores all matching patterns from LHS and afterwards executes the RHS for every match 
}

rule copyData1{
     node1:MyNode;
     m:NodeHelper;
     
     if{
         node1.id == m.id;
     }
     
     modify{                                    //Put your RHS Statement here
     }
}

 

Variant 2 (with caller rules)

Concept

Caller rule

 

Example code (using an exec statement)

//Rule to call the copyData rule
 rule callCopyDataHelper{
     node1:MyNode;
     modify{
         exec(copyData(node1)*);    //Executes the rule "copyData" with input argument node1 until it fails    
     }
 }
//Rule to copy the data
 rule copyData(n:MyNode) {
     m:NodeHelper;
     if{ n.id == m.id && n.name != m.name; }	//If two nodes have the same ID AND not the same name yet. (Stoping Criterion: The name checking makes sure, that "copyData" can not be executed for ever in "callCopyDataHelper")
     modify{
       eval{
           m.name = n.name;                 
       }
    }     
}

 

Example code (using a for loop)

The same aim can be reached by calling the “copyData”-Rule with a for-loop in a sequence. Have a look at the example code below.

//Sequence to execute the copy rule
 sequence callerSequence {
     for{n:MyNode in nodes(MyNode);       //Iterates over all elements of the node set of type MyNode
         copyData2(n)                    //And executes the copyData2 rule (if necessary use ...* or [...]; you can find a list with sequence flow operators here.)
     }
 } 

//Rule to copy the data
 rule copyData2(n:MyNode) {
     m:NodeHelper;
     if{ n.id == m.id; }
     modify{
       eval{
           m.name = n.name;                 
       }
    }     
}

 

Was this article helpful?

Related Articles