-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimalGuessingBackwardsChaining.clp
314 lines (284 loc) · 8.49 KB
/
AnimalGuessingBackwardsChaining.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Neeraj Aggarwal
* 4/8/2016
* Adv Topics: Expert Systems
*
* Backward chaining implementation of the animal guessing game
*
* The user has the option to select either a dog, a cat, a panda, an eagle,
* a hummingbird, an octopus, or an eel. The program will then prompt the user with
* a series of questions using backwards chaining in order to determine the selected animal.
*
* Each animal has a set of conditions that must be true in order for that animal to be
* the answer. The goal of the program is to match all the conditions for one animal, thus
* identifying the animal that the user selected.
*
* Should any condition be false and eliminate the chance of an animal being a possibility,
* the program will automatically begin asking for conditions that meet another selected
* animal in order to verify or eliminate it.
*/
(clear)
(reset)
/*
* Set up backward chaining for each characteristic used to determine the animal
* the player has chosen.
*/
(do-backward-chaining land)
(do-backward-chaining domestic)
(do-backward-chaining barking)
(do-backward-chaining blackandwhite)
(do-backward-chaining fly)
(do-backward-chaining nectar)
(do-backward-chaining mascot)
(do-backward-chaining water)
(do-backward-chaining long)
(do-backward-chaining tentacles)
/*
* Ask Prompt
* Will receive a specific prompt, then read it to
* the user. It then reads an input from the user, and convert
* the answer into yes, no, or leave it if it is not a valid input.
* If it is not a valid input, it will notify the user, and continue
* to prompt until a valid input is given and will return it.
*
* @param ?prompt: The prompt that will be read to the user.
* @return: The valid input from the user (yes or no after converting).
*/
(deffunction ask (?prompt)
(printout t ?prompt crlf)
(bind ?input (convert (read)))
(while (not (validate ?input)) do ;loop and prompt user until valid input
(printout t "Not a valid input! Enter yes or no." crlf)
(bind ?input (convert (read)))
)
(return ?input)
)
/*
* Convert Function
* Will convert the user input to yes or no if it is an accepted input, or leave it
* if it is not accepted.
* Accepted inputs: yes, y, Y, YES, no, n, N, and NO. Any other input can be considered
* unconventional and thus the user will be reprompted.
*
* The function will first will check if the input is yes, y, Y, or YES. If it is, it will
* rebind the variable to yes. Then it will check if the input is no, n, N, or NO. It will
* rebind the variable to no if either of these is true. It then returns the variable, unchanged
* if no cases are matched.
*
* @param ?a: The input to be converted.
*/
(deffunction convert (?a)
(if (or (= ?a "yes") (= ?a "y") (= ?a "Y") (= ?a "YES")) then
(bind ?a yes)
)
(if (or (= ?a "no") (= ?a "n") (= ?a "N") (= ?a "NO")) then
(bind ?a no)
)
(return ?a)
)
/*
* Validate Function
* Will validate the converted input by checking if it is yes or no. If it is valid, it
* will return true. Otherwise, it will return false.
*
* @param ?input: The input to be validated if it is yes or no.
*/
(deffunction validate (?input)
(if (or (= ?input yes) (= ?input no)) then
(bind ?x TRUE)
else
(bind ?x FALSE)
)
(return ?x)
)
/*
* Initial method to be called. Will print out the available animals, and notify the user
* of the inputs. It will then initiate the inference process in order to determine the
* animal the user has selected.
*/
(deffunction play ()
(printout t "Choose between a dog, a cat, a panda, an eagle, a hummingbird,
an eel, or an octopus. Use 'yes' or 'no' as input" crlf)
(run)
)
/*
* The animal definitions containing the certain conditions that will be true for each animal.
* Should all the conditions of one animal be met, it will print out the animal.
*/
/*
* Animal definition for dog. A dog lives on land, is domestic, and barks. If
* conditions are met, it will identify the animal as a dog.
*/
(defrule dog
(land yes)
(domestic yes)
(barking yes)
=>
(printout t "The animal is a dog." crlf)
)
/*
* Animal definition for cat. A cat lives on land, is domestic, and does not bark. If
* conditions are met, it will identify the animal as a cat.
*/
(defrule cat
(land yes)
(domestic yes)
(barking no)
=>
(printout t "The animal is a cat." crlf)
)
/*
* Animal definition for panda. A panda lives on land, is not domestic, and is black and white. If
* conditions are met, it will identify the animal as a panda.
*/
(defrule panda
(land yes)
(domestic no)
(blackandwhite yes)
=>
(printout t "The animal is a panda." crlf)
)
/*
* Animal definition for eagle. An eagle flies, and is the mascot of America. If conditions
* are met, it will identify the animal as an eagle.
*/
(defrule eagle
(fly yes)
(mascot yes)
=>
(printout t "The animal is an eagle." crlf)
)
/*
* Animal definition for hummingbird. A hummingbird flies, and drinks nectar. If conditions
* are met, it will identify the animal as a hummingbird.
*/
(defrule hummingbird
(fly yes)
(nectar yes)
=>
(printout t "The animal is a hummingbird." crlf)
)
/*
* Animal definition for eel. An eel lives in water, and is long. If conditions
* are met, it will identify the animal as an eel.
*/
(defrule eel
(water yes)
(long yes)
=>
(printout t "The animal is a eel." crlf)
)
/*
* Animal definition for octopus. An octopus lives in water, and has tentacles. If conditions
* are met, it will identify the animal as an octopus.
*/
(defrule octupus
(water yes)
(tentacles yes)
=>
(printout t "The animal is an octopus." crlf)
)
/*
* Backward chaining implementation of rules to find the
* characteristics in order to determine the animal.
*/
/*
* If the characteristic of land is needed, ask the user if the animal lives on land and assert the
* answer with land as a fact.
*/
(defrule need-land-rule
(need-land ?)
=>
(assert (land (ask "Does the animal live on land?")))
)
/*
* If the characteristic of domestic is needed, ask the user if the animal is domestic and assert the
* answer with domestic as a fact.
*/
(defrule need-domestic-rule
(need-domestic ?)
=>
(assert (domestic (ask "domestic?")))
)
/*
* If the characteristic of barking is needed, ask the user if the animal barks and assert the
* answer with barking as a fact.
*/
(defrule need-barking-rule
(need-barking ?)
=>
(assert (barking (ask "barking?")))
)
/*
* If the characteristic of being black and white is needed, ask the user if the animal
* is black and white and assert the answer with blackandwhite as a fact.
*/
(defrule need-blackandwhite-rule
(need-blackandwhite ?)
=>
(assert (blackandwhite (ask "blackandwhite?")))
)
/*
* If the characteristic of flying is needed, ask the user if the animal flies and assert the
* answer with fly as a fact. The animal may not live on land to fly, thus land must be false in order
* to ask the user for this condition.
*/
(defrule need-fly-rule
(need-fly ?)
(land no)
=>
(assert (fly (ask "does it fly?")))
)
/*
* If the characteristic of nectar is needed, ask the user if the animal drinks nectar and assert the
* answer with nectar as a fact.
*/
(defrule need-nectar-rule
(need-nectar ?)
=>
(assert (nectar (ask "nectar?")))
)
/*
* If the characteristic of mascot is needed, ask the user if the animal is a mascot and assert the
* answer with mascot as a fact. The animal may not be a mascot and drink nectar, thus nectar must be
* false in order to ask the user for this condition.
*/
(defrule need-mascot-rule
(need-mascot ?)
(nectar no)
=>
(assert (mascot (ask "mascot?")))
)
/*
* If the characteristic of water is needed, ask the user if the animal lives in water and assert the
* answer with water as a fact. The animal may not live on land or fly to live in water, thus land and fly
* must be false in order to ask the user for this condition.
*/
(defrule need-water-rule
(need-water ?)
(land no)
(fly no)
=>
(assert (water (ask "water?")))
)
/*
* If the characteristic of being long is needed, ask the user if the animal is long and assert the
* answer with long as a fact.
*/
(defrule need-long-rule
(need-long ?)
=>
(assert (long (ask "long?")))
)
/*
* If the characteristic of having tentacles is needed, ask the user if the animal has tentcales and assert the
* answer with tentacles as a fact. The animal may not have tentacles or be long, thus long must be false in order
* to ask the user for this condition.
*/
(defrule need-tentacles-rule
(need-tentacles ?)
(long no)
=>
(assert (tentacles (ask "tentacles?")))
)
(play)