CLIPS is a type of computer language designed for writing applications called expert systems. An expert system is a program which is specifically intended to model human expertise or knowledge. In contrast, common programs such as payroll programs, word processors, spreadsheets, computer games, and so forth, are not intended to embody human expertise or knowledge. (One definition of an expert is someone more than 50 miles from home and carrying a briefcase.)
CLIPS is called an expert system tool because it is a complete environment for developing expert systems which includes features such as an integrated editor and a debugging tool. The word shell is reserved for that portion of CLIPS which performs inferences or reasoning. The CLIPS shell provides the basic elements of an expert system:
1. fact-list, and instance-list: Global memory for data
2. knowledge-base: Contains all the rules, the rule-base
3. inference engine: Controls overall execution of rules
Be careful! CLIPS is case sensitive language.
The Beginning and End
Be careful! CLIPS is case sensitive language.
The Beginning and End
We can start to program CLIPS by opening CLIPS prompt. You can exit CLIPS prompt by typing (exit).
Inserting a fact
We can put the data into fact-list using assert command.
Example: (assert (rainy)), (assert (weather rainy))
p.s. If you try to insert the same fact to your fact-list, CLIPS prompt will show message FALSE to notice that this fact was already in the fact-list.
p.s.2 However, (assert (duck)) and (assert (duck true)) don't have the same meaning in CLIPS (blue color means it can be any text).
Inserting multiple facts at the same time
You can also insert facts using one assert statement by typing (assert (fact1) (fact2) (fact3) ...).
Example: (assert (rainy)), (assert (weather rainy))
p.s. If you try to insert the same fact to your fact-list, CLIPS prompt will show message FALSE to notice that this fact was already in the fact-list.
p.s.2 However, (assert (duck)) and (assert (duck true)) don't have the same meaning in CLIPS (blue color means it can be any text).
Inserting multiple facts at the same time
You can also insert facts using one assert statement by typing (assert (fact1) (fact2) (fact3) ...).
To check the fact-list
After we assert the fact into the list, we can check it whether is in the list or not using facts command.
Example: (facts)
To clear the facts
Use clear command to remove all facts from memory. You can also use reset command to remove all facts, but reset command will not remove your rules and deffacts like clear command. clear command will clear everything to be the starting point.
Example: (clear), (reset)
Removing a fact
To remove the fact with index n in the fact-list, use retract command.
Example: (retract n) ; where n is a index number. (retract *) to remove all facts.
Watch that fact
CLIPS provides several commands to help you debug programs. One command allows you to continuously watch facts being asserted and retracted. This is more convenient than having to type in a (facts) command over and over again and trying to figure out what's changed in the fact-list. To cancel watching facts is to type unwatch facts.
Example: (watch facts), (unwatch facts)
p.s. The right double arrow symbol, ==>, means that a fact is entering memory while the left double arrow indicates a fact is leaving memory.
You can watch things as follows:
Define your rule
You can define the rule in the common like IF ... THEN ..., in CLIPS, use defrule keyword to create the rule.
Removing your rules
To comment your code in CLIPS
You can put the comment into your code using semicolon (;), then followed by your comment.
Example: (assert (today is raining season)) ; Hello, this is a raining season (comment code)
To see which rules are fired
Example: (agenda)
To clear the facts
Use clear command to remove all facts from memory. You can also use reset command to remove all facts, but reset command will not remove your rules and deffacts like clear command. clear command will clear everything to be the starting point.
Example: (clear), (reset)
Removing a fact
To remove the fact with index n in the fact-list, use retract command.
Example: (retract n) ; where n is a index number. (retract *) to remove all facts.
Watch that fact
CLIPS provides several commands to help you debug programs. One command allows you to continuously watch facts being asserted and retracted. This is more convenient than having to type in a (facts) command over and over again and trying to figure out what's changed in the fact-list. To cancel watching facts is to type unwatch facts.
Example: (watch facts), (unwatch facts)
p.s. The right double arrow symbol, ==>, means that a fact is entering memory while the left double arrow indicates a fact is leaving memory.
You can watch things as follows:
(watch facts)
(watch instances) ; used with objects
(watch slots) ; used with objects
(watch rules)
(watch activations)
(watch messages) ; used with objects
(watch message-handlers) ; used with objects
(watch generic-functions)
(watch methods) ; used with objects
(watch deffunctions)
(watch compilations) ; on by default
(watch statistics)
(watch globals)
(watch focus)
(watch all) ; watch everything
Define your rule
You can define the rule in the common like IF ... THEN ..., in CLIPS, use defrule keyword to create the rule.
The general syntax of a rule is shown following.
(defrule rule_name "optional_comment"
(pattern_1) ; Left-Hand Side (LHS)
(pattern_2) ; of the rule consisting of elements
. ; before the "=>"
.
.
(pattern_N)
=>
(action_1) ; Right-Hand Side (RHS)
(action_2) ; of the rule consisting of elements
. ; after the "=>"
.
(action_M)) ; the last ")" balances the opening ; "(" to the left of "defrule". Be
; sure all your parentheses balance ; or you will get error messages
Example:
LOGIC: IF weather is cloudy, THEN the rain will come.
CLIPS: (defrule rain
(weather is rain)
=>
(assert (rain will come))
)
LOGIC: IF weather is cloudy, THEN the rain will come.
CLIPS: (defrule rain
(weather is rain)
=>
(assert (rain will come))
)
Removing your rules
CLIPS also allows you to eliminate rules selectively by using the undefrule, then followed by a rule name.
Example: (undefrule rule_name) ; where rule_name is a rule name that want to remove. (undefrule *) to remove all rules.
Example: (undefrule rule_name) ; where rule_name is a rule name that want to remove. (undefrule *) to remove all rules.
To comment your code in CLIPS
You can put the comment into your code using semicolon (;), then followed by your comment.
Example: (assert (today is raining season)) ; Hello, this is a raining season (comment code)
To see which rules are fired
CLIPS always executes the actions on the RHS of the highest priority rule on the agenda. This rule is then removed from the agenda and the actions of the new highest salience rule is executed. This process continues until there are no more activations or a command to stop is encountered.
You can check what's on the agenda with the agenda command.Example: (agenda)
Running a program
To make a program run, just enter the run command. Type (run) and press the carriage return key.
Show the rules
Sometimes you may want to what is the content in your rule. So, you can use ppdefrule command to see it.
Example: (ppdefrule rule_name) ; where rule_name is your rule name that you defined.
Example: (ppdefrule rule_name) ; where rule_name is your rule name that you defined.
But if you can't remember the name of your rule, you can use rules command to list all of your rule names.
Example: (rules)
Example: (rules)
As you work with CLIPS, you may become tired of typing in the same assertions from the top-level. If you are going to use the same assertions every time a program is run, you can first load assertions from a disk using a batch file. An alternative way to enter facts is by using the define facts keyword, deffacts. For example,
CLIPS> (clear)
CLIPS> (unwatch facts)
CLIPS> (unwatch activations)
CLIPS> (deffacts walk "Some facts about walking"
(status walking) ; fact to be asserted
(walk-sign walk)) ; fact to be asserted
CLIPS> (reset) ; causes facts from defacts to be asserted
CLIPS> (facts)
f-0 (initial-fact)
f-1 (status walking)
f-2 (walk-sign walk)
For a total of 3 facts.
CLIPS>
The required name of this deffacts statement, walk, follows the deffacts keyword. Following the name is an optional comment in double quotes. Like the optional comment of a rule, the (deffacts) comment will be retained with the (deffacts) after it's been loaded by CLIPS. After the name or comment are the facts that will be asserted in the fact-list. The facts in a deffacts statement are asserted using the CLIPS (reset) command.
The (initial-fact) is put in automatically by a (reset). The fact-identifier of the initial-fact is always f-0. Even without any deffacts statements, a (reset) always will assert an (initial-fact).
The utility of (initial-fact) lies in starting the execution of a program. A CLIPS program will not start running unless there are rules whose LHS's are satisfied by facts. Rather than having to type in some fact to start things off, the (reset) command asserts it for you as well as asserting the facts in deffacts statements.
The (reset) has a further advantage compared to a (clear) command in that (reset) doesn't get rid of all the rules. The (reset) leaves your rules intact. Like (clear), it removes all activated rules from the agenda and also removes all old facts from the fact-list. Giving a (reset) command is a recommended way to start off program execution, especially if the program has been run before and the fact-list is cluttered with old facts.
In summary, the (reset) does three things for facts.
(1) It removes existing facts from the fact-list, which may remove activated rules from the agenda.
(2) It asserts (initial-fact).
(3) It asserts facts from existing (deffacts) statements.
Actually, the (reset) also does corresponding operations on objects. It deletes instances, creates initial-object, and asserts instances from definstances.
Remove deffacts
To remove deffacts, use undeffacts command, then followed by deffact name. Or use clear to remove everytihing (hahaha).Remove deffacts
Example: (unndeffacts def_fact_name) ; where def_fact_name is your defined deffact. (undeffacts *) to remove all deffacts.
Matching a pattern
CLIPS has a command called matches that can tell you which patterns in a rule match facts. Patterns which do not match prevent the rule from becoming activated. One common reason that a pattern won't match a fact results from misspelling an element in the pattern or in the assertion of the fact.
Example: (matches rule_name) ; where rule_name is your rule name that you defined.
Print a result
Print a result
You can print out information using the printout function. CLIPS also has a carriage return/linefeed keyword called crlf which is very useful in improving the appearance of output by formatting it on different lines. For a change, the crlf is not included in parentheses.
Example: (printout t "Hello" crlf)) => It will print "Hello", and the cursor will start at a new line.
Example: (printout t "Hello" crlf)) => It will print "Hello", and the cursor will start at a new line.
Declare variables
The name of a variable, or variable identifier, is always written by a question mark followed by a symbol that is the name of the variable. The general format is
?<variable-name>
For examples: ?noun, ?color, ?sensor, ?valve
To use variables, you can assert the variable as a fact. For example, enter
(defrule make-quack
(duck-sound ?sound)
=>
(assert (sound-is ?sound)))
Now, assert (duck-sound quack), then (run) program. You will get the fact (sound-is quack) that you input as ?sound variable. You also can keep an address of the fact into a variable using <- as a following example:
(defrule get-married
Now, assert (duck-sound quack), then (run) program. You will get the fact (sound-is quack) that you input as ?sound variable. You also can keep an address of the fact into a variable using <- as a following example:
(defrule get-married
?duck <- (bachelor Dopey)
=>
(printout t "Dopey is now happily married " ?duck crlf)
(retract ?duck))
If you assert (bachelor Dopey), then (run) the program. Before the rule is fired, you have one fact (bachelor Dopey). After the rule is fired, that fact will be kept in ?duck variable. Then, you can retract to remove that fact using the variable name to tell CLIPS a fact-address
If you assert (bachelor Dopey), then (run) the program. Before the rule is fired, you have one fact (bachelor Dopey). After the rule is fired, that fact will be kept in ?duck variable. Then, you can retract to remove that fact using the variable name to tell CLIPS a fact-address
Instead of binding a field value to a variable, the presence of a nonempty field can be detected alone using a wildcard. For example, suppose you're running a dating service for ducks, and a duckette asserts that she only dates ducks whose first name is Richard. Actually, two criteria are in this specification since there is an implication that the duck must have more than one name. So a plain (bachelor Richard) isn't adequate because there is only one name in the fact.
This type of situation, in which only part of the fact is specified, is very common and very important. To solve this problem, a wildcard can be used to fire the Richards.
The simplest form of wildcard is called a single-field wildcard and is shown by a question mark, "?". The "?" is also called a single-field constraint. CLIPS> (clear)
CLIPS> (defrule dating-ducks
(bachelor Dopey ?)
=>
(printout t "Date Dopey" crlf))
CLIPS>
(deffacts duck
(bachelor Dicky)
(bachelor Dopey)
(bachelor Dopey Mallard)
(bachelor Dinky Dopey)
(bachelor Dopey Dinky Mallard))
CLIPS> (reset)
CLIPS> (run)
Date Dopey
It will print Date Dopey because your fact contain Dopey Mallard that starting with Dopey as same as the pattern, then followed by any value of a single field.
If you allow multiple fields, you can use multifield wildcard. This is a dollar sign followed by a question mark, "$?", and represents zero or more fields. Remember that each field is separated by space.
Wildcards have another important use because they can be attached to a symbolic field to create a variable such as ?x, $?x, ?name, or $?name. The variable can be a single‑field variable or a multifield variable depending on whether a "?" or "$?" is used on the LHS. Note that on the RHS only a ?x is used, where the "x" can be any variable name. You can think of the "$" as a function whose argument is a single-field wildcard or a single-field variable and returns a multifield wildcard or a multifield variable, respectively.
It will print Date Dopey because your fact contain Dopey Mallard that starting with Dopey as same as the pattern, then followed by any value of a single field.
If you allow multiple fields, you can use multifield wildcard. This is a dollar sign followed by a question mark, "$?", and represents zero or more fields. Remember that each field is separated by space.
Wildcards have another important use because they can be attached to a symbolic field to create a variable such as ?x, $?x, ?name, or $?name. The variable can be a single‑field variable or a multifield variable depending on whether a "?" or "$?" is used on the LHS. Note that on the RHS only a ?x is used, where the "x" can be any variable name. You can think of the "$" as a function whose argument is a single-field wildcard or a single-field variable and returns a multifield wildcard or a multifield variable, respectively.
Additional Constraints
Not condition, in CLIPS, it is called a ~ constraint. Its symbol is the tilde "~". The ~ constraint acts on the one value that immediately follows it and will not allow that value.The first is called a ~ constraint. Its symbol is the tilde "~". The ~ constraint acts on the one value that immediately follows it and will not allow that value.
Example:
Example:
(defrule walk
(light ~green)
=>
(printout t "Don't walk" crlf))
Then you assert (light red) to a fact-list, it will print "Don't walk" because your fact is red, not green. This rule will be fired.
Or condition is the bar constraint, "|". The "|" connective constraint is used to allow any of a group of values to match.
Example:
(defrule cautious
Then you assert (light red) to a fact-list, it will print "Don't walk" because your fact is red, not green. This rule will be fired.
Or condition is the bar constraint, "|". The "|" connective constraint is used to allow any of a group of values to match.
Example:
(defrule cautious
(light yellow|blinking-yellow)
=>
=>
(printout t "Be cautious" crlf))
Then, you assert (light yellow) or (light blinking-yellow). This rule will be fired.
And condition is the & connective constraint. The symbol of the & connective constraint is the ampersand, "&". The & constraint forces connected constraints to match in union. However, if you use and condition, and you also want to print the value that you input, you can't use a variable to replace them. But you can do like a following example:
Then, you assert (light yellow) or (light blinking-yellow). This rule will be fired.
And condition is the & connective constraint. The symbol of the & connective constraint is the ampersand, "&". The & constraint forces connected constraints to match in union. However, if you use and condition, and you also want to print the value that you input, you can't use a variable to replace them. But you can do like a following example:
(defrule not-yellow-red
(light ?color&~red&~yellow)
=>
(printout t "Go, since light is " ?color crlf))
Then you assert (light blue), it will print blue instead.
Reading a user input
CLIPS can read the information that you type from the keyboard using the read function. The following example shows how (read) is used to input data. Note that no extra (crlf) is needed after the (read) to put the cursor on a new line. The (read) automatically resets the cursor to a new line.
Example:
(defrule read-input
Reading a user input
CLIPS can read the information that you type from the keyboard using the read function. The following example shows how (read) is used to input data. Note that no extra (crlf) is needed after the (read) to put the cursor on a new line. The (read) automatically resets the cursor to a new line.
Example:
(defrule read-input
(initial-fact)
=>
(printout t "Name a primary color" crlf)
(assert (color (read))))
When you (run) the program. It will print "Name a primary color", then asks you to input the color. Next, it will keep your input from keyboard to be a new fact in the fact-list.
All of them are just basic usage of CLIPS. If you want to know more please visit at their official website: http://clipsrules.sourceforge.net/.
All of information is summarized from CLIPS official website documentation.
No comments:
Post a Comment