Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/test_classman_tables.py
169689 views
1
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
2
# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.
3
# SUMOPy module
4
# Copyright (C) 2012-2021 University of Bologna - DICAM
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file test_classman_tables.py
16
# @author Joerg Schweizer
17
# @date 2012
18
19
#from classman import *
20
21
from test_classman_classes import *
22
from classman import *
23
from arrayman import *
24
is_all = 0
25
if 1 | is_all:
26
pass
27
if 1 | is_all:
28
print 'Lines, Poly example'
29
30
###########################################################################
31
# Instance creation
32
33
lines = Lines('lines')
34
# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],
35
# [10.0,20.0,20.0,20.0,20.0,10.0],
36
# [20.0,30.0,30.0,20.0,30.0,20.0],
37
# ]
38
39
# vertices = [ [(0.0,10.0),(10.0,10.0)],
40
# [(10.0,20.0),(20.0,20.0)],
41
# [(20.0,30.0),(30.0,20.0)],
42
# ]
43
vertices = [
44
[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 0
45
[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 1
46
[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 2
47
]
48
polygons = [
49
np.array([[0.0, 0.0, 0.0], [0.2, 0.0, 0.0], [0.2, 0.0, 0.1], [0.3, 0.3, 0.3]]), # 0
50
np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 1
51
np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 2
52
]
53
ids_sumo = ['aa10', 'bb22', 'cc333']
54
# lines.add_rows(3)
55
lines.add_rows(3, vertices=vertices, polygons=polygons, ids_sumo=ids_sumo)
56
lines.print_attrs()
57
print '\n indexmap', lines.ids_sumo.get_indexmap()
58
print 'direct access vertex=\n', lines.vertices.value
59
print 'id for index bb22=', lines.ids_sumo.get_id_from_index('bb22')
60
print 'ids for index bb22,cc333=', lines.ids_sumo.get_ids_from_indices(['bb22', 'cc333'])
61
# lines.del_row(2)
62
lines.print_attrs()
63
print 'id for index bb22=', lines.ids_sumo.get_id_from_index('cc333')
64
65
lines.ids_sumo[2] = 'xy'
66
67
print '\n indexmap', lines.ids_sumo.get_indexmap()
68
lines.print_attrs()
69
if 0 | is_all:
70
class Lines(TableObjman):
71
72
def __init__(self, ident, parent=None, **kwargs):
73
74
self._init_objman(ident, parent=parent, **kwargs)
75
76
self.add_col(ColConf('vertices', np.zeros((2, 3), float),
77
groupnames=['internal'],
78
perm='rw',
79
name='Vertex',
80
is_save=True,
81
info='Vertex coordinate vectors of points. with format [[[x11,y11,z11],[x12,y12,z12]],[[x21,y21,z21],[x22,y22,z122]],...]',
82
))
83
84
self.add_col(ColConf('polygons', None,
85
dtype='object',
86
groupnames=['landuse'],
87
perm='rw',
88
name='Polygon',
89
info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',
90
))
91
92
self.add_col(ColConf('ids_sumo', None,
93
dtype='object',
94
is_index=True,
95
perm='rw',
96
name='Polygon',
97
info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',
98
))
99
100
###########################################################################
101
# Instance creation
102
103
lines = Lines('lines')
104
# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],
105
# [10.0,20.0,20.0,20.0,20.0,10.0],
106
# [20.0,30.0,30.0,20.0,30.0,20.0],
107
# ]
108
109
# vertices = [ [(0.0,10.0),(10.0,10.0)],
110
# [(10.0,20.0),(20.0,20.0)],
111
# [(20.0,30.0),(30.0,20.0)],
112
# ]
113
vertices = [
114
[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 0
115
[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 1
116
[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 2
117
]
118
polygons = [
119
np.array([[0.0, 0.0, 0.0], [0.2, 0.0, 0.0], [0.2, 0.0, 0.1], [0.3, 0.3, 0.3]]), # 0
120
np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 1
121
np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 2
122
]
123
ids_sumo = ['aa10', 'bb22', 'cc333']
124
# lines.add_rows(3)
125
lines.add_rows(3, vertices=vertices, polygons=polygons, ids_sumo=ids_sumo)
126
lines.print_attrs()
127
print 'direct access vertex=\n', lines.vertices.value
128
print 'direct access polygons=\n', lines.polygons.value
129
print 'id for index bb22=', lines.ids_sumo.get_id_from_index('bb22')
130
print 'ids for index bb22,cc333=', lines.ids_sumo.get_ids_from_indices(['bb22', 'cc333'])
131
lines.del_row(2)
132
lines.print_attrs()
133
print 'id for index bb22=', lines.ids_sumo.get_id_from_index('cc333')
134
if 0 | is_all:
135
class Lines(ArrayObjman):
136
137
def __init__(self, ident, parent=None, **kwargs):
138
139
self._init_objman(ident, parent=parent, **kwargs)
140
141
self.add_col(ArrayConf('vertices', np.zeros((2, 3), float),
142
groupnames=['internal'],
143
perm='rw',
144
name='Vertex',
145
is_save=True,
146
info='Vertex coordinate vectors of points. with format [[[x11,y11,z11],[x12,y12,z12]],[[x21,y21,z21],[x22,y22,z122]],...]',
147
))
148
149
self.add_col(ArrayConf('polygons', None,
150
dtype='object',
151
groupnames=['landuse'],
152
perm='rw',
153
name='Polygon',
154
info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',
155
))
156
157
###########################################################################
158
# Instance creation
159
160
lines = Lines('lines')
161
# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],
162
# [10.0,20.0,20.0,20.0,20.0,10.0],
163
# [20.0,30.0,30.0,20.0,30.0,20.0],
164
# ]
165
166
# vertices = [ [(0.0,10.0),(10.0,10.0)],
167
# [(10.0,20.0),(20.0,20.0)],
168
# [(20.0,30.0),(30.0,20.0)],
169
# ]
170
vertices = [
171
[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 0
172
[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 1
173
[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 2
174
]
175
polygons = [
176
np.array([[0.0, 0.0, 0.0], [0.2, 0.0, 0.0], [0.2, 0.0, 0.1], [0.3, 0.3, 0.3]]), # 0
177
np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 1
178
np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 2
179
]
180
# lines.add_rows(3)
181
lines.add_rows(3, vertices=vertices, polygons=polygons)
182
lines.print_attrs()
183
print 'direct access vertex=\n', lines.vertices.value
184
print 'direct access polygons=\n', lines.polygons.value
185
186
if 0 | is_all:
187
class ZonesTab(ArrayObjman):
188
def __init__(self, ident, parent=None, **kwargs):
189
self._init_objman(ident, parent=parent, **kwargs)
190
191
self.add_col(ColConf('shapes', [],
192
groupnames=['state'],
193
perm='rw',
194
is_save=True,
195
name='Shape',
196
info='Shape of zone which is a list of (x,y) coordinates',
197
))
198
199
class OdTripsTab(ArrayObjman):
200
def __init__(self, ident, parent, zones, **kwargs):
201
self._init_objman(ident, parent=parent, **kwargs)
202
203
self.add_col(IdsConf('ids_orig', zones, is_child=False,
204
groupnames=['state'],
205
is_save=True,
206
name='ID Orig',
207
info='ID of traffic assignment zone of origin of trip.',
208
))
209
210
self.add_col(IdsConf('ids_dest', zones, is_child=False,
211
groupnames=['state'],
212
is_save=True,
213
name='ID Dest',
214
info='ID of traffic assignment zone of destination of trip.',
215
))
216
217
self.add_col(ColConf('tripnumbers', 0,
218
groupnames=['state'],
219
perm='rw',
220
is_save=True,
221
name='Trip number',
222
info='Number of trips from zone with ID Orig to zone with ID Dest.',
223
))
224
225
class OdModesTab(ArrayObjman):
226
def __init__(self, ident, parent=None, **kwargs):
227
self._init_objman(ident, parent=parent, **kwargs)
228
229
self.add_col(ObjsConf('odtrips',
230
groupnames=['state'],
231
is_save=True,
232
name='OD matrix',
233
info='Matrix with trips from origin to destintion',
234
))
235
236
class OdIntervalsTab(ArrayObjman):
237
def __init__(self, ident, parent=None, **kwargs):
238
self._init_objman(ident, parent=parent, **kwargs)
239
240
self.add_col(ColConf('t_start', 0.0,
241
groupnames=['state'],
242
perm='rw',
243
is_save=True,
244
name='Start time',
245
unit='s',
246
info='Start time of interval',
247
))
248
249
self.add_col(ColConf('t_end', 3600.0,
250
groupnames=['state'],
251
perm='rw',
252
is_save=True,
253
name='End time',
254
unit='s',
255
info='End time of interval',
256
))
257
258
self.add_col(ObjsConf('odmodes',
259
groupnames=['state'],
260
is_save=True,
261
name='Modes',
262
info='Transport mode',
263
))
264
265
###########################################################################
266
# Instance creation
267
268
demand = BaseObjman('demand')
269
270
zones = ZonesTab('zones', parent=demand)
271
demand.zones = demand.attrsman.add(ObjConf(zones))
272
shapes = [[(0.0, 10.0), (10.0, 10.0), (10.0, 0.0)],
273
[(10.0, 20.0), (20.0, 20.0), (20.0, 10.0)],
274
[(20.0, 30.0), (30.0, 20.0), (30.0, 20.0)],
275
]
276
zones.add_rows(3, shapes=shapes)
277
278
odintervals = OdIntervalsTab('odintervals', parent=demand, info='OD data for different time intervals')
279
demand.odintervals = demand.attrsman.add(ObjConf(odintervals))
280
odintervals.add_rows(2, t_start=[0, 3600], t_end=[3600, 7200])
281
for id_odmodes in odintervals.get_ids():
282
odmodes = OdModesTab((odintervals.odmodes.attrname, id_odmodes), parent=odintervals)
283
odintervals.odmodes[id_odmodes] = odmodes
284
285
odmodes.add_rows(2)
286
for id_odtrips in odmodes.get_ids():
287
odtrips = OdTripsTab((odmodes.odtrips.attrname, id_odtrips), odmodes, zones)
288
odtrips.add_rows(3, ids_orig=[3, 2, 1], ids_dest=[3, 3, 3], tripnumbers=[10, 200, 555])
289
odmodes.odtrips[id_odtrips] = odtrips
290
291
demand.attrsman.print_attrs()
292
odintervals.print_attrs()
293
294
for id_odmodes in odintervals.get_ids():
295
print '\nMODE:'
296
odintervals.odmodes[id_odmodes].print_attrs()
297
print '\nTRIPS:'
298
for id_odtrips in odmodes.get_ids():
299
odmodes.odtrips[id_odtrips].print_attrs()
300
301
302
if 0 | is_all:
303
class ZonesTab(TableObjman):
304
def __init__(self, ident, parent=None, **kwargs):
305
self._init_objman(ident, parent=parent, **kwargs)
306
307
self.add_col(ColConf('shapes', [],
308
groupnames=['state'],
309
perm='rw',
310
is_save=True,
311
name='Shape',
312
info='Shape of zone which is a list of (x,y) coordinates',
313
))
314
315
class OdTripsTab(TableObjman):
316
def __init__(self, ident, parent, zones, **kwargs):
317
self._init_objman(ident, parent=parent, **kwargs)
318
319
self.add_col(IdsConf('ids_orig', zones, is_child=False,
320
groupnames=['state'],
321
is_save=True,
322
name='ID Orig',
323
info='ID of traffic assignment zone of origin of trip.',
324
))
325
326
self.add_col(IdsConf('ids_dest', zones, is_child=False,
327
groupnames=['state'],
328
is_save=True,
329
name='ID Dest',
330
info='ID of traffic assignment zone of destination of trip.',
331
))
332
333
self.add_col(ColConf('tripnumbers', 0,
334
groupnames=['state'],
335
perm='rw',
336
is_save=True,
337
name='Trip number',
338
info='Number of trips from zone with ID Orig to zone with ID Dest.',
339
))
340
341
class OdModesTab(TableObjman):
342
def __init__(self, ident, parent=None, **kwargs):
343
self._init_objman(ident, parent=parent, **kwargs)
344
345
self.add_col(ObjsConf('odtrips',
346
groupnames=['state'],
347
is_save=True,
348
name='OD matrix',
349
info='Matrix with trips from origin to destintion',
350
))
351
352
class OdIntervalsTab(TableObjman):
353
def __init__(self, ident, parent=None, **kwargs):
354
self._init_objman(ident, parent=parent, **kwargs)
355
356
self.add_col(ColConf('t_start', 0.0,
357
groupnames=['state'],
358
perm='rw',
359
is_save=True,
360
name='Start time',
361
unit='s',
362
info='Start time of interval',
363
))
364
365
self.add_col(ColConf('t_end', 3600.0,
366
groupnames=['state'],
367
perm='rw',
368
is_save=True,
369
name='End time',
370
unit='s',
371
info='End time of interval',
372
))
373
374
self.add_col(ObjsConf('odmodes',
375
groupnames=['state'],
376
is_save=True,
377
name='Modes',
378
info='Transport mode',
379
))
380
381
###########################################################################
382
# Instance creation
383
384
demand = BaseObjman('demand')
385
386
zones = ZonesTab('zones', parent=demand)
387
demand.zones = demand.attrsman.add(ObjConf(zones))
388
shapes = [[(0.0, 10.0), (10.0, 10.0), (10.0, 0.0)],
389
[(10.0, 20.0), (20.0, 20.0), (20.0, 10.0)],
390
[(20.0, 30.0), (30.0, 20.0), (30.0, 20.0)],
391
]
392
zones.add_rows(3, shapes=shapes)
393
394
odintervals = OdIntervalsTab('odintervals', parent=demand, info='OD data for different time intervals')
395
demand.odintervals = demand.attrsman.add(ObjConf(odintervals, is_child=True))
396
odintervals.add_rows(2, t_start=[0, 3600], t_end=[3600, 7200])
397
for id_odmodes in odintervals.get_ids():
398
odmodes = OdModesTab((odintervals.odmodes.attrname, id_odmodes), parent=odintervals)
399
odintervals.odmodes[id_odmodes] = odmodes
400
401
odmodes.add_rows(2)
402
for id_odtrips in odmodes.get_ids():
403
odtrips = OdTripsTab((odmodes.odtrips.attrname, id_odtrips), odmodes, zones)
404
odtrips.add_rows(3, ids_orig=[3, 2, 1], ids_dest=[3, 3, 3], tripnumbers=[10, 200, 555])
405
odmodes.odtrips[id_odtrips] = odtrips
406
407
demand.attrsman.print_attrs()
408
odintervals.print_attrs()
409
410
for id_odmodes in odintervals.get_ids():
411
print '\nMODE:'
412
odintervals.odmodes[id_odmodes].print_attrs()
413
print '\nTRIPS:'
414
for id_odtrips in odmodes.get_ids():
415
odmodes.odtrips[id_odtrips].print_attrs()
416
417
418
if 0 | is_all:
419
420
net = BaseObjman('net')
421
422
# TODO: could be put in 2 statements
423
edges = TableObjman('edges', parent=net, info='Network edges')
424
nodes = TableObjman('nodes', parent=net, info='Network nodes')
425
426
net.edges = net.get_attrsman().add(ObjConf(edges, is_child=True))
427
net.nodes = net.get_attrsman().add(ObjConf(nodes, is_child=True))
428
429
net.edges.add_col(IdsConf('ids_node_from', net.nodes,
430
groupnames=['state'],
431
is_save=True,
432
name='ID from nodes',
433
info='ID from nodes',
434
))
435
436
net.edges.add_col(IdsConf('ids_node_to', net.nodes,
437
groupnames=['state'],
438
is_save=True,
439
name='ID to nodes',
440
info='ID to nodes',
441
))
442
443
net.nodes.add_col(ColConf('coords', (0.0, 0.0),
444
groupnames=['state'],
445
perm='rw',
446
is_save=True,
447
name='Coords',
448
info='Coordinates',
449
))
450
451
net.nodes.add_rows(4,
452
# 1 2 3 4
453
coords=[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)],
454
)
455
456
net.edges.add_rows(2)
457
net.edges.ids_node_from[[1, 2]] = [1, 4]
458
net.edges.ids_node_to[[1, 2]] = [3, 2]
459
net.get_attrsman().print_attrs()
460
net.edges.print_attrs()
461
net.nodes.print_attrs()
462
463
if 0 | is_all:
464
tab1 = TableObjman('simple_table')
465
466
tab1.add_col(ColConf('surname', 'xx',
467
groupnames=['state'],
468
perm='rw',
469
is_save=True,
470
name='Family name',
471
info='Name of Family',
472
))
473
474
tab1.add_col(ColConf('streetname', 'via della...',
475
groupnames=['state'],
476
perm='rw',
477
is_save=False,
478
name='Street name',
479
info='Name of the street',
480
))
481
tab1.add_rows(4,
482
surname=['walt', 'greg', 'bob', 'duck'],
483
streetname=['a', 'bb', 'ccc', 'dddd'],
484
)
485
486
print 'direct access: tab1.surname.value', tab1.surname.value
487
tab1.print_attrs()
488
489
if 0 | is_all:
490
491
tab1 = TableObjman('tab1')
492
print '\ntab1.ident', tab1.ident
493
494
tab2 = TableObjman('tab2', parent=tab1)
495
print '\ntab2.ident', tab2.ident
496
497
# TODO: seperate attrname from linked obj ident because restrictive and makes problems with multiple tab destinations
498
# this should be possible ...following the path of attrnames of absident
499
# -
500
tab1.add_col(IdsConf('tab1', tab2))
501
502
tab2.add_col(IdsConf('tab2', tab1, is_child=False))
503
504
tab2.add_col(ColConf('surname', 'xx',
505
groupnames=['state'],
506
perm='rw',
507
is_save=True,
508
name='Family name',
509
info='Name of Family',
510
))
511
512
tab2.add_col(ColConf('streetname', 'via della...',
513
groupnames=['state'],
514
perm='rw',
515
is_save=False,
516
name='Street name',
517
info='Name of the street',
518
))
519
tab2.add_rows(4,
520
surname=['walt', 'greg', 'bob', 'duck'],
521
streetname=['a', 'bb', 'ccc', 'dddd'],
522
tab1=[2, 1, 3, 1, ],
523
)
524
525
tab2.print_attrs()
526
527
tab1.add_rows(3,
528
tab2=[3, 1, 2],
529
)
530
531
tab1.print_attrs()
532
533
534
if 0 | is_all:
535
obj = TestTabman()
536
537
print '\nobj.ident', obj.ident
538
539
# streetname
540
# print 'This is the value of the attribute: obj.streetname=',obj.streetname
541
# print 'This is the configuration instance of the attribute x',obj.attrsman.x
542
obj.attrsman.print_attrs()
543
544
545
if 0 | is_all:
546
obj = TestTableObjMan()
547
548
print '\nobj.ident', obj.ident
549
550
# streetname
551
# print 'This is the value of the attribute: obj.streetname=',obj.streetname
552
# print 'This is the configuration instance of the attribute x',obj.attrsman.x
553
obj.print_attrs()
554
555