next up previous

6 Defglobal Construct

With the defglobal construct, global variables can be defined, set, and accessed within the CLIPS environment. Global variables can be accessed as part of the pattern-matching process, but changing them does not invoke the pattern-matching process. The bind function is used to set the value of global variables. Global variables are reset to their original value when the reset command is performed or when bind is called for the global with no values. This behavior can be changed using the set-reset-globals function. Global variables can be removed by using the clear command or the undefglobal command. If the item is being watched (see section 13.2), then an informational message will be displayed each time the value of a global variable is changed.


(defglobal [<defmodule-name>] <global-assignment>*)
<global-assignment> ::= <global-variable> = <expression>
<global-variable>   ::= ?*<symbol>*

There may be multiple defglobal constructs and any number of global variables may be defined in each defglobal statement. The optional <defmodulename> indicates the module in which the defglobals will be defined. If none is specified, the globals will be placed in the current module. If a variable was defined in a previous defglobal construct, its value will be replaced by the value found in the new defglobal construct. If an error is encountered when defining a defglobal construct, any global variable definitions that occurred before the error was encountered will still remain in effect.

Commands that operate on defglobals such as ppdefglobal and undefglobal expect the symbolic name of the global without the astericks (e.g. use the symbol max when you want to refer to the global variable ?*max*).

Global variables may be used anyplace that a local variable could be used (with two exceptions). Global variables may not be used as a parameter variable for a deffunction, defmethod, or message-handler. Global variables may not be used in the same way that a local variable is used on the LHS of a rule to bind a value. Therefore, the following rule is illegal

(defrule example
   (fact ?*x*)

The following rule, however, is legal.

(defrule example
   (fact ?y&:(> ?y ?*x*))

Note that this rule will not necessarily be updated when the value of ?*x* is changed. For example, if ?*x* is 4 and the fact (fact 3) is added, then the rule is not satisfied. If the value of ?*x* is now changed to 2, the rule will not be activated.


   ?*x* = 3
   ?*y* = ?*x*
   ?*z* = (+ ?*x* ?*y*)
   ?*q* = (create$ a b c))

next up previous