def test_2(): """Create network that matches test definition file 2""" names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) CK1, CK2, AND1, NAND1, OR1, NOR1 = names.lookup( ["CK1", "CK2", "AND1", "NAND1", "OR1", "NOR1"]) devices.make_device(CK1, devices.CLOCK, 2) devices.make_device(CK2, devices.CLOCK, 1) devices.make_device(AND1, devices.AND, 2) devices.make_device(NAND1, devices.NAND, 2) devices.make_device(OR1, devices.OR, 2) devices.make_device(NOR1, devices.NOR, 2) network.make_connection(CK1, None, AND1, names.query("I1")) network.make_connection(CK2, None, AND1, names.query("I2")) network.make_connection(CK2, None, NAND1, names.query("I2")) network.make_connection(CK2, None, OR1, names.query("I2")) network.make_connection(CK2, None, NOR1, names.query("I2")) network.make_connection(AND1, None, NAND1, names.query("I1")) network.make_connection(NAND1, None, OR1, names.query("I1")) network.make_connection(OR1, None, NOR1, names.query("I1")) monitors.make_monitor(NOR1, None) return names, devices, network, monitors
def main(): # Initialise instances of the four inner simulator classes names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) [s1, s2] = devices.names.lookup(["S1", "S2"]) devices.make_switch(s1, 0) devices.make_switch(s2, 0) devices.make_gate(1, devices.names.query("NAND"), 2) devices.make_gate(2, devices.names.query("NAND"), 2) network.make_connection(s1, None, 1, devices.names.query("I1")) network.make_connection(s2, None, 2, devices.names.query("I2")) network.make_connection(1, None, 2, devices.names.query("I1")) network.make_connection(2, None, 1, devices.names.query("I2")) print(network.check_network()) print(monitors.make_monitor(1, None)) monitors.display_signals() userint = UserInterface(names, devices, network, monitors) userint.command_interface()
def run_parser(self, file_path): """Call parse_network() from path specified. To do so first reinitzialize all modules and cycles_completed. """ # clear all at the begging self.cycles_completed = 0 self.names = Names() self.devices = Devices(self.names) self.network = Network(self.names, self.devices) self.monitors = Monitors(self.names, self.devices, self.network) self.scanner = Scanner(file_path, self.names) self.parser = Parser(self.names, self.devices, self.network, self.monitors, self.scanner) # Capture the stdout from parse_network() captured_stdout = io.StringIO() with redirect_stdout(captured_stdout): if self.parser.parse_network(): self.parse_success = True self.log_message(_("Succesfully parsed network.")) else: self.parse_success = False self.log_message(_("Failed to parse network.")) # Show error messages captured in activity log self.log_message(captured_stdout.getvalue(), self.MONOSPACE_FONT)
def startup_parser(data): new_names = Names() new_scan = Scanner(data, new_names, True) new_devices = Devices(new_names) new_network= Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) parse = Parser(new_names, new_devices, new_network, new_monitors, new_scan) return parse
def new_parser(path): """Return a new instance of the Parser class.""" new_names = Names() new_scan = Scanner(path, new_names) new_devices = Devices(new_names) new_network = Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) return Parser(new_names, new_devices, new_network, new_monitors, new_scan)
def main(arg_list): """Parse the command line options and arguments specified in arg_list. Run either the command line user interface, the graphical user interface, or display the usage message. """ usage_message = ("Usage:\n" "Show help: logsim.py -h\n" "Command line user interface: logsim.py -c <file path>\n" "Graphical user interface: logsim.py <file path>") try: options, arguments = getopt.getopt(arg_list, "hc:") except getopt.GetoptError: print("Error: invalid command line arguments\n") print(usage_message) sys.exit() # Initialise instances of the four inner simulator classes names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) for option, path in options: if option == "-h": # print the usage message print(usage_message) sys.exit() elif option == "-c": # use the command line user interface scanner = Scanner(path, names) parser = Parser(names, devices, network, monitors, scanner) if parser.parse_network(): # Initialise an instance of the userint.UserInterface() class userint = UserInterface(names, devices, network, monitors) userint.command_interface() if not options: # no option given, use the graphical user interface if len(arguments) != 1: # wrong number of arguments print("Error: one file path required\n") print(usage_message) sys.exit() [path] = arguments scanner = Scanner(path, names) parser = Parser(names, devices, network, monitors, scanner) if parser.parse_network(): # Initialise an instance of the gui.Gui() class app = wx.App() builtins._ = wx.GetTranslation locale = wx.Locale() locale.Init(wx.LANGUAGE_DEFAULT) locale.AddCatalogLookupPathPrefix('./locale') locale.AddCatalog('zh_CN') gui = Gui("Logic Simulator", path, names, devices, network, monitors) gui.Show(True) app.MainLoop()
def create_parser(path): """Create a new parser for every test""" names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) scanner = Scanner(path, names) parser = Parser(names, devices, network, monitors, scanner) return parser
def parser_device(): """Return a new instance of the Parse class.""" names = Names() scanner = Scanner(file_device, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) new_parser = Parser(names, devices, network, monitors, scanner) return new_parser
def create_parser(input_file): """Return a new parser instance.""" path = "./test_specfiles/test_parse/" + input_file # Initialise instances of the four inner simulator classes names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) scanner = Scanner(path, names) return Parser(names, devices, network, monitors, scanner)
def new_parser(): """Return a new parser instance.""" file_name = 'file.txt' new_names = Names() new_devices = Devices(new_names) new_network = Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) new_scanner = Scanner(file_name, new_names) return Parser(new_names, new_devices, new_network, new_monitors, new_scanner)
def new_parser(): # Build a fixture and all the following tests uses it path = 'test_def.txt' names = Names() scanner = Scanner(path, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) new_parser = Parser(names, devices, network, monitors, scanner) return new_parser
def new_parser_with_errors(): """Return a new instance of the Parse class.""" new_names = Names() new_scanner = Scanner(file_errors, new_names) new_devices = Devices(new_names) new_network = Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) new_parser = Parser(new_names, new_devices, new_network, new_monitors, new_scanner) return new_parser
def test_1(): """Create network that matches test definition file 1""" names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) [SW1, SW2, SW3, SW4, D1, CK1, XOR1] = \ names.lookup(["SW1", "SW2", "SW3", "SW4", "D1", "CK1", "XOR1"]) print(names.query("SW1")) devices.make_device(SW1, devices.SWITCH, 1) devices.make_device(SW2, devices.SWITCH, 1) devices.make_device(SW3, devices.SWITCH, 1) devices.make_device(SW4, devices.SWITCH, 0) devices.make_device(D1, devices.D_TYPE) devices.make_device(CK1, devices.CLOCK, 2) devices.make_device(XOR1, devices.XOR) network.make_connection(SW1, None, XOR1, names.query("I1")) network.make_connection(SW2, None, XOR1, names.query("I2")) network.make_connection(XOR1, None, D1, names.query("DATA")) network.make_connection(CK1, None, D1, names.query("CLK")) network.make_connection(SW3, None, D1, names.query("SET")) network.make_connection(SW4, None, D1, names.query("CLEAR")) monitors.make_monitor(D1, names.query("Q")) monitors.make_monitor(D1, names.query("QBAR")) return names, devices, network, monitors
def parser_connect(): """Return a new instance of the Parse class.""" names = Names() scanner = Scanner(file_connect, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) new_parser = Parser(names, devices, network, monitors, scanner) [clk, dt] = new_parser.names.lookup(["CLK", "D1"]) new_parser.devices.make_device(clk, devices.CLOCK, 10) new_parser.devices.make_device(dt, devices.D_TYPE) return new_parser
def deviceline_parser(): """Return a new instance of the Parser module, using a test description file.""" new_names = Names() new_scanner = Scanner("test_def_files/for_check_deviceline.txt", new_names) new_devices = Devices(new_names) new_network = Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) return Parser( new_names, new_devices, new_network, new_monitors, new_scanner)
def on_menu(self, event): """Handle the event when the user selects a menu item.""" Id = event.GetId() if Id == 103: exitconf = self.exitconfirmation.ShowModal() if exitconf == wx.ID_YES: self.Close(True) if Id == 102: wx.MessageBox( _(u"Display the signal traces at different monitored outputs. \nRed trace represents '1', blue trace represents '0'.\nOutputs to be monitored can be selected by clicking 'Monitor'.\nSwitches levels can be selected by clicking 'Set Switches'" ), _(u"About Logsim"), wx.ICON_INFORMATION | wx.OK) if Id == wx.ID_OPEN: with wx.FileDialog(self) as fileDialog: if fileDialog.ShowModal() == wx.ID_CANCEL: return path = fileDialog.GetPath() self.Close(True) app = wx.App() error = ErrorFrame() names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) self.names = names self.devices = devices self.network = network self.monitors = monitors self.scanner = Scanner(path, self.names) self.parser = Parser(self.names, self.devices, self.network, self.monitors, self.scanner) global global_cycles_completed global hold global hold_monitor global_cycles_completed = 0 hold = {} hold_monitor = {} try: self.parser.parse_network() gui = Gui("LogicSim", path, self.names, self.devices, self.network, self.monitors) gui.Show(True) except: pass error.ShowModal() app.MainLoop()
def parser_io(): """Return a new instance of the Parse class.""" names = Names() scanner = Scanner(file_io, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) new_parser = Parser(names, devices, network, monitors, scanner) # Initially populate new_devices with some devices [new_parser.DT] = new_parser.names.lookup(["d1"]) new_parser.devices.make_device(new_parser.DT, devices.D_TYPE) return new_parser
def make_parser(self): """Initialise a parser for the testcase.""" if self.parser is not None: return None names = Names() scanner = Scanner(self.testfile_name, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) self.parser = Parser(names, devices, network, monitors, scanner, test_mode=True)
def test_parse_network_correct(): """Test the overall parse.py flow with a complicated enough but correct definition file.""" my_names = Names() my_scanner = Scanner("test_def_files/sequential.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) # Check that no semantic or syntax errors were made assert my_parser.parse_network()
def new_parser(filename): """Return a new instance of the Parser class and parse the definition file. Parameters ---------- filename: The name of the definition file in the specfiles directory """ SPECFILES_DIR = "testfiles/parser/" path = SPECFILES_DIR + filename names = Names() scanner = Scanner(path, names) devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) return Parser(names, devices, network, monitors, scanner)
def test_parse_network_incorrect_semantics12(): """Test the overall parse.py flow with a definition file with incorrect semantics. Basically, this file opens a block comment but does not close it. Ensure that parser does not get stuck in infinite loop.""" my_names = Names() my_scanner = Scanner("test_def_files/for_check_infiniteloop.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) # Check that parse_network() returns False assert not my_parser.parse_network()
def test_parse_network_incorrect_syntax(): """Test the overall parse.py flow with a definition file with incorrect syntax.""" my_names = Names() my_scanner = Scanner("test_def_files/srbistablewrong.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) assert not my_parser.parse_network() assert len(my_parser.syntax_errors_list) == 2 assert my_parser.syntax_errors_list[0] == "start" assert my_parser.syntax_errors_list[1] == my_scanner.DEVICES_ID
def main(arg_list): """Parse the command line options and arguments specified in arg_list. Run either the command line user interface, the graphical user interface, or display the usage message. """ usage_message = ("Usage:\n" "Show help: logsim.py -h\n" "Command line user interface: logsim.py -c <file path>\n" "Graphical user interface: logsim.py <file path>") try: options, arguments = getopt.getopt(arg_list, "hc:") except getopt.GetoptError: print("Error: invalid command line arguments\n") print(usage_message) sys.exit() # Initialise instances of the four inner simulator classes names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) for option, path in options: if option == "-h": # print the usage message print(usage_message) sys.exit() elif option == "-c": # use the command line user interface scanner = Scanner(path, names) parser = Parser(names, devices, network, monitors, scanner) if parser.parse_network(): # Initialise an instance of the userint.UserInterface() class userint = UserInterface(names, devices, network, monitors) userint.command_interface() if not options: # no option given, use the graphical user interface # Initialise an instance of the gui.Gui() class app = wx.App() gui = Gui("Logic Simulator") # gui = Gui("Logic Simulator", None, names, devices, network, monitors) gui.Show(True) app.MainLoop()
def new_parser(): """Return a new instance of the Parse class.""" new_names = Names() new_scanner = Scanner(file_no_errors, new_names) new_devices = Devices(new_names) new_network = Network(new_names, new_devices) new_monitors = Monitors(new_names, new_devices, new_network) new_parser = Parser(new_names, new_devices, new_network, new_monitors, new_scanner) # Initially populate new_devices with some devices [AND_ID, NOR_ID, CLK_ID, SW_ID] = new_parser.names.lookup(["foo", "bar", "b", "sw"]) new_parser.devices.make_device(AND_ID, new_devices.AND, 3) new_parser.devices.make_device(NOR_ID, new_devices.NOR, 2) new_parser.devices.make_device(CLK_ID, new_devices.CLOCK, 1) return new_parser
def test_4(): """Create network that matches test definition file 4, a ripple counter""" names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) SW, CK, D1, D2, D3, D4 = names.lookup(["SW", "CK", "D1", "D2", "D3", "D4"]) devices.make_device(SW, devices.SWITCH, 0) devices.make_device(CK, devices.CLOCK, 1) devices.make_device(D1, devices.D_TYPE) devices.make_device(D2, devices.D_TYPE) devices.make_device(D3, devices.D_TYPE) devices.make_device(D4, devices.D_TYPE) network.make_connection(SW, None, D1, names.query("SET")) network.make_connection(SW, None, D1, names.query("CLEAR")) network.make_connection(SW, None, D2, names.query("SET")) network.make_connection(SW, None, D2, names.query("CLEAR")) network.make_connection(SW, None, D3, names.query("SET")) network.make_connection(SW, None, D3, names.query("CLEAR")) network.make_connection(SW, None, D4, names.query("SET")) network.make_connection(SW, None, D4, names.query("CLEAR")) network.make_connection(CK, None, D1, names.query("CLK")) network.make_connection(D1, names.query("QBAR"), D2, names.query("CLK")) network.make_connection(D2, names.query("QBAR"), D3, names.query("CLK")) network.make_connection(D3, names.query("QBAR"), D4, names.query("CLK")) network.make_connection(D1, names.query("QBAR"), D1, names.query("DATA")) network.make_connection(D2, names.query("QBAR"), D2, names.query("DATA")) network.make_connection(D3, names.query("QBAR"), D3, names.query("DATA")) network.make_connection(D4, names.query("QBAR"), D4, names.query("DATA")) monitors.make_monitor(CK, None) monitors.make_monitor(D1, names.query("Q")) monitors.make_monitor(D2, names.query("Q")) monitors.make_monitor(D3, names.query("Q")) monitors.make_monitor(D4, names.query("Q")) return names, devices, network, monitors
def test_parse_network_incorrect_semantics11(): """Test the overall parse.py flow with a definition file with incorrect semantics.""" my_names = Names() my_scanner = Scanner("test_def_files/monitordeviceabsent.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) # Check that parse_network() returns False assert not my_parser.parse_network() # Check that there are no syntax errors assert len(my_parser.syntax_errors_list) == 0 # Check that 1 semantic error is logged (from actual error) assert len(my_parser.semantic_errors_list) == 1 assert my_parser.semantic_errors_list[0] == my_network.DEVICE_ABSENT
def main(arg_parser): """Parse the command line options and arguments specified in arg_list. Run either the command line user interface, the graphical user interface, or display the usage message. """ # Add check to see if path leads to a .txt file if arg_parser.path is not None: if arg_parser.path[-3:] != "txt": raise TypeError("***ERROR: Please load in a .txt file") # Check if user tries to input translation into command line # interface if arg_parser.c and arg_parser.r: print("Cannot launch command line mode with translator") sys.exit() # Initialise instances of the four inner simulator classes names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) scanner = Scanner(arg_parser.path, names) parser = Parser(names, devices, network, monitors, scanner) if parser.parse_network(): if arg_parser.c: # Initialise instance of the userint.UserInterface() class userint = UserInterface(names, devices, network, monitors) userint.command_interface() else: if arg_parser.r: lang = u"el" else: lang = u"en" # Initialise an instance of the gui.Gui() class file_name = get_file_name(arg_parser.path) title = "Logic Simulator - " + file_name app = wx.App() gui = Gui(title, arg_parser.path, names, devices, network, monitors, lang) gui.Show(True) app.MainLoop()
def test_parse_network_incorrect_semantics6(): """Test the overall parse.py flow with a definition file with incorrect semantics.""" my_names = Names() my_scanner = Scanner("test_def_files/connectionportabsent.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) # Check that parse_network() returns False assert not my_parser.parse_network() # Check that there are no syntax errors assert len(my_parser.syntax_errors_list) == 0 # Check that 2 semantic errors are logged (one from actual error, one from # network.check_network()) assert len(my_parser.semantic_errors_list) == 2 assert my_parser.semantic_errors_list[0] == my_network.PORT_ABSENT
def test_parse_network_incorrect_semantics3(): """Test the overall parse.py flow with a definition file with incorrect semantics.""" my_names = Names() my_scanner = Scanner("test_def_files/devicequalifierpresent.txt", my_names) my_devices = Devices(my_names) my_network = Network(my_names, my_devices) my_monitors = Monitors(my_names, my_devices, my_network) my_parser = Parser( my_names, my_devices, my_network, my_monitors, my_scanner) # Check that parse_network() returns False assert not my_parser.parse_network() # Check that there are no syntax errors assert len(my_parser.syntax_errors_list) == 0 # Check that 2 semantic errors are raised (one from # network.check_network()) assert len(my_parser.semantic_errors_list) == 2 assert my_parser.semantic_errors_list[0] == my_devices.QUALIFIER_PRESENT
def test_3(): """Create network that matches test definition file 3""" names = Names() devices = Devices(names) network = Network(names, devices) monitors = Monitors(names, devices, network) S, R, CK, D = names.lookup(["S", "R", "CK", "D"]) devices.make_device(S, devices.SWITCH, 0) devices.make_device(R, devices.SWITCH, 0) devices.make_device(CK, devices.CLOCK, 1) devices.make_device(D, devices.D_TYPE) network.make_connection(CK, None, D, names.query("CLK")) network.make_connection(S, None, D, names.query("SET")) network.make_connection(R, None, D, names.query("CLEAR")) network.make_connection(D, names.query("QBAR"), D, names.query("DATA")) monitors.make_monitor(CK, None) monitors.make_monitor(D, names.query("Q")) monitors.make_monitor(S, None) monitors.make_monitor(R, None) return names, devices, network, monitors