source: src/mlx/gates.py@ 659:d0311805dcd4

Last change on this file since 659:d0311805dcd4 was 619:7763179ff6b0, checked in by István Váradi <ivaradi@…>, 10 years ago

Reworked the representation and handling of gate data (re #113)

File size: 7.5 KB
Line 
1#--------------------------------------------------------------------------------------
2
3## @package mlx.gates
4#
5# The module to handle the LHBP gate information.
6#
7
8#--------------------------------------------------------------------------------------
9
10class Gate(object):
11 """Information about a gate."""
12 def __init__(self, number, terminal, type,
13 availableFn = None):
14 """Construct the gate with the given information.
15
16 number is the gate's number as a string (as it can contain letters).
17 terminal is the terminal the gate belongs to (not used currently).
18 type is the gate's type: G for gate, S for stand.
19 availableFn is a function that can determine if the gate is available based on
20 the statuses of other gates. Its arguments are:
21 - a collection of the gates, and
22 - a set of occupied gate numbers."""
23 self._number = number
24 self._terminal = terminal
25 self._type = type
26 self._availableFn = availableFn
27
28 @property
29 def number(self):
30 """Get the number of the gate."""
31 return self._number
32
33 def isAvailable(self, gates, occupiedGateNumbers):
34 """Determine if this gate is available given the set of gates and
35 occupied gate numbers."""
36 if self._number in occupiedGateNumbers:
37 return False
38 return True if self._availableFn is None else \
39 self._availableFn(gates, occupiedGateNumbers)
40
41#--------------------------------------------------------------------------------------
42
43class Gates(object):
44 """A collection of gates."""
45 # Display info type: a gate (number)
46 DISPLAY_GATE=1
47
48 # Display info type: a space
49 DISPLAY_SPACE=2
50
51 # Display info type: a new column
52 DISPLAY_NEW_COLUMN=3
53
54 def __init__(self):
55 """Construct the gate collection."""
56 self._gates = []
57 self._displayInfos = []
58 self._numColumns = 1
59 self._numRows = 0
60 self._numRowsInColumn = 0
61
62 @property
63 def gates(self):
64 """Get an iterator over the gates."""
65 return iter(self._gates)
66
67 @property
68 def displayInfos(self):
69 """Get an iterator over the display info tuples.
70
71 Each tuple consists of
72 - a type (one of the DISPLAY_XXX constants), and
73 - an additional data (the gate for DISPLAY_GATE, None for others)."""
74 return iter(self._displayInfos)
75
76 @property
77 def numRows(self):
78 """Get the number of rows."""
79 return self._numRows
80
81 @property
82 def numColumns(self):
83 """Get the number of columns."""
84 return self._numColumns
85
86 def add(self, gate):
87 """Add a gate to the collection."""
88 self._gates.append(gate)
89 self._displayInfos.append((Gates.DISPLAY_GATE, gate))
90 self._addRow()
91
92 def addSpace(self):
93 """Add a space between subsequent gates."""
94 self._displayInfos.append((Gates.DISPLAY_SPACE, None))
95 self._addRow()
96
97 def addNewColumn(self):
98 """Start a new column of gates."""
99 self._displayInfos.append((Gates.DISPLAY_NEW_COLUMN, None))
100 self._numRowsInColumn = 0
101 self._numColumns += 1
102
103 def _addRow(self):
104 """Add a new row."""
105 self._numRowsInColumn += 1
106 if self._numRowsInColumn > self._numRows:
107 self._numRows = self._numRowsInColumn
108
109#--------------------------------------------------------------------------------------
110
111def availableIf(occupiedGateNumbers, othersAvailable = []):
112 """Determine if a gate is available.
113
114 othersAvailable is a list of numbers of gates, that must be available so
115 that the one we are considering is available."""
116 for otherNumber in othersAvailable:
117 if otherNumber in occupiedGateNumbers:
118 return False
119 return True
120
121#--------------------------------------------------------------------------------------
122
123def getAvilableIf(othersAvailable = []):
124 """Get a function that determines if a gate is available based on the
125 statuses of other gates."""
126 return lambda gates, occupiedGateNumbers: availableIf(occupiedGateNumbers,
127 othersAvailable =
128 othersAvailable)
129
130#--------------------------------------------------------------------------------------
131
132# The gates at LHBP
133lhbpGates = Gates()
134
135lhbpGates.add(Gate("1", "1", "S"))
136lhbpGates.add(Gate("2", "1", "S"))
137lhbpGates.add(Gate("3", "1", "S"))
138lhbpGates.add(Gate("4", "1", "S"))
139lhbpGates.add(Gate("5", "1", "S"))
140lhbpGates.add(Gate("6", "1", "S"))
141lhbpGates.add(Gate("25", "1", "S"))
142lhbpGates.add(Gate("26", "1", "S"))
143lhbpGates.add(Gate("27", "1", "S"))
144lhbpGates.addSpace()
145
146lhbpGates.add(Gate("31", "2B", "G"))
147lhbpGates.add(Gate("32", "2B", "G"))
148lhbpGates.add(Gate("33", "2B", "G"))
149lhbpGates.addNewColumn()
150
151lhbpGates.add(Gate("34", "2B", "G"))
152lhbpGates.add(Gate("35", "2B", "G"))
153lhbpGates.add(Gate("36", "2B", "G"))
154lhbpGates.add(Gate("37", "2B", "G"))
155lhbpGates.add(Gate("38", "2B", "G"))
156lhbpGates.add(Gate("39", "2B", "G"))
157lhbpGates.addSpace()
158
159lhbpGates.add(Gate("42", "2A", "G"))
160lhbpGates.add(Gate("43", "2A", "G"))
161lhbpGates.add(Gate("44", "2A", "G"))
162lhbpGates.add(Gate("45", "2A", "G"))
163lhbpGates.addNewColumn()
164
165lhbpGates.add(Gate("107", "1", "S"))
166lhbpGates.add(Gate("108", "1", "S"))
167lhbpGates.add(Gate("109", "1", "S"))
168lhbpGates.add(Gate("R110", "1", "S",
169 availableFn = getAvilableIf(othersAvailable = ["R111"])))
170lhbpGates.add(Gate("R111", "1", "S",
171 availableFn = getAvilableIf(othersAvailable = ["R110", "R112"])))
172lhbpGates.add(Gate("R112", "1", "S",
173 availableFn = getAvilableIf(othersAvailable = ["R111"])))
174lhbpGates.add(Gate("R113", "1", "S",
175 availableFn = getAvilableIf(othersAvailable = ["R114"])))
176lhbpGates.add(Gate("R114", "1", "S",
177 availableFn = getAvilableIf(othersAvailable = ["R113"])))
178lhbpGates.add(Gate("R115", "1", "S"))
179lhbpGates.add(Gate("R116", "1", "S"))
180lhbpGates.add(Gate("R117", "1", "S"))
181lhbpGates.addNewColumn()
182
183lhbpGates.add(Gate("R210", "2A", "S",
184 availableFn = getAvilableIf(othersAvailable = ["R212A"])))
185lhbpGates.add(Gate("R211", "2A", "S",
186 availableFn = getAvilableIf(othersAvailable = ["R212A"])))
187lhbpGates.add(Gate("R212", "2A", "S",
188 availableFn = getAvilableIf(othersAvailable = ["R212A"])))
189lhbpGates.add(Gate("R212A", "2A", "S",
190 availableFn = getAvilableIf(othersAvailable = ["R210", "R211", "R212"])))
191lhbpGates.addSpace()
192
193lhbpGates.add(Gate("R220", "2B", "S"))
194lhbpGates.add(Gate("R221", "2B", "S"))
195lhbpGates.add(Gate("R222", "2B", "S"))
196lhbpGates.add(Gate("R223", "2B", "S"))
197lhbpGates.addSpace()
198
199lhbpGates.add(Gate("R224", "2A", "R"))
200lhbpGates.add(Gate("R225", "2A", "S"))
201lhbpGates.add(Gate("R226", "2A", "S"))
202lhbpGates.add(Gate("R227", "2A", "S"))
203lhbpGates.addNewColumn()
204
205lhbpGates.add(Gate("R270", "2A", "S"))
206lhbpGates.add(Gate("R271", "2A", "S"))
207lhbpGates.add(Gate("R272", "2A", "S"))
208lhbpGates.add(Gate("R274", "2A", "S"))
209lhbpGates.add(Gate("R275", "2A", "S"))
210lhbpGates.add(Gate("R276", "2A", "S"))
211lhbpGates.add(Gate("R277", "2A", "S"))
212lhbpGates.add(Gate("R278", "2A", "S",
213 availableFn = getAvilableIf(othersAvailable = ["R278A"])))
214lhbpGates.add(Gate("R278A", "2A", "S",
215 availableFn = getAvilableIf(othersAvailable = ["R278", "R279"])))
216lhbpGates.add(Gate("R279", "2A", "S",
217 availableFn = getAvilableIf(othersAvailable = ["R278A"])))
Note: See TracBrowser for help on using the repository browser.