You are on page 1of 73

______________________________________________________________________

:
CLIPS


2006

.. , . ,



IV 230100

..
:
CLIPS. . : - , 2006. 73.

CLIPS. CLIPS
LISP- ,
(), .
, .
IV ,
230100
,
,
CLIPS.


, 2006.

1.

CLIPS. CLIPS
6
1.1.

CLIPS

1.2.
CLIPS

1.2.1.

1.2.2.

1.2.3.

10

1.3. CLIPS

11

1.3.1.

11

1.3.2.

14

1.3.3.

16

1.3.4.

17

1.4. CLIPS

20

1.5.

22

2.
2.1. 1.

29
29

2.1.1.

29

2.1.2.

30

2.1.3.

32

2.1.4.

34

2.1.5.

36

2.2. 2.

36

2.3. 3.

37

2.4.

39
3

3. - CLIPS

40

3.1. -
CLIPS

40

3.2. CLIPS

44

3.3.

47

4. CLIPS

54

4.1.

54

4.2.

54

4.3.

56

4.4.

60

4.5.

63

4.6.

72

72


CLIPS (C Language Integrated Production System)

(). ,
, CLIPS . CLIPS

: , () .
,
.
.

CLIPS

LISP-

, .
, CLIPS :
-.
,
:
1) CLIPS. CLIPS.
CLIPS. .
. . .
CLIPS.
2) . .
. ( ).
.
CLIPS. .
3) - CLIPS.
CLPS. .
CLIPS.
5

4) CLIPS. .
. .
.
.
1. CLIPS. CLIPS
1.1.


CLIPS

CLIPS Language Integrated Production


System. NASA (NASA's
Johnson Space Center) 1980- ,
LISP, OPS5 ART.
, LISP
, LISP-
, LISP .

, , NASA
.
, ,

.
.
- CLIPS ObjectOriented Language (COOL)
1990- .
UNIX, DOS, Windows Macintosh.

FTP .
CLIPS
, .
6

,
,
.
CLIPS
.

(fact base) (rule base). :
;
, ,
.
CLIPS
, . ,
:
(1) ;
(2) , ;
(3) , .

-.
1.2. CLIPS
CLIPS :
;
;
.
1.2.1.
CLIPS
:
float, integer, symbol, string, external-address, fact-address, instance-name instanceaddress.
float integer,
7

- symbol string.
.
(0-9),
(.), (+) (-) () .
, . ,
, , (
integer CLIPS long integer).
(float - double float).
.
.
,
,
.
: 237 15 +12 -32
: 2373 15.09 +12.0 -32.3-7
, ,
symbol.
symbol CLIPS ,
ASCII.

-,

symbol

. :
ASCII ( , , CR, LF), ,
(, ), &, |, <, ~, ;. -
symbol <,
symbol. , symbol ?
$?,
. , CLIPS .
:
foo Hello

B76-HI bad_value 127 742-42-42 @+=-% Search


8

string - ,
.
, (\).
(\). : foo

a and b

number a\quote
, abcd , abcd. , .
1.2.2.
CLIPS ,

(, ).
. - , (, )
CLIPS. ,
CLIPS.
, CLIPS.
CLIPS , (. .1).
1

+
*
/
**
Abs
Sqrt
Mod
Min
Max

deffunction
CLIPS CLIPS. ,
, , , CLIPS.
CLIPS :
.
, , ,
.
, .
.
:
(+ 3 4 5)
(* 5 6.0 2)
(+3 (* 8 9) 4)
(* 8 (+ 3 (* 2 3 4) 9) (* 3 4))
1.2.3.
CLIPS :
defmodule, defrule, deffacts, deftemplate, defglobal, deffunction, defclass, definstances,
defmessage-handler, defgeneric.
. .
CLIPS (
, , , ..).
, ,
CLIPS.
.
( defglobal) . ,
CLIPS (;). , (;)
10

, CLIPS. (;)
, .
1.3. CLIPS
1.3.1.

CLIPS. ,
, fact-list.
, .
, , .
,
, (
).
. ,
(), .

. reset clear
.
.
, (
assert, modify duplicate),
(. ).
-
. f . , f-10 10.
: .

,
(, ) , 11

. . , . :
(the pump is on)
(altitude is 10000 feet)
(grocery_list bread milk eggs)
(
, symbol),
.

( ): test, and, or, not, declare, logical, object, exists
forall.

,
, ,
, . ()
,
. ,
,
deftemplate.

.
deftemplate
, , .
, ,
. ,
. ,
, ( ),
. , ,
.
deftemplate :
(deftemplate <name>
12

(slot-1)
(slot-2)

(slot-N))

:
(deftemplate prospect
(slot name (default ?DERIVE))
(slot assets (default rich))
(slot age (default 80)))
.
symbol,
, - .
.
, . :
(client (name Joe Brown) (id X9345A))
(point-mass (x-velocity 100) (y-velocity -200))
(class (teacher Martha Jones) (#-students 30) (room 37A))
(grocery-list (#-of-items 3) (items bread milk eggs))
, .

( assert),
( retract), (
modify) ( duplicate)
. :
(assert (light green))
, deffacts
. :
(deffacts today
13

(status walking)
(weather is warm))
deffacts deffacts,
( today), ,
.
undeffacts. :
(undeffacts today)
CLIPS (
reset) , deffacts, .
, (initialfact). f-0.
.
1.3.2.
CLIPS
. ,
, .
,
. :
(), if-then
, (), then
.
( ),
, . CLIPS
, ,
. .
,
, , .
CLIPS,
,
, , .
14

.
,
(initial-fact).
, .
CLIPS.
,
, . , , , CLIPS . , , ,
, .
, , (
),
.. ,
, .. .
if-then
. if-then
, . . ,
, , ,
, .
(,
).
defrul:
(defrule rule_name optional_comment
(patten_1)
(patten_2)

(patten_N)
15

=>
(action_1)
(action_2)

(action_M))
:
(defrule take-a-vacation
(work done)
(money plenty)
(reservations made)
=>
(printout t Let's go! crlf))
1.3.3.
, CLIPS
. , ,
, ,
.
,
. :
?<variable-name>
:
?x ?sensor ?noun ?color
.
, ,
.
, .

. :
(defrule make-quack
16

(duck-sound ?sound)
=>
(assert (sound-is ?sound)))
, , ,
bind.
(defrule addition
(numbers ?x ?y)
=>
(assert (answer (+ ?x ?y)))
(bind ?answer (+ ?x ?y))
(printout t answer is ?answer crlf))
,
.
.
<.
:
(defrule get-married
?duck < (bachelor Dopey)
=>
(retract ?duck))
,
CLIPS, defglobal.
, . CLIPS
, ( ).
1.3.4.
CLIPS , . :
17

;
test;
;
.

.

, .
: ~, | &.

, . :
(defrule walk
(light ~green)
=>
(printout t Don't walk crlf))
,
. :
(defrule cautious
(light yellow | blinking-yellow)
=>
(printout t Be cautious crlf))

, . :
(defrule cautious
(light ?color & yellow | blinking-yellow)
=>
(printout t Be cautious because light is ?color crlf))
test
test ,
18

,
. , .
, ,
, , test.
test :
(test (_ _1 _2))
_ - ,
. CLIPS
(. . 2).
2

eq
neq
=
<>
>=
>
<=
<

( )

( )

, eq neq,
.
_1 _, _2 .

, . :
(defrule addition
(numbers ?x ?y)
=>
(assert (answer (+ ? ?))))

=, CLIPS,
, . :
19

(defrule addition
(numbers ?x ?y)
(stock ?ID = (sqrt (+ (** ?x 2) (** ? 2))))
=>
(printout t stock ID = ?ID crlf))

CLIPS ,
(, ). deffunction .
,
CLIPS. defmodule .
1.4. CLIPS
CLIPS :
1) ,
;
2) , GUI-;
3) , .
CLIPS
CLIPS.
CLIPS>
.
, , . ,
. ,
CLIPS , ..
. ,
.
. (
). :
20

CLIPS> (+ 3 4)
7
CLIPS> (defglobal ?*x* = 3)
CLIPS> ?*x*
3
CLIPS> red
red
CLIPS>
3 4, 7.
?**, 3. ?**
, 3. ,
red.
:
1) ;
2) ;
3) CLIPS;
4) CLIPS.
1.
1) (,
CLIPS Notepad) :
(defrule startup
=>
(assert (light green)))
(defrule one
(light green)
=>
(printout t Go !!! crlf))
2) (, TEST.CLP).
21

3) CLIPS ( CLIPS).
4) startup one CLIPS
Load File.
5)
(reset) (run).
1.5.
1.

1, .

2.

2,

, , .
, , ,

.
.
:
GERALD
+ DONALD
--------------ROBERT
GERALD, DONALD ROBERT
, .
.


. , , - , - .. ,

22

( - ), ,
:
(((D+D) mod 10) = T) & (D ? T),

(1)

? .
.
L R, (L*10 + L*10 + D + D) 100
(R*10 + ). L R
D . .. ( - ),
, , :
(((L*10 + L*10 + D + D) mod 100) = (R*10 + T)) & (D ? T ? L ? R).

(2)

, , ( - ), .
CLIPS
, CLIPS.
:
(combination D 0), (combination D 1), (combination A 1) ..
( - ),
(1):
(defrule find_solution
(combination D ?d)
(combination ?t&~?d)
(test (= (mod (+ ?d ?d) 10) ?t) )
=>
(printout t A Solution is: t t)
(printout t D = ?d t)
(printout t T = ?t t))
, , :
23

1) (combination D ?d) - ,
combination D . ,
?d.
2) (combination T ?t&~?d) - ,
combination . ,
, ?d,
?t. & AND, ~ NOT.
?t&~?d , ?t ,
?d.
3) (test (= (mod (+ ?d ?d) 10) ?t)) -
(mod (+ ?d ?d) 10) (?t) (. (1)).
(printout t A Solution is: t t) . , , , , . , , t. ,
, .
t ,
. crlf.
. ,
. :
(printout t = ?t t)
=, ?t
.
( ), .
:
(defrule find_solution
(combination D ?d&~0)
(combination T ?t&~?d)
(test (= (mod (+ ?d ?d) 10) ?t))
24

(combination L ?l&~?d&~?t)
(combination R ?r&~?d&~?t&~?l)
(test (= (mod (+ ?d ?d (* 10 ?l) (* 10 ?l)) 100) (+ (* 10 ?r) ?t)))
(combination A ?a&~?d&~?t&~?l&~?r)
(combination E ?e&~?d&~?t&~?l&~?r&~?a)
(test (= (mod (+ ?d ?d (* 10 ?l) (* 10 ?l) (* 100 ?a) (* 100 ?a)) 1000) (+ (* 100 ?e)
(* 10 ?r) ?t)))
(combination N ?n&~?d&~?t&~?l&~?r&~?a&~?e)
(combination B ?b&~?d&~?t&~?l&~?r&~?a&~?e&~?n)
(test (= (mod (+ ?d ?d (* 10 ?l) (* 10 ?l) (* 100 ?a) (* 100 ?a) (* 1000 ?r) (* 1000 ?
n)) 10000) (+ (* 1000 ?b) (* 100 ?e) (* 10 ?r) ?t)))
(combination O ?o&~?d&~?t&~?l&~?r&~?a&~?e&~?n&~?b)
(test (= (mod (+ ?d ?d (* 10 ?l) (* 10 ?l) (* 100 ?a) (* 100 ?a) (* 1000 ?r) (* 1000 ?
n) (* 10000 ?e) (* 10000 ?o)) 100000) (+ (* 10000 ?o) (* 1000 ?b) (* 100 ?e) (* 10 ?
r) ?t)))
(combination G ?g&~?d&~?t&~?l&~?r&~?a&~?e&~?n&~?b&~?o&~0)
(test (= (+ ?d ?d (* 10 ?l) (* 10 ?l) (* 100 ?a) (* 100 ?a) (* 1000 ?r) (* 1000 ?n) (*
10000 ?e) (* 10000 ?o) (* 100000 ?g) (* 100000 ?d)) (+ (* 100000 ?r) (* 10000 ?o) (*
1000 ?b) (* 100 ?e) (* 10 ?r) ?t)))
=>
(printout t A Solution is: t t) (printout t G = ?g t) (printout t E = ?e t)
(printout t R = ?r t)
(printout t A = ?a t) (printout t L = ?l t) (printout t D = ?d t) (printout t
O = ?o t)
(printout t N = ?n t) (printout t B = ?b t) (printout t T = ?t t) (printout t t)
(printout t ?g ?e ?r ?a ?l ?d t)
(printout t + ?d ?o ?n ?a ?l ?d t)
(printout t --------- t)
(printout t = ?r ?o ?b ?e ?r ?t t t))
25

, , .
assert.
(assert (combination D 0)
(combination D 1)
(combination A 1)
(combination L 9)
...)
, .. 100, -.
:
(number 0), (number 1), (number 2), ..., (number 9);
(letter G), (letter E), (letter R), (letter A), (letter L), (letter D), (letter O),(letter N), (letter
B), (letter T).
, ,
.
(defrule generate_combination
(number ?x)
(letter ?y)
=>
(assert (combination ?y ?x)))
, , (number ?x), (letter ?y),
, (combination ?y ?x),
.
, , , -, , -, ,
. .. ,
.
(defrule startup
=>
26

(printout t t The problem is: t t)


(printout t GERALD t) (printout t + DONALD t)
(printout t --------------- t) (printout t = ROBERT t t)
(assert (number 0) (number 1) (number 2) (number 3) (number 4) (number 5)
(number 6) (number 7) (number 8) (number 9) (letter G) (letter E) (letter R) (letter A)
(letter L) (letter D) (letter O) (letter N) (letter B) (letter T)))
, : startup,
generate_combination find_solution.
,
. , ,
, ,
.
, ,
.
CLIPS
1)

CLIPS.

,
CLIPS
(Turn Dribble On) File.
, CLIPS.
, CLIPS (Turn
Dribble Off). ,
CLIPS, .
2)

CLIPS.

3)

Wordgame.clp.

4)

CLIPS Wordgame.clp
Load File.
. startup .
CLIPS (initial27

fact) f-0.
(reset).
,
(initial-fact) ,
(deffacts). (reset) ,
( Agenda).
, (run).
CLIPS , .
, , (Ctrl+C).
CLIPS,
, .. CLIPS
, (clear).
(clear) CLIPS .
(reset),
.
3. ,
:
SEND
+ MORE
-----------MONEY
4.

CROSS
+ ROADS
------------DANGER
1.5,

1, 2 3 ,
CLIPS.

2.
2.1. 1. .
28

2.1.1.
:
.
:
CLIPS .


.
, HP, Canon, Lexmark, Epson.
: , .
,
, .
, .
.
.
2-3,
7 .
- ,
. .
,
. :
1) ( ,
, . .)
2) ( ,
..)
3) ( ,
, ..).

, ,
Epson Stylus C60.
29



ES C60 Problem Solver,
Epson Stylus C60, , Web Epson http://support.epson.ru/
Epson http://www.epson.ru/.
2.1.2.

.

(. .3 . 4).
3




1) 1) 1)
2)

2)

2)


3)

3)

3)

4)

4)

4)

5)
4

1)

1)

1)

30

2)

2)

2)


. (
) ( ).
(. .5).
5

. 5

31

2.1.3.

.

: (), ().
- ,
, ()
, .
, , ,
.
. ,
.
( )
( ). -
, (),
, .
32

,
, ,
.

.
.
, .
, .
, , ,
.

1. , .
2. , .
3. , .
4. , .
5. , .
6. , .
7. , .
8. , .
9. , .
10. , .
11. , .
12. , .
13. ,
.
2.1.4.
.
, , CLIPS.
(defrule n1 ; , .
(horizontal strips)
33

=>
(printout t leveling of the printed head t))
(defrule n2 ; , .
(vertical strips)
=>
(printout t correct installation of ink t))
(defrule n3 ; , .
(passing of colours)
=>
(printout t correct loading of a paper t))
(defrule n4 ; , .
(incorrect colours)
=>
(printout t cleaning of the printed head t))
(defrule n5 ; , .
(blurred impress)
=>
(printout t installation of essential drivers t))
(defrule n6 ; , .
(incorrect characters)
=>
(printout t installation of essential drivers t))
(defrule n7
; , .
(incorrect fields)
=>
(printout t correctness of installation of parameters page t))
(defrule n8
; , .
(impression of clean pages)
34

=>
(printout t the installation of essential drivers t))
(defrule n9

; , .

(impression is lubricated)
=>
(printout t correct loading of a paper t))
(defrule n10

; , .

(paper does not move)


=>
(printout t leveling of the printed head t))
(defrule n11
; , .
(submission of several pages for once)
=>
(printout t correct loading of a paper t))
(defrule n12

; , .

(paper is jammed)
=>
(printout t correct loading of a paper t))
(defrule n13

; ,

; .
(paper is pressed or is not injected completely)
=>
(printout t correct loading of a paper t))

CLIPS , .
2.1.5.
,
.
35

5 .

1) (reset);
2) CLIPS - ,
, : (assert (paper is jammed));
3) .
2.2. 2.
: ,
.
:
CLIPS .

1. (there is no spark on suppositories),
(engine is not started).
2. (poor connection of wires),
(small current in primary kz).
3. (small current in primary kz),
(small current in secondary kz).
4. (small current in secondary kz),
(weak spark on suppositories).
5. (weak spark on suppositories),
(bad accumulator).
6. (bad accumulator),
(small current in primary kz).
7. (crankshaft is rotated weakly),
(bad accumulator).
8. (strike condenser), (there is
no current in primary kz).
36

9. (there is no current in primary kz),


(there is no current in secondary kz).
10. (there is no current in secondary kz),
(there is no spark on suppositories).

1. (weak spark on suppositories).
2. (crankshaft is rotated weakly).
:
1) CLIPS
(
);
2) (clear);
3) (load);
4) deffacts:
(deffacts sf (weak spark on suppositories) (crankshaft is rotated weakly))
5) (reset);
6) (run);
7) .
2.3. 3.
: , ,
.
:
CLIPS .
, .
( ).


.
37


(. .6 . 7).
6

(health)

1) (bad)

(vacati

(chara

on

cter)

(family

time)

status)
1)
1) (winter)

1)

(below

(active

averag

e)
2)

2)
(middl

e)
3)

2) (spring)
(avera

ge)
3) (good)

2)
(passiv
e)

3) (summer)

(norma
l)
4)

4) (autumn)
(excell
ent)
7

(active rest)
1) (walking tour)
2) (tourist trip)

(passive rest)
1) (dacha)
2) (resort)
3) (village)
4) (home)

(. .8).
8
38


---

---

---

---

---


10 .


CLIPS ,
.

5 .

1) (reset);
2) CLIPS - ,
, : (assert (excellent health) (active
character));
3) .
2.4.
1. 1, 2 3,
. 2.4
.

2. 4.

39

:
.
:
CLIPS .
2.4
,
.

3. - CLIPS
3.1. -
CLIPS
- CLIPS
,

. COOL -
CLIPS (COOL CLIPS object-oriented language).
CLIPS .
,
, () .
,
() .
,
.
,
,

.

40

- ,
,
().

,
, .
,
,
,
. , , .
,
.
, ,
,
. ,
, , ,
,
. ,
,
.
, , ship (),
x-velocity ( ) y-velocity ( ).
ship, Titanic
x-velocity y-velocity .
speed,
x-velocity y-velocity (
).
,
( SmallTalk speed ship,
++ - ship.)
41

, ()
, , ..
.
, ,
Titanic ,
. Titanic
ship, speed.
,
. , ,
,
. ,
,
.
.
x ,
Titanic.

. , -
. ,
, ,
. .
-,

. ,
,
,
. ,
, , ..
.
42

, .
.
,
, -, ,


. -
.
, CLIPS ship
titanic. ship,
x-velocity y-velocity:
(defclass ship
(is-a USER)
(role concrete)
(pattern-match reactive)
(slot x-velocity (create-accessor read-write))
(slot y-velocity (create-accessor read-write)))
. -
COOL. ,
ship ;
ship , ..
( , ,
++);
ship ,
,
.
.
,
Titanic.
43

definstances,
.
CLIPS.
(definstances ships
(titanic of ship (x-velocity 12) (y-velocity 10)))
ship.

. ,
,
.
(defmessage-handler ship speed ( )
(sqrt (+ (* ?self:x-velocity ?self:x-velocity)
(* ?self:y-velocity ?self:y-velocity))))

CLIPS

(reset),
(send [titanic] speed),

CLIPS
titanic.

3.2.
CLIPS
CLIPS

CLIPS

defclass.

- .

, 1,
:
44

. 1.

(defclass polygon (is-a USER))


(defclass quadrilateral (is-a polygon))
(defclass trapezium (is-a quadrilateral))
(defclass parallelogram (is-a trapezium))
(defclass rectangle (is-a parallelogram))
(defclass square (is-a rectangle))
, polygon ()
USER, ,
. is-a (),
, .

..

, .
CLIPS ,
,
. polygon
quadrilateral:
(defclass polygon (is-a USER)
(role abstract)
(slot no-of-sides (default 4)))
(defclass quadrilateral (is-a polygon)
(role concrete))
polygon , .. ,
. quadrilateral
quadrilateral
, .. (
). polygon no-of-sides (
) 4.
, .

no-of-sides.
45

COOL , .. ,
. ,
visibility (), ,
. private ,
, public

.
.
defmessage-handler,
CLIPS.
(defmessage-handler polygon sides ( )
?self:no-of-sides)
sides polygon
no-of-sides , .
, ().
, , ,
, square-one.
(definstances geometry
(square-one of square))

CLIPS

(reset).

,
(send [square-one] sides)
CLIPS
4
, ?self:no-of-sides
square-one,
sides. ?
self ,
,
.
46

3.3.
1. 3.1 CLIPS.
5 x
y. 3.3
.
2. 3.2 .
. 3.3
.
3. , ,
square,
, square-one,

.

square:
(defclass square (is-a rectangle)
(slot length-of-sides)
(slot area))
, square : length-of-sides
, area .
,
:
(definstances geometry
(square-one of square
(length-of-sides 10)))
put-print-area
square, bind
area .

CLIPS

(reset).

,
47

(send [square-one] put-print-area).


3.3
.
4. ,

. CLIPS ,
,

.
;;*********************************************************************
;;* Smith & Wesson
;;*********************************************************************
;; REVOLVER,
(defclass revolver
(is-a USER) (role concrete) (slot barrel) (slot barrel-pin) (slot cyl-stop) (slot cyl)
(slot handspring))
;; REVOLVER. .
(definstances guns
(M19 of revolver (barrel 4499) (barrel-pin 4499) (cyl-stop 4499) (cyl 4499)
(handspring 5022)))
;; .
(defmessage-handler revolver part-no (?part)
(dynamic-get ?part))
;;
;;
(deffunction prompt ( )
(printout t crlf USER> ))
;;
;; :
(barrel).
(deffunction parts-list ( )
48

(printout t crlf barrel cylinder ejector trigger hammer firing-pin cylinder-stop


cylinder-hand yoke frame sideplate rear-sight front-sight crlf))
;; .
(deffunction choose-list ()
(printout t crlf Please choose from the following list: crlf))
;; , 19.
(deffunction kind-list ( )
(printout t crlf M10 M12 M13 M14 M15 M16 M17 M18 M19 crlf))
;;
;; (part), (symptom), ,
;; (subpart), .
;; (check), .
(deftemplate problem
(field part (type SYMBOL) (default nil)) (field symptom (type SYMBOL) (default
nil))
(field subpart (type SYMBOL) (default nil)) (field check (type SYMBOL) (default
nil)))
;; : (part),
;; c (action), , (subpart),
;; , (check), ,
;; (remark), .
(deftemplate repair
(field part (type SYMBOL) (default nil)) (field action (type SYMBOL) (default
nil))
(field subpart (type SYMBOL) (default nil)) (field check (type SYMBOL) (default
nil))
(field remark (type STRING) (default )))
;;*************************
;;

;;*************************
49

;; START. : :
;; .
(defrule start
?start-token <- (initial-fact)
=>
(retract ?start-token)
(printout t crlf What part of the gun are you problem with? crlf)
;; ?
(choose-list) (parts-list) (prompt) (bind ?part (read))
(assert (problem (part ?part))))
;; FINISH. : ,
;; TO: .
(defrule finish
(repair (check done) (remark ?rem&~ ))
=>
(printout t crlf ?rem crlf)
(printout t crlf Glad to be of service! crlf)
;; !
(halt)) ;; .
;; CHECK-REPAIR. : ,
;; , : ,
;; () .
(defrule check-repair
?rep <- (repair (part ?part) (action ?action&~replace)
(subpart ?sub&~nil&~?part))
(problem (part ?part) (symptom ?sym))
=>
(printout t crlf If you ?action the ?sub that should fix the ?sym
problem with the ?part crlf)
;; ?action ?sub - ?sym ?part
50

(modify ?rep (check done)))


;; CHECK-REPLACE. :
;; : ,
;; () .
;;
;; .
(defrule check-replace
(repair (part ?part) (action replace)) (not (model ?mod&~nil))
?prob <- (problem (part ?part) (symptom ?sym))
=>
(printout t crlf You have to replace the ?part to fix the ?sym problem crlf)
;; ?part ?sym
(assert (model nil)))
;; REPLACE. :
;; :

(defrule replace
?rep <- (repair (action replace)) ?mod <- (model nil)
=>
(printout t crlf What model of revolver do you have ? crlf)
;; ?
(kind-list) (prompt) (bind ?answer (read)) (retract ?mod) (assert (model ?answer))
(modify ?rep (check part-no)))
;; PART-NO. :
;; :

, ,

.
(defrule part-no
(model ?mod&~nil)
?rep <- (repair (part ?part) (action replace) (check part-no))
=>
(bind ?no (send (symbol-to-instance-name ?mod) part-no ?part))
51

(printout t crlf The part number of the ?mod ?part is ?no crlf)
;; ?mod ?part ?no
(modify ?rep (check done)))
;; BARREL ()
;; BARREL-SYMPTOM. :
;; : ().
(defrule barrel-symptom
?prob <- (problem (part barrel) (symptom nil) (subpart nil))
=>
(printout t crlf Is there a problem inside barrel? crlf)
;; ?
(prompt) (bind ?answer (read))
(if (eq ?answer yes) then (modify ?prob (subpart bore))))
;; BARREL-INSIDE. :
;; : , ( ).
(defrule barrel-inside
?prob <- (problem (part barrel) (symptom nil) (subpart bore))
=>
(printout t crlf What is the problem inside the barrel? crlf)
;; ?
(choose-list) (printout t crlf leading rust jam crlf)
;;
(prompt) (bind ?answer (read)) (modify ?prob (symptom ?answer)))
;; BARREL-RUST. :
;; : .
(defrule barrel-rust
?prob <- (problem (part barrel) (symptom rust))
=>
(printout t crlf Are there pits inside the barrel? crlf)
;; ?
52

(prompt) (bind ?answer (read))


(if (eq ?answer yes) then (assert (repair (action replace) (part barrel) (subpart bore)
(remark Please consult your local dealer)))
;;
else (assert (repair (action clean) (part barrel) (subpart bore)
(remark Gun should be kept clean and dry)))
;;
))
;; BARREL-LEADING. :
;; : .
(defrule barrel-leading
?prob <- (problem (part barrel) (symptom leading) (check nil))
=>
(modify ?prob (check ammo))
(printout t crlf You may be using the wrong ammunition crlf))
;; ,
;; BARREL-LEADING-CHECK.
;; : ,
;; :

(defrule barrel-leading-check
?prob <- (problem (part barrel) (symptom leading) (check ammo))
=>
(assert (repair (part barrel) (action clean) (subpart bore)
(remark Use Lewis Lead Remover))))
;; Lewis
CLIPS (reset).
(run).
5
, (barrel) (M19).
53

(inside), (rust)
(leading). 3.3
.
5.
-
.
3.3 a
.
4. CLIPS
4.1.
CLIPS,
. , ,
: (
), (, , ).
.
1. , , ,
. . , .
, ?
2. . , . : , a
: . ,
?
4.2.

. ,
, .
, ,
, , , , ,
. () , ,
54

, , F(A) , ,
, .
1. , .
, , .
:
T(A) => F(A) v T(B).
,

T(A) => T(B).
. , :
F(A) => ~(F(A) v T(B)).
:
F(A) => ~F(A) ^ ~T(B) F(A) => T(A) ^ F(B).
, ,
,

( ).
, ,
,
, ( ).
, , ,
,
, .
,
,
.
,
, ..
, , ,
. .
55

0. : .
?

. , , , , .. .
. , ..
, , . ,

, .. ,
.
.

..

:
;
.
4.3.
,
.

?
-,
:

, .

, , .
, .

, .

, ().
,
.
56

(),

. , , ,
, , (
) . ,
.

, ,

(reasons),
. , , ,
, (),
, ,
,

, . ,
,
.
, -.

. CLIPS-
:
;; statement ()
;; ( speaker).
;; ( claim).
;; - ( reason),
;; ,
;; (tag) - .
(deftemplate statement
(field speaker (type SYMBOL))
(multifield claim (type SYMBOL))
(multifield reason (type INTEGER) (default 0))
57

(field tag (type INTEGER) (default 1)))


,
(),

.
(statement (speaker A) (claim F A))
:
, , ,
1.
, reason (
0), .. ,
, , .
claim reason multifield,
.


. ,
, ,
.
.
;; , , , ,
;; ... , ;
;; F ... , .
;; (reason)
;; - ( statement)
;; ( claim).
;; scope,
;; .
(deftemplate claim
58

(multifield content (type SYMBOL))


(multifield reason (type INTEGER) (default 0))
(field scope (type SYMBOL)))
,
, , (
claim):
(claim (content F A) (reason 1) (scope truth)).
, claim statement.
(reason) . scope
claim
.
(world),
. world ,
.

, .
, () F(A) ,
, .. .
:
;; world ,
;;
;; .
;; tag,
;; scope.
(deftemplate world
(field tag (type INTEGER) (default 1))
(field scope (type SYMBOL) (default truth)))
,
world,
tag 1,
59

. the-facts
0 :
;; , .
(deffacts the-facts (world) (statement (speaker A) (claim F A)))
deffacts ,
( , ),
CLIPS
reset.
4.4.
,
0 .
, unwrap-true unwrap-false,
, , ,
,
claim.
;; .
(defrule unwrap-true
(world (tag ?N) (scope truth))
(statement (speaker ?X) (claim $?Y) (tag ?N))
=>
(assert (claim (content T ?X) (reason ?N) (scope truth)))
(assert (claim (content $?Y) (reason ?N) (scope truth))))
(defrule unwrap-false
(world (tag ?N) (scope falsity))
(statement (speaker ?X) (claim $?Y) (tag ?N))
=>
(assert (claim (content F ?X) (reason ?N) (scope falsity)))
(assert (claim (content NOT $?Y) (reason ?N) (scope falsity))))
60

,
claim.
, .
~() F(A), a ~F(A) (), ,
, .

, .
;;
(defrule not1
?F <- (claim (content NOT T ?P))
=>
(modify ?F (content F ?P)))
(defrule not2
?F <- (claim (content NOT F ?P))
=>

(modify ?F (content T ?P)))


;;
;; .
(defrule contra-truth
(declare (salience 10))
?W <- (world (tag ?N) (scope truth))
?S <- (statement (speaker ?Y) ( tag ?N))
?P <- (claim (content T ?X) (reason ?N) (scope truth))
?Q <- (claim (content F ?X) (reason ?N) (scope truth))
=>
(printout t crlf Statement is inconsistent if ?Y is a knight.
;; , ?Y .
t crlf)
61

(retract ?Q) (retract ?P)


(modify ?W (scope falsity)))
, ,
,
,
world falsity ().

, ..
.
;;
;; .
(defrule contra-falsity
(declare (salience 10))
?W <- (world (tag ?N) (scope falsity))
?S <- (statement (speaker ?Y) (tag ?N))
?P <- (claim (content F ?X) (reason ?N) (scope falsity))
?Q <- (claim (content T ?X) (reason ?N) (scope falsity))
=>
(printout t crlf Statement is inconsistent if ?Y is a knave.
;; , ?Y .
t crlf)
(modify ?W (scope contra)))
sweep ,
.
;; ,
;; .
(defrule sweep
(declare (salience 20))
(world (tag ?N) (scope falsity))
?F <- (claim (reason ?N) (scope truth))
62

=>
(retract ?F))
, contra-truth, contra-falsity sweep
( salience),
. ,
, ,
, .
,
, 0, ,

, ,
world.
4.5.
,
.
, ,
. .
3. , , ,
. : .
?
,
, ,
F(A) ^ F(B).

. ,
, ,

, .

63


, , , ,
,
F(A) ^ F(B)

T(A) v T(B).
,
, ,

T()

T(A) v T(B) , ()
, . ,
, T(A) v T(B) ,
, .. F(A). () ,
T(B).
, , T(A) v T(B)
F(A). T()
.
CLIPS
.
, .
,
, .
, , ~(F(A) ^ T()),

NOT AND F A T B.
,
, .
, .
.
64

B
, T() F(A) AND.


, flip, F
.
(defrule not-or
?F <- (claim (content NOT OR ?P ?X ?Q ?Y))
=>
(modify ?F (content AND (flip ?P) ?X (flip ?Q) ?Y)))
(defrule not-and
?F <- (claim (content NOT AND ?P ?X ?Q ?Y))
=>
(modify ?F (content OR (flip ?P) ?X (flip ?Q) ?Y)))
flip

NOT AND F A T B

OR T A F B,

OR NOT F A NOT .
flip :
(deffunction flip (?P)
(if (eq ?P T) then F else T))


T(A) v T(B)

F(A) ^ T(B),

65

F(B) ^ (T(A) v T(B))

~(F(A) v F(B)) ^ (T(A) v T(B)),



.

6
. , F(A) T(A) v F(B).
,

T(A) F(A), T(A).



F(B), , , ,
.
,
claim context:
(deftemplate claim
(multifield content (type SYMBOL))
(multifield reason (type INTEGER) (default 0))
(field scope (type SYMBOL))
(field context (type INTEGER) (default 0)))
0 context ,
, 1 - , 2
. , ,

T(A) v F(B),
T(A) 1, a F(B) 2.
, .. 0.

66

world
context. . , ,
world
(world (tag 1) (scope truth) (context 2)).
, :
, (tag), 1,
, ,
.
world .
;; world ,
;;
;; .
;; tag,
;; -
;; scope.
;; context
;; .
;; 0 ,
;; 1 , 2 .
(deftemplate world
(field tag (type INTEGER) (default 1))
(field scope (type SYMBOL) (default truth))
(field context (type INTEGER) (default 0)))
, .
.
(defrule left-or
?W <- (world (tag ?N) (context 0))
(claim (content OR ?P ?X ?Q ?Y) (reason ?N) (scope ?V))
=>
67

(modify ?W (context 1))


(assert (claim (content ?P ?X) (reason ?N) (scope ?V) (context 1))))
1 context world
claim.

.
(defrule right-or
?W <- (world (tag ?N) (context 1))
(claim (content OR ?P ?X ?Q ?Y) (reason ?N) (scope ?V))
=>
(modify ?W (context 2))
(assert (claim (content ?Q ?Y) (reason ?N) (scope ?V) (context 2))))
, ,
.
;;
;;
;; world.
(defrule contra-truth-scope
(declare (salience 10))
(world (tag ?N) (scope truth) (context ?T))
(claim (content T ?X) (reason ?N) (scope truth) (context ?S&:(< ?S ?T)))
?Q <- (claim (content F ?X) (reason ?N) (scope truth) (context ?T))
=>
(printout t Disjunct ?T is inconsistent with earlier truth context.
;; ?T
;; .
crlf)
(retract ?Q))
;;
;;
68

;; world.
(defrule contra-falsity-scope
(declare (salience 10))
?W <- (world (tag ?N) (scope falsity) (context ?T))
(claim (content F ?X) (reason ?N) (scope falsity) (context ?S&:(< ?S ?T)))
?Q <- (claim (content T ?X) (reason ?N) (scope falsity) (context ?T))
=>
(printout t Disjunct ?T is inconsistent with earlier falsity context.
;; ?T
;; .
crlf)
(retract ?Q))
contra-truth
contra-falsity.
;;
;;
;; world.
(defrule contra-truth
(declare (salience 10))
?W <- (world (tag ?N) (scope truth))
?P <- (claim (content T ?X) (reason ?N) (context ?S) (scope truth))
?Q <- (claim (content F ?X) (reason ?N) (context ?S) (scope truth))
=>
(printout t Statement is inconsistent if ?X is a knight
;; , ?X .
crlf)
(retract ?Q) (retract ?P)
(modify ?W (scope falsity) (context 0)))
;;
;;
69

;; world.
(defrule contra-falsity
(declare (salience 10))
?W <- (world (tag ?N) (scope falsity))
?P <- (claim (content F ?X) (reason ?N) (context ?S) (scope falsity))
?Q <- (claim (content T ?X) (reason ?N) (context ?S) (scope falsity))
=>
(printout t Statement is inconsistent whether ?X is knight or knave.
;; , ,
;; ?X .
crlf)
(modify ?W (scope contra)))

,
, .
(defrule consist-truth
(declare (salience -10))
?W <- (world (tag ?N) (scope truth))
(statement (speaker ?Y) (tag ?N))
=>
(printout t Statement is consistent:
;; :
crlf)
(modify ?W (scope consist)))
(defrule consist-falsity
(declare (salience -10))
?W <- (world (tag ?N) (scope falsity))
(statement (speaker ?Y) (tag ?N))
=>
(printout t Statement is consistent:
70

;; :
crlf)
(modify ?W (scope consist)))
(defrule true-knight
(world (tag ?N) (scope consist))
?C <- (claim (content T ?X) (reason ?N))
=>
(printout t ?X is a knight
;; ?X
crlf)
(retract ?C))
(defrule false-knave
(world (tag ?M) (scope consist))
?C <- (claim (content F ?X) (reason ?N))
=>
(printout t ?X is a knave
;; ?X
crlf)
(retract ?C))
.
, ,
.
(defrule conj
(world (tag ?N) (context ?S))
(claim (content AND ?P ?X ?Q ?Y) (reason ?N) (scope ?V))
=>
(assert (claim (content ?P ?X) (reason ?N) (scope ?V) (context ?S)))
(assert (claim (content ?Q ?Y) (reason ?N) (scope ?V) (context ?S))))
,
3:
71

(deffacts the-facts
(world)
(statement (speaker A) (claim AND F A F B) (tag 1)))
4.6.
1.

CLIPS P0.

2.

CLIPS P3.

1. .., .. .
.: , 2001.
2. .., ..
. - .: , 1992.
3. . . - .: . ,
2001.
4. . . . :
. - .: . , 2003.
5. .., .. . .:
- , 1999.
6. . 3 . .: , 1990.
7. .. . . .
.: , 2000.
8. .-. . .: , 1991.
9. .. . .: ,
1990.
10. . . .: , 1985.
11. . . . .: , 1987.
12. . . .: , 1989.
13. ., . . . .:
, 1987.
72

:
CLIPS

____________________________________________________________________

6084 1/16. .
100 .
. . 4,5.

____________________________________________________________________


630092, . , . . , 20

73