Monday, July 24, 2017

One Page Rule System Primer

A rule based (or production rule)  system lets you define facts and rules and then makes inferences from the facts and rules. Such a system sacrifices efficient crafted order of operation and complex data structures for automated processing with relatively limpid data and rules. The system applies all rules to all facts until it can do nothing more.

A fact is a word followed by 0 or more data literals set off in parentheses. Examples are  (fact),  (word a n d) and (longer 1 a # “string” 5.6). For now, let word have its standard meaning. Remember that subsequent values must all be literals; Numbers, words and strings are allowed along with system special values.. Several symbols special to the language are generally not allowed: to wit, things like ?  and $?.

All existing facts are indexed in a working memory. We can find any fact by its index. The system finds facts by matching memory to patterns. It can remember at fact index as well.

A pattern is a word followed by 0 or more literals or pattern expressions. Patterns are used in only in rules.  A fact can be used as a pattern - that is, it would match an identical fact. However,  most interesting facts contain pattern expressions. They are either ? or $?.

The symbol ? matches one item. Thus the pattern (word a ? d) would match the similar fact above by accepting n for ?. Note that (word ? ?) and (word ? ? ? ?) would not match because there are either too few  or  too many items in the fact.

The symbol $? matches 0 or more items in sequence.  Remember that it may match nothing at all. Thus all the following patterns match the ‘word’ fact above: (word $?), (word a $? ?) and even (word a n d $? $? $?).

For any completely successful match, the pattern expressions captures a data value. To recall that data match later in the rule, we must attach a name to the pattern expression.  The the pattern (word ?letter $?more) captures the first letter as ?letter with the value a and the remaining characters as $?more with the list (n d) as its value. Sometimes the list is empty!

Rules have a pattern part and an action part. Rules are matched only when all the patterns are consistently matched and any tests of pattern variable values are passed. IThe rule with the facts and pattern matches form an activation. One from all current activations is chosen to be fired. Firing performs the actions specified in the tail of the rule are carried out with the pattern expressions values. The most common actions are to assert or retract a fact.

The system does a match - select - fire cycle. It uses a strategy to select which instantiation to fire. It continues as long as some facts match with some rule in some unused way. It does not repeat old inferences. It generally does not make duplicate facts. This is a powerful if often inefficient paradigm which can do anything a computer can do. Yet it uses only literals and lists for data and rules for direction. The rest it does on its own.

No comments:

Post a Comment