Example #1
0
def show_first_menu():
    # Record which models are in the models folder
    models_list = [
        f for f in listdir(models_path)
        if isfile(join(models_path, f)) and f[-4:] == ".dat"
    ]

    first_menu = ConsoleMenu("Main menu")

    submenu = SelectionMenu(models_list, "Load Model")
    submenu_item = SubmenuItem("Load a model",
                               submenu,
                               menu=first_menu,
                               should_exit=True)

    first_menu.append_item(submenu_item)

    first_menu.start()
    first_menu.join()

    if submenu.selected_option >= len(models_list):
        show_first_menu()
        return
    elif submenu.selected_option == -1:
        return

    selected_model = models_list[submenu.selected_option]

    net, jtree = util.load_model(models_path + selected_model)
    if net is not None and jtree is not None:
        jtree.initialize_tables(net)
        print("Model loaded succesfully")
        show_loaded_model_menu(selected_model, net, jtree)
    else:
        show_first_menu()
Example #2
0
    def test_action(self):
        root_menu = ConsoleMenu("root_menu", "test_action")
        submenu1 = ConsoleMenu("submenu1", "test_action")
        submenu2 = ConsoleMenu("submenu2", "test_action")
        submenu_item_1 = SubmenuItem("submenu_item_1", submenu1, menu=root_menu)
        submenu_item_2 = SubmenuItem("submenu_item_2", submenu2, menu=root_menu)

        root_menu.append_item(submenu_item_1)
        root_menu.append_item(submenu_item_2)

        root_menu.start()
        root_menu.wait_for_start(timeout=10)
        self.assertIs(ConsoleMenu.currently_active_menu, root_menu)
        submenu_item_1.action()
        submenu1.wait_for_start(timeout=10)
        self.assertIs(ConsoleMenu.currently_active_menu, submenu1)
        ConsoleMenu.currently_active_menu.exit()
        submenu1.join(timeout=10)
        self.assertIs(ConsoleMenu.currently_active_menu, root_menu)
        submenu_item_2.action()
        submenu2.wait_for_start(timeout=10)
        self.assertIs(ConsoleMenu.currently_active_menu, submenu2)
 def test_currently_active_menu(self):
     menu1 = ConsoleMenu("menu1", "test_currently_active_menu_1")
     menu2 = ConsoleMenu("menu2", "test_currently_active_menu_2")
     menu3 = ConsoleMenu("menu3", "test_currently_active_menu_3")
     self.assertIsNone(ConsoleMenu.currently_active_menu)
     menu1.start()
     menu1.wait_for_start(10)
     self.assertIs(ConsoleMenu.currently_active_menu, menu1)
     menu2.start()
     menu2.wait_for_start(10)
     self.assertIs(ConsoleMenu.currently_active_menu, menu2)
     menu3.start()
     menu3.wait_for_start(10)
     self.assertIs(ConsoleMenu.currently_active_menu, menu3)
     menu3.join(timeout=5)
     menu2.join(timeout=5)
     menu1.join(timeout=5)
class TestSampleMenu(BaseTestCase):
    def setUp(self):
        super(TestSampleMenu, self).setUp()

        self.menu = ConsoleMenu("self.menu", "TestSampleMenu")
        self.item1 = MenuItem("self.item1", self.menu)
        self.item2 = MenuItem("self.item2", self.menu)
        self.menu.append_item(self.item1)
        self.menu.append_item(self.item2)
        self.menu.start()
        self.menu.wait_for_start(timeout=10)

    def tearDown(self):
        super(TestSampleMenu, self).tearDown()
        self.menu.exit()
        self.menu.join(timeout=10)

    def test_go_down(self):
        self.menu.go_down()
        self.assertEqual(self.menu.current_option, 1)
        self.assertIs(self.menu.current_item, self.item2)
        self.menu.go_down()
        self.assertEqual(self.menu.current_option, 2)
        self.assertEqual(self.menu.current_item, self.menu.exit_item)
        self.menu.go_down()
        self.assertEqual(self.menu.current_option, 0)
        self.assertIs(self.menu.current_item, self.item1)

    def test_go_up(self):
        self.menu.go_up()
        self.assertEqual(self.menu.current_option, 2)
        self.assertIs(self.menu.current_item, self.menu.exit_item)
        self.menu.go_up()
        self.assertEqual(self.menu.current_option, 1)
        self.assertEqual(self.menu.current_item, self.item2)
        self.menu.go_up()
        self.assertEqual(self.menu.current_option, 0)
        self.assertIs(self.menu.current_item, self.item1)

    def test_go_to(self):
        self.menu.go_to(1)
        self.assertEqual(self.menu.current_option, 1)
        self.assertEqual(self.menu.current_item, self.item2)

    def test_select(self):
        self.menu.select()
        self.assertEqual(self.menu.selected_option, 0)
        self.assertIs(self.menu.selected_item, self.item1)
        self.menu.go_down()
        self.menu.select()
        self.assertEqual(self.menu.selected_option, 1)
        self.assertIs(self.menu.selected_item, self.item2)
        self.menu.go_down()
        self.menu.select()
        self.assertEqual(self.menu.selected_option, 2)
        self.assertIs(self.menu.selected_item, self.menu.exit_item)
        self.menu.join(timeout=10)
        self.assertFalse(self.menu.is_alive())

    def test_exit(self):
        self.assertTrue(self.menu.is_alive())
        self.menu.exit()
        self.menu.join(timeout=10)
        self.assertFalse(self.menu.is_alive())
Example #5
0
def show_loaded_model_menu(model, net, jtree):
    # Record which models are in the models folder
    models_list = [
        f for f in listdir(models_path)
        if isfile(join(models_path, f)) and str(f)[-4:] == ".dat"
    ]

    menu = ConsoleMenu("Main menu - " + model)

    describe_bnet = FunctionItem("Describe Bayesian Net",
                                 function=lambda net: print(str(net)),
                                 args=[net])

    describe_jtree = FunctionItem("Describe Junction Tree",
                                  function=lambda jtree: print(str(jtree)),
                                  args=[jtree])

    visualize = FunctionItem(
        "Visualize Bayesian Net and Junction Tree",
        function=lambda path: os.system("visualize.py " + path),
        args=[models_path + model])

    propagate = FunctionItem(
        "Propagate evidence",
        function=lambda jtree:
        [jtree.sum_propagate(),
         print("Evidence propagated")],
        args=[jtree])

    load_new_model = SelectionMenu(models_list, "Load New Model")
    load_new_model_item = SubmenuItem("Load a new model",
                                      load_new_model,
                                      menu=menu,
                                      should_exit=True)

    add_evidence = FunctionItem("Add evidence",
                                function=add_evidence_option,
                                args=[jtree])
    get_probability = FunctionItem("Get probability of a variable",
                                   function=get_probability_option,
                                   args=[jtree])
    reset_model = FunctionItem(
        "Reset Model",
        function=lambda net, jtree:
        [jtree.initialize_tables(net),
         print("Tables reinitialized")],
        args=[net, jtree])

    menu.append_item(describe_bnet)
    menu.append_item(describe_jtree)
    menu.append_item(visualize)
    menu.append_item(add_evidence)
    menu.append_item(get_probability)
    menu.append_item(propagate)
    menu.append_item(reset_model)
    menu.append_item(load_new_model_item)

    menu.start()
    menu.join()

    if load_new_model.selected_option >= len(models_list):
        show_loaded_model_menu(model, net, jtree)
        return
    elif load_new_model.selected_option == -1:
        return

    selected_model = models_list[load_new_model.selected_option]
    net, jtree = util.load_model(models_path + selected_model)
    if net is not None and jtree is not None:
        jtree.initialize_tables(net)
        print("Model loaded succesfully")
        show_loaded_model_menu(selected_model, net, jtree)
    else:
        show_first_menu()