-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem5.clp
89 lines (76 loc) · 1.64 KB
/
problem5.clp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(deftemplate square
(slot id)
(slot side-length)
)
(deftemplate rectangle
(slot id)
(slot width)
(slot height)
)
(deftemplate circle
(slot id)
(slot radius)
)
(defrule area-square
(square (id ?id) (side-length ?side))
=>
(assert (area ?id (* ?side ?side)))
)
(defrule area-rectangle
(rectangle (id ?id) (width ?width) (height ?height))
=>
(assert (area ?id (* ?width ?height)))
)
(defrule area-circle
(circle (id ?id) (radius ?radius))
=>
(assert (area ?id (* 3.14 (* ?radius ?radius))))
)
(defrule perimeter-square
(square (id ?id) (side-length ?side-length))
=>
(assert (perimeter ?id (* 4 ?side-length)))
)
(defrule perimeter-rectangle
(rectangle (id ?id) (width ?width) (height ?height))
=>
(assert (perimeter ?id (+ (* 2 ?width) (* 2 ?height))))
)
(defrule perimeter-circle
(circle (id ?id) (radius ?radius))
=>
(assert (perimeter ?id (* 2 3.14 ?radius)))
)
(defrule init-sum-area
(not (sum-area ?))
=>
(assert (sum-area 0))
)
(defrule init-sum-perimeter
(not (sum-perimeter ?))
=>
(assert (sum-perimeter 0))
)
(defrule sum-area
?p <- (area ?id ?area)
?f <- (sum-area ?sum)
=>
(retract ?f)
(retract ?p)
(assert (sum-area (+ ?sum ?area)))
)
(defrule sum-perimeter
?p <- (perimeter ?id ?perimeter)
?f <- (sum-perimeter ?sum)
=>
(retract ?f)
(retract ?p)
(assert (sum-perimeter (+ ?sum ?perimeter)))
)
(deffacts test
(square (id A) (side-length 3))
(square (id B) (side-length 5))
(rectangle (id C) (width 2) (height 5))
(circle (id D) (radius 2))
(circle (id E) (radius 6))
)