-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobex.clp
337 lines (294 loc) · 11.6 KB
/
jobex.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
;;;======================================================
;;; JOBEX: The JOB EXpert system.
;;; This helps you select a job based on who you are
;;;
;;; CLIPS Version 6.31
;;;
;;; To execute, merely load, reset and run.
;;;======================================================
;;****************
;; DEFFUNCTIONS *
;;****************
;; This function iteratively asks the questions
(deffunction next-questionnaire (?questionnaire ?allowed-values)
(printout t ?questionnaire crlf)
(printout t "reply: ")
(bind ?reply (read))
(printout t crlf)
(if (lexemep ?reply) then (bind ?reply (lowcase ?reply)))
(while (not (member ?reply ?allowed-values)) do
(printout t ?questionnaire crlf)
(printout t "reply: ")
(bind ?reply (read))
(printout t crlf)
(if (lexemep ?reply) then (bind ?reply (lowcase ?reply))))
?reply)
;;*****************
;; INIT
;;*****************
;; This rule prints the initial information about the program
(defrule start
(declare (salience 10000))
=>
(set-fact-duplication TRUE)
(printout t "" crlf)
(printout t "The JOBEX career quiz will help you decide the sort of jobs that will suit you best:" crlf)
(printout t "" crlf)
(printout t "Accepted replies:" crlf)
(printout t "-------------------------------------" crlf)
(printout t " Value Meaning " crlf)
(printout t "-------------------------------------" crlf)
(printout t " -1 Definitely Not " crlf)
(printout t " -0.8 Almost certainly not " crlf)
(printout t " -0.6 Probably not " crlf)
(printout t " -0.4 Maybe not " crlf)
(printout t " 0 Unknown " crlf)
(printout t " 0.4 Maybe " crlf)
(printout t " 0.6 Probably " crlf)
(printout t " 0.8 Almost certainly " crlf)
(printout t " 1 Definitely " crlf)
(printout t "-------------------------------------" crlf)
(printout t "" crlf))
;; Form of the facts
(deftemplate a-fact
(slot name)
(slot cf (default 0)))
;;*********************
;; COMBINE CERTAINTIES
;;*********************
;; Certainty factor combination rules
(defrule combine-certainties-1 (declare (salience 100)(auto-focus TRUE))
?fact1 <- (a-fact (name ?id) (cf ?cf1))
?fact2 <- (a-fact (name ?id) (cf ?cf2))
(test (neq ?fact1 ?fact2))
(test (> ?cf1 0))
(test (> ?cf2 0))
=>
(retract ?fact1)
(modify ?fact2 (cf (+ ?cf1 (* ?cf2 (- 1 ?cf1))))))
(defrule combine-certainties-2 (declare (salience 100)(auto-focus TRUE))
?fact1 <- (a-fact (name ?id) (cf ?cf1))
?fact2 <- (a-fact (name ?id) (cf ?cf2))
(test (neq ?fact1 ?fact2))
(test (< ?cf1 0))
(test (< ?cf2 0))
=>
(retract ?fact1)
(modify ?fact2 (cf (+ ?cf1 (* ?cf2 (+ 1 ?cf1))))))
(defrule combine-certainties-3 (declare (salience 100)(auto-focus TRUE))
?fact1 <- (a-fact (name ?id) (cf ?cf1))
?fact2 <- (a-fact (name ?id) (cf ?cf2))
(test (neq ?fact1 ?fact2))
(test (> ?cf1 0))
(test (< ?cf2 0))
=>
(retract ?fact1)
(modify ?fact2 (cf (/ (+ ?cf1 ?cf2) (- 1 (min (abs ?cf1) (abs ?cf2)))))))
;;*******************
;; QUESTIONNAIRE
;;*******************
;; Form of the questions
(deftemplate questionnaire
(slot a-fact (default ?NONE))
(slot the-questionnaire (default ?NONE))
(slot already-asked (default FALSE)))
;; Rule for asking a question
(defrule ask-a-questionnaire
?f <- (questionnaire (already-asked FALSE)
(the-questionnaire ?the-questionnaire)
(a-fact ?the-fact))
=>
(modify ?f (already-asked TRUE))
(bind ?accepted (create$ -1 -0.8 -0.6 -0.4 0 0.4 0.6 0.8 1))
(assert (a-fact (name ?the-fact) (cf (next-questionnaire ?the-questionnaire ?accepted)))))
;; List of questions
(deffacts questionnaire-facts
(questionnaire (a-fact q1)
(the-questionnaire "I spend most of my spare time actively socialising with friends, attending parties, shopping, etc. "))
(questionnaire (a-fact q2)
(the-questionnaire "I enjoy watching and playing team sports. "))
(questionnaire (a-fact q3)
(the-questionnaire "Being around a group of people gives me energy. "))
(questionnaire (a-fact q4)
(the-questionnaire "I would rather cook than go eat out in a restaurant. "))
(questionnaire (a-fact good-with-computers)
(the-questionnaire "I am very good with computers. "))
(questionnaire (a-fact q6)
(the-questionnaire "I improvise based on my past experience rather than looking for theoretical understanding. "))
(questionnaire (a-fact q7)
(the-questionnaire "I am comfortable working with ambiguous or incomplete information/data and guessing its meaning."))
(questionnaire (a-fact q8)
(the-questionnaire "I like to work around targets, deadlines and routines for managing my life. ")))
;;******************
;; THE RULES
;;******************
;; Based on the answers to the questions these rules infer facts
(defrule rule1
(or
(a-fact (name q1) (cf ?cf1))
(a-fact (name likes-sports) (cf ?cf1)))
=>
(if (>= ?cf1 0.4)
then
(assert (a-fact (name is-social) (cf (* 0.8 ?cf1))))
(assert (a-fact (name is-active) (cf (* 0.6 ?cf1))))
(assert (a-fact (name likes-outdoor-activities) (cf (* 0.8 ?cf1))))
(assert (a-fact (name likes-indoor-activities) (cf (* 0.8 ?cf1))))
(assert (a-fact (name likes-spending) (cf (* 0.8 ?cf1))))
else (if (<= ?cf1 -0.4)
then (assert (a-fact (name is-introvert) (cf (* 0.6 ?cf1))))
(assert (a-fact (name likes-indoor-activities) (cf (* 0.9 ?cf1)))))))
(defrule rule2
(and
(a-fact (name q2) (cf ?cf1))
(a-fact (name q3) (cf ?cf2)))
=>
(if (and (>= ?cf1 0.4) (>= ?cf2 0.4))
then
(assert (a-fact (name likes-sports) (cf (* 0.8 (min ?cf1 ?cf2)))))
(assert (a-fact (name is-social) (cf (* 0.6 (min ?cf1 ?cf2)))))
(assert (a-fact (name is-leader) (cf (* 0.7 (min ?cf1 ?cf2)))))
(assert (a-fact (name likes-teaching) (cf (* 0.7 (min ?cf1 ?cf2)))))
else (if (and (<= ?cf1 -0.4) (<= ?cf2 -0.4))
then (assert (a-fact (name is-introvert) (cf (* 0.6 (min ?cf1 ?cf2)))))
(assert (a-fact (name is-follower) (cf (* 0.7 (min ?cf1 ?cf2))))))))
(defrule rule3
(or
(a-fact (name q4) (cf ?cf1))
(a-fact (name is-social) (cf ?cf1)))
=>
(if (>= ?cf1 0.4)
then
(assert (a-fact (name is-introvert) (cf (* 0.9 ?cf1))))
(assert (a-fact (name is-follower) (cf (* 0.7 ?cf1))))
(assert (a-fact (name is-leader) (cf (* 0.7 ?cf1))))
(assert (a-fact (name likes-teaching) (cf (* -0.7 ?cf1))))
else (if (<= ?cf1 -0.4)
then (assert (a-fact (name is-active) (cf (* 0.6 ?cf1)))))))
(defrule rule4
(a-fact (name good-with-computers) (cf ?cf1))
=>
(if (>= ?cf1 0.4)
then
(assert (a-fact (name likes-indoor-activities) (cf (* 0.9 ?cf1))))
(assert (a-fact (name likes-outdoor-activities) (cf (* -0.7 ?cf1))))
(assert (a-fact (name is-follower) (cf (* -0.7 ?cf1))))
else (if (<= ?cf1 -0.4)
then (assert (a-fact (name is-active) (cf (* 0.6 ?cf1)))))))
(defrule rule5
(and
(a-fact (name q6) (cf ?cf1))
(a-fact (name is-follower) (cf ?cf2)))
=>
(if (<= ?cf1 -0.4)
then
(assert (a-fact (name likes-studying) (cf (* 0.5 (min ?cf1 ?cf2)))))
(assert (a-fact (name likes-teaching) (cf (* 0.7 (min ?cf1 ?cf2)))))
else (if (>= ?cf1 0.4)
then (assert (a-fact (name is-active) (cf (* -0.3 (min ?cf1 ?cf2))))))))
(defrule rule6
(a-fact (name q7) (cf ?cf1))
=>
(if (>= ?cf1 0.4)
then
(assert (a-fact (name likes-studying) (cf (* 0.9 ?cf1))))
(assert (a-fact (name likes-teaching) (cf (* 0.7 ?cf1))))))
(defrule rule7
(a-fact (name q8) (cf ?cf1))
=>
(if (>= ?cf1 0.4)
then
(assert (a-fact (name is-follower) (cf (* 0.9 ?cf1))))
(assert (a-fact (name is-leader) (cf (* -0.9 ?cf1))))))
;;************************
;;* JOB SELECTION RULES
;;************************
;; Based on the infered facts these rules infer the best job
(defrule print-results
(a-fact (name q1) (cf ?cf1))
=>
(printout t "The recommended jobs for you are:" crlf)
(printout t crlf)
(assert (finished)))
(defrule job-selection-rule-1
(finished)
(a-fact (name is-leader) (cf ?cf1))
(a-fact (name is-social) (cf ?cf2))
=>
(if (>= (* 0.7 (min ?cf1 ?cf2)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Manager with cf " (* 0.7 (min ?cf1 ?cf2)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))
(defrule job-selection-rule-2
(finished)
(a-fact (name is-social) (cf ?cf1))
(a-fact (name is-leader) (cf ?cf2))
(a-fact (name likes-teaching) (cf ?cf3))
=>
(if (>= (* 0.8 (min ?cf1 ?cf2 ?cf3)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Professor with cf " (* 0.8 (min ?cf1 ?cf2 ?cf3)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf))))
(defrule job-selection-rule-3
(finished)
(a-fact (name likes-spending) (cf ?cf1))
(a-fact (name likes-outdoor-activities) (cf ?cf2))
(a-fact (name likes-sports) (cf ?cf3))
=>
(if (>= (* 0.9 (min ?cf1 ?cf2 ?cf3)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "HR Manager with cf " (* 0.9 (min ?cf1 ?cf2 ?cf3)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))
(defrule job-selection-rule-4
(finished)
(a-fact (name likes-indoor-activities) (cf ?cf1))
(a-fact (name likes-studying) (cf ?cf2))
(a-fact (name is-introvert) (cf ?cf3))
(a-fact (name good-with-computers) (cf ?cf4))
=>
(if (>= (* 0.8 (min ?cf1 ?cf2 ?cf3 ?cf4)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Computer Programmer with cf " (* 0.8 (min ?cf1 ?cf2 ?cf3 ?cf4) ) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))
(defrule job-selection-rule-5
(finished)
(a-fact (name likes-indoor-activities) (cf ?cf1))
(a-fact (name likes-studying) (cf ?cf2))
(a-fact (name is-leader) (cf ?cf3))
=>
(if (>= (* 0.7 (min ?cf1 ?cf2 ?cf3)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Mathematician with cf " (* 0.7 (min ?cf1 ?cf2 ?cf3)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))
(defrule job-selection-rule-6
(finished)
(a-fact (name is-leader) (cf ?cf1))
(a-fact (name likes-studying) (cf ?cf2))
=>
(if (>= (* 0.8 (min ?cf1 ?cf2)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Architect with cf " (* 0.8 (min ?cf1 ?cf2)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))
(defrule job-selection-rule-7
(finished)
(a-fact (name is-follower) (cf ?cf1))
(a-fact (name likes-outdoor-activities) (cf ?cf2))
=>
(if (>= (* 0.9 (min ?cf1 ?cf2)) 0.4)
then
(printout t "------------------------------------------------------" crlf)
(printout t "Construction Worker with cf " (* 0.9 (min ?cf1 ?cf2)) crlf)
(printout t "------------------------------------------------------" crlf)
(printout t crlf)))