Path: blob/main/tools/contributed/sumopy/agilepy/lib_base/test_classman_tables.py
169689 views
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo1# Copyright (C) 2016-2025 German Aerospace Center (DLR) and others.2# SUMOPy module3# Copyright (C) 2012-2021 University of Bologna - DICAM4# This program and the accompanying materials are made available under the5# terms of the Eclipse Public License 2.0 which is available at6# https://www.eclipse.org/legal/epl-2.0/7# This Source Code may also be made available under the following Secondary8# Licenses when the conditions for such availability set forth in the Eclipse9# Public License 2.0 are satisfied: GNU General Public License, version 210# or later which is available at11# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html12# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1314# @file test_classman_tables.py15# @author Joerg Schweizer16# @date 20121718#from classman import *1920from test_classman_classes import *21from classman import *22from arrayman import *23is_all = 024if 1 | is_all:25pass26if 1 | is_all:27print 'Lines, Poly example'2829###########################################################################30# Instance creation3132lines = Lines('lines')33# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],34# [10.0,20.0,20.0,20.0,20.0,10.0],35# [20.0,30.0,30.0,20.0,30.0,20.0],36# ]3738# vertices = [ [(0.0,10.0),(10.0,10.0)],39# [(10.0,20.0),(20.0,20.0)],40# [(20.0,30.0),(30.0,20.0)],41# ]42vertices = [43[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 044[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 145[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 246]47polygons = [48np.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]]), # 049np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 150np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 251]52ids_sumo = ['aa10', 'bb22', 'cc333']53# lines.add_rows(3)54lines.add_rows(3, vertices=vertices, polygons=polygons, ids_sumo=ids_sumo)55lines.print_attrs()56print '\n indexmap', lines.ids_sumo.get_indexmap()57print 'direct access vertex=\n', lines.vertices.value58print 'id for index bb22=', lines.ids_sumo.get_id_from_index('bb22')59print 'ids for index bb22,cc333=', lines.ids_sumo.get_ids_from_indices(['bb22', 'cc333'])60# lines.del_row(2)61lines.print_attrs()62print 'id for index bb22=', lines.ids_sumo.get_id_from_index('cc333')6364lines.ids_sumo[2] = 'xy'6566print '\n indexmap', lines.ids_sumo.get_indexmap()67lines.print_attrs()68if 0 | is_all:69class Lines(TableObjman):7071def __init__(self, ident, parent=None, **kwargs):7273self._init_objman(ident, parent=parent, **kwargs)7475self.add_col(ColConf('vertices', np.zeros((2, 3), float),76groupnames=['internal'],77perm='rw',78name='Vertex',79is_save=True,80info='Vertex coordinate vectors of points. with format [[[x11,y11,z11],[x12,y12,z12]],[[x21,y21,z21],[x22,y22,z122]],...]',81))8283self.add_col(ColConf('polygons', None,84dtype='object',85groupnames=['landuse'],86perm='rw',87name='Polygon',88info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',89))9091self.add_col(ColConf('ids_sumo', None,92dtype='object',93is_index=True,94perm='rw',95name='Polygon',96info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',97))9899###########################################################################100# Instance creation101102lines = Lines('lines')103# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],104# [10.0,20.0,20.0,20.0,20.0,10.0],105# [20.0,30.0,30.0,20.0,30.0,20.0],106# ]107108# vertices = [ [(0.0,10.0),(10.0,10.0)],109# [(10.0,20.0),(20.0,20.0)],110# [(20.0,30.0),(30.0,20.0)],111# ]112vertices = [113[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 0114[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 1115[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 2116]117polygons = [118np.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]]), # 0119np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 1120np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 2121]122ids_sumo = ['aa10', 'bb22', 'cc333']123# lines.add_rows(3)124lines.add_rows(3, vertices=vertices, polygons=polygons, ids_sumo=ids_sumo)125lines.print_attrs()126print 'direct access vertex=\n', lines.vertices.value127print 'direct access polygons=\n', lines.polygons.value128print 'id for index bb22=', lines.ids_sumo.get_id_from_index('bb22')129print 'ids for index bb22,cc333=', lines.ids_sumo.get_ids_from_indices(['bb22', 'cc333'])130lines.del_row(2)131lines.print_attrs()132print 'id for index bb22=', lines.ids_sumo.get_id_from_index('cc333')133if 0 | is_all:134class Lines(ArrayObjman):135136def __init__(self, ident, parent=None, **kwargs):137138self._init_objman(ident, parent=parent, **kwargs)139140self.add_col(ArrayConf('vertices', np.zeros((2, 3), float),141groupnames=['internal'],142perm='rw',143name='Vertex',144is_save=True,145info='Vertex coordinate vectors of points. with format [[[x11,y11,z11],[x12,y12,z12]],[[x21,y21,z21],[x22,y22,z122]],...]',146))147148self.add_col(ArrayConf('polygons', None,149dtype='object',150groupnames=['landuse'],151perm='rw',152name='Polygon',153info='Polygons [[ (x11,y11,z11), (x12,y12,z13), (x13, y13,z13),...],[...]]',154))155156###########################################################################157# Instance creation158159lines = Lines('lines')160# vertices = [ [0.0,10.0,10.0,10.0,10.0,0.0],161# [10.0,20.0,20.0,20.0,20.0,10.0],162# [20.0,30.0,30.0,20.0,30.0,20.0],163# ]164165# vertices = [ [(0.0,10.0),(10.0,10.0)],166# [(10.0,20.0),(20.0,20.0)],167# [(20.0,30.0),(30.0,20.0)],168# ]169vertices = [170[[0.0, 0.0, 0.0], [0.2, 0.0, 0.0]], # 0171[[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]], # 1172[[0.5, 0.0, 0.1], [1.9, 0.0, 0.0]], # 2173]174polygons = [175np.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]]), # 0176np.array([[0.3, 0.0, 0.0], [0.9, 0.0, 0.0]]), # 1177np.array([[0.5, 0.0, 0.1], [1.9, 0.0, 0.0], [0.2, 0.2, 0.2]]), # 2178]179# lines.add_rows(3)180lines.add_rows(3, vertices=vertices, polygons=polygons)181lines.print_attrs()182print 'direct access vertex=\n', lines.vertices.value183print 'direct access polygons=\n', lines.polygons.value184185if 0 | is_all:186class ZonesTab(ArrayObjman):187def __init__(self, ident, parent=None, **kwargs):188self._init_objman(ident, parent=parent, **kwargs)189190self.add_col(ColConf('shapes', [],191groupnames=['state'],192perm='rw',193is_save=True,194name='Shape',195info='Shape of zone which is a list of (x,y) coordinates',196))197198class OdTripsTab(ArrayObjman):199def __init__(self, ident, parent, zones, **kwargs):200self._init_objman(ident, parent=parent, **kwargs)201202self.add_col(IdsConf('ids_orig', zones, is_child=False,203groupnames=['state'],204is_save=True,205name='ID Orig',206info='ID of traffic assignment zone of origin of trip.',207))208209self.add_col(IdsConf('ids_dest', zones, is_child=False,210groupnames=['state'],211is_save=True,212name='ID Dest',213info='ID of traffic assignment zone of destination of trip.',214))215216self.add_col(ColConf('tripnumbers', 0,217groupnames=['state'],218perm='rw',219is_save=True,220name='Trip number',221info='Number of trips from zone with ID Orig to zone with ID Dest.',222))223224class OdModesTab(ArrayObjman):225def __init__(self, ident, parent=None, **kwargs):226self._init_objman(ident, parent=parent, **kwargs)227228self.add_col(ObjsConf('odtrips',229groupnames=['state'],230is_save=True,231name='OD matrix',232info='Matrix with trips from origin to destintion',233))234235class OdIntervalsTab(ArrayObjman):236def __init__(self, ident, parent=None, **kwargs):237self._init_objman(ident, parent=parent, **kwargs)238239self.add_col(ColConf('t_start', 0.0,240groupnames=['state'],241perm='rw',242is_save=True,243name='Start time',244unit='s',245info='Start time of interval',246))247248self.add_col(ColConf('t_end', 3600.0,249groupnames=['state'],250perm='rw',251is_save=True,252name='End time',253unit='s',254info='End time of interval',255))256257self.add_col(ObjsConf('odmodes',258groupnames=['state'],259is_save=True,260name='Modes',261info='Transport mode',262))263264###########################################################################265# Instance creation266267demand = BaseObjman('demand')268269zones = ZonesTab('zones', parent=demand)270demand.zones = demand.attrsman.add(ObjConf(zones))271shapes = [[(0.0, 10.0), (10.0, 10.0), (10.0, 0.0)],272[(10.0, 20.0), (20.0, 20.0), (20.0, 10.0)],273[(20.0, 30.0), (30.0, 20.0), (30.0, 20.0)],274]275zones.add_rows(3, shapes=shapes)276277odintervals = OdIntervalsTab('odintervals', parent=demand, info='OD data for different time intervals')278demand.odintervals = demand.attrsman.add(ObjConf(odintervals))279odintervals.add_rows(2, t_start=[0, 3600], t_end=[3600, 7200])280for id_odmodes in odintervals.get_ids():281odmodes = OdModesTab((odintervals.odmodes.attrname, id_odmodes), parent=odintervals)282odintervals.odmodes[id_odmodes] = odmodes283284odmodes.add_rows(2)285for id_odtrips in odmodes.get_ids():286odtrips = OdTripsTab((odmodes.odtrips.attrname, id_odtrips), odmodes, zones)287odtrips.add_rows(3, ids_orig=[3, 2, 1], ids_dest=[3, 3, 3], tripnumbers=[10, 200, 555])288odmodes.odtrips[id_odtrips] = odtrips289290demand.attrsman.print_attrs()291odintervals.print_attrs()292293for id_odmodes in odintervals.get_ids():294print '\nMODE:'295odintervals.odmodes[id_odmodes].print_attrs()296print '\nTRIPS:'297for id_odtrips in odmodes.get_ids():298odmodes.odtrips[id_odtrips].print_attrs()299300301if 0 | is_all:302class ZonesTab(TableObjman):303def __init__(self, ident, parent=None, **kwargs):304self._init_objman(ident, parent=parent, **kwargs)305306self.add_col(ColConf('shapes', [],307groupnames=['state'],308perm='rw',309is_save=True,310name='Shape',311info='Shape of zone which is a list of (x,y) coordinates',312))313314class OdTripsTab(TableObjman):315def __init__(self, ident, parent, zones, **kwargs):316self._init_objman(ident, parent=parent, **kwargs)317318self.add_col(IdsConf('ids_orig', zones, is_child=False,319groupnames=['state'],320is_save=True,321name='ID Orig',322info='ID of traffic assignment zone of origin of trip.',323))324325self.add_col(IdsConf('ids_dest', zones, is_child=False,326groupnames=['state'],327is_save=True,328name='ID Dest',329info='ID of traffic assignment zone of destination of trip.',330))331332self.add_col(ColConf('tripnumbers', 0,333groupnames=['state'],334perm='rw',335is_save=True,336name='Trip number',337info='Number of trips from zone with ID Orig to zone with ID Dest.',338))339340class OdModesTab(TableObjman):341def __init__(self, ident, parent=None, **kwargs):342self._init_objman(ident, parent=parent, **kwargs)343344self.add_col(ObjsConf('odtrips',345groupnames=['state'],346is_save=True,347name='OD matrix',348info='Matrix with trips from origin to destintion',349))350351class OdIntervalsTab(TableObjman):352def __init__(self, ident, parent=None, **kwargs):353self._init_objman(ident, parent=parent, **kwargs)354355self.add_col(ColConf('t_start', 0.0,356groupnames=['state'],357perm='rw',358is_save=True,359name='Start time',360unit='s',361info='Start time of interval',362))363364self.add_col(ColConf('t_end', 3600.0,365groupnames=['state'],366perm='rw',367is_save=True,368name='End time',369unit='s',370info='End time of interval',371))372373self.add_col(ObjsConf('odmodes',374groupnames=['state'],375is_save=True,376name='Modes',377info='Transport mode',378))379380###########################################################################381# Instance creation382383demand = BaseObjman('demand')384385zones = ZonesTab('zones', parent=demand)386demand.zones = demand.attrsman.add(ObjConf(zones))387shapes = [[(0.0, 10.0), (10.0, 10.0), (10.0, 0.0)],388[(10.0, 20.0), (20.0, 20.0), (20.0, 10.0)],389[(20.0, 30.0), (30.0, 20.0), (30.0, 20.0)],390]391zones.add_rows(3, shapes=shapes)392393odintervals = OdIntervalsTab('odintervals', parent=demand, info='OD data for different time intervals')394demand.odintervals = demand.attrsman.add(ObjConf(odintervals, is_child=True))395odintervals.add_rows(2, t_start=[0, 3600], t_end=[3600, 7200])396for id_odmodes in odintervals.get_ids():397odmodes = OdModesTab((odintervals.odmodes.attrname, id_odmodes), parent=odintervals)398odintervals.odmodes[id_odmodes] = odmodes399400odmodes.add_rows(2)401for id_odtrips in odmodes.get_ids():402odtrips = OdTripsTab((odmodes.odtrips.attrname, id_odtrips), odmodes, zones)403odtrips.add_rows(3, ids_orig=[3, 2, 1], ids_dest=[3, 3, 3], tripnumbers=[10, 200, 555])404odmodes.odtrips[id_odtrips] = odtrips405406demand.attrsman.print_attrs()407odintervals.print_attrs()408409for id_odmodes in odintervals.get_ids():410print '\nMODE:'411odintervals.odmodes[id_odmodes].print_attrs()412print '\nTRIPS:'413for id_odtrips in odmodes.get_ids():414odmodes.odtrips[id_odtrips].print_attrs()415416417if 0 | is_all:418419net = BaseObjman('net')420421# TODO: could be put in 2 statements422edges = TableObjman('edges', parent=net, info='Network edges')423nodes = TableObjman('nodes', parent=net, info='Network nodes')424425net.edges = net.get_attrsman().add(ObjConf(edges, is_child=True))426net.nodes = net.get_attrsman().add(ObjConf(nodes, is_child=True))427428net.edges.add_col(IdsConf('ids_node_from', net.nodes,429groupnames=['state'],430is_save=True,431name='ID from nodes',432info='ID from nodes',433))434435net.edges.add_col(IdsConf('ids_node_to', net.nodes,436groupnames=['state'],437is_save=True,438name='ID to nodes',439info='ID to nodes',440))441442net.nodes.add_col(ColConf('coords', (0.0, 0.0),443groupnames=['state'],444perm='rw',445is_save=True,446name='Coords',447info='Coordinates',448))449450net.nodes.add_rows(4,451# 1 2 3 4452coords=[(0.0, 0.0), (1.0, 0.0), (1.0, 1.0), (0.0, 1.0)],453)454455net.edges.add_rows(2)456net.edges.ids_node_from[[1, 2]] = [1, 4]457net.edges.ids_node_to[[1, 2]] = [3, 2]458net.get_attrsman().print_attrs()459net.edges.print_attrs()460net.nodes.print_attrs()461462if 0 | is_all:463tab1 = TableObjman('simple_table')464465tab1.add_col(ColConf('surname', 'xx',466groupnames=['state'],467perm='rw',468is_save=True,469name='Family name',470info='Name of Family',471))472473tab1.add_col(ColConf('streetname', 'via della...',474groupnames=['state'],475perm='rw',476is_save=False,477name='Street name',478info='Name of the street',479))480tab1.add_rows(4,481surname=['walt', 'greg', 'bob', 'duck'],482streetname=['a', 'bb', 'ccc', 'dddd'],483)484485print 'direct access: tab1.surname.value', tab1.surname.value486tab1.print_attrs()487488if 0 | is_all:489490tab1 = TableObjman('tab1')491print '\ntab1.ident', tab1.ident492493tab2 = TableObjman('tab2', parent=tab1)494print '\ntab2.ident', tab2.ident495496# TODO: seperate attrname from linked obj ident because restrictive and makes problems with multiple tab destinations497# this should be possible ...following the path of attrnames of absident498# -499tab1.add_col(IdsConf('tab1', tab2))500501tab2.add_col(IdsConf('tab2', tab1, is_child=False))502503tab2.add_col(ColConf('surname', 'xx',504groupnames=['state'],505perm='rw',506is_save=True,507name='Family name',508info='Name of Family',509))510511tab2.add_col(ColConf('streetname', 'via della...',512groupnames=['state'],513perm='rw',514is_save=False,515name='Street name',516info='Name of the street',517))518tab2.add_rows(4,519surname=['walt', 'greg', 'bob', 'duck'],520streetname=['a', 'bb', 'ccc', 'dddd'],521tab1=[2, 1, 3, 1, ],522)523524tab2.print_attrs()525526tab1.add_rows(3,527tab2=[3, 1, 2],528)529530tab1.print_attrs()531532533if 0 | is_all:534obj = TestTabman()535536print '\nobj.ident', obj.ident537538# streetname539# print 'This is the value of the attribute: obj.streetname=',obj.streetname540# print 'This is the configuration instance of the attribute x',obj.attrsman.x541obj.attrsman.print_attrs()542543544if 0 | is_all:545obj = TestTableObjMan()546547print '\nobj.ident', obj.ident548549# streetname550# print 'This is the value of the attribute: obj.streetname=',obj.streetname551# print 'This is the configuration instance of the attribute x',obj.attrsman.x552obj.print_attrs()553554555