Optimizing the object model

You can decrease the cost of pattern matching by changing the representation of the object model.

Suppose that you are filtering a set of ordered objects in which each object is identified by a rank. If you want to retrieve an object next to another just by the use of the ranks, you can write a complex rule such as the following:

Low performing rule

The next rule expresses the following behavior: If you look for the object of rank ?n and if you find an object whose rank is greater than ?n and there is no object whose rank is between their ranks, then the next object has successfully been found.

rule next {
   when {
      element( ?n:rank );
      ?next : element( ?n2:rank & > ?n );
      not element( rank > ?n & < ?n2);
   }
   then {
      do something with ?next
   }
};

In such a rule, the object model and the reasoning process are not efficient. Performance is low.

More efficient rule

To improve performance, each object keeps a pointer to the object next to it. You can easily use this representation for the pattern matching process, as in the following example.

rule next {
   when {
      element( ?n:rank;  ?next: next );
   }
   then {
      do something with ?next
   }
};