def check_pos_is_value(self, n, pos, error=1): """ checks to see if a given node has a free end to connect to :param n: the node to check :param pos: the end position to check :param error: whether an error should be returned if end is not free, default=1, will error :type n: GraphNodeStatic :type pos: int :type error: int :return: free end pos """ if pos == -1: avail_pos = n.available_children_pos() if len(avail_pos) == 0: raise exceptions.GraphException( "cannot add connection to node has no available ends") return avail_pos[0] else: if n.available_pos(pos) == 0 and error: raise exceptions.GraphException( "graph pos is not available: " + str(pos) + " on node: " + str(n.index) ) elif n.available_pos(pos) == 0: return None return pos
def decrease_level(self): """ Decreases the level of nodes to be added. default level is 0. This is useful when removing or adding a set of nodes. Think of level as a grouping mechanism """ if self.level == 0: raise exceptions.GraphException("cannot decrease level anymore is " "already 0") self.level -= 1
def remove_connection(self, c): """ removes a connection to another node :param c: a connection to another node :type c: GraphConnection :return: None """ if c not in self.connections: raise exceptions.GraphException( "cannot remove connection not in node") self.connections.remove(c)
def remove_connection(self, c): """ Remove a connection at a specific position in connection list :param c: the connection to remove :type c: GraphConnection :return: None """ if c not in self.connections: raise exceptions.GraphException( "cannot remove connection not in node") i = self.connections.index(c) self.connections[i] = None
def end_index(self, n_index): """ get the end index of a given node specified by its index :param n_index: index of the node you want to find its end_index in this connection :type n_index: int :return: end index of specified node in this connection :rtype: int """ if n_index == self.node_1.index: return self.end_index_1 elif n_index == self.node_2.index: return self.end_index_2 else: raise exceptions.GraphException( "cannot call end_index with node not in connection")
def partner(self, n_index): """ get other node in the connection. :param n_index: index of node you know but want to find the other node :type n_index: int :return: other node :rtype: GraphNode """ if n_index == self.node_1.index: return self.node_2 elif n_index == self.node_2.index: return self.node_1 else: raise exceptions.GraphException( "cannot call partner with node not in connection, index: " +\ n_index + " if this is not a number you messed up")
def get_availiable_pos(self, n, pos): """ checks to see if a given node has a free end position will return and error if it doesn't :param n: the graph node in question :param pos: the end position (connection point) :type n: GraphNodeStatic :type pos: int :return: returns a list of positions if successful """ if pos == -1: avail_pos = n.available_children_pos() return avail_pos else: if n.available_pos(pos) == 0: raise exceptions.GraphException( "graph pos is not available: " + str(pos) + " " + n.data.name) return [pos]