|
4 | 4 | from collections import defaultdict
|
5 | 5 | from .errors import DataJointError
|
6 | 6 |
|
| 7 | +def extract_master(part_table): |
| 8 | + """ |
| 9 | + given a part table name, return master part. None if not a part table |
| 10 | + """ |
| 11 | + match = re.match(r"(?P<master>`\w+`.`#?\w+)__\w+`", part_table) |
| 12 | + return match['master'] + '`' if match else None |
| 13 | + |
| 14 | + |
7 | 15 |
|
8 | 16 | def topo_sort(graph):
|
9 | 17 | """
|
10 | 18 | topological sort of a dependency graph that keeps part tables together with their masters
|
11 | 19 | :return: list of table names in topological order
|
12 | 20 | """
|
| 21 | + |
13 | 22 | graph = nx.DiGraph(graph) # make a copy
|
14 | 23 |
|
15 | 24 | # collapse alias nodes
|
16 | 25 | alias_nodes = [node for node in graph if node.isdigit()]
|
17 | 26 | for node in alias_nodes:
|
18 |
| - direct_edge = ( |
19 |
| - next(x for x in graph.in_edges(node))[0], |
20 |
| - next(x for x in graph.out_edges(node))[1], |
21 |
| - ) |
22 |
| - graph.add_edge(*direct_edge) |
| 27 | + try: |
| 28 | + direct_edge = ( |
| 29 | + next(x for x in graph.in_edges(node))[0], |
| 30 | + next(x for x in graph.out_edges(node))[1], |
| 31 | + ) |
| 32 | + except StopIteration: |
| 33 | + pass # a disconnected alias node |
| 34 | + else: |
| 35 | + graph.add_edge(*direct_edge) |
23 | 36 | graph.remove_nodes_from(alias_nodes)
|
24 | 37 |
|
25 | 38 | # Add parts' dependencies to their masters' dependencies
|
26 | 39 | # to ensure correct topological ordering of the masters.
|
27 |
| - part_pattern = re.compile(r"(?P<master>`\w+`.`#?\w+)__\w+`") |
28 | 40 | for part in graph:
|
29 |
| - # print part tables and their master |
30 |
| - match = part_pattern.match(part) |
31 |
| - if match: |
32 |
| - master = match["master"] + "`" |
| 41 | + # find the part's master |
| 42 | + master = extract_master(part) |
| 43 | + if master: |
33 | 44 | for edge in graph.in_edges(part):
|
34 |
| - if edge[0] != master: |
35 |
| - graph.add_edge(edge[0], master) |
| 45 | + parent = edge[0] |
| 46 | + if parent != master and extract_master(parent) != master: |
| 47 | + graph.add_edge(parent, master) |
36 | 48 |
|
37 |
| - sorted_nodes = list(nx.algorithms.topological_sort(graph)) |
| 49 | + sorted_nodes = list(nx.topological_sort(graph)) |
38 | 50 |
|
39 | 51 | # bring parts up to their masters
|
40 |
| - pos = len(sorted_nodes) |
41 |
| - while pos > 0: |
42 |
| - pos -= 1 |
| 52 | + pos = len(sorted_nodes) - 1 |
| 53 | + placed = set() |
| 54 | + while pos > 1: |
43 | 55 | part = sorted_nodes[pos]
|
44 |
| - match = part_pattern.match(part) |
45 |
| - if match: |
46 |
| - master = match["master"] + "`" |
47 |
| - print(part, master) |
| 56 | + master = extract_master(part) |
| 57 | + if not master or part in placed: |
| 58 | + pos -= 1 |
| 59 | + else: |
| 60 | + placed.add(part) |
48 | 61 | try:
|
49 | 62 | j = sorted_nodes.index(master)
|
50 | 63 | except ValueError:
|
51 | 64 | # master not found
|
52 |
| - continue |
53 |
| - if pos > j + 1: |
54 |
| - print(pos, j) |
55 |
| - # move the part to its master |
56 |
| - del sorted_nodes[pos] |
57 |
| - sorted_nodes.insert(j + 1, part) |
58 |
| - pos += 1 |
| 65 | + pass |
| 66 | + else: |
| 67 | + if pos > j + 1: |
| 68 | + # move the part to its master |
| 69 | + del sorted_nodes[pos] |
| 70 | + sorted_nodes.insert(j + 1, part) |
59 | 71 |
|
60 | 72 | return sorted_nodes
|
61 | 73 |
|
@@ -202,16 +214,14 @@ def descendants(self, full_table_name):
|
202 | 214 | :return: all dependent tables sorted in topological order. Self is included.
|
203 | 215 | """
|
204 | 216 | self.load(force=False)
|
205 |
| - nodes = self.subgraph( |
206 |
| - nx.algorithms.dag.descendants(self, full_table_name) |
207 |
| - ).copy() |
208 |
| - return [full_table_name] + nodes.topo_sort() |
| 217 | + nodes = self.subgraph(nx.descendants(self, full_table_name)) |
| 218 | + return [full_table_name] + nodes.topo_sort() |
209 | 219 |
|
210 | 220 | def ancestors(self, full_table_name):
|
211 | 221 | """
|
212 | 222 | :param full_table_name: In form `schema`.`table_name`
|
213 | 223 | :return: all dependent tables sorted in topological order. Self is included.
|
214 | 224 | """
|
215 | 225 | self.load(force=False)
|
216 |
| - nodes = self.subgraph(nx.algorithms.dag.ancestors(self, full_table_name)).copy() |
| 226 | + nodes = self.subgraph(nx.ancestors(self, full_table_name)) |
217 | 227 | return reversed(nodes.topo_sort() + [full_table_name])
|
0 commit comments