Esempio n. 1
0
def write_core_wgraph(simschema, filename="coreschema"):
    """view core graph on simulate schema graph"""
    fns = filename + '.dot'
    fls = open(fns, 'w')
    dot = DotGraph(fls)
    relations = simschema.get_wgraph_from_schema()
    wgraph = WGraph(relations)
    core = wgraph.get_core_graph()
    order = simschema.ordered
    nodes = set([])
    for node in range(len(core)):
        if core[node] != []:
            nodes.add(node)
#            for end_node in core[node]:
#                dot.add_edge(order[node], order[end_node[0]], end_node[1])
    for node in nodes:
        start_node = relations[node]
        for end_node in start_node:
            if end_node[0] in nodes:
                dot.add_edge(order[node], order[end_node[0]], end_node[1])
    dot.finish_output()
Esempio n. 2
0
 def __init__(self, connectivity, weighted=False):
     """Connectivity describes which tables have foreign keys
     into which other tables."""
     if weighted:
         graph = WGraph(connectivity)
     else:
         graph = Graph(connectivity)
     self.graph = graph
     glen = len(self.graph)
     self._mst = [[]] * glen
     self.undirected_graph = graph.get_undirected()
     self._spanning = []
     self.weighted = weighted
     # traversing graph
     #    get spanning path of BFS from this node
     #    self._spannning stores those path
     for node_index in range(0, glen):
         span = self.undirected_graph.breadth_first_search(node_index)
         self._spanning.append(span)
     if weighted:
         for index in range(0, glen):
             mst = prim(self.undirected_graph._graph, range(glen), index)
             self._mst[index] = UWGraph(mst, index)
             self.undirected_graph.gen_dist_matrix()
Esempio n. 3
0
    def gen_subgraph(self, splition):
        """
        split this schema into sub graphs
            1. nodes not in splition can be attached freely
            2. first generate sub schema, and then generate sub graph
            3. sub graphs will extend as fk links direction
            4. sub graphs should be kept in sequence
        split could happen on different connectives:
        """
        core_node = set([])
        in_splition = []
        for split in splition:
            if not set(split).issubset(set(self.nodelist.keys())):
                continue
            newsplit = []
            for node in split:
                if node not in self.nodelist.keys():
                    raise Exception("Wrong table inserted %s" % node)
                try:
                    core_node.add(self.ordered.index(self.nodelist[node]))
                except:
                    raise Exception("%d or %s is not a real table" % \
                        (len(self.ordered), node))
                newsplit.append(self.ordered.index(self.nodelist[node]))
            in_splition.append(newsplit)
#        full_node = set(range(len(self.ordered)))

#        external_node = full_node.difference(core_node)
        if len(in_splition) == 0:
            return [], []
        #subschema_list = []
        sub_nodelist = []
        #sub_link = []


        relations = self.get_wgraph_from_schema()
        graph = WGraph(relations)
        graph_list = []
#        self.print_graph(graph._graph)
#        print "===============external node=================="
#        print external_node
#        print "========end of external node=================="
        split = in_splition[0]
#        root = split[0]
#        print "===============split from user=================="
#        print split
        current_left = core_node.difference(set(split))
        subgraph = graph.get_expanding(split, current_left)
        #split = set(split).union(external_node)
        #print split
#        print "========end of split from user=================="
        #subgraph = graph.get_subgraph(split)
#        print subgraph
            # remove the edges out of the split++external
        #subgraph = subgraph.detect_connection(root)
            # the subgraph might be divided in several pieces
            # pick the one we needed
        subnodes = set([])
        for idx in range(len(subgraph._graph)):
            if subgraph._graph[idx] != ():
                subnodes.add(idx)
                for nod in subgraph._graph[idx]:
                    subnodes.add(nod[0])
        sub_nodelist.append(subnodes)
        graph_list.append(subgraph)
#            subgraph = subgraph.get_undirected().breadth_first(root)
        current_left = core_node.difference(set(split))
        for split in in_splition[1:]:
            # two operation might be seprated:
            #   1. get extension
            #   2. generate nodelist and links
#            root = split[0]
#            print "===============split from user=================="
#            print split
#            print "========end of split from user=================="
            current_left = current_left.difference(set(split))
            subgraph = graph.get_expanding(split, current_left)
            # remove the edges out of the split++external
            # the subgraph might be divided in several pieces
            # pick the one we needed
            subnodes = set([])
            for idx in range(len(subgraph._graph)):
                if subgraph._graph[idx] != ():
                    subnodes.add(idx)
                    for nod in subgraph._graph[idx]:
                        subnodes.add(nod[0])
            sub_nodelist.append(subnodes)
            graph_list.append(subgraph)
#            subgraph = subgraph.get_undirected().breadth_first(root)
#        multidot = MultiDot("subs")
#        for graph in graph_list:
#            nodes = graph._graph
#            dot = multidot.get_dot()
#            for index in range(len(nodes)):
#                start_node = nodes[index]
#                for end_node in start_node:
#                    dot.add_edge(order[index], order[end_node])
#            dot.finish_output()
        return graph_list, sub_nodelist