Rules define the "code" of your expert system. They manipulate the facts in the expert system to infer new facts or to perform actions. Rules are basically IF-THEN constructs for reasoning about the domain. The inference engine matches rule conditions (the IF portion) with facts in the expert system and flags rules which have been matched (called a rule activation). After the inference engine has determined all the rule matches, it selects one rule based on its strategy and a rule's salience (priority) and fires that rule. When a rule is fired, its actions (the THEN portion) are executed one by one.
For example, say you want a rule that approves a loan based on an applicants credit history and salary. In pseudocode, your rule may look like this:
IF applicant has salary greater than $35,000 and a fair credit history
THEN extend applicant a loan of $100,000 for 30 years
First, you need to define the classes of objects (fact templates) in your expert system
(deftemplate applicant (slot name) (slot salary) (slot credit-history))
(deftemplate loan (slot applicant) (slot amount) (slot years))
Now, we have defined what we need to create the rule:
(defrule Loan-Approval "Approve loan"
?applicant <- (applicant (salary ?S) (credit-history good))
(test (> ?S 35000))
(assert (loan (applicant ?applicant) (amount 100000) (years 30)))
A few notes about our rule. Every rule must be identified with a unique name. The LHS of the rule is the ?applicant <- (applicant (salary ?S) (credit-history good))
(test (> ?S 35000))
This states, First match for an applicant fact, where the credit history equals good and bind the salary slot to the variable ?S and the entire fact to the variable ?applicant AND test that the variable ?S is greater than 35000. THEN, assert a new loan fact where the applicant is the applicant fact we matched on the LHS of the rule and extend a 100,000 loan for 30 years.
Now, if we asserted the fact:
(assert (applicant (name tom) (salary 40000) (credit-history bad)))
and Run the inference engine. What would happen? In this case, nothing happens. The one applicant we have has bad credit history so this rule would never activate. Alright, assert the following fact:
(assert (applicant (name Jeff) (salary 34000) (credit-history good)))
and Run the inference engine. What happens? Still nothing, though we got farther. The inference engine obtained what we call a partial match for the Loan-Approval rule. The first condition, for a good credit-history, matched. However, the fact that match the first condition failed the second test condition element, that the salary be greater than 35000.
Ok, let's assert the following fact and run:
(assert (applicant (name David) (salary 50000) (credit-history good)))
Hey! We finally had the rule fire. It created the following new fact:
(loan (applicant <f-5>) (amount 100000) (years 30))
where f-5 is the Fact ID of the David applicant fact.
See the Rules definition for the syntax of a rule.