Exemplo n.º 1
0
 def connect(source, target):
     new_edge = Edge.find_by_source_and_target(source.id, target.id)
     if not new_edge:
         Edge(source.id, target.id)
     else:
         return
     source.add_out_neighbour(target)
     target.add_in_neighbour(source)
Exemplo n.º 2
0
 def weight_is_1_split(self, node):
     #n : length of node
     new_node_list = []
     n = len(node.label)
     alive_in_neighbours = [x for x in node.in_neighbours if x.end < 0]
     for in_neighbour in alive_in_neighbours:
         #m : length of inneighbour
         m = len(in_neighbour.label)
         #create new name
         new_name = "%s%s" % (in_neighbour.label[m-n:m-n+1], node.label)
         #create new node
         #Is new node repeat
         new_node = Node.find_node_by_label(new_name)
         if new_node is not None:
             print("Node is existed:")
             print(new_node)
         else:
             new_node = Node(new_name, start=self.round)
             print("New node is :")
             print(new_node)
         #connect innei and new node
         new_edge = Edge.find_by_source_and_target(in_neighbour.id, new_node)
         if new_edge is None:
             new_edge = Edge(in_neighbour.id, new_node.id, start=self.round)
             #print("new edge is :")
             #print(new_edge)
         if in_neighbour not in new_node.in_neighbours:
             new_node.add_in_neighbour(in_neighbour)
         if new_node not in in_neighbour.in_neighbours:
             in_neighbour.add_out_neighbour(new_node)
         #connect outnei and new node
         alive_out_neighbours = [x for x in node.out_neighbours if x.end < 0]
         for out_neighbour in alive_out_neighbours:
             new_edge2 = Edge.find_by_source_and_target(new_node.id, out_neighbour.id)
             if new_edge2 is None:
                 Edge(new_node.id, out_neighbour.id, start=self.round)
             if out_neighbour not in new_node.out_neighbours:
                 new_node.add_out_neighbour(out_neighbour)
             if new_node not in out_neighbour.in_neighbours:
                 out_neighbour.add_in_neighbour(new_node)
         new_node_list.append(new_node)
     #set the value of original node's end to now round
     Node.end_node(node, self.round)
     return new_node_list
Exemplo n.º 3
0
 def weight_not_1_split(self, node):
     new_node_list = []
     node_alive_out_neighbours = [x for x in node.out_neighbours if x.end < 0]
     node_alive_in_neighbours = [x for x in node.in_neighbours if x.end < 0]
     for logical_node in node.logical_nodes:
         new_node = Node(label=logical_node.label,
                         weight=logical_node.weight,
                         start=self.round)
         new_node.out_neighbours.extend(node_alive_out_neighbours)
         new_node.logical_nodes.extend(logical_node.logical_nodes)
         sub_logical_nodes = []
         def preorder(n):
             sub_logical_nodes.append(n.label[:-1])
             if n.weight > 1:
                 preorder(n.logical_nodes[0])
                 preorder(n.logical_nodes[1])
         preorder(new_node)
         if sub_logical_nodes:
             label_length = len(sub_logical_nodes[0])
         for in_neighbour in node_alive_in_neighbours:
             if in_neighbour.label[-label_length:] in sub_logical_nodes:
                 new_edge = Edge.find_by_source_and_target(in_neighbour.id, new_node.id)
                 if not new_edge:
                     Edge(in_neighbour.id, new_node.id, start=self.round)
                 if in_neighbour not in new_node.in_neighbour:
                     new_node.add_in_neighbour(in_neighbour)
                 if new_node not in in_neighbour.out_neighbours:
                     in_neighbour.add_out_neighbour(new_node)
         for out_neighbour in new_node.out_neighbours:
             new_edge = Edge.find_by_source_and_target(new_node.id, out_neighbour.id)
             if not new_edge:
                 Edge(new_node.id, out_neighbour.id, start=self.round)
             if out_neighbour not in new_node.out_neighbours:
                 new_node.add_out_neighbour(out_neighbour)
             if new_node not in out_neighbour.in_neighbours:
                 out_neighbour.add_in_neighbour(new_node)
         new_node_list.append(new_node)
     Node.end_node(node, self.round)
     return new_node_list