next up previous

5.1 DEFINING RULES

Rules are defined using the defrule construct.

Syntax

(defrule <rule-name> [<comment>]
   [<declaration>]               ; Rule Properties
   <conditional-element>*        ; Left-Hand Side (LHS)
   =>
   <action>*)                    ; Right-Hand Side (RHS)

Redefining a currently existing defrule causes the previous defrule with the same name to be removed even if the new definition has errors in it. The LHS is made up of a series of conditional elements (CEs) which typically consist of pattern conditional elements (or just simply patterns) to be matched against pattern entities. An implicit and conditional element always surrounds all the patterns on the LHS. The RHS contains a list of actions to be performed when the LHS of the rule is satisfied. In addition, the LHS of a rule may also contain declarations about the ruleís properties immediately following the ruleís name and comment (see section 5.4.10 for more details). The arrow (=>) separates the LHS from the RHS. There is no limit to the number of conditional elements or actions a rule may have (other than the limitation placed by actual available memory). Actions are performed sequentially if, and only if, all conditional elements on the LHS are satisfied.

If no conditional elements are on the LHS, the pattern CE (initialfact) or (initialobject) is automatically used. If no actions are on the RHS, the rule can be activated and fired but nothing will happen.

As rules are defined, they are incrementally reset. This means that CEs in newly defined rules can be satisfied by pattern entities at the time the rule is defined, in addition to pattern entities created after the rule is defined (see sections 13.1.7, 13.6.9, and 13.6.10 for more details).

Example

(defrule example-rule   "This is an example of a simple rule"
   (refrigerator light on)
   (refrigerator door open)
   =>
   (assert (refrigerator food spoiled)))


next up previous