-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy patheditor.py
269 lines (198 loc) · 8.54 KB
/
editor.py
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
from tkinter import Tk, Text, Menu, filedialog, Label, Button, END, W, E, FALSE
from tkinter.scrolledtext import ScrolledText
from prologpy.solver import Solver
def is_file_path_selected(file_path):
return file_path is not None and file_path != ""
def get_file_contents(file_path):
"""Return a string containing the file contents of the file located at the
specified file path """
with open(file_path, encoding="utf-8") as f:
file_contents = f.read()
return file_contents
class Editor(object):
def __init__(self, root_):
self.root = root_
self.file_path = None
self.root.title("Prolog Interpreter")
# Create a rule label
self.rule_editor_label = Label(
root, text="Prolog Rules: ", padx=10, pady=1
)
self.rule_editor_label.grid(
sticky="W", row=0, column=0, columnspan=2, pady=3
)
# Create rule editor where we can edit the rules we want to enter:
self.rule_editor = ScrolledText(
root, width=100, height=30, padx=10, pady=10
)
self.rule_editor.grid(
sticky=W + E, row=1, column=0, columnspan=2, padx=10
)
self.rule_editor.config(wrap="word", undo=True)
self.rule_editor.focus()
# Create a query label:
self.query_label = Label(root, text="Prolog Query:", padx=10, pady=1)
self.query_label.grid(sticky=W, row=2, column=0, columnspan=2, pady=3)
# Create the Prolog query editor we'll use to query our rules:
self.query_editor = Text(root, width=77, height=2, padx=10, pady=10)
self.query_editor.grid(sticky=W, row=3, column=0, pady=3, padx=10)
self.query_editor.config(wrap="word", undo=True)
# Create a run button which runs the query against our rules and outputs the
# results in our solutions text box / editor.
self.run_button = Button(
root,
text="Find Query Solutions",
height=2,
width=20,
command=self.run_query,
)
self.run_button.grid(sticky=E, row=3, column=1, pady=3, padx=10)
# Create a solutions label
self.solutions_label = Label(
root, text="Query Solutions:", padx=10, pady=1
)
self.solutions_label.grid(
sticky="W", row=4, column=0, columnspan=2, padx=10, pady=3
)
# Create a text box which we'll use to display our Prolog query solutions:
self.solutions_display = ScrolledText(
root, width=100, height=5, padx=10, pady=10
)
self.solutions_display.grid(
row=5, column=0, columnspan=2, padx=10, pady=7
)
# Finally, let's create the file menu
self.menu_bar = self.create_file_menu()
def create_file_menu(self):
"""Create a menu which will allow us to open / save our Prolog rules, run our
query, and exit our editor interface """
menu_bar = Menu(root)
file_menu = Menu(menu_bar, tearoff=0)
file_menu.add_command(
label="Open...", underline=1, command=self.open_file
)
file_menu.add_separator()
file_menu.add_command(
label="Save", underline=1, command=self.save_file
)
file_menu.add_command(
label="Save As...", underline=5, command=self.save_file_as
)
file_menu.add_separator()
file_menu.add_command(label="Run", underline=1, command=self.run_query)
file_menu.add_separator()
file_menu.add_command(
label="Exit", underline=2, command=self.root.destroy
)
menu_bar.add_cascade(label="File", underline=0, menu=file_menu)
self.root.config(menu=menu_bar)
return menu_bar
def set_busy(self):
# Show a busy cursor and update the UI
self.root.config(cursor="watch")
self.root.update()
def set_not_busy(self):
# Show a regular cursor
self.root.config(cursor="")
def run_query(self):
"""Interpret the entered rules and query and display the results in the
solutions text box """
# Delete all of the text in our solutions display text box
self.solutions_display.delete("1.0", END)
self.set_busy()
# Fetch the raw rule / query text entered by the user
rules_text = self.rule_editor.get(1.0, "end-1c")
query_text = self.query_editor.get(1.0, "end-1c")
# Create a new solver so we can try to query for solutions.
try:
solver = Solver(rules_text)
except Exception as e:
self.handle_exception("Error processing prolog rules.", str(e))
return
# Attempt to find the solutions and handle any exceptions gracefully
try:
solutions = solver.find_solutions(query_text)
except Exception as e:
self.handle_exception("Error processing prolog query.", str(e))
return
# If our query returns a boolean, we simply display a 'Yes' or a 'No'
# depending on its value
if isinstance(solutions, bool):
self.solutions_display.insert(END, "Yes." if solutions else "No.")
# Our solver returned a map, so we display the variable name to value mappings
elif isinstance(solutions, dict):
self.solutions_display.insert(
END,
"\n".join(
"{} = {}"
# If our solution is a list contining one item, we show that
# item, otherwise we display the entire list
.format(variable, value[0] if len(value) == 1 else value)
for variable, value in solutions.items()
),
)
else:
# We know we have no matching solutions in this instance so we provide
# relevant feedback
self.solutions_display.insert(END, "No solutions found.")
self.set_not_busy()
def handle_exception(self, error_message, exception=""):
"""Handle the exception by printing an error message as well as exception in
our solution text editor / display """
self.solutions_display.insert(END, error_message + "\n")
self.solutions_display.insert(END, str(exception) + "\n")
self.set_not_busy()
def set_rule_editor_text(self, text):
self.rule_editor.delete(1.0, "end")
self.rule_editor.insert(1.0, text)
self.rule_editor.edit_modified(False)
def open_file(self, file_path=None):
# Open a a new file dialog which allows the user to select a file to open
if file_path is None:
file_path = filedialog.askopenfilename()
if is_file_path_selected(file_path):
file_contents = get_file_contents(file_path)
# Set the rule editor text to contain the selected file contents
self.set_rule_editor_text(file_contents)
self.file_path = file_path
def save_file(self):
"""If we have specified a file path, save the file - otherwise, prompt the
user to specify the file location prior to saving the file """
if self.file_path is None:
result = self.save_file_as()
else:
result = self.save_file_as(file_path=self.file_path)
return result
def write_editor_text_to_file(self, file):
editor_text = self.rule_editor.get(1.0, "end-1c")
file.write(bytes(editor_text, "UTF-8"))
self.rule_editor.edit_modified(False)
def save_file_as(self, file_path=None):
# If there is no file path specified, prompt the user with a dialog which
# allows him/her to select where they want to save the file
if file_path is None:
file_path = filedialog.asksaveasfilename(
filetypes=(
("Text files", "*.txt"),
("Prolog files", "*.pl *.pro"),
("All files", "*.*"),
)
)
try:
# Write the Prolog rule editor contents to the file location
with open(file_path, "wb") as file:
self.write_editor_text_to_file(file)
self.file_path = file_path
return "saved"
except FileNotFoundError:
return "cancelled"
def undo(self):
self.rule_editor.edit_undo()
def redo(self):
self.rule_editor.edit_redo()
if __name__ == "__main__":
root = Tk()
editor = Editor(root)
# Don't allow users to re-size the editor
root.resizable(width=FALSE, height=FALSE)
root.mainloop()