-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_userint.py
286 lines (238 loc) · 10 KB
/
gui_userint.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
"""Implement the interactive command line user interface for the GUI.
Used in the GUI project to enable the user to enter commands
to run the simulation or adjust the network properties.
Classes:
--------
GuiUserInterface - reads and parses user commands.
"""
class GuiUserInterface:
"""Read and parse user commands.
This class allows the user to enter certain commands.
These commands enable the user to run or continue the simulation for a
number of cycles, set switches, add or zap monitors, show help, or quit
the program.
Parameters
-----------
names: instance of the names.Names() class.
devices: instance of the devices.Devices() class.
network: instance of the network.Network() class.
monitors: instance of the monitors.Monitors() class.
Public methods:
---------------
command_interface(self): Reads in the commands and calls the corresponding
functions.
get_line(self): Prints a prompt for the user and updates the user entry.
read_command(self): Returns the first non-whitespace character.
get_character(self): Moves the cursor forward by one character in the user
entry.
skip_spaces(self): Skips whitespace characters until a non-whitespace
character is reached.
read_string(self): Returns the next alphanumeric string.
read_name(self): Returns the name ID of the current string.
read_signal_name(self): Returns the device and port IDs of the current
signal name.
read_number(self, lower_bound, upper_bound): Returns the current number.
help_command(self): Prints a list of valid commands.
switch_command(self): Sets the specified switch to the specified signal
level.
monitor_command(self): Sets the specified monitor.
zap_command(self): Removes the specified monitor.
run_network(self, cycles): Runs the network for the specified number of
simulation cycles.
run_command(self): Runs the simulation from scratch.
continue_command(self): Continues a previously run simulation.
"""
def __init__(self, names, devices, network, monitors, refresh_canvas):
"""Initialise variables."""
self.names = names
self.devices = devices
self.monitors = monitors
self.network = network
self.refresh_canvas = refresh_canvas
self.cycles_completed = 0 # number of simulation cycles completed
self.character = "" # current character
self.line = "" # current string entered by the user
self.cursor = 0 # cursor position
def command_interface(self, user_input, output_cmd, input_cmd):
"""Read the command entered and call the corresponding function."""
self.cursor = 0
self.output_cmd = output_cmd
self.input_cmd = input_cmd
self.line = user_input # get the user entry
command = self.read_command()
if command != "q":
if command == "h":
self.help_command()
elif command == "s":
self.switch_command()
elif command == "m":
self.monitor_command()
elif command == "z":
self.zap_command()
elif command == "r":
self.run_command()
elif command == "c":
self.continue_command()
else:
self.output_cmd(_("Invalid command. Enter 'h' for help."))
def read_command(self):
"""Return the first non-whitespace character."""
self.skip_spaces()
return self.character
def get_character(self):
"""Move the cursor forward by one character in the user entry."""
if self.cursor < len(self.line):
self.character = self.line[self.cursor]
self.cursor += 1
else: # end of the line
self.character = ""
def skip_spaces(self):
"""Skip whitespace until a non-whitespace character is reached."""
self.get_character()
while self.character.isspace():
self.get_character()
def read_string(self):
"""Return the next alphanumeric string."""
self.skip_spaces()
name_string = ""
if not self.character.isalpha(): # the string must start with a letter
self.output_cmd(_("Error! Expected a name."))
return None
while self.character.isalnum():
name_string = "".join([name_string, self.character])
self.get_character()
return name_string
def read_name(self):
"""Return the name ID of the current string if valid.
Return None if the current string is not a valid name string.
"""
name_string = self.read_string()
if name_string is None:
return None
else:
name_id = self.names.query(name_string)
if name_id is None:
self.output_cmd(_("Error! Unknown name."))
return name_id
def read_signal_name(self):
"""Return the device and port IDs of the current signal name.
Return None if either is invalid.
"""
device_id = self.read_name()
if device_id is None:
return None
elif self.character == ".":
port_id = self.read_name()
if port_id is None:
return None
else:
port_id = None
return [device_id, port_id]
def read_number(self, lower_bound, upper_bound):
"""Return the current number.
Return None if no number is provided or if it falls outside the valid
range.
"""
self.skip_spaces()
number_string = ""
if not self.character.isdigit():
self.output_cmd(_("Error! Expected a number."))
return None
while self.character.isdigit():
number_string = "".join([number_string, self.character])
self.get_character()
number = int(number_string)
if upper_bound is not None:
if number > upper_bound:
self.output_cmd(_("Number out of range."))
return None
if lower_bound is not None:
if number < lower_bound:
self.output_cmd(_("Number out of range."))
return None
return number
def help_command(self):
"""Print a list of valid commands."""
self.output_cmd(_("User commands:"))
self.output_cmd(_("r N - run the simulation for N cycles"))
self.output_cmd(_("c N - continue the simulation for N cycles"))
self.output_cmd(_("s X N - set switch X to N (0 or 1)"))
self.output_cmd(_("m X - set a monitor on signal X"))
self.output_cmd(_("z X - zap the monitor on signal X"))
self.output_cmd(_("h - help (this command)"))
self.output_cmd(_("q - quit the program"))
def switch_command(self):
"""Set the specified switch to the specified signal level."""
switch_id = self.read_name()
if switch_id is not None:
switch_state = self.read_number(0, 1)
if switch_state is not None:
if self.devices.set_switch(switch_id, switch_state):
self.output_cmd(_("Successfully set switch."))
else:
self.output_cmd(_("Error! Invalid switch."))
def monitor_command(self):
"""Set the specified monitor."""
monitor = self.read_signal_name()
if monitor is not None:
[device, port] = monitor
monitor_error = self.monitors.make_monitor(
device, port, self.cycles_completed
)
if monitor_error == self.monitors.NO_ERROR:
self.output_cmd(_("Successfully made monitor."))
else:
self.output_cmd(_("Error! Could not make monitor."))
def zap_command(self):
"""Remove the specified monitor."""
monitor = self.read_signal_name()
if monitor is not None:
[device, port] = monitor
if self.monitors.remove_monitor(device, port):
self.output_cmd(_("Successfully zapped monitor"))
else:
self.output_cmd(_("Error! Could not zap monitor."))
def run_network(self, cycles):
"""Run the network for the specified number of simulation cycles.
Return True if successful.
"""
for _ in range(cycles):
if self.network.execute_network():
self.monitors.record_signals()
else:
self.output_cmd(_("Error! Network oscillating."))
return False
# self.monitors.display_signals()
self.refresh_canvas()
return True
def run_command(self):
"""Run the simulation from scratch."""
self.cycles_completed = 0
cycles = self.read_number(0, None)
if cycles is not None: # if the number of cycles provided is valid
self.monitors.reset_monitors()
self.output_cmd(
"".join([_("Running for "), str(cycles), _(" cycles")])
)
self.devices.cold_startup()
if self.run_network(cycles):
self.cycles_completed += cycles
def continue_command(self):
"""Continue a previously run simulation."""
cycles = self.read_number(0, None)
if cycles is not None: # if the number of cycles provided is valid
if self.cycles_completed == 0:
self.output_cmd(_("Error! Nothing to continue. Run first."))
elif self.run_network(cycles):
self.cycles_completed += cycles
self.output_cmd(
" ".join(
[
_("Continuing for"),
str(cycles),
_("cycles."),
_("Total:"),
str(self.cycles_completed),
]
)
)