def start(self): source = globals.main_view_widget.ids.algorithm_txt_input.text isOk = True if globals.main_view_widget.ids.play_btn.visible == True: try: if source == "": globals.error_popup_widget = popup_widget.ErrorPopupWidget( ) globals.error_popup_widget.setText( "You must enter the starting node first!") globals.popup_window = Popup( title="Error", content=globals.error_popup_widget, size_hint=(None, None), size=(400, 200), auto_dismiss=False) globals.popup_window.open() globals.error_popup_widget.ids.error_popup_btn.bind( on_press=popup_widget.ErrorPopupWidget.closePopUp) isOk = False else: source = int(source) source = globals.graph_manager.getNodeWidgetById(source) if source == None: raise GraphException( "The starting node does not exist") except: raise GraphException("Invalid number for the starting node") if isOk == True and self.isFirstTime: self.visited = [False ] * (len(globals.graph_manager.node_widgets) ) # Mark all the vertices as not visited self.queue = [] # Create a queue for BFS self.queue.append( source) # Mark the source node as visited and enqueue it self.visited[globals.graph_manager.getIndexFromListOfNodes( source)] = True Clock.schedule_interval( partial(self.BFSUtil, self.queue, self.visited), 1) self.isFirstTime = False elif isOk == True: Clock.schedule_interval( partial(self.BFSUtil, self.queue, self.visited), 1)
def update_text_on_delete(self, text, nodeId): # this function updates the text when the user eliminates a node by double click if text != "": lines = text.split('\n') if lines != None: new_text = "" for line in lines: if line != "": value = self.interpretLine(line) if value == -1: raise GraphException("Invalid format for the adjacency list!") elif len(value) == 0: pass elif len(value) == 1: if value[0] != nodeId: new_text += line + "\n" elif len(value) == 2 or len(value) == 3: if value[0] != nodeId and value[1] != nodeId: new_text += line + "\n" elif value[0] == nodeId: node = self.getNodeWidgetById(value[1]) if len(node.neighbors) == 1: # if it has only one neighbor, with the id nodeId, it won't have anymore after deleting new_text += str(value[1]) + "\n" elif value[1] == nodeId: node = self.getNodeWidgetById(value[0]) if len(node.neighbors) == 1: # if it has only one neighbor, with the id nodeId, it won't have anymore after deleting new_text += str(value[0]) + "\n" globals.main_view_widget.ids.input_text.text = new_text
def start(self): self.sn = 1 try: self.sn = int( globals.main_view_widget.ids.algorithm_txt_input.text) except: raise GraphException('The starting node must be a valid node') self.dijkstra(self.sn)
def getNodeWidgetById(self, node_id): # returns None if it does not exists try: node_id = int(node_id) except: raise GraphException('Node id must be a positive integer.') node = None for node_widget in self.node_widgets: if node_widget.Id == node_id: node = node_widget break return node