-
Notifications
You must be signed in to change notification settings - Fork 0
/
expert_system.rb
299 lines (292 loc) · 8.5 KB
/
expert_system.rb
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
def shutdown
puts "Shutting down gracefully..."
sleep 1
exit(false)
end
Signal.trap("INT") {
shutdown
exit(false)
}
Signal.trap("TERM") {
shutdown
exit(false)
}
define_method :clearAll do | factArray, rulesToCode, factStatement, rules |
rules.clear
rulesToCode.clear
factStatement.clear
('A'..'Z').each do |c|
factArray.store(c, false)
end
end
define_method :runExpression do |rulesToCode, index|
begin
eval "if #{rulesToCode[index][0]}\n#{rulesToCode[index][2]} end"
if rulesToCode[index][1] == "<=>"
rulesToCode[index][0].gsub!(/\==/, '=')
rulesToCode[index][2].gsub!(/\=/, '==')
eval "if #{rulesToCode[index][2]}\n#{rulesToCode[index][0]} end"
end
rescue Exception => e
puts "Rule #{index + 1}: syntax error"
return false
end
return true
end
define_method :engine do |rulesToCode|
i = 1
rulesToCode.each_with_index do |line, index|
rulesToCode.each_with_index do |line, index|
if index < i
if runExpression(rulesToCode, index) == false
return false
end
else
break
end
end
if i == rulesToCode.size && i > 1
rulesToCode.each_with_index do |line, index|
runExpression(rulesToCode, index)
end
end
i += 1
end
return true
end
define_method :runEngine do |filename, factArray, rulesToCode, factStatement, rules|
clearAll(factArray, rulesToCode, factStatement, rules)
file = File.open(filename)
read = file.read
read.gsub!(/#.*$/, '')
read.split('').each do |c|
if !c.match(/[A-Z()!?+^|=<>\s*\t*\n+]/)
puts "Unknow character present"
file.close
return
end
end
if !read.match(/^=[A-Z]*\s*$/)
puts "Facts: syntax error"
file.close
return
elsif read.match(/^\?\s*$/)
puts "Queries: none"
file.close
return
elsif !read.match(/^\?[A-Z]+\s*$/)
puts "Queries: syntax error"
file.close
return
end
if facts = read.match(/^=[A-Z]+\s*$/).to_s
('A'..'Z').each do |c|
if facts.include? c
factArray[c] = true
end
end
else
facts = ""
end
if !facts.empty?
factStatement = facts.gsub!(/[\=\n]/, '').split('').each { |fact| factStatement << fact }
else
factStatement = ""
end
read.each_line do |line|
if line.match(/^\(?!?[A-Z]/)
rules.push(line)
end
end
if rules.any?
duplicates = rules.each_with_object([]) { |e, a| a << e if rules.count(e) > 1 }
if duplicates.any?
puts "Rules: duplicates expression"
clearAll(factArray, rulesToCode, factStatement, rules)
file.close
return
end
rulesError = false
rules.each_with_index do |line, index|
if !line.match(/^\(?!?[A-Z]\s+([+|^]|=>|<=>)\s+\(?!?[A-Z]\)?/) or line.match(/[A-Z]\s*[A-Z]/) \
or line.match(/([+|^]|=>|<=>)\s*([+|^]|=>|<=>)/)
puts "Rule #{index + 1}: syntax error"
clearAll(factArray, rulesToCode, factStatement, rules)
file.close
return
else
expression = line.gsub!(/^/, ' ')
expression = line.split(/(=>|<=>)/)
if expression.to_s.count("(") != expression.to_s.count(")")
puts "Rule #{index + 1}: parentheses not properly closed"
clearAll(factArray, rulesToCode, factStatement, rules)
rulesError = true
break
elsif expression[2].match(/[\||^]/)
puts "Rule #{index + 1}: ambiguous ruleset \"" + expression[1] + " " + expression[2].strip + "\""
clearAll(factArray, rulesToCode, factStatement, rules)
rulesError = true
break
else
if expression[0].match(/\([A-Z]/)
expression[0].gsub!(/([A-Z])/, 'factArray["\1"] == true')
else
expression[0].gsub!(/([^!])([A-Z])/, ' factArray["\2"] == true')
end
expression[0].gsub!(/(!)([A-Z])/, 'factArray["\2"] == false')
if expression[2].match(/\([A-Z]/)
expression[2].gsub!(/([A-Z])/, 'factArray["\1"] == true')
else
expression[2].gsub!(/([^!])([A-Z])/, ' factArray["\2"] = true')
end
expression[2].gsub!(/(!)([A-Z])/, 'factArray["\2"] = false')
expression.map { |e| e.gsub!("+", "&&") }
expression.map { |e| e.gsub!("|", "||") }
expression.map do |e|
if e.match(/\(?factArray\["[A-Z]"\] == (true|false)\s+\^\s+factArray\["[A-Z]"\] == (true|false)\)?/)
e.gsub!(/(factArray\["[A-Z]"\] == (true|false))/, '(\1)')
end
end
rulesToCode << expression
end
end
end
end
if !rulesError
if engine(rulesToCode) == false
clearAll(factArray, rulesToCode, factStatement, rules)
return
end
if queries = read.match(/^\?[A-Z]+\s*$/).to_s
puts queries.strip
('A'..'Z').each do |c|
if queries.include? c
puts "#{c} = " + factArray[c].to_s
end
end
end
end
file.close
return
end
factArray = {}
('A'..'Z').each do |c|
factArray.store(c, false)
end
puts "\033[31mWelcome to this interactive Expert System."
puts "Type \"help\" to print a list of available commands."
puts "By default, all facts are false, and can only be made true"
puts "by the initial facts statement, or by application of a rule.\033[0m"
time = Time.now.getutc.strftime("%H:%M:%S")
print "\033[33m#{time} [No file loaded] >> \033[0m"
filename = ""
factStatement = []
rulesToCode = []
rules = []
noFile = true
while input = $stdin.gets
input ||= ""
input.chomp!
args = input.split(' ')
if input.match(/^quit\s*$/)
shutdown
elsif input.match(/^reset\s*$/)
clearAll(factArray, rulesToCode, factStatement, rules)
noFile = true
puts "Cleared"
elsif input.match(/^rules\s*$/)
if rules.empty?
puts "Rules: none"
else
rules.each do |rule|
puts rule.strip
end
end
elsif input.match(/^fact\s+[A-Za-z]+\s+=\s+(true|false)\s*$/)
if args[3] == "true"
args[1].split('').each do |letter|
isFact = false
factStatement.each do |fact|
if fact == letter.upcase
isFact = true
end
end
if isFact == false
factStatement << letter.upcase
end
end
else
args[1].split('').each do |letter|
factStatement.each do |fact|
if fact == letter.upcase
factStatement.delete(fact)
end
end
end
end
puts "Success"
elsif input.match(/^save\s*$/)
if rulesToCode.empty?
puts "Rules: none"
else
('A'..'Z').each do |c|
factArray.store(c, false)
end
factStatement.each do |fact|
factArray.store(fact, true)
end
engine(rulesToCode)
puts "Saved and reevaluated"
end
elsif input.match(/^facts\s*$/)
factArray.each { |key, value| puts "#{key} = #{value}" }
elsif input.match(/^facts:statement\s*$/)
puts "=" + factStatement.join(',').gsub(',', '').chars.sort.join
elsif input.match(/^query\s+[A-Za-z]+\s*$/)
args[1].split('').each do |letter|
puts letter.upcase + " = " + factArray[letter.upcase].to_s
end
elsif input.match(/^help\s*$/)
puts "====================================== COMMANDS ======================================="
puts "# #"
puts "# run [file path] : load a file and run it. Reset all facts #"
puts "# #"
puts "# fact [letter] = [true/false] : set the fact statement (not saved !) #"
puts "# save : save the new facts and reevaluate the rules #"
puts "# query [letters] : print the fact(s) corresponding #"
puts "# #"
puts "# rules : print all rules #"
puts "# facts : print all saved facts #"
puts "# facts:statement : print all facts statements #"
puts "# reset : reset all facts and rules #"
puts "# quit : exit the program #"
puts "# #"
puts "======================================================================================="
elsif input.match(/^run\s+.+\s*$/)
filename = args[1]
if !File.file?(filename)
puts filename + " is not a file."
elsif !File.readable?(filename)
puts filename + ": permission denied"
elsif File.zero?(filename)
puts filename + " is empty."
else
runEngine(filename, factArray, rulesToCode, factStatement, rules)
if !rules.empty?
noFile = false
else
noFile = true
end
end
else
puts "Unknow command"
end
time = Time.now.getutc.strftime("%H:%M:%S")
print "\033[33m#{time} "
if noFile == false
print "[" + filename + "]"
else
print "[No file loaded]"
end
print " >> \033[0m"
end