Wednesday, January 1, 2014

Rule matching with the Observer pattern

A rule matching algorithm has an intuitive implementation with the use of the observer pattern. Model state data as observables and productions as observers of state objects. Updates to the state objects trigger notices to productions. Notices to productions can include the state object which sent the notice. Productions request to add themselves to a set of rules-to-execute. Conflict resolution between productions can be done on addition to the set or in a separate phase.

Running time estimations

Updates to N state objects with at most M productions depending on any rule takes O(N*M) time if the productions know which rule fired and productions get notified independently for each state object of all changes to their preconditions. Deleting a production takes O(B) time to unregister the production from each of B data objects. Deleting N data objects with at most M dependents depending on at most B data objects takes O(N*M*B) time if the objects must unregister themselves when they can't be satisfied (presumably they can't if all precondition variables must be present to fire), or O(N*M) otherwise.

This algorithm improves over a naive implementation of searching through the whole list of productions on each update, but doesn't perform as well as Rete matching on updates.

~~~~