home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 2 / crawlyvol2.bin / program / compiler / clips / clip_doc / refman.man < prev   
Encoding:
Text File  |  1993-09-01  |  64.6 KB  |  2,986 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21. CLIPS
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29. "C" Language Integrated Production System
  30.  
  31.  
  32.  
  33. A product of the
  34.  
  35. Mission Planning & Analysis Division's
  36.  
  37. Artificial Intelligence Section
  38.  
  39. CLIPS Reference Manual
  40.  
  41. Version 3.0   July 1986
  42.  
  43.  
  44.  
  45. Table of Contents
  46.  
  47.  
  48.  
  49.     Section                                                                     
  50. Page
  51.  
  52. 1.0    Introduction      4
  53.  
  54.  
  55.  
  56. 2.0    CLIPS Overview     
  57.  
  58.     2.1    Rules          5
  59.  
  60.     2.2    Facts          5
  61.  
  62.     2.3    Reference Manual Syntax      6
  63.  
  64.     2.4    defrule      6
  65.  
  66.     2.5    deffacts      7
  67.  
  68.     2.6    Commenting CLIPS Rules      7
  69.  
  70.     2.7    Integration with External Languages      8
  71.  
  72.  
  73.  
  74. 3.0    LHS Syntax- Patterns      
  75.  
  76.     3.1    salience      9
  77.  
  78.     3.2    Literal Patterns      9
  79.  
  80.     3.3    Wildcard: Single and Multi-field    10
  81.  
  82.     3.4    Variables: Single and Multi-field    11
  83.  
  84.     3.5    Field Constraints    13
  85.  
  86.         3.5.1    Logical operators    13
  87.  
  88.         3.5.2    Predicate functions    15
  89.  
  90.     3.6    test Function    16
  91.  
  92.     3.7    Logical Pattern Blocks    19
  93.  
  94.         3.7.1    Inclusive or    19
  95.  
  96.         3.7.2    Explicit and    21
  97.  
  98.     3.8    Pattern Negation- not    21
  99.  
  100.     3.9    Pattern Bindings    22
  101.  
  102.  
  103.  
  104. 4.0    RHS Syntax: Actions    
  105.  
  106.     4.1    assert    24
  107.  
  108.     4.2    retract    24
  109.  
  110.     4.3    printout    25
  111.  
  112.     4.4    bind        25
  113.  
  114.     4.5    Pattern Expansion    26
  115.  
  116.     4.6    Defined Functions    26
  117.  
  118.         4.6.1    read    27
  119.  
  120.         4.6.2    gensym    27
  121.  
  122.         4.6.3    Functions for Multi-field Variables    27
  123.  
  124.  
  125.  
  126. Table of Contents
  127.  
  128.  
  129.  
  130.     Section                                                                     
  131. Page
  132.  
  133. 4.0    RHS Syntax: Actions (cont'd)    
  134.  
  135.     4.7    call        28
  136.  
  137.         4.7.1    setgen    29
  138.  
  139.     4.8    if..then..else    29
  140.  
  141.     4.9    while        30
  142.  
  143.  
  144.  
  145. 5.0    Integrating CLIPS with External Functions    
  146.  
  147.     5.1    Declaring User Defined External Functions to CLIPS    31
  148.  
  149.     5.2    Passing variables from CLIPS to External Functions    31
  150.  
  151.     5.3    Passing data from an External Function to CLIPS    33
  152.  
  153.     5.4    Extended Function LIbraries for CLIPS    33
  154.  
  155.  
  156.  
  157. 6.0    Using CLIPS    
  158.  
  159.     6.1    Interactive Interface commands    34
  160.  
  161.     6.2    Embedded CLIPS commands    36
  162.  
  163.  
  164.  
  165.  
  166.  
  167.     Appendices
  168.  
  169. A     Defined Functions Provided by CLIPS    37
  170.  
  171.  
  172.  
  173. B    Installing CLIPS    38
  174.  
  175.  
  176.  
  177. C    Performance Notes    40
  178.  
  179.  
  180.  
  181. D    Glossary        42
  182.  
  183. Section 1: Introduction
  184.  
  185.  
  186.  
  187. The CLIPS is a tool or shell for developing expert systems. It is a forward
  188. chaining rule-based language using the Rete algorithm. CLIPS was designed
  189. to allow expert system development and delivery on conventional computers.
  190. The primary design goals were portability, efficiency and functionality. To
  191. meet these goals, CLIPS is written in and fully integrated with the C
  192. language. Future versions of CLIPS may be provided in other languages. An
  193. Ada version is currently under development.
  194.  
  195.  
  196.  
  197. This document is the Reference Manual to CLIPS. It provides the definitive
  198. description of CLIPS syntax and examples of usage. A separate manual, the
  199. CLIPS User's Guide, provides an introduction to rule-based systems using
  200. CLIPS. 
  201.  
  202.  
  203.  
  204. CLIPS was developed by the Artificial Intelligence Section of the Mission
  205. Planning and Analysis Division at NASA/Johnson Space Center. The
  206. Artificial Intelligence Section is headed by Bob Savely. The initial version
  207. of CLIPS was created by Frank Lopez. Gary Riley reworked the initial version
  208. and developed the release version. Chris Culbert wrote the Reference Manual
  209. and Joseph Giarratano wrote the Users Guide. Suggestions, testing and other
  210. additional aids were provided by Marlon Boarnet, Lui Wang, Brian Donnell,
  211. and Kirt Fields.
  212.  
  213.  
  214.  
  215.  
  216.  
  217. Section 2: CLIPS Overview
  218.  
  219.  
  220.  
  221. 2.1    Rules
  222.  
  223. The primary knowledge representation methodology in CLIPS is the rule
  224. system.  A rule is a collection of conditions and the actions to be taken if
  225. the conditions are met. The developer of an expert system defines the rules
  226. which describe how to solve a problem. CLIPS provides the mechanism which
  227. attempts to match the rules to the current state of the system. The current
  228. state is represented by a list of facts.
  229.  
  230.  
  231.  
  232. 2.2    Facts
  233.  
  234. Facts are the basic form of data in a CLIPS system. Each fact represents a
  235. piece of information which has been asserted into the current list of
  236. facts, called the fact-list. Rules execution (or firing) is based on the
  237. existence or non-existence of facts. A fact is constructed of a number of
  238. fields separated by spaces. CLIPS facts are free form, which means that
  239. any kind of information may be put into any field. Any number of fields may
  240. be stored in a fact. Facts may be asserted into the fact-list prior to
  241. starting execution and may be added (asserted) or removed (retracted) as
  242. the action of a rule firing.
  243.  
  244.  
  245.  
  246. CLIPS represents fields in the form of floating point numbers or character
  247. strings. A field in CLIPS is any string that starts with an alphanumeric
  248. character  and is followed by zero or more letters, numbers, underscores, or
  249. dashes. CLIPS is case sensitive, i.e. a pattern with upper-case letters will
  250. only match a field with upper-case letters. Spaces or other special
  251. characters can only be included by placing double quotes (") before and after
  252. the field. Double quotes may be embedded within a field by placing a
  253. backslash (\) in front of the quotes. Note that a string surrounded by double
  254. quotes will only match another string that has the same exact set of
  255. characters, including the double quotes. 
  256.  
  257.  
  258.  
  259. Any field which uses a number as the first character will be converted to
  260. single precision floating point, regardless of whether or not a decimal point
  261. was included. The number of significant digits will depend upon the machine
  262. implementation. Double quotes can be used to make a string field that starts
  263. with a numeric character.
  264.  
  265.  
  266.  
  267. The first fact in the fact-list is always asserted by the system prior to  the
  268. start of a program's execution. This fact, (initial-fact), can be treated
  269. like any other fact and may be matched or retracted. If a fact is asserted
  270. into the fact-list that exactly matches a fact that already exists, then the
  271. new assertion will be ignored.
  272.  
  273. 2.3  Reference Manual Syntax
  274.  
  275. The terminology used throughout this manual to describe CLIPS syntax is
  276. fairly common to computer reference manuals. Plain words or symbols are
  277. to be typed exactly as they appear, particularly parentheses. Symbols
  278. enclosed in single angle brackets, such as <name>, represent a single field
  279. to be defined by the user. Symbols enclosed in double angle brackets, such as
  280. <<pattern>>, represent one or more fields which must be defined by the user.
  281. Symbols enclosed within square brackets, such as [<<comment>>] are
  282. optional parameters. White spaces (tabs, spaces, carriage returns) are used
  283. by CLIPS only as delimiters between fields and are otherwise ignored
  284. (unless inside double quotes). Examples in this manual show recommended
  285. indentation style.
  286.  
  287.  
  288.  
  289. 2.4  defrule
  290.  
  291. Each rule in CLIPS must have at least one condition and one action. There is
  292. no limit to the number of conditions or actions a rule may have. To declare a
  293. rule, the defrule construct is used.
  294.  
  295.  
  296.  
  297. All constructs in CLIPS are surrounded by parenthesis. The entire defrule
  298. opens with a left parentheses, and closes with a right parentheses. Each
  299. pattern or action also opens and closes with parenthesis.
  300.  
  301.  
  302.  
  303. Syntax:
  304.  
  305.  
  306.  
  307.        (defrule <name> ["<<comment>>"]
  308.  
  309.         (<<pattern 1>>)
  310.  
  311.         [    % 
  312.  
  313.             %                 ; Left-Hand Side
  314.  
  315.             %                   ;       (LHS)
  316.  
  317.         (<<pattern n>>)]
  318.  
  319.              =>
  320.  
  321.         (<<action 1>>)
  322.  
  323.         [    % 
  324.  
  325.             %            ; Right-Hand Side
  326.  
  327.             %     ;       (RHS)
  328.  
  329.         (<<action m>>)])    
  330.  
  331.  
  332.  
  333. where <name> is the name of the rule and is an alphanumeric word of any
  334. length. The comment is optional and can be any string enclosed within
  335. double quotes.  The Left-Hand side (LHS) is  made up of a series of one or
  336. more patterns which represent the condition elements for the rule. There is
  337. always an implicit AND surrounding all the patterns on the LHS. On the
  338. Right-hand side (RHS) we have a list of one or more actions to be performed
  339. when the LHS of the rule is satisfied. The arrow (=>) separates the LHS from
  340. the RHS. Actions are performed sequentially if and only if all condition
  341. elements on the LHS are satisfied.
  342.  
  343.  
  344.  
  345. Example:
  346.  
  347.  
  348.  
  349.     (defrule example-rule   "This is an example of a simple rule"
  350.  
  351.         (foo bar 1)
  352.  
  353.         (foo bar 2)
  354.  
  355.         =>
  356.  
  357.         (assert (foo bar 3)))
  358.  
  359.  
  360.  
  361.  
  362.  
  363. 2.5  deffacts
  364.  
  365. Facts can be added to the initial fact-list prior to the execution of any
  366. rules. This is done with the deffacts construct:
  367.  
  368.  
  369.  
  370. Syntax:
  371.  
  372.  
  373.  
  374.     (deffacts <name> ["<<comment>>"]
  375.  
  376.         (<<fact 1>>)
  377.  
  378.         [    %
  379.  
  380.             %
  381.  
  382.             %
  383.  
  384.         (<<fact n>>)]) 
  385.  
  386.  
  387.  
  388. where <name> is the name given to the deffacts construction. An optional 
  389. comment may also be included. There may be multiple deffacts constructs.
  390. Any number of facts may be asserted into the initial fact-list in each
  391. deffacts statement. Facts asserted through deffacts may be retracted or
  392. pattern matched like any other fact. The initial fact-list, including any
  393. defined deffacts, is always reconstructed after a reset.
  394.  
  395.  
  396.  
  397. Example:
  398.  
  399.  
  400.  
  401.     (deffacts initial-facts
  402.  
  403.        "This is a set of facts needed prior to execution"
  404.  
  405.         (foo bar 1)
  406.  
  407.         (foo bar 2))
  408.  
  409.  
  410.  
  411.  
  412.  
  413. 2.6  Commenting CLIPS Rules
  414.  
  415. As with any programming language, it is highly beneficial to comment the
  416. code. Both defrule and deffacts allow a comment directly following the
  417. construct name. Also, comments can be placed after CLIPS code lines by
  418. using a semicolon, ";".  Everything from the ";" until the next return
  419. character will be ignored by the CLIPS reader. If the ";" is the first
  420. character in the line, the entire line will be treated as a comment. The
  421. examples in sections 2.4 and 2.5 provide examples of commented code.
  422.  
  423.  
  424.  
  425. 2.7  Integration with External Languages
  426.  
  427. CLIPS is fully integrated with external languages. Currently, the only
  428. language supported is C. Users may define their own C functions and call
  429. them from within CLIPS. There are two types of external functions which
  430. can be called from within a CLIPS rule: 
  431.  
  432.  
  433.  
  434. 1)    a defined function can be used on the LHS of a rule inside a test or as
  435. a predicate, or a defined function can be used on the RHS of a rule in a
  436. number of ways (see sections 3.5, 3.6 and 4.6)
  437.  
  438.  
  439.  
  440. 2)    an effect function used on the RHS of a rules in a call statement (see
  441. section 4.7)
  442.  
  443.  
  444.  
  445. How the function is called will determine how the return value is
  446. interpreted. All external function are defined in the same way and may do
  447. anything the user desires. Only the return value is of importance to CLIPS.
  448. Defined functions are called for their return value and typically should  not
  449. have side effects. Effect functions are called for their side effect and their
  450. return value is not meaningful. This is discussed in more detail in Section
  451. 5.0.
  452.  
  453.  
  454.  
  455. CLIPS may also be fully embedded within a user program and called as a
  456. subroutine. This use of CLIPS is discussed in Section 6.2.
  457.  
  458. Section 3: LHS Syntax- Patterns
  459.  
  460.  
  461.  
  462. 3.1    salience
  463.  
  464. A salience statement allows the user to assign a priority to a rule. When
  465. multiple rules are in the agenda, the rule with the highest priority will fire
  466. first.
  467.  
  468.  
  469.  
  470. Syntax:
  471.  
  472.  
  473.  
  474.     (declare (salience <num>))
  475.  
  476.  
  477.  
  478. where <num> must be an integer. If a salience statement is used, it must be
  479. the first pattern on the LHS of a rule. If not specified, the salience value for
  480. a rule defaults to zero. Salience values may be either positive or negative.
  481. The largest allowed value is 10000. The smallest allowed value is -10000.
  482.  
  483.  
  484.  
  485. Example:
  486.  
  487.  
  488.  
  489.     (defrule test
  490.  
  491.         (declare (salience 99))     ; salience declaration
  492.  
  493.         (initial-fact)                    ; matches the initial fact
  494.  
  495.         =>
  496.  
  497.         (printout "I Have a salience value of 99." crlf))
  498.  
  499.  
  500.  
  501.  
  502.  
  503. 3.2    Literal Patterns
  504.  
  505. The most basic pattern is one that precisely defines the exact fact that will
  506. match.  This is called a literal pattern. All fields in a literal pattern must
  507. be matched by all fields in a pattern in the fact-list. There are no variables
  508. in a literal pattern.
  509.  
  510.  
  511.  
  512. Examples:
  513.  
  514.   Pattern on LHS of rule              Fact in the fact-list                  
  515. Matches? 
  516.  
  517.        (NASA AI Section)                (NASA AI Section)    Y
  518.  
  519.        (NASA AI SECTION)                (NASA AI Section)    N
  520.  
  521.     (Lisp Machines)                  (Lisp Machines)    Y
  522.  
  523.     (Symbolics)                      (Lisp Machine Inc)    N
  524.  
  525.        (Prolog Ada C)                   ("Prolog Ada C")    N
  526.  
  527.     ("John loves Mary")              ("John loves Mary")    Y
  528.  
  529.  
  530.  
  531. (Note that space or control characters inside double quotes could cause
  532. problems)
  533.  
  534.  
  535.  
  536. 3.3  Wildcards: Single and Multi-field
  537.  
  538. CLIPS has two kinds of wildcard symbols that may be used to represent
  539. fields in a pattern. These are the question, "?" and dollar-question, "$?" 
  540. wildcards.  CLIPS interprets these wildcard symbols as standing in place of
  541. some part of a fact. The question wildcard matches any value (numeric or
  542. string) stored in exactly one field in the fact. It is a single-field wildcard
  543. symbol. The dollar-question wildcard matches any value in zero or more
  544. fields in a fact. It is a multi-field wildcard symbol, standing in the place
  545. of multiple fields. Single and multi-field wildcards may be combined in a
  546. single pattern in any combination.
  547.  
  548.  
  549.  
  550. Examples:
  551.  
  552.  
  553.  
  554.   Pattern on LHS of rule              Fact in the fact-list                  
  555. Matches? 
  556.  
  557.     (foo bar ?)    (foo bar)    N
  558.  
  559.     (foo bar ?)    (foo bar mumble)    Y
  560.  
  561.     (foo bar ?)    (foo bar "mumble")    Y
  562.  
  563.     (foo bar ?)    (foo bar mumble mumble)    N
  564.  
  565.     (foo bar ?)    (foo mumble bar)    N
  566.  
  567.  
  568.  
  569.     (foo bar $?)    (foo bar)    Y
  570.  
  571.     (foo bar $?)    (foo bar mumble)    Y
  572.  
  573.     (foo bar $?)    (foo bar "mumble")    Y
  574.  
  575.     (foo bar $?)    (foo bar mumble mumble)    Y
  576.  
  577.     (foo bar $?)    (foo mumble bar)    N
  578.  
  579.  
  580.  
  581.     (foo ? ?)    (foo bar)    N
  582.  
  583.     (foo ? ?)    (foo bar mumble)    Y
  584.  
  585.     (foo ? ?)    (foo bar "mumble")    Y
  586.  
  587.     (foo ? ?)    (foo bar mumble mumble)    N
  588.  
  589.     (foo ? ?)    (foo mumble bar)    Y
  590.  
  591.  
  592.  
  593.     (foo ? $?)    (foo bar)    Y
  594.  
  595.     (foo ? $?)    (foo bar mumble)    Y
  596.  
  597.     (foo ? $?)    (foo bar "mumble")    Y
  598.  
  599.     (foo ? $?)    (foo bar mumble mumble)    Y
  600.  
  601.     (foo ? $?)    (foo mumble bar)    Y
  602.  
  603.  
  604.  
  605.     (foo $? ?)    (foo bar)    Y
  606.  
  607.     (foo $? ?)    (foo bar mumble)    Y
  608.  
  609.     (foo $? ?)    (foo bar "mumble")    Y
  610.  
  611.     (foo $? ?)    (foo bar mumble mumble)    Y
  612.  
  613.     (foo $? ?)    (foo mumble bar)    Y
  614.  
  615.  
  616.  
  617.  
  618.  
  619. Multi-field wildcard and literal fields can be combined to provide very
  620. powerful constructs. A pattern to match all the facts that have the word
  621. CLIPS in any field could be written as:
  622.  
  623.  
  624.  
  625. ($? CLIPS $?)
  626.  
  627.  
  628.  
  629. Some examples of what this pattern would match are:
  630.  
  631.  
  632.  
  633. (CLIPS foo bar mumble)
  634.  
  635. (foo CLIPS bar)
  636.  
  637. (foo bar CLIPS)
  638.  
  639. (CLIPS)
  640.  
  641. (CLIPS foo CLIPS)
  642.  
  643.  
  644.  
  645. The last fact will match twice, for "CLIPS" appears twice in the fact. The
  646. use of the multi-field wildcard should be confined to cases of patterns in
  647. which the single-field wildcard cannot create a pattern that satisfies the
  648. match required, since the multi-field wildcard produces every possible
  649. match combination that can be derived from a fact. This derivation of
  650. matches requires a significant amount of time to perform compared to the
  651. time needed to perform a single-field match.
  652.  
  653.  
  654.  
  655.  
  656.  
  657. 3.4    Variables: Single and Multi-field
  658.  
  659. Wildcard symbols replace portions of a fact pattern and accept any value.
  660. The value of the field being replaced may be captured in a variable for
  661. comparison, display, or other manipulations. This is done by directly
  662. following the wildcard symbol with a variable name.
  663.  
  664.  
  665.  
  666. Syntax:
  667.  
  668.  
  669.  
  670.     ?<name>    ; a single value variable
  671.  
  672.     $?<name>    ; a multi-value variable
  673.  
  674.  
  675.  
  676. where <name> is an alphanumeric string. It must start with an alphabetic
  677. character, and cannot include any spaces. Double quotes are not allowed as
  678. part of a variable name. The rules for pattern matching are exactly the same
  679. as those for wildcard symbols. A variable will bind the field's value(s) to
  680. that name from its first binding until the end of all actions for that rule.
  681. The binding will only be true within the scope of the rule in which it occurs.
  682. Each rule has its own private list of variable names with their associated
  683. values (i.e. variables are local to a rule). The variables can be passed to
  684. external functions.
  685.  
  686.  
  687.  
  688. Examples:
  689.  
  690.  
  691.  
  692.   Pattern on LHS       Fact in the fact-list          ?x bound to:      
  693.  
  694.     (foo bar ?x)    (foo bar mumble)    mumble
  695.  
  696.     (foo bar ?x)    (foo bar "mumble")    "mumble"
  697.  
  698.  
  699.  
  700.   Pattern on LHS      Fact in the fact-list          $?x bound to:      
  701.  
  702.     (foo bar $?x)    (foo bar)    NIL
  703.  
  704.     (foo bar $?x)    (foo bar mumble)    (mumble)
  705.  
  706.     (foo bar $?x)    (foo bar "mumble")    ("mumble")
  707.  
  708.     (foo bar $?x)    (foo bar dig dig)    (dig dig)
  709.  
  710.  
  711.  
  712.   Pattern on LHS       Fact in the fact-list          ?x bound to:      ?y 
  713. bound to:
  714.  
  715.     (foo ?x ?y)    (foo bar mumble)    bar    mumble
  716.  
  717.     (foo ?x ?y)    (foo bar "mumble")    bar    "mumble"
  718.  
  719.     (foo ?x ?y)    (foo bar mumble mumble)         --No match!--
  720.  
  721.     (foo ?x ?y)    (foo mumble bar)    mumble    bar
  722.  
  723.  
  724.  
  725.   Pattern on LHS       Fact in the fact-list          ?x bound to:      $?y 
  726. bound to:
  727.  
  728.     (foo ?x $?y)    (foo bar)    bar    (nothing)
  729.  
  730.     (foo ?x $?y)    (foo bar mumble)    bar    mumble
  731.  
  732.     (foo ?x $?y)    (foo bar "mumble")    bar    "mumble"
  733.  
  734.     (foo ?x $?y)    (foo bar dig dig)    bar    dig, dig
  735.  
  736.     (foo ?x $?y)    (foo mumble bar)    mumble    bar
  737.  
  738.  
  739.  
  740.   Pattern on LHS       Fact in the fact-list          $?x bound to:      ?y 
  741. bound to:
  742.  
  743.     (foo $?x ?y)    (foo bar)    (nothing)    bar
  744.  
  745.     (foo $?x ?y)    (foo bar mumble)    bar    mumble
  746.  
  747.     (foo $?x ?y)    (foo bar "mumble")    bar    "mumble"
  748.  
  749.     (foo $?x ?y)    (foo bar dig dig)    bar, dig    dig
  750.  
  751.     (foo $?x ?y)    (foo mumble bar)    mumble    bar
  752.  
  753.  
  754.  
  755. Once the initial binding of a variable occurs, all references to that same
  756. variable have to match the same value that the first binding matched. This
  757. applies to both single and multi-field variables. It also applies across
  758. patterns.
  759.  
  760.  
  761.  
  762.   Pattern on LHS of rule              Fact in the fact-list                  
  763. Matches? 
  764.  
  765.     (foo ?x bar ?x)    (foo dig bar dig)    Y
  766.  
  767.     (foo ?x bar ?x)    (foo dig bar get)    N
  768.  
  769.     (foo ?x bar ?x)    (foo dig dig dig)    N
  770.  
  771.     (foo ?x bar ?x)    (foo dig bar "dig")    N
  772.  
  773.  
  774.  
  775.  Multiple pattern examples:
  776.  
  777.  
  778.  
  779.  Patterns on LHS of rule           Facts in the fact-list                    
  780. Fires? 
  781.  
  782. (defrule ..........
  783.  
  784.     (foo bar ?x)    (foo bar mumble)    Y
  785.  
  786.     (foo get ?x)    (foo get mumble)    
  787.  
  788.     => ......
  789.  
  790.  
  791.  
  792. (defrule ..........
  793.  
  794.     (foo bar ?x)    (foo bar mumble)    N
  795.  
  796.     (foo get ?x)    (foo get dig)
  797.  
  798.     => ......
  799.  
  800.  
  801.  
  802. (defrule ..........
  803.  
  804.     (foo bar $?x)    (foo bar dig mumble)    Y
  805.  
  806.     (foo get $?x)    (foo get dig mumble)    
  807.  
  808.     => ......
  809.  
  810.  
  811.  
  812. (defrule ..........
  813.  
  814.     (foo bar $?x)    (foo bar dig mumble)    N
  815.  
  816.     (foo get $?x)    (foo get dug baby)
  817.  
  818.     => ......
  819.  
  820.  
  821.  
  822.  
  823.  
  824. 3.5  Field Constraints
  825.  
  826. Field constraints are functions which constrain the range of values a
  827. particular field within a pattern may have. There are two types of field
  828. constraints: logical operators or predicate functions.
  829.  
  830.  
  831.  
  832. 3.5.1  Logical Operators
  833.  
  834. There are three logical operators available for constraining values inside a
  835. pattern. These are the & (AND), | (OR), and ~ (NOT) operators. The logical
  836. operators can be combined in almost any manner or number to carefully
  837. constrain the value of specific fields while pattern matching. 
  838.  
  839.  
  840.  
  841. Syntax:
  842.  
  843.  
  844.  
  845.     (<relation>  ~<value>)    ; the NOT operator
  846.  
  847.     (<relation>  <value1>|<value2>)    ; the OR operator
  848.  
  849.     (<relation>  ~<value1>&~<value2>)    ; the AND operator
  850.  
  851.  
  852.  
  853. The AND operator is typically used only in conjunction with the other
  854. logical operators or variable bindings. Variable binding may be used along
  855. with logical operators.
  856.  
  857.  
  858.  
  859. More Syntax:
  860.  
  861.  
  862.  
  863.     (<relation>  ?x&<value1>|<value2>)    ; the OR operator with variable
  864.  
  865.     (<relation>  ?x&~<value>)    ; the NOT operator with variable
  866.  
  867.  
  868.  
  869. If this is the first occurrence of the variable name, then the field will be
  870. constrained according to the logical operators only. The resulting value will
  871. be stored in the variable. If the variable has been previously bound, then it
  872. is considered an additional constraint along with the logical operators, i.e.
  873. the field must have the same value already bound to the variable and it must
  874. additionally match the constraints defined by the logical operators.
  875.  
  876.  
  877.  
  878. Examples:
  879.  
  880.   Pattern on LHS of rule              Fact in the fact-list                  
  881. Matches? 
  882.  
  883.     (foo ~bar)    (foo bar)    N
  884.  
  885.     (foo ~bar)    (foo mumble)    Y
  886.  
  887.  
  888.  
  889.     (foo bar|dig)    (foo bar)    Y
  890.  
  891.     (foo bar|dig)    (foo dig)    Y
  892.  
  893.     (foo bar|dig)    (foo mumble)    N
  894.  
  895.  
  896.  
  897.     (foo ~bar&~dig)    (foo bar)    N
  898.  
  899.     (foo ~bar&~dig)    (foo dig)    N
  900.  
  901.     (foo ~bar&~dig)    (foo mumble)    Y
  902.  
  903.  
  904.  
  905.     (foo ~bar&dig|get)    (foo bar)    N
  906.  
  907.     (foo ~bar&dig|get)    (foo dig)    Y
  908.  
  909.     (foo ~bar&dig|get)    (foo get)    Y
  910.  
  911.     (foo ~bar&dig|get)    (foo mumble)    N
  912.  
  913.  
  914.  
  915. Examples with variables/single patterns:
  916.  
  917.    Pattern on LHS        Fact in the fact-list          ?x bound to         
  918. Matches?
  919.  
  920.     (foo ?x&~bar)    (foo bar)    ---    N
  921.  
  922.     (foo ?x&~bar)    (foo mumble)    mumble    Y
  923.  
  924.  
  925.  
  926.     (foo ?x&bar|dig)    (foo bar)    bar    Y
  927.  
  928.     (foo ?x&bar|dig)    (foo dig)    dig    Y
  929.  
  930.     (foo ?x&bar|dig)    (foo mumble)    ---    N
  931.  
  932.  
  933.  
  934.  
  935.  
  936.  
  937.  
  938.  
  939.  
  940.  
  941.  
  942. Examples with variables/multiple patterns:
  943.  
  944.    Pattern on LHS        Fact in the fact-list          ?x bound to            
  945. Fires?
  946.  
  947. (defrule .......    
  948.  
  949.     (foo1 ?x)    (foo1 bar)    bar    N
  950.  
  951.     (foo2 ?x&~bar)    (foo2 bar)
  952.  
  953.     => ......
  954.  
  955.  
  956.  
  957. (defrule .......    
  958.  
  959.     (foo1 ?x)    (foo1 bar)    bar    N
  960.  
  961.     (foo2 ?x&~bar)    (foo2 mumble) 
  962.  
  963.     => ......
  964.  
  965.  
  966.  
  967. (defrule .......    
  968.  
  969.     (foo1 ?x)    (foo1 dig)    dig    Y
  970.  
  971.     (foo2 ?x&~bar)    (foo2 dig)
  972.  
  973.     => ......
  974.  
  975.  
  976.  
  977. (defrule .......    
  978.  
  979.     (foo1 ?x)    (foo1 bar)    bar    Y
  980.  
  981.     (foo2 ?x&bar|dig)    (foo2 bar)    
  982.  
  983.     => ......
  984.  
  985.  
  986.  
  987. (defrule .......    
  988.  
  989.     (foo1 ?x)    (foo1 bar)    bar    N
  990.  
  991.     (foo2 ?x&bar|dig)    (foo2 dig)
  992.  
  993.     => ......
  994.  
  995.  
  996.  
  997. (defrule .......    
  998.  
  999.     (foo1 ?x)    (foo1 mumble)    mumble    N
  1000.  
  1001.     (foo2 ?x&bar|dig)    (foo2 mumble)
  1002.  
  1003.     => ......
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009. 3.5.2  Predicate Functions
  1010.  
  1011. Sometimes it becomes necessary to constrain a field to more complex
  1012. values than can be defined by logic operators. CLIPS allows the use of
  1013. predicate functions to accomplish this. Predicate functions check to see
  1014. if the value of the field meets the constraints defined in the function. If it
  1015. does, the function returns true (non-zero) and pattern matching continues.
  1016. Otherwise, it returns false (0) and the pattern fails to match. Predicate
  1017. functions are called with a special form of the AND operator (&:). The value
  1018. of the field must be bound to a variable and passed to the function.
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024. Syntax:
  1025.  
  1026.  
  1027.  
  1028.     (<relation> ?x&:(<function> <<arguments>>))
  1029.  
  1030.  
  1031.  
  1032. Multiple predicate functions may be used to constrain a single field. They
  1033. are evaluated from left to right. A number of predicate functions are
  1034. provided by CLIPS and users may also develop their own predicate functions.
  1035. Predicate functions operate on the value attempting to be matched or may
  1036. have arguments passed to them. The predicate functions provided by CLIPS
  1037. are:
  1038.  
  1039.  
  1040.  
  1041.     Function    Purpose
  1042.  
  1043.     (numberp <arg>)    Is the value a number?
  1044.  
  1045.     (stringp <arg>)    Is the value a string (non-number)?
  1046.  
  1047.     (evenp <arg>)    Is the value an even number?
  1048.  
  1049.     (oddp <arg>)    Is the value an odd number?
  1050.  
  1051.  
  1052.  
  1053. User defined predicate functions must take arguments as defined in Section
  1054. 5.2 and should return zero (0) for false and a non-zero number for true.
  1055.  
  1056.  
  1057.  
  1058. In addition to these functions, any defined function may be used as predicate
  1059. function, though only the comparison functions are generally meaningful.
  1060. Use of defined functions as predicates will be described with examples in
  1061. section 3.6. A complete list of defined functions can be found in Appendix A.
  1062.  
  1063.  
  1064.  
  1065. Examples:
  1066.  
  1067.   Pattern on LHS of rule              Fact in the fact-list                  
  1068. Matches? 
  1069.  
  1070.     (foo ?x&:(numberp ?x))    (foo 2)    Y
  1071.  
  1072.     (foo ?x&:(numberp ?x))    (foo bar)    N
  1073.  
  1074.  
  1075.  
  1076.     (foo ?x&:(stringp ?x))    (foo 2)    N
  1077.  
  1078.     (foo ?x&:(stringp ?x))    (foo bar)    Y
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084. 3.6    test Function
  1085.  
  1086. The field functions allow very descriptive constraints to be applied to
  1087. pattern matching. An additional constraint capability is provided with the
  1088. test function. With test you can compare the variable bindings that have
  1089. already occurred in any manner. You can do mathematical comparisons on
  1090. variables (e.g. is the difference between ?x and ?y greater than some
  1091. value?), you can do complex logical or equality comparisons, or you can call
  1092. external functions which compare variables in any way the user desires.
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098. Any kind of defined functions (see section 4.6) may be embedded within a
  1099. test operation. All defined functions use the prefix notation, so the operands
  1100. to a test function always appear after the function name. A number of
  1101. logical, comparison, and arithmetic functions are provided by CLIPS. 
  1102.  
  1103.  
  1104.  
  1105. Syntax:
  1106.  
  1107.  
  1108.  
  1109.     (test    (<defined-function> <<arguments>>))
  1110.  
  1111.  
  1112.  
  1113. Test functions can be nested and are evaluated from the inside out. The
  1114. functions inherit their syntax and terminology from both LISP and C. They
  1115. are:
  1116.  
  1117.  
  1118.  
  1119. Symbol    Function      Use    Means
  1120.                               Logical
  1121.  
  1122.        !         not (inverse)    logical       -
  1123.  
  1124.         &&        and     logical        -
  1125.  
  1126.         ||        or     logical        -
  1127.  
  1128.  
  1129.  
  1130.                            Comparison
  1131.  
  1132.        =         equal (numeric)    (test (= ?x ?y))    ?x = ?y
  1133.  
  1134.     eq    equal (any)    (test (eq ?x ?y))    ?x eq ?y
  1135.  
  1136.         !=        not equal     (test (!= ?x ?y))    ?x - ?y
  1137.  
  1138.         >=        greater than or equal     (test (>= ?x ?y))    ?x >= ?y
  1139.  
  1140.         >         greater than     (test (> ?x ?y))    ?x > ?y
  1141.  
  1142.         <=        less than or equal     (test (<= ?x ?y))    ?x <= ?y
  1143.  
  1144.         <         less than     (test (< ?x ?y))    ?x < ?y
  1145.  
  1146.  
  1147.  
  1148.                          Arithmetic
  1149.  
  1150.         /         division     (test (/ ?x ?y))    ?x / ?y
  1151.  
  1152.         *         multiplication     (test (* ?x ?y))    ?x * ?y
  1153.  
  1154.         **        exponentiation     (test (** ?x ?y))    ?x^?y
  1155.  
  1156.         +         addition     (test (+ ?x ?y))    ?x + ?y
  1157.  
  1158.         -         subtraction     (test (- ?x ?y))    ?x - ?y
  1159.  
  1160.  
  1161.  
  1162. The comparison functions (=, !=, >=, etc) are only valid for comparing
  1163. numeric fields. When checking for equality of strings, the eq comparison
  1164. function should be used. eq can compare strings or numbers and will not
  1165. produce an error when comparing a string to a number.
  1166.  
  1167.  
  1168.  
  1169. Examples:
  1170.  
  1171.  
  1172.  
  1173. The following example checks to see if the difference between ?y and ?x is
  1174. greater than three.
  1175.  
  1176.   Patterns on LHS of rule             Fact in the fact-list                    
  1177. Fires? 
  1178.  
  1179. (defrule .....
  1180.  
  1181.     (foo ?x)    (foo 6)    Y
  1182.  
  1183.     (bar ?y)    (bar 9)
  1184.  
  1185.     (test (> (- ?y ?x) 3))
  1186.  
  1187.     =>  .....
  1188.  
  1189.  
  1190.  
  1191. The next example checks to see if there is a positive slope between two
  1192. points on a line.
  1193.  
  1194.  
  1195.  
  1196.  Patterns on LHS of rule             Fact in the fact-list                     
  1197. Fires? 
  1198.  
  1199. (defrule .....
  1200.  
  1201.     (point ?a ?x1 ?y1)    (point 1 4.00 7.00)    Y
  1202.  
  1203.     (point ?b&~?a ?x2 ?y2)    (point 2 5.00 9.00)    
  1204.  
  1205.     (test (< 0 (/ (- ?y2 ?y1)
  1206.  
  1207.                        (- ?x2 ?x1))))
  1208.  
  1209.     =>  .....
  1210.  
  1211.  
  1212.  
  1213. All defined functions may be used as predicates, although it is usually 
  1214. meaningful only for the comparison functions.
  1215.  
  1216.  
  1217.  
  1218. Examples/multiple patterns:
  1219.  
  1220.   Patterns on LHS of rule             Fact in the fact-list                    
  1221. Fires? 
  1222.  
  1223. (defrule .....
  1224.  
  1225.     (foo1 ?y)    (foo1 3)    Y
  1226.  
  1227.     (foo2 ?x&:(> ?x ?y))    (foo2 5)
  1228.  
  1229.     =>  .....
  1230.  
  1231.  
  1232.  
  1233. (defrule .....
  1234.  
  1235.     (foo1 ?y)    (foo1 9)    N
  1236.  
  1237.     (foo2 ?x&:(> ?x ?y))    (foo2 5)
  1238.  
  1239.     =>  .....
  1240.  
  1241.  
  1242.  
  1243. (defrule .....
  1244.  
  1245.     (foo1 ?y)    (foo1 4)    Y
  1246.  
  1247.     (foo2 ?x&:(= ?y ?x))    (foo2 4)    
  1248.  
  1249.     =>  .....
  1250.  
  1251.  
  1252.  
  1253. (defrule .....
  1254.  
  1255.     (foo1 ?y)    (foo1 bar)    N
  1256.  
  1257.     (foo2 ?x&:(= ?x ?y))    (foo2 5)                    (this is an error!)
  1258.  
  1259.     =>  .....
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265. (defrule .....
  1266.  
  1267.     (foo1 ?y)    (foo1 "4")    N
  1268.  
  1269.     (foo2 ?x&:(= ?y ?x))    (foo2 4)                       (also an error!)
  1270.  
  1271.     =>  .....
  1272.  
  1273.  
  1274.  
  1275. These last two errors could be handled better as follows:
  1276.  
  1277.  
  1278.  
  1279.   Patterns on LHS of rule             Fact in the fact-list                    
  1280. Fires? 
  1281.  
  1282. (defrule .....
  1283.  
  1284.     (foo1 ?y&:(numberp ?y))    (foo1 bar)    N
  1285.  
  1286.     (foo2 ?x&:(= ?x ?y))    (foo2 5)                         (not an error)
  1287.  
  1288.     =>  .....
  1289.  
  1290.  
  1291.  
  1292. or
  1293.  
  1294.  
  1295.  
  1296. (defrule .....
  1297.  
  1298.     (foo1 ?y)    (foo1 bar)    N
  1299.  
  1300.     (foo2 ?x&:(eq ?x ?y))    (foo2 5)                         (not an error)
  1301.  
  1302.     =>  .....
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308. 3.7  Logical Pattern Blocks
  1309.  
  1310. The LHS of CLIPS rules are made up of a series of patterns that represent
  1311. the conditions that must be satisfied in order for the rule to be placed on
  1312. the agenda. Unless otherwise noted, CLIPS assumes that all rules have an
  1313. implicit and surrounding the patterns on the LHS. This means that all
  1314. conditions on the LHS must be met before the rule can be activated. There is
  1315. no need to explicitly define an implicit and condition. However, it is
  1316. possible to change this default and define other combinations of conditions
  1317. which would cause a rule to fire. This is done with logical pattern
  1318. blocks, which allow patterns to be combined using inclusive or, and
  1319. explicit and logic. The entire logic block is treated as a single condition on
  1320. the LHS and must be satisfied along with all other conditions before the rule
  1321. can fire. Logical blocks may be mixed with other logical blocks in any order
  1322. and patterns within the logical block may use field constraints. As with all
  1323. CLIPS features, the blocks are marked by opening and closing parentheses. 
  1324.  
  1325.  
  1326.  
  1327. 3.7.1  Inclusive or
  1328.  
  1329. The or logical block allows any one of several patterns to trigger a rule
  1330. firing. If any of the patterns inside the or block exist, the constraint is
  1331. satisfied. If all other LHS conditions are true, the rule will be activated.
  1332. Any number of patterns may be within an or block. The exact same effect
  1333. could be accomplished by writing multiple rules with similar left and right
  1334. hand sides.
  1335.  
  1336. Syntax:
  1337.  
  1338.  
  1339.  
  1340.     (defrule <name>
  1341.  
  1342.         [(<<additional patterns>>)]          
  1343.  
  1344.         (or    (<<pattern 1>>) 
  1345.  
  1346.                   %
  1347.  
  1348.                   %                
  1349.  
  1350.             (<<pattern n>>))    
  1351.  
  1352.         [(<<additional patterns>>)]          
  1353.  
  1354.         =>             
  1355.  
  1356.         (<<actions>>))                
  1357.  
  1358.  
  1359.  
  1360. If more than one of the patterns in the or block can be met, the rule will
  1361. fire for each possible combination of conditions.
  1362.  
  1363.  
  1364.  
  1365. Example:
  1366.  
  1367.  
  1368.  
  1369.     (defrule system-fault
  1370.  
  1371.         (error-status unknown)
  1372.  
  1373.         (or    (temp high)
  1374.  
  1375.             (valve broken)
  1376.  
  1377.             (pump off))
  1378.  
  1379.         =>         
  1380.  
  1381.         (printout "The system has a fault." crlf))
  1382.  
  1383.  
  1384.  
  1385. Note, the above example could be written as three separate rules:
  1386.  
  1387.  
  1388.  
  1389.     (defrule system-fault
  1390.  
  1391.         (error-status unknown)
  1392.  
  1393.         (pump off)
  1394.  
  1395.         =>         
  1396.  
  1397.         (printout "The system has a fault." crlf))
  1398.  
  1399.  
  1400.  
  1401.      (defrule system-fault
  1402.  
  1403.         (error-status unknown)
  1404.  
  1405.         (valve broken)
  1406.  
  1407.         =>         
  1408.  
  1409.         (printout "The system has a fault." crlf))
  1410.  
  1411.  
  1412.  
  1413.     (defrule system-fault
  1414.  
  1415.         (error-status unknown)
  1416.  
  1417.         (temp high)
  1418.  
  1419.         =>         
  1420.  
  1421.         (printout "The system has a fault." crlf))
  1422.  
  1423.  3.7.2  Explicit and
  1424.  
  1425. An explicit and is provided to allow the mixing of and and or conditions.
  1426. This allows logical combinations of patterns within an or block. The
  1427. condition will be satisfied when all the patterns inside the explicit and
  1428. block are satisfied (plus any additional patterns). Any number of patterns
  1429. may be placed in an and block. Any number of ands may be within an or
  1430. block.
  1431.  
  1432.  
  1433.  
  1434. Syntax:
  1435.  
  1436.  
  1437.  
  1438.     (defrule <name>
  1439.  
  1440.         [(<<additional patterns>>)]          
  1441.  
  1442.         (or    (and    (<<pattern 1>>) 
  1443.  
  1444.                        %
  1445.  
  1446.                       %                
  1447.  
  1448.                 (<<pattern n>>))
  1449.  
  1450.             (<<other patterns>>))   
  1451.  
  1452.         [(<<additional patterns>>)]          
  1453.  
  1454.         =>             
  1455.  
  1456.         (<<actions>>))                
  1457.  
  1458.  
  1459.  
  1460. Example:
  1461.  
  1462.  
  1463.  
  1464.     (defrule system-flow
  1465.  
  1466.         (error-status confirmed)
  1467.  
  1468.         (or    (and    (temp high)
  1469.  
  1470.                 (valve closed))
  1471.  
  1472.             (and    (temp low)
  1473.  
  1474.                 (valve open)))
  1475.  
  1476.         =>         
  1477.  
  1478.         (printout "The system is having a flow problem." crlf))
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484. 3.8  Pattern negation- not         
  1485.  
  1486. Sometimes, the lack of information is meaningful, i.e. you wish to fire a
  1487. rule if a fact does not exist in the fact-list. The not function provides this
  1488. capability. As with logic blocks, any number of normal patterns may also be
  1489. on the LHS of the rule, and field constraints may be used within the negated
  1490. pattern.
  1491.  
  1492.  
  1493.  
  1494. Syntax:
  1495.  
  1496.  
  1497.  
  1498.     (defrule <name>
  1499.  
  1500.         (<<pattern 1>>)
  1501.  
  1502.         (not    (<<pattern 2>>))
  1503.  
  1504.         [(<<additional patterns>>)]          
  1505.  
  1506.         =>             
  1507.  
  1508.         (<<actions>>)) 
  1509.  
  1510.       
  1511.  
  1512. Only one pattern may be negated at a time. Multiple patterns may be negated
  1513. by using multiple not statements. And and or logic blocks may not be
  1514. placed inside a not pattern, although a not may be placed inside an and or
  1515. or. Care must be taken when combining not with or and and blocks; the
  1516. results aren't always obvious! The same is true for variable bindings within
  1517. a negated pattern. Variables that are previously bound may be used freely
  1518. inside a negated pattern. However, variables bound for the first time within
  1519. a negated pattern can only be used in that pattern. Since negated facts are
  1520. treated specially by CLIPS, users should always perform a reset after
  1521. creating or loading rules which include negated patterns.
  1522.  
  1523.  
  1524.  
  1525. Example:
  1526.  
  1527.  
  1528.  
  1529.     (defrule high-flow-rate
  1530.  
  1531.         (temp high)
  1532.  
  1533.         (valve open)
  1534.  
  1535.         (not    (error-status confirmed))
  1536.  
  1537.         =>
  1538.  
  1539.         (printout "Recommend closing of valve due to high temp" crlf))
  1540.  
  1541.  
  1542.  
  1543.     (defrule check-valve
  1544.  
  1545.         (check-status ?valve)
  1546.  
  1547.         (not    (valve-broken ?valve))
  1548.  
  1549.         =>
  1550.  
  1551.         (printout "Device " ?valve " is OK" crlf))
  1552.  
  1553.  
  1554.  
  1555.     (defrule double-pattern
  1556.  
  1557.         (foo bar)
  1558.  
  1559.         (not (foo bar ?x ?x))
  1560.  
  1561.         =>
  1562.  
  1563.         (printout "No foo bar mumble mumble patterns!" crlf ))
  1564.  
  1565.  
  1566.  
  1567. Negating a NOT is illegal, i.e.  (not (not (<<pattern>>))) is not allowed.
  1568.  
  1569.  
  1570.  
  1571.  
  1572.  
  1573. 3.9  Pattern Bindings
  1574.  
  1575. Certain RHS actions, such as retract, can operate on an entire fact. To
  1576. signify which fact they are to act upon, a variable can be bound to an entire
  1577. fact, as follows:
  1578.  
  1579. Syntax:
  1580.  
  1581.  
  1582.  
  1583.     ?<var-name><-(<<fields>>)
  1584.  
  1585.  
  1586.  
  1587. The left arrow, "<-", is a required part of the syntax.
  1588.  
  1589.  
  1590.  
  1591. Example:
  1592.  
  1593.  
  1594.  
  1595.     (defrule dummy
  1596.  
  1597.         (foo 1)
  1598.  
  1599.         ?fact<-(dummy pattern)
  1600.  
  1601.         =>
  1602.  
  1603.         (retract ?fact))
  1604.  
  1605.  
  1606.  
  1607. Section 4: RHS Syntax - Actions
  1608.  
  1609.  
  1610.  
  1611. 4.1  Assert
  1612.  
  1613. Assert allows you to add a fact to the fact-list. Only one fact may be
  1614. asserted in each assert statement. However, multiple asserts may be placed
  1615. on the RHS of a rule.
  1616.  
  1617.  
  1618.  
  1619. Syntax:
  1620.  
  1621.  
  1622.  
  1623.     (assert    (<<pattern>>))
  1624.  
  1625.  
  1626.  
  1627. The fact asserted may contain bound variables, calls to defined functions,
  1628. (see section 4.6), and literals. If an identical copy of the fact already exists
  1629. in the fact-list, the fact will not be added.
  1630.  
  1631.  
  1632.  
  1633. Example:
  1634.  
  1635.  
  1636.  
  1637.     (assert    (foo bar))    ; assert a simple fact
  1638.  
  1639.  
  1640.  
  1641.     (defrule close-valve
  1642.  
  1643.         (temp ?sensor high)
  1644.  
  1645.         (valve ?v open)
  1646.  
  1647.         (not    (error-status confirmed))
  1648.  
  1649.         =>
  1650.  
  1651.         (assert  (Set ?v close))    ;assert a fact with variables
  1652.  
  1653.         (printout "Close valve due to high temp" crlf))
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659. 4.2  Retract  
  1660.  
  1661. Retract allows you to remove facts from the fact-list. The fact (or facts)
  1662. must have been bound on the LHS as described in section 3.9. Multiple facts
  1663. may be retracted with a single retract statement. The retraction of a fact
  1664. also removes all rules from the agenda that depended upon that fact for
  1665. activation.
  1666.  
  1667.  
  1668.  
  1669. Syntax:
  1670.  
  1671.  
  1672.  
  1673.     (retract    ?<fact1>[ . . . ?<factN>])
  1674.  
  1675.  
  1676.  
  1677. Example:
  1678.  
  1679.  
  1680.  
  1681.     (retract ?f1 ?f2 ?f3)
  1682.  
  1683.  
  1684.  
  1685.     (defrule change-valve-status
  1686.  
  1687.         ?f1<-(valve ?v open)
  1688.  
  1689.         ?f2<-(Set ?v close)
  1690.  
  1691.         =>
  1692.  
  1693.         (retract ?f1 ?f2)
  1694.  
  1695.         (assert  (valve ?v close)))
  1696.  
  1697.  
  1698.  
  1699.  
  1700.  
  1701. 4.3  Printout
  1702.  
  1703. Printout allows simple output to the standard output device (usually the
  1704. screen). It will evaluate variable bindings and print the value of a variable
  1705. in the output string. Any number of variables or strings may be sent for
  1706. printout.
  1707.  
  1708.  
  1709.  
  1710. Syntax:
  1711.  
  1712.  
  1713.  
  1714.     (printout <item> ... <item> [crlf])
  1715.  
  1716.  
  1717.  
  1718. Where <item> is a string delimited by double quotes ("), or a bound variable
  1719. The word crlf will force a carriage return/newline and may be placed
  1720. anywhere in the printout string.
  1721.  
  1722.  
  1723.  
  1724. Example:
  1725.  
  1726.  
  1727.  
  1728.     (defrule change-valve-status
  1729.  
  1730.         ?f1<-(valve ?v open)
  1731.  
  1732.         ?f2<-(Set ?v close)
  1733.  
  1734.         =>
  1735.  
  1736.         (retract ?f1 ?f2)
  1737.  
  1738.         (assert  (valve ?v close))
  1739.  
  1740.         (printout "The valve " ?v " has been closed" crlf))
  1741.  
  1742.  
  1743.  
  1744.  
  1745.  
  1746. 4.4  Bind
  1747.  
  1748. Occasionally it is important to create new variables or modify the value of
  1749. previously bound variables on the RHS of a rule. The bind function provides
  1750. this capability for all defined functions (see section 4.6) which return a
  1751. value suitable for binding.
  1752.  
  1753.  
  1754.  
  1755. Syntax:
  1756.  
  1757.  
  1758.  
  1759.     (bind ?<var-name> (<defined-function>))
  1760.  
  1761.  
  1762.  
  1763. Where <var-name> must be a variable name (it may have been previously
  1764. bound), and valid <defined-functions> will be discussed in section 4.6.
  1765.  
  1766.  
  1767.  
  1768. Example:
  1769.  
  1770.  
  1771.  
  1772.     (bind ?input (read))
  1773.  
  1774.  
  1775.  
  1776.     (bind ?value (+ ?x ?y))
  1777.  
  1778.  
  1779.  
  1780.  
  1781.  
  1782. 4.5  Pattern Expansion
  1783.  
  1784. Sometimes it is preferable to expand a pattern within an assertion. This 
  1785. can be done with the equals (=) operator. The equals operator allows you to
  1786. call a defined function (see section 4.6) inside an assert. The value is
  1787. incorporated directly into the pattern at the position the function was
  1788. called.
  1789.  
  1790.  
  1791.  
  1792. Syntax:
  1793.  
  1794.  
  1795.  
  1796.     (assert ([<<fields>>] =(<defined-function> <<args...>>) [<<fields>>])
  1797.  
  1798.  
  1799.  
  1800. This is exactly the same as binding a variable to the return from a defined
  1801. function using the bind function and then placing the variable inside the
  1802. assert.
  1803.  
  1804.  
  1805.  
  1806. Example:
  1807.  
  1808.  
  1809.  
  1810.     (assert (foo bar =(+ ?x ?y)))
  1811.  
  1812.  
  1813.  
  1814.     (defrule user-close-valve
  1815.  
  1816.         (goal close valve)
  1817.  
  1818.         (input-from user)
  1819.  
  1820.         =>
  1821.  
  1822.         (printout "Which valve should be closed? ")
  1823.  
  1824.         (assert (close-valve =(read))))
  1825.  
  1826.  
  1827.  
  1828.  
  1829.  
  1830. 4.6  Defined functions
  1831.  
  1832. A number of CLIPS actions, such as bind or assert, allow calls to a defined
  1833. function. These functions can be defined by the user or provided by CLIPS.
  1834. User functions must be defined according to the method described in section
  1835. 5.0 and variables may be passed to these functions. The function must return
  1836. either a pointer to a single string or a floating point number. All defined
  1837. functions use the prefix notation, so the arguments always appear after the
  1838. function name. Appendix A gives a complete list of defined functions. In
  1839. addition to the defined functions used with test, there are functions which
  1840. are usually used only on the RHS. These are:
  1841.  
  1842.  
  1843.  
  1844.    function    Capability
  1845.  
  1846.     read    read input from the keyboard
  1847.  
  1848.     gensym    create a random (string) word
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854. 4.6.1  read
  1855.  
  1856. The read function allows a user to input information for a single field. All
  1857. the standard field rules (e.g. multiple words must be embedded within
  1858. quotes) apply.
  1859.  
  1860.  
  1861.  
  1862. Example:
  1863.  
  1864.  
  1865.  
  1866.     (bind ?input (read))
  1867.  
  1868.  
  1869.  
  1870.     (assert (new fact =(read) input))
  1871.  
  1872.  
  1873.  
  1874.  
  1875.  
  1876. 4.6.2  gensym
  1877.  
  1878. The gensym function returns a special, sequenced word, typically stored as
  1879. a single field. This is primarily for tagging patterns which need a unique
  1880. identifier but the user doesn't care what the identifier is. Multiple calls to
  1881. gensym are guaranteed to return different identifiers, however, they could
  1882. conflict with user defined fields. The gensym returns strings of the form:
  1883.  
  1884.  
  1885.  
  1886. genX
  1887.  
  1888.  
  1889.  
  1890. where X is a number. The first call to gensym returns gen1, all subsequent
  1891. calls increment the number. If users plan to use the gensym feature, they
  1892. should avoid creating facts which include a user defined field of this form.
  1893.  
  1894.  
  1895.  
  1896. Example:
  1897.  
  1898.  
  1899.  
  1900.     (assert (new-id  =(gensym) flag1 7))
  1901.  
  1902.  
  1903.  
  1904. which, on the first call, generates a fact of the form:
  1905.  
  1906.  
  1907.  
  1908.     (new-id gen1 flag1 7.00)
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914. 4.6.3  Functions to operate on Multi-field variables
  1915.  
  1916. A number of functions to operate on multi-field variables ($? variables)
  1917. will be provided in future releases of CLIPS as defined functions.
  1918.  
  1919.  
  1920.  
  1921. Examples using Arithmetic Defined Functions:
  1922.  
  1923. A number of the defined functions provide computational capability. They
  1924. should only be used on numeric arguments. CLIPS does not check the validity
  1925. of the numeric operation, so the user is responsible for preventing error.
  1926.  
  1927.  
  1928.  
  1929.     (bind ?var (/ ?x ?y))    ;means ?var = ?x / ?y
  1930.  
  1931.  
  1932.  
  1933.     (bind ?var (* ?x ?y))    ;means ?var = ?x * ?y
  1934.  
  1935.  
  1936.  
  1937.     (bind ?var (** ?x ?y))    ;means ?var = ?x^?y
  1938.  
  1939.  
  1940.  
  1941.     (bind ?var (+ ?x ?y))    ;means ?var = ?x + ?y
  1942.  
  1943.  
  1944.  
  1945.     (bind ?var (- ?x ?y))    ;means ?var = ?x - ?y
  1946.  
  1947.  
  1948.  
  1949. The examples all use the bind function, but arithmetic function calls can be
  1950. used anywhere a defined function is valid.
  1951.  
  1952.  
  1953.  
  1954.  
  1955.  
  1956. 4.7  Call
  1957.  
  1958. The defined functions discussed in Section 4.6 are usually called for their
  1959. return value. Effect functions are called for effect only. The call function
  1960. allows this capability from the RHS of a rule. Effect functions should be
  1961. written in C and must follow the conventions described in Section 5.0.
  1962. Arguments may be passed to the functions within the limitations described
  1963. in that section. 
  1964.  
  1965.  
  1966.  
  1967. Syntax:
  1968.  
  1969.  
  1970.  
  1971.     (call (<function-name> [<<args...>>]))
  1972.  
  1973.  
  1974.  
  1975. Functions called in this manner must be called for effect only. The return
  1976. value will not be captured.
  1977.  
  1978.  
  1979.  
  1980. Example:
  1981.  
  1982.  
  1983.  
  1984.      (defrule display-valve-system
  1985.  
  1986.         (valve ?v1 open)
  1987.  
  1988.         (valve ?v2&~v1 closed)
  1989.  
  1990.         =>
  1991.  
  1992.         (call (display-valves ?v1 ?v2)))
  1993.  
  1994.  
  1995.  
  1996. CLIPS provides one pre-defined effect function, called setgen.
  1997.  
  1998.  
  1999.  
  2000.  
  2001.  
  2002. 4.7.1  setgen
  2003.  
  2004. The setgen function allows the user to set the starting number used by 
  2005. gensym (see section 4.6.2).
  2006.  
  2007.  
  2008.  
  2009. Syntax:
  2010.  
  2011.  
  2012.  
  2013.     (call (setgen <num>))
  2014.  
  2015.  
  2016.  
  2017. where <num> must be a positive integer value. All subsequent calls to
  2018. gensym will return a sequnced word with the numeric portion of the word
  2019. starting at <num>.
  2020.  
  2021.  
  2022.  
  2023. Example:
  2024.  
  2025.  
  2026.  
  2027.     (call (setgen 32))    ; calls to gensym will return
  2028.                     ;   gen32, gen 33, etc.
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034. 4.8  If...then...else
  2035.  
  2036. Under certain circumstances, it is preferable to take actions based on
  2037. parameter testing on the RHS of a rule instead of writing two rules. CLIPS
  2038. provides an if...then...else structure to allow this kind of operation.
  2039.  
  2040.  
  2041.  
  2042. Syntax:
  2043.  
  2044.  
  2045.  
  2046.     (if (<defined-function> <<args...>>)
  2047.  
  2048.      then
  2049.  
  2050.         (<<action 1>>)
  2051.  
  2052.             %
  2053.  
  2054.         (<<action n>>)
  2055.  
  2056.     [else
  2057.  
  2058.         (<<action 1>>)
  2059.  
  2060.             %
  2061.  
  2062.         (<<action n>>)])
  2063.  
  2064.  
  2065.  
  2066. Any number of allowable RHS actions may be used inside the then or else
  2067. section, including another if...then...else structure. The else portion is
  2068. optional. All defined functions are available for use in this structure and are
  2069. used as they are in test. Variables used in the comparison must have been
  2070. previously bound.
  2071.  
  2072.  
  2073.  
  2074. Example:
  2075.  
  2076.  
  2077.  
  2078.     (defrule closed-valves
  2079.  
  2080.         (temp high)
  2081.  
  2082.         (valve ?v closed)
  2083.  
  2084.         =>
  2085.  
  2086.         (if (= ?v 6)
  2087.  
  2088.          then
  2089.  
  2090.             (printout "The special valve " ?v " is closed!" crlf)
  2091.  
  2092.             (assert (some special operation))
  2093.  
  2094.          else
  2095.  
  2096.             (printout "Valve " ?v " is normally closed" crlf)))
  2097.  
  2098.  
  2099.  
  2100.  
  2101.  
  2102. 4.9  While
  2103.  
  2104. The while structure is provided to allow simple looping on the RHS of a
  2105. rule. Its use is similar to that of if...then...else:
  2106.  
  2107.  
  2108.  
  2109. Syntax:
  2110.  
  2111.  
  2112.  
  2113.     (while (<defined-function> [<<args...>>])
  2114.  
  2115.         (<<action 1>>)
  2116.  
  2117.             %
  2118.  
  2119.         (<<action n>>))
  2120.  
  2121.  
  2122.  
  2123. Again, all defined functions are available for use in while. Any number of
  2124. allowable RHS actions may be placed inside the while block, including
  2125. if...then...else or additional while structures. 
  2126.  
  2127.  
  2128.  
  2129. Example:
  2130.  
  2131.  
  2132.  
  2133.     (defrule open-valves
  2134.  
  2135.         (valves-open-through ?v)
  2136.  
  2137.         =>
  2138.  
  2139.         (while (> ?v 0)
  2140.  
  2141.             (printout "Valve " ?v " is open" crlf)
  2142.  
  2143.             (bind ?v (- ?v 1))))
  2144.  
  2145.  
  2146.  
  2147.  
  2148.  
  2149. Section 5: Integrating CLIPS with External Functions
  2150.  
  2151.  
  2152.  
  2153. A user can define external functions for use on both the LHS and the RHS of
  2154. rules. If a defined function is to be used as a predicate, it must return a
  2155. floating point number. All other defined functions must return either a
  2156. string or a floating point. Effect functions are called merely for effect
  2157. and their return value is not of concern. Both defined functions and
  2158. effect functions are described to CLIPS in the same way. Since C is the
  2159. only external language currently supported, all of the examples are in C.
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165. 5.1  Declaring User Defined External Functions to CLIPS
  2166.  
  2167. A function called usrfuncs must be included inside the user's source code.
  2168. This function should call the define_function routine for every function
  2169. the user wants CLIPS to know about. The user's source code is then compiled
  2170. and linked with CLIPS. 
  2171.  
  2172.  
  2173.  
  2174. A sample usrfuncs declaration follows:
  2175.  
  2176.  
  2177.  
  2178. usrfuncs()
  2179.  
  2180.    {
  2181.  
  2182.     define_function("fun",'s',fun);
  2183.  
  2184.     define_function("dummy",'i',my_dummy);
  2185.  
  2186.     /* Additional define function statements could go here. */
  2187.  
  2188.    }
  2189.  
  2190.  
  2191.  
  2192. The first argument to define_function is the CLIPS name, a string
  2193. representation of the  name that will be used inside CLIPS rules. The second
  2194. argument is the return type where 'i' = integer, 'f' = float, 'c' = character, 
  2195. and
  2196. 's' = pointer to character.  The third argument is a pointer to the actual
  2197. function, the compiled function name. The string representation (first
  2198. argument) need not be the same as the actual function name (third
  2199. argument). 
  2200.  
  2201.  
  2202.  
  2203. User defined functions are searched before system functions. If the user
  2204. defines a CLIPS name which is the same as one of the defined functions
  2205. already provided, the user function will be executed in its place. Appendix A
  2206. contains a list of the defined functions provided by CLIPS.
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212. 5.2  Passing variables from CLIPS to External Functions
  2213.  
  2214. Although arguments are listed directly following a function name inside
  2215. CLIPS rules, CLIPS will call the function without any arguments. Instead,
  2216. the parameters are stored in internal CLIPS arrays and can be accessed by
  2217. calling the functions:
  2218.  
  2219.  
  2220.  
  2221.     int num_args();
  2222.  
  2223.     char *rstring(<arg>);
  2224.  
  2225.     float  rfloat(<arg>);
  2226.  
  2227.  
  2228.  
  2229. A call to num_args() will return an integer telling how many arguments the
  2230. function was called with. A call to rstring() returns a character pointer, and
  2231. rfloat() returns a floating point number. The parameters have to be
  2232. requested by the called function one at a time. This is done by specifying the
  2233. parameter position number as the <arg> to rstring() or rfloat().
  2234.  
  2235.  
  2236.  
  2237. Example:
  2238.  
  2239.  
  2240.  
  2241. (assert (foo bar =(fun ?x "wow" 18.9))
  2242.  
  2243.  
  2244.  
  2245. The function fun() would look something like this:
  2246.  
  2247.  
  2248.  
  2249. #include "clips.h"    /* this declaration should appear on     */
  2250.  
  2251.         /* top of each of your "C" source files. */
  2252.  
  2253.  
  2254.  
  2255. char *fun()
  2256.  
  2257.    {
  2258.  
  2259.     char *arg1;
  2260.  
  2261.     char *arg2;       /* pointers to characters, arrays */
  2262.  
  2263.     float arg3;        /* floating point argument.          */
  2264.  
  2265.     int num_passed;
  2266.  
  2267.  
  2268.  
  2269.     num_passed = num_args();    /*  find out how many args where passed */
  2270.  
  2271.      %
  2272.  
  2273.     %
  2274.  
  2275.     arg1 = rstring(1);     /* ask for the first argument */
  2276.  
  2277.     arg2 = rstring(2);     /* and for the second one.      */
  2278.  
  2279.     arg3 = rfloat(3);     /* and the third is a float      */
  2280.  
  2281.     %
  2282.  
  2283.     %
  2284.  
  2285.     return("sample text");    /* returns a string */
  2286.  
  2287.    }
  2288.  
  2289.  
  2290.  
  2291. Note that rfloat() is called with a 3 for the third parameter, even though it
  2292. is the first floating point number accessed. Also, fun() should be defined in
  2293. usrfuncs().
  2294.  
  2295.  
  2296.  
  2297. 5.3  Passing data from an External Function to CLIPS
  2298.  
  2299. An external function can assert a new fact into the CLIPS fact-list.  This is
  2300. done by specifying a global C function declaration:
  2301.  
  2302.  
  2303.  
  2304. FACT *assert();
  2305.  
  2306.  
  2307.  
  2308. To assert a fact from the user function, simply call assert():
  2309.  
  2310.  
  2311.  
  2312. assert("string");
  2313.  
  2314.  
  2315.  
  2316. where "string" is a single string made up of floating point numbers and
  2317. words. The return value is variable of type FACT, which can not be used in
  2318. any meaningful way, as of yet. Future functions may be able to use this
  2319. information.
  2320.  
  2321.  
  2322.  
  2323. Examples:
  2324.  
  2325.  
  2326.  
  2327.    assert("99 is a number");
  2328.  
  2329.    assert("Hello there");
  2330.  
  2331.    assert("10 plus two is 12");
  2332.  
  2333.  
  2334.  
  2335. To construct a string based on variable data, use sprintf:
  2336.  
  2337.  
  2338.  
  2339. char string[50];
  2340.  
  2341. sprintf(string,"valve %f %s",number, char_pointer);
  2342.  
  2343. assert(string);
  2344.  
  2345.  
  2346.  
  2347. 5.4  Extended Function Libraries for CLIPS
  2348.  
  2349. The method used by CLIPS to incorporate user defined functions allows the
  2350. creation of whole libraries of external functions; making CLIPS highly
  2351. extensible. An example of such an extended package is included with the
  2352. CLIPS source. This example adds a number of math and trigonometric
  2353. functions to CLIPS for use as defined functions. Users who wish to create
  2354. other extended packages for CLIPS should follow the style shown in the
  2355. math package. Documentation should be included demonstrating how to use
  2356. the functions and also how to install them.
  2357.  
  2358.  
  2359.  
  2360. Section 6: Using CLIPS
  2361.  
  2362.  
  2363.  
  2364. CLIPS expert systems may be executed in two ways; interactively using a
  2365. simple, line type interface, or as embedded expert systems in which the
  2366. user provides a main program and controls execution of the expert system
  2367. as part of a sequential program.
  2368.  
  2369.  
  2370.  
  2371. 6.1  Interactive Interface Commands
  2372.  
  2373. CLIPS provides a simple, interactive, line type interface for high
  2374. portability. The standard usage is: Create or edit rules using any standard
  2375. text editor and save the file(s) as a text file. Exit the editor and execute
  2376. CLIPS. Load rule file(s) into CLIPS. The interface provides commands for
  2377. viewing the current state of the system, tracing execution, adding or
  2378. removing information, or clearing CLIPS.  Following is a list of all the
  2379. available CLIPS interface commands:
  2380.  
  2381.  
  2382.  
  2383. CLIPS Environment Commands
  2384.  
  2385. (reset)    Resets the database. Removes all activations from the
  2386. agenda and all facts from the database, and asserts all
  2387. facts listed in deffacts statements into the database.
  2388.  
  2389.  
  2390.  
  2391. (clear)    Removes all rules and deffacts from the environment. 
  2392. Removes all activations from the agenda and all facts
  2393. from the database.
  2394.  
  2395.  
  2396.  
  2397. (run [<run-limit>])    Starts execution of the rules.  If <run-limit> is
  2398. specified, execution will cease after <run-limit> rule
  2399. firings or when the agenda contains no rule
  2400. activations, otherwise execution will cease when the
  2401. agenda contains no rule activations.
  2402.  
  2403.     Example: (run 3)
  2404.  
  2405.  
  2406.  
  2407. (load <file-name>)    Loads the rules stored in the file specified by
  2408. <file-name> into the environment.
  2409.  
  2410.     Example:  (load "rules.txt")
  2411.  
  2412.  
  2413.  
  2414. (save <file-name>)    Saves all of the rules in the current environment into
  2415. the file specified by <file-name>.
  2416.  
  2417.     Example:  (save "rules.txt")
  2418.  
  2419.  
  2420.  
  2421.  
  2422.  
  2423. CLIPS System Status Commands
  2424.  
  2425. (facts)    Displays all facts stored in the fact-list.
  2426.  
  2427.  
  2428.  
  2429. (rules)    Displays the names of all rules stored in the database.
  2430.  
  2431.  
  2432.  
  2433. (agenda)    Displays all activations on the agenda.
  2434.  
  2435.  
  2436.  
  2437. (pprule <rule>)    Displays the text of the rule named <rule>.
  2438.  
  2439.     Example:  (pprule bogus-name)
  2440.  
  2441.  
  2442.  
  2443. CLIPS Debugging Commands
  2444.  
  2445. (watch <item>)    If <item> is facts, all fact assertions and retractions
  2446. will be displayed.  If <item> is rules, all rule firings
  2447. will be displayed.  If <item> is activations, all rule
  2448. activations and deactivations will be displayed.
  2449.  
  2450.     Example:  (watch rules)
  2451.  
  2452.  
  2453.  
  2454. (unwatch <item>)    Deactivates the watch command for <item>. 
  2455.  
  2456.     Example:  (unwatch facts)
  2457.  
  2458.  
  2459.  
  2460. Additional CLIPS Commands
  2461.  
  2462. (assert <fact>)    Asserts <fact> into the database.
  2463.  
  2464.     Example:  (assert (this is a fact))
  2465.  
  2466.  
  2467.  
  2468. (retract <fact#>)    Retracts the fact number <fact#> from the database.
  2469.  
  2470.     Example:  (retract 1)
  2471.  
  2472.  
  2473.  
  2474. (excise <rule>)    Remove the rule named <rule> from the system without
  2475. changing anything system state.
  2476.  
  2477.  
  2478.  
  2479. (undeffacts <name>)    Reverse the affect of a deffacts statement. All facts
  2480. listed in the deffacts construct named <name> will no
  2481. longer be put into the initial fact-list after a reset.
  2482.  
  2483.  
  2484.  
  2485. Defrule and deffacts blocks, as defined in Section 2, may also be entered at
  2486. top level of the interactive interface.
  2487.  
  2488.  
  2489.  
  2490.  
  2491.  
  2492. 6.2    Embedded CLIPS Commands
  2493.  
  2494. CLIPS can be embedded within other programs. In fact, it is relatively
  2495. simple. To embed CLIPS, add this include statement to your main program
  2496. file:
  2497.  
  2498.  
  2499.  
  2500. #include <clips.h>
  2501.  
  2502.  
  2503.  
  2504. (This will have to be tailored so that the compiler on your system can find
  2505. the CLIPS include file.) Your main program must initialize CLIPS by calling
  2506. the function init_clips() sometime prior to loading rules. Also,
  2507. usrfuncs() must be defined if CLIPS calls any external functions. Compile
  2508. and link all your code with all CLIPS files EXCEPT the CLIPS main.o. 
  2509.  
  2510.  
  2511.  
  2512. Within your program, several functions are available to set up and control
  2513. CLIPS:
  2514.  
  2515.  
  2516.  
  2517.  
  2518.  
  2519. init_clips()    Initializes the CLIPS system. Must be called prior to
  2520. any other CLIPS function call. No meaningful return
  2521. value.
  2522.  
  2523.  
  2524.  
  2525. run(<run_limit>)    Fires <run_limit> number of rules, which should be an
  2526. integer. If <run_limit> is minus one (-1), then rules
  2527. will fire until the agenda is empty. Returns an  integer
  2528. value: the number of rules that were fired.
  2529.  
  2530.  
  2531.  
  2532. clear_clips()    Clears the CLIPS environment. No meaningful return
  2533. value. See clear in section 6.1.
  2534.  
  2535.  
  2536.  
  2537. reset_clips()    Resets the CLIPS environment. No meaningful return
  2538. value. See reset in section 6.1.
  2539.  
  2540.  
  2541.  
  2542. load_rules(<file>)    Loads a set of rules into the CLIPS database. Returns
  2543. an integer value. If positive, value is the number of
  2544. constructs (defrules and deffacts) loaded. If negative,
  2545. then an error was encountered during the load process.
  2546. If an error is encountered, load_rules will still
  2547. attempt to read the entire file. 
  2548.  
  2549.  
  2550.  
  2551. assert(<pattern>)    Asserts a fact into the CLIPS fact-list. <pattern> must
  2552. be a pointer to a string, as discussed in section 5.3. It
  2553. returns a pointer to a FACT structure, which is not
  2554. very meaningful externally.
  2555.  
  2556.  
  2557.  
  2558. Appendix A: Defined Functions Provided by CLIPS
  2559.  
  2560.  
  2561.  
  2562. The following table lists all the defined functions provided by CLIPS along
  2563. with examples of their use.
  2564.  
  2565.  
  2566.  
  2567. Symbol    Function      Use    Means
  2568.                               Logical
  2569.  
  2570.        !         not (inverse)    logical       -
  2571.  
  2572.         &&        and     logical        -
  2573.  
  2574.         ||        or     logical        -
  2575.  
  2576.  
  2577.  
  2578.                            Comparison
  2579.  
  2580.        =         equal (numeric)    (test (= ?x ?y))    ?x = ?y
  2581.  
  2582.     eq    equal (all)    (test (eq ?x ?y))    ?x eq ?y
  2583.  
  2584.         !=        not equal     (test (!= ?x ?y))    ?x - ?y
  2585.  
  2586.         >=        greater than or equal     (test (>= ?x ?y))    ?x >= ?y
  2587.  
  2588.         >         greater than     (test (> ?x ?y))    ?x > ?y
  2589.  
  2590.         <=        less than or equal     (test (<= ?x ?y))    ?x <= ?y
  2591.  
  2592.         <         less than     (test (< ?x ?y))    ?x < ?y
  2593.  
  2594.  
  2595.  
  2596.                          Arithmetic
  2597.  
  2598.         /         division     (test (/ ?x ?y))    ?x / ?y
  2599.  
  2600.         *         multiplication     (test (* ?x ?y))    ?x * ?y
  2601.  
  2602.         **        exponentiation     (test (** ?x ?y))    ?x^?y
  2603.  
  2604.         +         addition     (test (+ ?x ?y))    ?x + ?y
  2605.  
  2606.         -         subtraction     (test (- ?x ?y))    ?x - ?y
  2607.  
  2608.  
  2609.  
  2610.                           Predicate
  2611.  
  2612.     Function           Use    Means
  2613.  
  2614.     (numberp <arg>)    (foo ?x&:(numberp ?x))    Is the value a number?
  2615.  
  2616.     (stringp <arg>)    (foo ?x&:(stringp ?x))    Is the value a string?
  2617.  
  2618.     (evenp <arg>)    (foo ?x&:(evenp ?x))    Is the value an even number?
  2619.  
  2620.     (oddp <arg>)    (foo ?x&:(oddp ?x))    Is the value an odd number?
  2621.  
  2622.  
  2623.  
  2624.  
  2625.  
  2626. =   is for comparison between numbers
  2627.  
  2628. eq is for any kind of comparison
  2629.  
  2630. Appendix B: Installing CLIPS
  2631.  
  2632.  
  2633.  
  2634. CLIPS was designed for portability. To this date, CLIPS has been installed on
  2635. over half a dozen kinds of computers with only minor modifications to the
  2636. source code. In many cases, no modifications were required. It should run on
  2637. any system which supports a full Kernighan and Ritchie C compiler. The beta
  2638. release of the source code is available from the Mission Planning and
  2639. Analysis Division's Artificial Intelligence Section until August 1986. All
  2640. future releases will be available through COSMIC. Only the source code files
  2641. are provided. Installing the executable version is up to the user. This section
  2642. describes a step by step plan of how to do so in a fairly generic manner.
  2643.  
  2644.  
  2645.  
  2646. 1)    Load the source code onto your system
  2647.  
  2648.     The following C source files are necessary to set up the CLIPS system:
  2649.  
  2650.  
  2651.  
  2652.         clips.c    main.c    npsr.c
  2653.  
  2654.         usrint.c    parser.c    sysdep.c
  2655.  
  2656.         structdef.h    constdef.h    clips.h
  2657.  
  2658.  
  2659.  
  2660. 2)    Modify all include statements
  2661.  
  2662.     Any include statements in each of the ".c" files, of the form:
  2663.  
  2664.  
  2665.  
  2666.     #include "constdef.h"
  2667.  
  2668.       #include "structdef.h"
  2669.  
  2670.     #include "clips.h"
  2671.  
  2672.  
  2673.  
  2674.     may have to be changed to match the way your compiler searches for
  2675. include files.
  2676.  
  2677.  
  2678.  
  2679. 3)    Compile all of the ".c" files to object code 
  2680.  
  2681.     The ".h" files are include file used by the other files and do not need to be
  2682. compiled. 
  2683.  
  2684.  
  2685.  
  2686. 4)    Create the interactive CLIPS executable element
  2687.  
  2688.     To create the interactive CLIPS executable, link together all the object
  2689. files. This executable will provide the interactive interface defined in
  2690. section 6.1. If user defined functions are needed, compile the source code
  2691. for those functions and modify the usrfuncs definition in main.c to
  2692. reflect your functions. Link your object files with the rest of the CLIPS
  2693. files and the modified main file to create an interactive CLIPS with
  2694. external user functions.
  2695.  
  2696.  
  2697.  
  2698.  
  2699.  
  2700. 5)    (optional) Create an embedded CLIPS executable element
  2701.  
  2702.     To create an embedded CLIPS, remove the object file "main.o" from the
  2703. link list and instead link with your own main function. Be sure to follow
  2704. the guidelines discussed in section 6.2 for embedded CLIPS applications. 
  2705.  
  2706.   
  2707.  
  2708. 6)    Test the executable version
  2709.  
  2710.     The executable version of CLIPS can be tested as follows: execute CLIPS
  2711. in the interactive mode, load the MAB.CLP file, reset and run. The monkey
  2712. should eat the banannas if CLIPS is working properly.
  2713.  
  2714.  
  2715.  
  2716. The sysdep.c source file contains some potentially system dependent code. 
  2717. Functions are provided, for example, to allow for timing. The default
  2718. settings within sysdep.c are settings which should run on any computer.  You
  2719. may wish to change sysdep.c to customize it for a specific computer.
  2720. Eventually, stub functions may be included in this file to support features
  2721. like windowed interfaces, mouse support, or other fancy, machine dependent
  2722. features.
  2723.  
  2724.  
  2725.  
  2726. Special Installation notes
  2727.  
  2728. CLIPS was designed for portability and strictly follows the K&R definition
  2729. of the language. The one exception to this is in variable and function names.
  2730. To improve code readability, we have used fairly lengthy (more than 8
  2731. characters) variable names. Every compiler we have used supports this.
  2732. However, some compilers need a special option turned on during compilation.
  2733. If your compiler does not default to long variable namnes (at least 14
  2734. characters) then that option needs to be turned on during compilation. 
  2735.  
  2736.  
  2737.  
  2738. Another potentially important note. Some microcomputer compilers support
  2739. either large or small memory compilation. CLIPS should always use the
  2740. large memory option. 
  2741.  
  2742.  
  2743.  
  2744. The memory allocation functions are included in the sysdep.c file because
  2745. experience has shown that this is an area which may benefit from
  2746. customization. Some compilers provide lower level memory allocation than
  2747. malloc. Changing the malloc and free calls in sysdep.c to those specific
  2748. for your system may provide significant performance benefits.
  2749.  
  2750.  
  2751.  
  2752. Any undocumented features (bugs) should be brought to the attention of
  2753.  
  2754.  
  2755.  
  2756.       Gary Riley or Chris Culbert
  2757.  
  2758.       NASA/Johnson Space Center
  2759.  
  2760.       Mission Planning & Analysis Division
  2761.  
  2762.       Artificial Intelligence Section - FM72
  2763.  
  2764.       Houston, Texas   77058
  2765.  
  2766. Appendix C: Performance Notes
  2767.  
  2768.  
  2769.  
  2770. CLIPS is a rule language based on the Rete algorithm. The Rete algorithm
  2771. was specifically designed to provide very efficient pattern matching. CLIPS
  2772. has attempted to implement this algorithm in a manner which combines
  2773. efficient performance with powerful features. When used properly, CLIPS
  2774. can provide very reasonable performance, especially on powerful computers.
  2775. However, to use CLIPS properly requires some understanding of how the
  2776. pattern matcher works.
  2777.  
  2778.  
  2779.  
  2780. Prior to beginning execution, each rule is loaded into the system and a
  2781. network of all the patterns which appear on the LHS of any rule is
  2782. constructed. As facts are asserted into the fact-list, the facts are filtered
  2783. through the pattern network. If the form of the pattern (number of fields and
  2784. literal fields) matches any of the patterns in the network, the rule(s) with
  2785. that pattern are partially instantiated. When facts exist which match all the
  2786. patterns on the LHS of the rule, then variable bindings (if any) are
  2787. considered. They are considered from the top to the bottom, i.e. the first
  2788. pattern on the LHS of a rule is considered, then the second, and so on. If the
  2789. field values for all patterns are consistent with the constraints applied to
  2790. the variables, then the rule(s) are activated and placed on the agenda.
  2791.  
  2792.  
  2793.  
  2794. This is a very simplistic description of what occurs in CLIPS, but it gives
  2795. the basic idea. A number of important considerations come out of this. Basic
  2796. pattern matching is done by filtering through the pattern network. The time
  2797. involved in doing this is fairly constant. The slow portion comes from
  2798. comparing variable bindings across patterns. Therefore, the single most
  2799. important performance factor is the ordering of patterns on the LHS of the
  2800. rule. Unfortunately, there are no hard and fast methods which will always
  2801. order the patterns properly. At best, there seem to be three "quasi" methods
  2802. for ordering the patterns:
  2803.  
  2804.  
  2805.  
  2806. 1)    Most specific to most general. The more wildcards or unbound variables
  2807. there are in a pattern, the lower it should go. If the rule firing can be
  2808. controlled by a single pattern, place that pattern first. This technique is
  2809. often used to provide control structure in an expert system, i.e. some
  2810. kind of "phase" fact. Putting this kind of pattern first will guarantee that
  2811. the rest of the rule will not be considered until that pattern exists. This
  2812. is most effective if the single pattern is a literal pattern. If multiple
  2813. patterns with variable bindings control rule firing, then arrange the
  2814. patterns so that the most important variables are bound first and then
  2815. compared as soon as possible to the other pattern constraints.
  2816.  
  2817.  
  2818.  
  2819. 2)    Patterns with the lowest number of occurrences in the fact-list should
  2820. go near the top. A large number of patterns of a particular form in the
  2821. fact-list cause can cause numerous partial instantiations of a rule which
  2822. have to be "weeded" out by comparing the variable bindings, a slower
  2823. operation.
  2824.  
  2825.  
  2826.  
  2827. 3)    Volatile patterns (ones that are retracted and asserted continuously)
  2828. should go last, particularly if the rest of the patterns are mostly
  2829. independent. Every time a fact is asserted, it must be filtered through
  2830. the network. If it causes a partial rule instantiation, the variable
  2831. bindings must then be considered. By putting volatile patterns last, the
  2832. variable bindings will only be checked if all the rest of the patterns
  2833. already exist.
  2834.  
  2835.  
  2836.  
  2837. These rules are not independent and commonly conflict with each other. At
  2838. best they provide some rough guidelines. Since all system have these
  2839. characteristics in different proportions, it is not evident at a glance what
  2840. the most efficient manner of ordering patterns for a given system is. The
  2841. best approach is to develop the rules without considering ordering. When the
  2842. reasoning is fairly well verified, then experiment with the patterns until
  2843. the optimum configuration is found.
  2844.  
  2845.  
  2846.  
  2847. Another performance issue is the use of multi-field variables and wildcards
  2848. ($?). Although they provide a powerful capability, they must be used very
  2849. carefully. Since they can bind to zero or more fields, they can cause
  2850. multiple instantiations of a single rule. In particular, the use of multiple
  2851. multi-field variables in one pattern can cause a very large number of
  2852. instantiations (see example, section 3.3).
  2853.  
  2854.  
  2855.  
  2856. Some final notes on performance: Our experience suggests that you should
  2857. keep the expert system "lean and mean". Don't use the fact-list as a
  2858. database for storage of extraneous information. Store and pattern match
  2859. only on that information necessary for reasoning. Keep the pattern matching
  2860. to a minimum and be as specific as possible. Lots of short, simple rules
  2861. perform better than long, complex rules. 
  2862.  
  2863.  
  2864.  
  2865.  
  2866.  
  2867.  
  2868.  
  2869. Appendix D: Glossary
  2870.  
  2871.  
  2872.  
  2873. This section defines some of the terminology used throughout this manual. 
  2874.  
  2875.  
  2876.  
  2877. activation
  2878.  
  2879.     A rule is activated if all it's conditions are satisfied and it is ready to
  2880. fire. An activated rule is placed on the agenda.
  2881.  
  2882.  
  2883.  
  2884. agenda
  2885.  
  2886.     The agenda is a list of all the rules that are presently ready to fire. It
  2887. is sorted by salience values. When a rule is activated, it is placed in
  2888. front of all the rules that have salience values less than or equal to
  2889. its own. The rule at the front of the agenda is the next rule that will
  2890. fire.  
  2891.  
  2892.  
  2893.  
  2894. defined function
  2895.  
  2896.     A special class of external function, defined by the user or provided by
  2897. CLIPS. These functions are called for their return value and must
  2898. return a valid CLIPS data type (floating point number or string
  2899. pointer).
  2900.  
  2901.  
  2902.  
  2903. effect function
  2904.  
  2905.     A special class of external function, defined by the user of provided by 
  2906. CLIPS. These functions are called for side effect only. They are called 
  2907. from the RHS using call.
  2908.  
  2909.  
  2910.  
  2911. external function
  2912.  
  2913.     A function defined by the user or provided by CLIPS and called from
  2914. within CLIPS rules. One of two types, defined functions or effect
  2915. functions.
  2916.  
  2917.  
  2918.  
  2919. fact
  2920.  
  2921.     A set of words (strings or numbers) separated by spaces. Facts can
  2922. have any number of fields and can be in any order. Facts are the data
  2923. which the expert system reasons about. Facts represent the current
  2924. state of the world.
  2925.  
  2926.  
  2927.  
  2928. field
  2929.  
  2930.     A single word in a fact. Must be either a string or a number.
  2931.  
  2932.  
  2933.  
  2934. fire
  2935.  
  2936.     A rule is said to fire if all it's conditions are satisfied and the actions
  2937. are executed.
  2938.  
  2939. Appendix E: Glossary (cont'd)
  2940.  
  2941.  
  2942.  
  2943. instantiated
  2944.  
  2945.     The pattern or variable has matched successfully against a fact or
  2946. field.
  2947.  
  2948.  
  2949.  
  2950. LHS
  2951.  
  2952.     Left Hand Side: The logical pattern structure that must be satisfied in
  2953. order for the RHS's actions to be performed. 
  2954.  
  2955.  
  2956.  
  2957. pattern
  2958.  
  2959.     A fact. (see fact)
  2960.  
  2961.  
  2962.  
  2963. RHS
  2964.  
  2965.     Right Hand Side: The actions to be performed when a rule's LHS is
  2966. satisfied.  
  2967.  
  2968.  
  2969.  
  2970. rule
  2971.  
  2972.     A collection of patterns and actions. When all patterns are satisfied,
  2973. the actions will be taken. Rules are the basic unit of knowledge in
  2974. CLIPS.
  2975.  
  2976.  
  2977.  
  2978. salience
  2979.  
  2980.     A priority number given to a rule.  When multiple rules are ready for
  2981. firing, they are fired in order of priority. The default salience is zero
  2982. (0).  
  2983.  
  2984.  
  2985.  
  2986.