-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_tracer.py
104 lines (87 loc) · 3.95 KB
/
net_tracer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from Precompiler import *
DOWNSTREAM_CON = 1
UPSTREAM_CON = 2
def net_tracer(network,forcedOrigin=None):
'''
Returns a list of sites which are along the real->sink chain
'''
real_sites_counter = 0
realSiteQueue = []
rsc = [] # real to sink chain
for site in network.siteTable:
if site.isReal:
if forcedOrigin and site == forcedOrigin:
real_sites_counter += 1
realSiteQueue.append(site)
rsc.append([])
else:
real_sites_counter += 1
realSiteQueue.append(site)
rsc.append([])
rsc_c = 0
while len(realSiteQueue) > 0:
c = realSiteQueue.pop(0)
queue = [c]
while (queue):
curr = queue.pop()
conTup = curr.connectedSites()
current_flow = None
for con in conTup:
if con[1] == DOWNSTREAM_CON:
current_flow = con[2]
foundDown = True # We found a new downstream
if current_flow is None:
# We must be dealing with upstreams only
continue
dsDest = current_flow.downstreamSite
# Get the other flow which is upstream of the shared confluence
conTup = dsDest.connectedSites()
other_flow = None
numUpStr = 0
for con in conTup:
if con[1] == UPSTREAM_CON and con[0] != curr:
other_flow = con[2]
numUpStr += 1
# Special cases -----
if not (current_flow.downstreamSite.assignedID is -1 or current_flow.downstreamSite.assignedID is None ):
# Already assigned! Do not do anything more
continue # So we dont add to the queue again
if dsDest == network.calculateSink()[0] and not dsDest.isConfluence():
# The next site is a sink downstream; apply special rule
dsDest.assignedID = curr.assignedID + current_flow.length
break
if not other_flow is None:
if current_flow < other_flow:
# current <= other
if (curr.assignedID is -1 or curr.assignedID is None ):
r = curr.id
else:
r = curr.assignedID
l = r - other_flow.thisAndUpstream
current_flow.downstreamSite.assignedID = r + current_flow.length # Conf. ID
if curr == c:
current_flow.downstreamSite.downwardRefID = r
#assign other upstream site
#other_flow.upstreamSite.id = current_flow.upstreamSite.id - other_flow.thisAndUpstream
else:
# current > other
if (curr.assignedID is -1 or curr.assignedID is None ):
l = curr.id
else:
l = curr.assignedID
r = l + current_flow.length
current_flow.downstreamSite.assignedID = r + other_flow.thisAndUpstream
#current_flow.downstreamSite.downwardRefID = r
#Assign other upstream site
#other_flow.upstreamSite.id = other_flow.downstreamSite.id - other_flow.thisAndUpstream
else:
# We have no other flow, we just need to progress downstream
if (curr.assignedID is -1 or curr.assignedID is None ):
l = curr.id
else:
l = curr.assignedID
current_flow.downstreamSite.assignedID = l + current_flow.length
queue.append(current_flow.downstreamSite)
rsc[rsc_c].append(current_flow.downstreamSite)
rsc_c += 1
return rsc