-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBetterFamily.CLP
238 lines (194 loc) · 6.25 KB
/
BetterFamily.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
;================================================================
; Author: James Anthony Ortiz
; File: BetterFamily.CLP
; Description: Improved version of family program,
; with the use of control facts, error checking and salience.
; ===============================================================
;==========================================
; deftemplate family
;
;===========================================
(deftemplate family
(slot mother)
(slot father)
(multislot daughters)
(multislot sons))
; =================================
; deffacts family-tree
;
; =================================
(deffacts family-tree
(family (mother Viola) (father Holt)
(daughters Frances Mildred))
(family (mother Cora) (father Walter)
(daughters Jaunita Dorothy Peggy) (sons Norm Carson))
(family (mother Frances) (father Norm)
(daughters Susan) (sons Norman Steve))
(family (mother Susan) (father Charlie)
(daughters Melissa) (sons Chris))
(family (mother Linda) (father Steve)
(daughters Kristin) (sons Stephen Jonathan))
(family (mother Kristin) (father Ryan)
(sons RJ))
(family (mother Sandy) (father Stephen)
(sons Austin Parker))
(family (mother Amy) (father Jonathan)
(sons Grayson)))
; ======================================
; defrule Start
; Begins program for BetterFamily by asserting
; choose-action.
; ======================================
(defrule start
=>
(assert (phase choose-action)))
; ====================================
; defrule menu
; Provides prompt for the screen:
; ====================================
(defrule menu
(phase choose-action)
=>
(printout t "Tell me what you're looking for:
1) The parents of someone.
2) The mother of someone.
3) The father of someone.
4) I'd like to quit!
What's your pleasure (1-4)? ")
(assert (user-select (read))))
; =======================================
; defrule good-choice
; Checks to see if the value entered is
; correct from the user.
; =======================================
(defrule good-choice
?phase <- (phase choose-action)
?choice <- (user-select ?select&1 | 2 | 3 | 4)
=>
(retract ?phase ?choice)
(assert (selection ?select))
(assert (phase select-child)))
; ========================================
; defrule incorrect-choice
; Tells user that the input is incorrect
; and retracts the choice.
; ========================================
(defrule incorrect-choice
?phase <- (phase choose-action)
?choice <- (user-select ?select&~1&~2&~3&~4)
=>
(retract ?phase ?choice)
(assert (phase choose-action))
(printout t ?select " is not a valid response, silly! Give me a 1 through 4." crlf))
; ==========================================
; defrule quit
; Allows the user to quit the program.
; ==========================================
(defrule quit
?phase <- (phase select-child)
?choice <- (selection ?select&4)
=>
(retract ?phase ?choice)
(printout t "Thanks for chatting with me!" crlf))
; ===========================================
; defrule get-name
; Allows the user to enter a name given the
; selection.
; ===========================================
(defrule get-name
?phase <- (phase select-child)
?choice <- (selection ?select&1 | 2 | 3)
=>
(retract ?phase)
(assert (phase select-parent))
(printout t "... and who is that special someone? ")
(assert (get-someone (read))))
; ===========================================
; defrule correct-name
; Finds if the name is in the list.
; ===========================================
(defrule correct-name
?phase <- (phase select-parent)
?name <- (get-someone ?person)
(or (family (daughters $? ?person $?))
(family (sons $? ?person $?)))
=>
(retract ?phase)
(assert (phase return-parents)))
; =========================================
; defrule get-parents
; Obtain information from the parents
; in the family list.
; =========================================
(defrule get-parents
?phase <- (phase return-parents)
?name <- (get-someone ?person)
?choice <- (selection ?select&1)
(and (or (family (mother ?mom) (father ?dad) (sons $? ?person $?))
(family (mother ?mom) (father ?dad) (daughters $? ?person $?))))
=>
(retract ?phase ?name ?choice)
(assert (phase choose-action))
(printout t "Here are the parents of " ?person ":
" ?mom " is " ?person "'s mother.
" ?dad " is " ?person "'s father." crlf))
; ==========================================
; defrule get-mother
; Obtain information from the mother in the
; family list.
; ==========================================
(defrule get-mother
?phase <- (phase return-parents)
?name <- (get-someone ?person)
?choice <- (selection ?select&2)
(and (or (family (mother ?mom) (father ?dad) (sons $? ?person $?))
(family (mother ?mom) (father ?dad) (daughters $? ?person $?))))
=>
(retract ?phase ?name ?choice)
(assert (phase choose-action))
(printout t ?mom " is " ?person "'s mother." crlf))
; =============================================
; defrule get-father
; Obtain information related to the father in
; the family list.
; =============================================
(defrule get-father
?phase <- (phase return-parents)
?name <- (get-someone ?person)
?choice <- (selection ?select&3)
(and (or (family (mother ?mom) (father ?dad) (sons $? ?person $?))
(family (mother ?mom) (father ?dad) (daughters $? ?person $?))))
=>
(retract ?phase ?name ?choice)
(assert (phase choose-action))
(printout t ?dad " is " ?person "'s father." crlf))
; ===================================
; defrule no-info
; If the person entered does not have
; any information from the list
; ===================================
(defrule no-info
?phase <- (phase select-parent)
?name <- (get-someone ?person)
?choice <- (selection ?select)
(and (or (family (mother ?person))
(family (father ?person))))
=>
(retract ?phase ?name ?choice)
(assert (phase choose-action))
(printout t "I can find no information on " ?person
"'s parents." crlf))
; ===================================
; defrule incorrect-name
; If the person is not in the list,
; show a message.
; ===================================
(defrule incorrect-name
?phase <- (phase select-parent)
?name <- (get-someone ?person)
?choice <- (selection ?select)
(family (mother ?mom) (father ?dad) (daughters $?girl) (sons $?boy))
=>
(retract ?phase ?name ?choice)
(assert (phase choose-action))
(printout t "I know nothing about " ?person "!" crlf))