Quick Tutorial to CLIPS for Hwk 4

CSE 4392 / CSE 5392 Smart Home Technologies

Spring 2006



Clips is an expert system shell that permits the construction of an inference system. It provides the basic inference engine.

The graphical interface of CLIPS

The graphical interface of CLIPS gives access to the basic functions to run the system. To run the simulation example, you have to first load the home.clp file (using load in the File menu). This file contains the basic simulation rules for the home and the inhabitant and intruder. Then load the homeautomat.clp file which contains some basic action and access rules. During loading the main window of xclips will indicate all syntax errors that are in the rule files. If there are none it will issue a TRUE at the end of the rule definitions. After loading the rules you have to reset the inference engine (using reset from the Execution menu).

To see the current set of facts available you can open the facts window (under the Windows menu). After reset this will display the initial set of facts (defined in the rule base using a deffacts statement). The picture below shows the main window and the facts window of xclips after loading and resetting the home.clp and homeautomation.clp rules.




Now you can run the inference engine with the given facts and rules by selecting run from the Execution window (or step if you want to see the changes one rule firing at a time). The inference engine will determine which rules are active by matching the left hand side against the available facts and then select one of them to fire. In run mode it will do this until there is no more active rule in the system. The picture below shows the xclips interface after running the scenario in the home.clp file once. The facts on the right are the resulting facts at the end of simulation.






By default, the xclips main window only displays the printout commands that were embedded in the rules. In this case this is the location and condition of the room the inhabitant is in at any time step. The simulation runs in 2 minute time steps. If you want more information you can select additional features to be displayed. To select them, go to watch in the Wxecution menu and select the details you want to be displayed. The most helpful are activations and firings which will at any iteration display the active or firing rules, respectively. Note, however, that for this example the set of active rules at any iteration is relatively big and might thus not be very helpful. To improve this, you can select specific rules to be monitored using the defrule manager under the Browse menu. With this tool you can set the display properties for each rule (i.e. To display activation, firing, etc) individually. This is helpful when debugging your rules.

NOTE: Rules and facts in CLIPS are persistent. Existing rules are overwritten when a new rule with the same name is loaded. However, rules that are not overwritten will be maintained until CLIPS is cleared using the ClearCLIPS option in the Execution menu. This will remove all rules and facts from the system. Reset will reset all the facts to the initial configuration (defined using the deffacts construct) but will not change any of the existing rules.



Basic CLIPS Rule Syntax

CLIPS provides a large and diverse set of constructs that can be used in rules. The basic syntax of a rule is:

Conditions_to_match => Actions_to_perform

The syntax follows closely the syntax of LISP with statements being enclosed in parenthesis

Variables

CLIPS uses variable to provide more flexible rules. A variable is always preceeded by a ?, e.g. ?time is a variable called time. Variables can be set by matching them to an argument in a fact (see under condition syntax) or using a bind construct in the right hand side of the rule. For example, (bind ?time 700) assigns the value 700 to the variable ?time.

Condition Syntax

Conditions on the left hand side of a rule are matched by the inference engine to facts available in the system. As a result, constructs on the left hand side of the rule have to be such that they can be matched against fact prior to the firing of the rule. This implies that no calculations can be performed on the left hand side of a rule. The most common constructs of the left hand side of the rule are matching operations:

In addition to matching facts, the left hand side of a rule can also contain a statement of the priority of the rule. This statement has always to be the first statement in the rule and can be used to influence which rule will be fired first if multiple rules are active. For example, a rule starting with (declare (salience -10)) will be lower priority than a rule without this (the default salience of a rule is 0).

Action Syntax

The right hand side of a rule in CLIPS represents a set of actions to be performed. Commands here follow the LISP syntax, i.e. Are of the form (function arg1 arg2 ...), where each argument can in turn be the result of a function. There are a large number of predefined functions that can be used on the right hand side. Some of the most important here are:

There are also loop constructs, case statements, and a wide variety of mathematical and string operations that can be used as part of the program represented by the right hand side of the rule. For more detail see the corresponding sections in the basic user guide of CLIPS. To cause a sequence of rules to fire it is possible to set up facts that indicate that a rule should be used. In the homeautomation.clf file, action rules are defined for turning on lights and the AC system. Asserting the fact (toggle-light 1), for example, will cause a rule to become active and fire which changes the status of the lights in room 1.



Example Rules:

The following rule is used to turn on the light in room 4 if the inhabitant just moved from room 1 to room 3. This rule also establishes a fact that captures the previous location of the inhabitant (previous-loc x) which indicates that the inhabitant location in the previous iteration of the home was room x. On the left hand side this rule matches the current location of the inhabitant and assigns its value to the variable ?room. It then matches the previous location, saving the value in variable ?prevloc and keeping a handle to the rule in ?prev. The it matches the state of the lights in room 4 and saves the state in ?state. This left hand side will match as long as there is a previous location and the inhabitant is in the home.

If the rule firse, it first removes the fact indicating the previous location of the inhabitant and adds a new fact with the current location os the previous location for the next iteration. It then checks if the inhabitant is curently in room 3 (i.e. ?room is 3), came from room 1 (i.e. ?prevloc is 1), and if the light in room 4 is currently turned off (i.e. ?state is off) and if all of these conditions are met it sets the fact (toggle-light 4) which causes the toggle-light rule to become active and fire, in turn turning on the light in room 4.

(defrule turn-room4-light-after-room1-room3
(location ?room t)
?prev <- (previous-loc ?prevloc)
(lights 4 ?state)
=>
(retract ?prev)
(assert (previous-loc ?room))
(if (and (and (eq ?room 3) (eq ?prevloc 1)) (eq ?state off))
then
(assert (toggle-light 4))))



This is the rule that toggles the light in room x whenever (toggle-light x) is set:

(defrule toggle-light
?act <- (toggle-light ?room)
?olight <- (lights ?room ?state)
=>
(retract ?act ?olight)
(if (eq ?state off)
then
(assert (lights ?room on))
(printout t "Light in room " ?room " turned on" crlf)
else
(assert (lights ?room off))
(printout t "Light in room " ?room " turned off" crlf)))



Facts in CLIPS

Facts are the basic way to represent knowledge in CLIPS. Initial facts can be created using the deffacts construct. These facts are loaded when a reset is performed. Facts are subsequently manipulated by rules using the retract, assert, and modify operators. Similar to LISP, facts and variables are not strongly typed and get types assigned only when using operators on them. The following is the example instantiation of facts used for the assignment:

(deffacts initial-state
(run)
(time 7 15)
(location 1 t)
(location 2 f)
(location 3 f)
(location 4 f)
(location 5 f)
(location 6 f)
(lights 1 off)
(lights 2 off)
(lights 3 off)
(lights 4 off)
(lights 5 off)
(lights 6 off)
(ac 1 off)
(ac 2 off)
(ac 3 off)
(ac 4 off)
(ac 5 off)
(ac 6 off)
(temperature 1 70)
(temperature 2 70)
(temperature 3 70)
(temperature 4 70)
(temperature 5 70)
(temperature 6 70)
(robot-location 3)
(robot-sens 0 0 0)
(power 0)
(light-cost 1.5)
(heat-cost 20)
(ac-cost 25))