Trending Technology Machine Learning, Artificial Intelligent, Block Chain, IoT, DevOps, Data Science

Recent Post

Codecademy Code Foundations

Search This Blog

CLIPS Fact Commands in AI

CLIPS shell
  • 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

Making a List
  • As with other programming languages, CLIPS recognizes certain keywords. For example, if you want to put data in the fact-list, you can use the assert command.
         CLIPS> (assert (duck))
         CLIPS> (facts)
         f-0 (initial-fact)
         f-1 (duck)
        For a total of 2 facts.

  • What happens if you try to put a second duck into the fact-list?
  • Let's try it and see. Assert a new (duck), then issue a (facts) command as follows

Fact Command
  • The keyboard command to see facts is with the facts command. Enter (facts) in response to the CLIPS prompt and CLIPS will respond with a list of facts in the fact-list.
  • Be sure to put parentheses around the command or CLIPS will not accept it. The result of the (facts) command in this example should be
CLIPS> (facts)
f-0 (initial-fact)
f-1 (duck)
For a total of 2 facts

Clearing Up the Facts
  • The  (clear) commend actually does more than just remove facts. Besides removing all the facts, (clear) also removes all the rules.
CLIPS> (facts)
f-0 (initial-fact)
f-1 (duck)
f-2 (quack)
For a total of 3 facts.
CLIPS> (clear)
  • CLIPS> (clear)
  • CLIPS> (assert (a) (b) (c))
  • <Fact-3>
  • CLIPS> (facts)
  • f-0 (initial-fact)
  • f-1 (a)
  • f-2 (b)
  • f-3 (c)
  • For a total of 4 facts.

Retract that Fact
  • Removing facts from the fact-list is called retraction and is done with the retract command.
  • To retract a fact, you must specify the fact-index.
        CLIPS>  (retract 3)
  • You can also retract multiple facts at once, as shown by the following.
       CLIPS> (retract 1   3)
       CLIPS>  (facts)
       f-0  (initial-fact)
       f-2  (animal-sound quack)
        For a total of 2 facts
 You can just use (retract*) to retract all the facts, where the "*" indicates all.

Watch that Fact
  • One command allows you to continuously watch facts being asserted and retracted.
      CLIPS> (assert (animal-is duck) )
      ==> f-1 (animal-is duck)
      <Fact - 1>
      CLIPS> (retract 1)
      <== f-1 (animal-is duck)
     CLIPS> (facts)
     f-0  (initial-fact)
     For a total of 1 fact.
  • To turn off watching facts, enter (unwatch facts).

White space
  • Multiple fields normally are separated by white space consisting of one or more spaces, tabs, carriage returns, or line feeds.
(assert ( The
"Quack") )
be careful if you insert a carriage return inside of a string
CLIPS> (assert ( The
" ))
  • CLIPS is said to be case-sensitive because it distinguishes between uppercase and lowercase letters.
  • For example, assert the facts (duck) and (Duck) and then issue a facts) command. You'll see that CLIPS allows you to assert (duck) and (Duck) as different facts because CLIPS is case-sensitive.

Making Good Rules
  • To accomplish useful work, an expert system must have rules as well as facts.
  • The pseudocode for a rule about duck sounds might be
        IF the animal is a duck
        THEN the sound made is quack

                CLIPS> (assert (animal-is duck) )
                CLIPS> (defrule duck
                 (animal-is duck)
                 (assert (sound-is quack))))

(defrule duck  "Here comes the quack"       ; Rule header
(animal-is duck)                                           ; Pattern
 =>                                                                ; THEN arrow
( assert (sound-is quack)))                           ; Action
  • Only one rule name can exist at one time in CLIPS.
The general Syntax of a rule is shown following.

No comments:

Post a Comment

Popular Articles