Rule-based systems capitalize on the knowledge of experts by encapsulating it into a set of rules that can be programmatically evaluated against a set of inputs. This chapter examines a simple rule-based system used by a fictional financial lender named Zoom Lending. The application uses rules to determine how and where new fields will be added to the database.
Rules are typically stored in a database and consist of two parts: the condition, or if part, and the result, or then part. Four phases are used to apply all potential rules: condition matching, priority evaluation, application of results, and adjustment phases.
The Rule Maintenance pages allow the administrator to add or edit rules and rule types. Rule types are templates used to define similar rules. They specify distinct combinations of condition and result methods that will be called programmatically. Rules are ranked according to condition and result priorities. Rules marked as default will always be selected during the Condition Matching phase. The default rule applies if no other rules with the same result method are selected.
The sample application applies a threshold percentage to determine whether the question entered already exists in the database. The percentage value is stored in the Web.config file and can be altered without recompiling the application. The Microsoft Word API is used to check words against matching synonyms and thereby increases the likelihood that a match will be found.
The Condition Matching phase identifies all the rules for which the condition method returns a true value. The function associated with this phase utilizes the System.Reflection namespace to programmatically invoke condition methods.
The Priority Evaluation phase orders the rules identified in the first phase according to the result method name, default flag, and condition priority value. It will then accept the first rule with a distinct result method and pass the results to the next phase.
The Application of Results phase orders the rules to be executed according to the value of the result priority field. It then utilizes the invoke method of the System.Ref lection namespace to execute each result method. Once successfully executed, it logs a detailed entry record indicating that the rule was applied.
The Adjustment phase involves the ongoing analysis of records in the ResultLog and ResultLogDetail tables. These tables will record every time the application determines that a matching question exists, finds a matching condition, invokes a result method, or encounters an error. Adjustments to the threshold percentage or to the rules and rule types may be deemed necessary.