source: src/mlx/gates.py@ 1186:beb5a473fd54

python3
Last change on this file since 1186:beb5a473fd54 was 1172:6cb71d6b4572, checked in by István Váradi <ivaradi@…>, 6 months ago

The gate data can be read from a JSON file

File size: 12.7 KB
RevLine 
[619]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."""
[1172]12 @staticmethod
13 def fromJSON(data):
14 """Create the gate from the given JSON data."""
15 return Gate(data["number"], data["terminal"],
16 data["type"],
17 maxSpan = data["maxSpan"],
18 maxLength = data["maxLength"])
19
[619]20 def __init__(self, number, terminal, type,
[1154]21 availableFn = None, taxiThrough = False,
22 maxSpan = 0.0, maxLength = 0.0):
[619]23 """Construct the gate with the given information.
24
25 number is the gate's number as a string (as it can contain letters).
26 terminal is the terminal the gate belongs to (not used currently).
27 type is the gate's type: G for gate, S for stand.
28 availableFn is a function that can determine if the gate is available based on
29 the statuses of other gates. Its arguments are:
30 - a collection of the gates, and
31 - a set of occupied gate numbers."""
[1154]32 self.number = number
33 self.terminal = terminal
34 self.type = type
[619]35 self._availableFn = availableFn
[1154]36 self.taxiThrough = taxiThrough
37 self.maxSpan = maxSpan
38 self.maxLength = maxLength
[619]39
[1154]40 def isAvailable(self, plane, gates, occupiedGateNumbers):
41 """Determine if this gate is available for the given plane and the
42 given the set of gates and occupied gate numbers."""
43 if self.number in occupiedGateNumbers:
[619]44 return False
[1154]45 if self._availableFn is None or \
46 self._availableFn(gates, occupiedGateNumbers):
47 return plane is None or \
[1155]48 ((self.maxSpan<0.1 or plane.wingSpan <= self.maxSpan) and
[1156]49 (self.maxLength<0.1 or plane.fuselageLength <= self.maxLength) and
50 (not plane.hasStairs or self.type!="G"))
[1154]51 else:
52 return False
[619]53
[1172]54 def toJSON(self):
55 """Create a JSON representation of the gate."""
56 data = {}
57 for attributeName in ["number", "terminal", "type",
58 "maxSpan", "maxLength"]:
59 data[attributeName] = getattr(self, attributeName)
60 return data
61
[619]62#--------------------------------------------------------------------------------------
63
64class Gates(object):
65 """A collection of gates."""
66 # Display info type: a gate (number)
67 DISPLAY_GATE=1
68
69 # Display info type: a space
70 DISPLAY_SPACE=2
71
72 # Display info type: a new column
73 DISPLAY_NEW_COLUMN=3
74
[1172]75 @staticmethod
76 def fromJSON(data):
77 """Create a gates object from the given JSON data."""
78 gates = Gates()
79 for gateData in data:
80 gates.add(Gate.fromJSON(gateData))
81 return gates
82
[619]83 def __init__(self):
84 """Construct the gate collection."""
85 self._gates = []
86 self._displayInfos = []
87 self._numColumns = 1
88 self._numRows = 0
89 self._numRowsInColumn = 0
90
91 @property
92 def gates(self):
93 """Get an iterator over the gates."""
94 return iter(self._gates)
95
96 @property
97 def displayInfos(self):
98 """Get an iterator over the display info tuples.
99
100 Each tuple consists of
101 - a type (one of the DISPLAY_XXX constants), and
102 - an additional data (the gate for DISPLAY_GATE, None for others)."""
103 return iter(self._displayInfos)
104
105 @property
106 def numRows(self):
107 """Get the number of rows."""
108 return self._numRows
109
110 @property
111 def numColumns(self):
112 """Get the number of columns."""
113 return self._numColumns
114
115 def add(self, gate):
116 """Add a gate to the collection."""
117 self._gates.append(gate)
118 self._displayInfos.append((Gates.DISPLAY_GATE, gate))
119 self._addRow()
120
[1105]121 def find(self, gateNumber):
122 """Find a gate by its number."""
123 for gate in self._gates:
124 if gate.number == gateNumber:
125 return gate
126
[619]127 def addSpace(self):
128 """Add a space between subsequent gates."""
129 self._displayInfos.append((Gates.DISPLAY_SPACE, None))
130 self._addRow()
131
132 def addNewColumn(self):
133 """Start a new column of gates."""
134 self._displayInfos.append((Gates.DISPLAY_NEW_COLUMN, None))
135 self._numRowsInColumn = 0
136 self._numColumns += 1
137
[1154]138 def merge(self, otherGates):
139 """Merge the information from the given gate list (retrieved from the
140 MAVA server) into this gate list."""
141 for otherGate in otherGates.gates:
142 gate = self.find(otherGate.number)
143 if gate is None:
144 print("Received data for gate %s, but it does not exist locally!" %
145 (otherGate.number,))
146 else:
147 if gate.terminal != otherGate.terminal:
148 print("The terminal for gate %s is received as: %s" %
149 (gate.number, otherGate.terminal))
150 gate.terminal = otherGate.terminal
151 if gate.type != otherGate.type:
152 print("The type for gate %s is received as: %s" %
153 (gate.number, otherGate.type))
154 gate.type = otherGate.type
155
156 gate.maxSpan = otherGate.maxSpan
157 gate.maxLength = otherGate.maxLength
158
159 for gate in self.gates:
160 if gate.maxSpan==0.0 or gate.maxLength==0.0:
161 print("Gate %s has no maximal dimensions from the database" %
162 (gate.number,))
163
[1172]164 def toJSON(self):
165 """Convert the list of gates into a JSON data."""
166 return [gate.toJSON() for gate in self._gates]
167
[619]168 def _addRow(self):
169 """Add a new row."""
170 self._numRowsInColumn += 1
171 if self._numRowsInColumn > self._numRows:
172 self._numRows = self._numRowsInColumn
173
174#--------------------------------------------------------------------------------------
175
176def availableIf(occupiedGateNumbers, othersAvailable = []):
177 """Determine if a gate is available.
178
179 othersAvailable is a list of numbers of gates, that must be available so
180 that the one we are considering is available."""
181 for otherNumber in othersAvailable:
182 if otherNumber in occupiedGateNumbers:
183 return False
184 return True
185
186#--------------------------------------------------------------------------------------
187
[1151]188def getAvailableIf(othersAvailable = []):
[619]189 """Get a function that determines if a gate is available based on the
190 statuses of other gates."""
191 return lambda gates, occupiedGateNumbers: availableIf(occupiedGateNumbers,
192 othersAvailable =
193 othersAvailable)
194
195#--------------------------------------------------------------------------------------
196
197# The gates at LHBP
198lhbpGates = Gates()
199
[1158]200lhbpGates.add(Gate("R101", "1", "S", taxiThrough = True))
[1151]201lhbpGates.add(Gate("R102", "1", "S"))
202lhbpGates.add(Gate("R103", "1", "S"))
203lhbpGates.add(Gate("R104", "1", "S",
204 availableFn = getAvailableIf(othersAvailable = ["R105"])))
205lhbpGates.add(Gate("R105", "1", "S",
206 availableFn = getAvailableIf(othersAvailable = ["R104", "R106"])))
207lhbpGates.add(Gate("R106", "1", "S",
208 availableFn = getAvailableIf(othersAvailable = ["R105", "R108"])))
209lhbpGates.add(Gate("R107", "1", "S",
210 availableFn = getAvailableIf(othersAvailable = ["R108"])))
211lhbpGates.add(Gate("R108", "1", "S",
212 availableFn = getAvailableIf(othersAvailable = ["R106", "R107"])))
213
[619]214lhbpGates.addSpace()
[1151]215lhbpGates.add(Gate("R110", "1", "S",
216 availableFn = getAvailableIf(othersAvailable = ["R111"])))
217lhbpGates.add(Gate("R111", "1", "S",
218 availableFn = getAvailableIf(othersAvailable = ["R110", "R112"])))
219lhbpGates.add(Gate("R112", "1", "S",
220 availableFn = getAvailableIf(othersAvailable = ["R111"])))
221lhbpGates.add(Gate("R113", "1", "S",
222 availableFn = getAvailableIf(othersAvailable = ["R112", "R114"])))
223lhbpGates.add(Gate("R114", "1", "S",
224 availableFn = getAvailableIf(othersAvailable = ["R113"])))
225lhbpGates.add(Gate("R115", "1", "S"))
226lhbpGates.add(Gate("R116", "1", "S",
[1158]227 availableFn = getAvailableIf(othersAvailable = ["R117"]),
228 taxiThrough = True))
[1151]229lhbpGates.add(Gate("R117", "1", "S",
230 availableFn = getAvailableIf(othersAvailable = ["R116", "R117A"])))
231lhbpGates.add(Gate("R117A", "1", "S",
232 availableFn = getAvailableIf(othersAvailable = ["R116", "R117"])))
233lhbpGates.addNewColumn()
234
235lhbpGates.add(Gate("G150", "1", "S"))
236lhbpGates.add(Gate("G151", "1", "S"))
237lhbpGates.add(Gate("G152", "1", "S"))
238lhbpGates.add(Gate("G153", "1", "S"))
239lhbpGates.add(Gate("G154", "1", "S"))
240lhbpGates.add(Gate("G155", "1", "S"))
241
242lhbpGates.addSpace()
[1158]243lhbpGates.add(Gate("G170", "1", "S", taxiThrough = True))
244lhbpGates.add(Gate("G171", "1", "S", taxiThrough = True))
245lhbpGates.add(Gate("G172", "1", "S", taxiThrough = True))
[1151]246lhbpGates.addNewColumn()
[619]247
248lhbpGates.add(Gate("31", "2B", "G"))
249lhbpGates.add(Gate("32", "2B", "G"))
250lhbpGates.add(Gate("33", "2B", "G"))
[1151]251lhbpGates.add(Gate("34", "2B", "G",
252 availableFn = getAvailableIf(othersAvailable = ["34L", "34R"])))
253lhbpGates.add(Gate("34L", "2B", "G",
254 availableFn = getAvailableIf(othersAvailable = ["34", "34R"])))
255lhbpGates.add(Gate("34R", "2B", "G",
256 availableFn = getAvailableIf(othersAvailable = ["34L", "34"])))
257lhbpGates.add(Gate("35", "2B", "G",
258 availableFn = getAvailableIf(othersAvailable = ["35L", "35R"])))
259lhbpGates.add(Gate("35L", "2B", "G",
260 availableFn = getAvailableIf(othersAvailable = ["35", "35R"])))
261lhbpGates.add(Gate("35R", "2B", "G",
262 availableFn = getAvailableIf(othersAvailable = ["35L", "35"])))
263lhbpGates.add(Gate("36", "2B", "G",
264 availableFn = getAvailableIf(othersAvailable = ["36L", "36R"])))
265lhbpGates.add(Gate("36L", "2B", "G",
266 availableFn = getAvailableIf(othersAvailable = ["36", "36R"])))
267lhbpGates.add(Gate("36R", "2B", "G",
268 availableFn = getAvailableIf(othersAvailable = ["36L", "36"])))
269lhbpGates.addSpace()
[619]270
271lhbpGates.add(Gate("37", "2B", "G"))
272lhbpGates.add(Gate("38", "2B", "G"))
[1151]273lhbpGates.add(Gate("39", "2B", "G",
274 availableFn = getAvailableIf(othersAvailable = ["37L", "37R"])))
275lhbpGates.add(Gate("39L", "2B", "G",
276 availableFn = getAvailableIf(othersAvailable = ["37", "37R"])))
277lhbpGates.add(Gate("39R", "2B", "G",
278 availableFn = getAvailableIf(othersAvailable = ["37L", "37"])))
279lhbpGates.addNewColumn()
[619]280
281lhbpGates.add(Gate("42", "2A", "G"))
282lhbpGates.add(Gate("43", "2A", "G"))
283lhbpGates.add(Gate("44", "2A", "G"))
284lhbpGates.add(Gate("45", "2A", "G"))
[1151]285lhbpGates.addSpace()
[619]286
287lhbpGates.add(Gate("R210", "2A", "S",
[1151]288 availableFn = getAvailableIf(othersAvailable = ["R212A"]),
[1105]289 taxiThrough = True))
[619]290lhbpGates.add(Gate("R211", "2A", "S",
[1151]291 availableFn = getAvailableIf(othersAvailable = ["R212A"]),
[1105]292 taxiThrough = True))
[619]293lhbpGates.add(Gate("R212", "2A", "S",
[1151]294 availableFn = getAvailableIf(othersAvailable = ["R212A"]),
[1105]295 taxiThrough = True))
[619]296lhbpGates.add(Gate("R212A", "2A", "S",
[1151]297 availableFn = getAvailableIf(othersAvailable = ["R210", "R211", "R212"]),
[1105]298 taxiThrough = True))
[619]299lhbpGates.addSpace()
300
301lhbpGates.add(Gate("R220", "2B", "S"))
302lhbpGates.add(Gate("R221", "2B", "S"))
303lhbpGates.add(Gate("R222", "2B", "S"))
304lhbpGates.add(Gate("R223", "2B", "S"))
305lhbpGates.addSpace()
306
[1151]307lhbpGates.add(Gate("R224", "2A", "S"))
[619]308lhbpGates.add(Gate("R225", "2A", "S"))
309lhbpGates.add(Gate("R226", "2A", "S"))
310lhbpGates.add(Gate("R227", "2A", "S"))
311lhbpGates.addNewColumn()
312
313lhbpGates.add(Gate("R270", "2A", "S"))
314lhbpGates.add(Gate("R271", "2A", "S"))
315lhbpGates.add(Gate("R272", "2A", "S"))
[1151]316lhbpGates.add(Gate("R273", "2A", "S"))
[619]317lhbpGates.add(Gate("R274", "2A", "S"))
318lhbpGates.add(Gate("R275", "2A", "S"))
319lhbpGates.add(Gate("R276", "2A", "S"))
320lhbpGates.add(Gate("R277", "2A", "S"))
321lhbpGates.add(Gate("R278", "2A", "S",
[1151]322 availableFn = getAvailableIf(othersAvailable = ["R278A"]),
[1105]323 taxiThrough = True))
[619]324lhbpGates.add(Gate("R278A", "2A", "S",
[1158]325 availableFn = getAvailableIf(othersAvailable = ["R278", "R279"])))
[619]326lhbpGates.add(Gate("R279", "2A", "S",
[1151]327 availableFn = getAvailableIf(othersAvailable = ["R278A"]),
[1105]328 taxiThrough = True))
Note: See TracBrowser for help on using the repository browser.