Clips Expert System BASIC Programming Handbook (i)

Source: Internet
Author: User

Clips English full name for C languageintegrated Production system, the literal word is "C language Integration Production system", is by the U.S. National Space Agency Johnson Space Center Artificial Intelligence Department in 1985 launched the Expert System development tool, At the beginning of design, it is widely used in aerospace field. Clips is a language that supports a variety of programming styles, including rule-based, object-oriented, and process-oriented programming styles.

1. Introduce

This is the clips 6.3 Basic programming instruction manual, mainly for users interested in clips syntax. There is no need for background knowledge of any expert system to learn this manual with some knowledge of programming languages. Section 2nd of this manual describes the overall framework and basic objectives of the clips language. Sections 3rd through 11 detail the main contents of the clips language, including types, rules, object-oriented programming (cool), and so on. Section 12th describes action and function-related content, and section 13th describes the typical commands in the interactive interface.

This manual covers only the basics of clips programming, and is described in the Advanced Programming Guide for user-defined functions, embedded applications, and more. Users can implement a simple expert system from this manual.

2. Clips overview

2.1 Clips Interaction

The clips expert system can interact in three ways: 1 A simple command-line interaction interface, 2 provides an IDE interactive interface (described in the interfaces Manual), and 3 is called by embedding into other systems (described in the Advanced Programming Guide). In addition, we can write a series of commands to a file, read the file at clips startup to automatically execute the appropriate command, or use the batch command to bulk execute.

The most common way of interacting with clips is the first. Its standard use methods are:

(1) Using a text editor to build a knowledge base;

2 loading Knowledge base to clips;

3) inference execution;

Clips provides an interface to facilitate viewing of the current system state, tracking execution paths, adding or removing information, and emptying clips.

2.1.1 Command line interaction

The most important interaction that clips uses is the command-line approach (top-level command). When you enter the CLIPS interactive environment, the "clips>" prompt appears, and the command is interpreted after you enter the appropriate command. A command can be a function call, a struct definition, a global/local variable definition, a constant (including symbolic illuminated steady and numbers). If you enter a function call, the function is executed and the corresponding execution result is returned. The function call uses the prefix format-that is, first write the function name and then the operand; The structure definition creates a new type; a global symbol, local symbol, or constant, and the value of the symbol or constant is displayed; Local variables can change the BIND function in the command interaction interface. Its value is persisted to the reset or the clear command is invoked. The following is a simple example.

2.1.2 commands are automatically executed and loaded

In some operating systems, additional parameters are allowed to load automatically when the program starts. When clips is running on such an operating system, clips will be able to automatically run the appropriate command from the file at startup and load some structure definitions.

The syntax for starting clips on the command line looks like this:

Clips <option>*

<option>:: =-F <filename> |-f2 <filename | -L <filename>


-F: With the batch command, if the-F command file contains exit, then automatically exits the clips when the command in the file is finished, and if there is no Exit command, the command remains in the clips environment after execution.

-F2: With batch* command

-L: Same as load command

2.1.3 integration with other programming languages

When using an expert system, there are two main types of integration: embedding clips into other systems, calling external functions in clips, and clips two ways to support it, as described in the Advanced Programming Guide.

2.2 The grammar of the reference manual

The terminology used in this manual is consistent with the general-purpose computer reference manual. Literal characters are literal meanings; coarse nominal or characters represent an oral description of the input, and consecutive characters that are included with angle brackets represent a single entity of a user-supplied class object. A non-terminal "*" means 0 or more entities of a user-supplied class object, a non-terminal followed by "+" to represent 1 or more entities of a user-supplied class object, and an ellipsis between two non-non-terminal also indicates that the non-terminal occurs 1 or more times. Items in brackets (such as [<comment>]) indicate an optional; Represents the choice of multiple items; whitespace is used only to separate items, and ":: =" indicates how non-terminal can be replaced. Please see Appendix G for details.

2.3 Basic programming elements

Clips provides three basic programming elements: the original data type, the function to manipulate the data, and the structure for adding the knowledge base.

2.3.1 Data type

Clips offers 8 original data types: Float,integer,symbol,string,external-address,fact-address,instance-name and instance-address. The numbers are mainly expressed in float and integer, and the symbols are mainly expressed by symbols and strings.

The numbers are composed only of 0~9/./e/+/-, clips internally with a long to denote the integer type and a double for the float type. As with other programming languages, when comparing two floating-point numbers and comparing integers with floating-point numbers, be careful to compare using relative difference values instead of using comparison runs directly.

An example of an integral type is as follows:

237 15 +12-32

Examples of floating point are as follows:

237e3 15.09 +12.0-32.3e-7

The formal representation is as follows:

Symbol is character sensitive, starting with printable ASCII characters followed by 0 to multiple printable ASCII characters. Symbol with nonprinting characters (including spaces, tabs, carriage returns, line feeds), double quotes, "(", ")", "&", "|", "<", "~", ";" End tag as a symbol. “’;” The characters that are followed represent comments, and if symbol is included with "<>", you can include a symbol closing tag. In addition, because "?" and "$?" There are special meanings, and symbols cannot be used as starting characters in either of these types. Here are some examples of characters:

Foo Hello b76-hibad_value 127A 456-93-039 @+=-% 2each

Note that in fact the number is also a special case of symbol, but its particularity is divided into float and integer two types.

A string is any character enclosed in double quotes, and if it also protects double quotes, use \ to indicate that the following is an example of a string:

"Foo" "A and B" "Hello" "a\" quote "

External-address represents the data structure of the return value of API functions that are invoked by the clips and implemented by other languages. It is generated only when calling external functions, that is, you cannot define one of these types of data. Its specific use is described in the Advanced Programming Manual. Inside the clips, the type is printed in the following form:


XXXXX represents an external address.

Fact is a list of atomic values that are referenced by fact index values or names. It is printed in the clips internal form:


where xxx represents the index value of fact

Instance is an object that represents an instance or an example of a class. In clips, an object is defined as the instance of a Float,integer,symbol,string,multifield values,external-address,fact-address or a user-defined class. The instance of the user-defined class is created by Make-instance, which can be uniquely identified by reference address. All of these will be covered in detail in 2.4.2,,2.6 and section 9th.

Instance-name is a symbol that is expanded in brackets (but the outside brackets are not part of the instance-name), so the general notation is not enclosed in brackets. However, if cool (CLIPS Object oriented Language) is not included in the CLIPS configuration, general symbols can also be enclosed in brackets.

Instance-address occurs only when a binding function call returns a value to Instance-address, or when a variable is bound to an instance that matches the left part of the rule.

References to an instance of a user-defined class can be used either by using Instance-name or by using instance-address,instance-address only when emphasizing speed. The instance-address is printed in the clips internal form:


where xxx is the name of instance.

In clips, a placeholder that contains a value (which can be any type) is called field. The value of the base data type is called a single field value (Single-field). Constant indicates that the value can be represented directly as the invariant single field value of a series of strings. A multi-domain value (Multifield value) consists of 0 or more single field values, which are surrounded by clips using parentheses when displaying multiple-field values. Here are some examples of multi-domain values:

(a) (1 bar fool) (x 3.0 "Red" 567)

Note that "(a)" is different from "a". Multiple-domain values can occur mainly in the following situations:

1 call a function that returns a multi-domain value;

2 wildcard characters defined in Deffunction, Objectmessage-handle and method;

3 Bind the variable when the pattern matches.

Variable is a symbolic address for storing value, which is used in the clips construction type (such as Defrule,deffunction,defmethod,defmessage-handle), and its specific information is described in the appropriate section.

2.3.2 function

A function is a section of executable code in a clips that uses a function name to return useful values or produce useful side effects (such as displaying relevant information). The function described in this manual usually represents the functions that return useful values.

function includes user-defined functions and system-defined functions that are written in an external environment and linked to clips. System-defined functions have been integrated into the clips environment and can be invoked directly; user-defined functions are defined outside the clips environment and need to be added to the clips environment using extern. A detailed list of system definition functions is shown in Appendix H.

Deffunction allows users to define their own functions directly in the clips environment using clips syntax. The main difference between such functions and the functions defined by the user is that the functions defined using Deffunction are performed by clips interpretations, while user-defined functions are compiled externally and executed directly by clips. Details of the deffunction will be covered in section You can also use Defgeneric and defmethod to define common functions that implement polymorphism and can execute different code with different parameter types and numbers. Common functions are described in detail in section

Clips function calls are represented using prefix expressions. Here are some examples of multiplication and addition.

2.3.3 Structure

Clips has a large number of defining structures: Defmodule,defrule, deffacts, Deftemplate,defglobal, Deffunction, Defclass, Definstances, Defmessage-handler, Defgeneric and Defmethod. The difference between a struct and a function call is that the structure definition affects the clips environment, adds or deletes some knowledge to the knowledge base, but does not have any output; the function call does not usually change the clips environment (except for the clear, Reset, open File class function calls). But there will be a return value or output.

The clips annotation uses the ";" Callout, ";" all the contents of the line are treated as comments.

2.4 Data abstraction

Clips has three representations of data abstraction: Facts,objects and GlobalVariables.

2.4.1 Facts

Fact (Facts) is a high-level form of information representation in a clips system. Each fact represents a piece of information in the current fact list (fact-list). The fact is that rules (rule) Use the basic unit of data.

The fact list is done by means of the assert (add), retract (delete), modify (Modify), duplicate (copy) commands. The fact list is limited to the memory of the host, and if a fact is already in the fact list, the fact will be ignored unless you modify the properties of the fact list (see 13.4.4 and 13.4.5).

Some commands, such as retract, modify, duplicate, and so on, need to be aware of the fact that references are made, mainly through the fact index (starting from 0) and the fact address. The fact index is renumbered again starting at 0 after the reset or clear command. The fact address is bound primarily by the return value of the function call or by matching the left part of the rule.

The fact mark (fact identifier) is a simple way to show the facts. It is formatted as "F-factindex".

The storage of facts is divided into two ways: orderly and disorderly. ordered mode

An ordered fact consists of a symbolic field followed by 0 or more fields, separated by spaces between fields, with parentheses around the whole fact. The first symbol can identify the relationship between the members of the following domain. If you can use the fact that "(Father-of Jack Bill)" identifies Bill as Jack's father.

Unordered fact the first field can be any type in the base type, and the following fields are not sequential. However, both types of fact representations cannot use the following notation (these reserved words can be used for slot names but are not recommended), these are reserved words in clips and are used only in deftemplate.

Test, and, Or,not, declare, logical, object, exists, and forall disorder Mode

An ordered fact encodes information according to its location. When access to information is required, the user needs to know not only which facts are stored in the data, but also where the information exists in the domain of the fact. Unordered facts give a domain name to each domain and access the information in the domain through the domain name. The unordered fact is created by the Deftemplate widget, which is similar to the structure definition.

The difference between the disordered fact and the ordered fact is the symbol of the first field. All facts The first field is a symbol, if the symbol corresponds to the name of the deftemplate followed by the slot information, then the fact is a disorderly fact, otherwise, if only through a small parenthesis, then the ordered fact. Here are some examples of ordered facts and unordered facts.

A) Disordered facts

(Client (name "Joe Brown") (idx9345a))

(Point-mass (x-velocity) (y-velocity-200))

(Class (Teacher "Martha Jones") (#-students) (Room "37A")

(Grocery-list (#-of-items 3) (Items breadmilk eggs))

The unordered facts do not care about the order in which the fields appear, and the following represent the same fact:

(Class (Teacher "Martha Jones") (#-students) (Room "37A")

(Class (#-students) (teacher "Martha Jones") (Room "37A")

(Class (Room "37A") (#-students30) (teacher "Martha Jones")

b) Orderly Facts

(class "Martha Jones" "37A")

(class "Martha Jones" "37A")

(class "37A" "Marthajones")

The fact that you define with Deftemplate can be modified and replicated in addition to additions and deletions. The benefit of modification and replication is to change only those domains that need to change, without having to change the invariant domain. Initialization Facts

Deffacts can initialize a batch of facts that will add the facts to the fact list each time it is reset.

2.4.2 Objects

In clips, objects (object) can be defined as a symbol, a string, a floating-point number, an integer, a multi-domain value, an external reference address, and an instance of a user-defined class. An object contains two parts: properties and behavior. A class is a template that defines the behavior and properties of an object, and an instance of that class is an object. Here are some examples of objects and the classes they belong to.

In clips, objects are divided into two types: primitive type objects and instances of user-defined classes. The two types of objects are different in reference, creation, deletion, and property definitions.

Native type of Object

Instances of user-defined classes

Reference mode

Use the corresponding value directly

Reference by name or address

How to create

Clips implicit creation of

Created by message or a specialized function display

Delete method

Clips implicit deletion of

Show deletion by message or special function

Attribute definition

Clips internal predefined

Defined by slot

How to use

For general function


The main difference between an object slots and a fact template is the concept of inheritance. Inheritance makes the properties and behavior of an object come from the definition of the parent class. Cool supports multiple inheritance, and a class can inherit from multiple parent classes. Because inheritance is typically used to inherit slot and Message-handle, and basic types (such as float, Integer, and so on) are not slot and message-handle, they are not usually inherited from the base type. Initialization Object

Initializing a batch of objects by Definstances automatically adds the object instance to the instance list (instance-list) when Reset is invoked.

2.4.3 Global Variables

After the global variable is defined, the global variable in the C language is allowed to be used throughout the clips environment, but the global variable in clips is weakly typed, that is, it is not limited to a fixed type. In contrast, in a widget (such as Defrule, deffunction), you can define a local variable that can only be used within a defined structure.

2.5 Knowledge Representation

Clips provides heuristic and procedural paradigm representations of knowledge. This section describes both of these knowledge representations. The object-oriented knowledge representation is described in the next section.

2.5.1 heuristic Knowledge representation-rule (rules)

Rule is a primary way of representing knowledge in clips, which is used to represent heuristic knowledge, that is, to perform a series of operations when a certain condition is established. Expert system developers work together to solve a problem by defining a series of rules. A rule consists of two parts of the front and the rear. The front part of the rule is also called the Left (LHS), which means that the "if" part is a necessary condition for the rule to be activated; the back of the rule is also called the Right (RHS), which means the "yes" part, which represents the action performed when the rule is set up.

Each fact or instance of the left part of the rule is mainly to see whether it is on the fact list (fact-list) or on the instance list (Instance-list), and it uses the inference machine to automatically complete the detection of fact and instance matching.

A series of operations on the right side of a rule executes when a rule satisfies, and when more than one rule is met, the inference machine uses a conflict resolution policy to select one of the rules to execute. When this rule is executed, the inference machine then selects the next rule to execute until no rules are pending.

In many cases, a rule is considered a if-then structure of procedural programming, such as the C language, except that in procedural programming, the If-then structure executes only when the control flow executes to the statement, and the clips rule is always detected, So clips's rules are more like whenever-then. The inference machine always maintains the rule path of those conditions, so the rules can be executed immediately when they are satisfied, and from this point of view, the rules are somewhat like exception handling in Ada.

2.5.2 Knowledge representation of process

Clips also supports procedural paradigms to represent knowledge, like the C and Ada languages. Deffunctions and common function definitions allow users to define new execution units to produce useful side effects and return values. Users can call these new functions just as they would call clips's built-in functions. Message-handlers allows a user to define the behavior of an object by defining a response to a message. Defunctions, generic functions, and message-handlers are user-defined, procedural snippets that are clips interpreted and executed at the right time. The defmodules separates the different repositories. deffunctions

The user defines the new function directly in the clips environment through Deffunction. In previous clips releases, only users were allowed to define functions outside of clips using other languages (such as C, ADA, etc.) and then link to the clips environment through external function calls. The Deffunction function defines a series of representations, such as the RHS in a rule, that are executed by clips sequentially when the function is invoked. The return result of the function is the value of the last expression. Calling the Deffunction function is like calling other functions in clips. Its specific content will be introduced in chapter 7th. Generic functions

Generic functions is similar to deffunction and can be directly defined in clips and invoked in the same way. Unlike deffunction, it can support overloading. You can define different functions of the same name by different types and number of parameters. The specific content will be introduced in the 8th chapter. Object Information Delivery (objects message-passing)

Objects include two aspects: properties and behavior. The object property, which is the slots,slots of the object class, is the content described in 2.4.2. The object behavior is defined based on the Message-handlers action of the object's link. Objects are manipulated through message passing. To make the Rolls-Royce object (which is an instance of the car Class) start the engine, the user can invoke the "send" function to send a "start-engine" message to Rolls-Royce. After the Rolls-Royce object receives the message, it passes the message to the handler function Message-handler of the "Start-engine" message for its class (that is, the car Class) and its parent class, completing the appropriate functionality. The message's processing result is called with the function, which produces a valid side effect or returns a useful value. The deeper content will be covered in chapters 2.6 and 9th. Defmodules

Defmodules can differentiate between different knowledge bases. Each artifact must be subordinate to a module. The programmer can show which widget in the control module is visible to other modules, and the module can see which artifacts are in other modules. The visibility of facts and instances between different modules is also a similar control. A module can also control the execution flow of a rule. The specific content of this part will be introduced in chapter 10th.

2.6 Clips Object-oriented language

This section will briefly describe the object-oriented language (cool) in clips. Cool includes data abstraction and knowledge representation. This section will give a general introduction to cool, mainly including two aspects of cool in the acceptance of the concept. Examples of user-defined classes refer to section 2.3.1, the object structure refer to 2.4.2 and The details of cool will be covered in chapter 9th.

2.6.1 cool is different from pure object-oriented programming paradigm

In pure object-oriented programming, all elements in a program are objects, and objects can operate only through messages. In clips, objects are limited to the following: Floating-point and integer numbers, symbols, strings,multifield values, external-addresses, Fact-addresses and instances of user-defined classes. All objects can be manipulated by a message, and instances of user-defined classes must be operated by a message. If you add two numbers to a pure OOP system, you need to send the "add" message to the first number of objects and pass the second object as an argument to the first number of objects. In clips, you can call the "+" function to pass two numbers as two parameters of a function, or you can define a message handler on the number class like a pure OOP system.

All the program elements that are not objects, you are smart to manipulate them using non-OOP functions. If you print a rule, you can only call the Ppdefrule function, and you cannot send "print" to rules, because it is not an object.

The main features of 2.6.2 object-oriented programming

Object-oriented objects include the following 5 features: Abstraction (abstraction), Encapsulation (encapsulation), Inheritance (inheritance), polymorphism (polymorphism), dynamic binding (dynamical binding). Abstraction is a high-level, intuitive representation of complex concepts. Encapsulation is to hide the implementation details of the process and provide a well-defined interface externally. Inheritance means that a class can inherit properties and behavior from the definition of other classes. Polymorphism means that different objects use their own unique way to handle the same message. Dynamic binding is the ability to infer which message-handle is used to process a message only at run time.

The new class defined by cool is abstracted to the new data type. The slots and message-handlers in the type represent the properties and behavior of the class object.

Cool supports encapsulation, and instance operations for user-defined classes must be passed through information. An instance that does not define a corresponding Message-handler method cannot process the corresponding message.

Cool supports multiple inheritance, and its defined classes can inherit from multiple parent classes. All classes have an inheritance chain, and if there are identically defined properties and behaviors on the chain, the parent class attribute and behavior that is closest to the class is used.

Cool supports polymorphism by binding different message-handler (but using the same name) on different objects.

The dynamic binding of cool is performed when the object that is called by the Send function is affected until run time. For example, an instance name or variable points to an object when the message is sent, but may point to another object at another time.

2.6.3 instance set query and distribution is an action

For pattern matching for enhanced rules applied to objects, cool provides a useful query system for deciding, combining, and executing a set of actions defined by a user instance to meet user-defined criteria. The query system allows you to correlate to any instance. You can use the query system to decide whether a particular correlation set exists, you can save the collection for subsequent references, or you can iterate over the collection to perform an operation. One of the applications may be: Find all the boys and girls of the same age in the collection.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: