-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmorse.CLP
207 lines (169 loc) · 4.64 KB
/
morse.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
; ===================================================
; File: morse.CLP
; Date: 11/06/2019
; Author: James Anthony Ortiz
; Description: A morse-code translator
; used through the application of deffacts
; rules, and templates.
; ===================================================
(deftemplate conversion
(slot character)
(multislot morse-code))
(deffacts conversions
(conversion (character A) (morse-code * -))
(conversion (character B) (morse-code - * * *))
(conversion (character C) (morse-code - * - *))
(conversion (character D) (morse-code - * *))
(conversion (character E) (morse-code *))
(conversion (character F) (morse-code * * - *))
(conversion (character G) (morse-code - - *))
(conversion (character H) (morse-code * * * *))
(conversion (character I) (morse-code * *))
(conversion (character J) (morse-code * - - -))
(conversion (character K) (morse-code - * -))
(conversion (character L) (morse-code * - * *))
(conversion (character M) (morse-code - -))
(conversion (character N) (morse-code - *))
(conversion (character O) (morse-code - - -))
(conversion (character P) (morse-code * - - *))
(conversion (character Q) (morse-code - - * -))
(conversion (character R) (morse-code * - *))
(conversion (character S) (morse-code * * *))
(conversion (character T) (morse-code -))
(conversion (character U) (morse-code * * -))
(conversion (character V) (morse-code * * * -))
(conversion (character W) (morse-code * - -))
(conversion (character X) (morse-code - * * -))
(conversion (character Y) (morse-code - * - -))
(conversion (character Z) (morse-code - - * *)))
(defrule start
=>
(assert (phase start)))
(defrule enter-message
?phase <- (phase start)
=>
(retract ?phase)
(assert (phase start))
(printout t crlf "Enter a message (<CR> to end): ")
(assert (message (explode$ (readline))/)))
(defrule enter-message2
?phase <- (phase start)
=>
(retract ?phase)
(printout t crlf "Enter a message (<CR> to end): ")
(assert (message (explode$ (readline))/)))
(defrule end
(message /)
?phase <- (phase start)
=>
(retract ?phase))
(defrule translate
?message <- (message $?x / $?rest)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message)
(assert (rest $?rest))
(printout t "The message is " ?letter " " ))
(defrule translate2
?message <- (message $?x / $?rest)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message)
(assert (message $?rest))
(assert (rest $?x)))
(defrule translate3
?message <- (message $?x / $?rest)
?letter <- (letter $?l)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message ?letter)
(assert (message $?rest))
(assert (letter $?l ?letter)))
(defrule translate4
?message <- (message $?x / $?rest)
?letter <- (letter $?l)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message ?letter)
(assert (message $?rest))
(assert (letter $?l ?letter))
(assert (phase print)))
(defrule translate5
?message <- (message $?x / $?rest)
?ltr <- (letter $?l)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message ?ltr)
(assert (message $?rest))
(assert (letter $?l ?letter))
(assert (phase print)))
(defrule rest
?message <- (rest $?x / $?rest)
(conversion (character ?letter) (morse-code $?x))
=>
(retract ?message)
(assert (rest $?rest))
(printout t ?letter " " ))
(defrule rest2
?letters <- (letter $?x)
?phase <- (phase print)
=>
(retract ?letters ?phase)
(printout t "The message is: " $?x ))
(defrule rest3
?letters <- (letter $?x)
?phase <- (phase print)
=>
(retract ?letters ?phase)
(assert (phase start))
(printout t "The message is: " $?x ))
(defrule rest4
?letters <- (letter $?x)
?msg <- (message)
=>
(retract ?letters ?msg)
(assert (phase start))
(printout t "The message is: " (implode$ ?x) ))
(defrule bad-message1
(conversion (morse-code $?y))
(not (or (message $?y)
(message /)
(rest $?y /)))
?message <- (message $?x)
=>
(retract ?message)
(assert (phase start))
(printout t "Can't decode this message." ))
(defrule bad-message2
(conversion (morse-code $?y))
(not (and (or (message $?y)
(message /)
(rest $?y /))))
(or ?message <- (message $?x)
?message <- (rest $?x))
=>
(retract ?message)
(assert (phase start))
(printout t "Can't decode this message." ))
(defrule bad-message3
(conversion (morse-code $?y))
(not (and (or (message $?y)(message /)(rest $?y /)(rest))))
(or ?message <- (message $?x)
?message <- (rest $?x))
=>
(retract ?message)
(assert (phase start))
(printout t "Can't decode this message." ))
(defrule bad-message4
(conversion (morse-code $?y))
(not (and (or (message $?y)
(message /))))
?message <- (message $?x)
=>
(retract ?message)
(assert (phase start))
(printout t "Can't decode this message." ))
(defrule del
?rest <- (rest)
=>
(retract ?rest))