KnowledgeWorks and Prolog User Guide > 6 Advanced Topics > 6.2 Optimization > 6.2.1 Forward Chaining

NextPrevUpTopContentsIndex

6.2.1.2 Efficient Forward Chaining Rule Preconditions

Forward chaining rules are more efficient if the more restrictive preconditions (that is, the ones which will have fewer matches) are written first. Computationally cheap Lisp tests should be used wherever possible as they reduce the search space of the rule interpreter. The Lisp tests should where possible be broken into sufficiently small pieces that they can be applied as early on as possible.

For example, the precondition fragment

(train ?t position ?p1)
(test (> ?p1 5))
(signal ?s position ?p2)
(test (> ?p2 6))

is better than

(train ?t position ?p1)
(signal ?s position ?p2)
(test (and (> ?p1 5) (> ?p2 5)))

because in the first example the Lisp tests can be applied directly to the trains and signals respectively before looking at combinations of trains and signals, whereas in the second case all the combinations must be produced before the Lisp test can be applied. Simply separating the tests is enough for the rule compiler to apply them to the right object base matches -- the precise order of the tests is unimportant.


KnowledgeWorks and Prolog User Guide (Windows version) - 22 Dec 2009

NextPrevUpTopContentsIndex