2019-02-01 17:00:55 +00:00
|
|
|
import io
|
2018-12-07 15:45:35 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
import unittest
|
|
|
|
|
2018-12-14 19:23:18 +00:00
|
|
|
import configurator
|
2018-12-10 16:59:26 +00:00
|
|
|
|
2018-12-13 20:02:07 +00:00
|
|
|
# Absolute path of the directory the script is in
|
|
|
|
SCRIPTDIR = os.path.dirname(__file__)
|
2018-12-07 15:45:35 +00:00
|
|
|
|
2018-12-14 18:43:13 +00:00
|
|
|
|
2018-12-13 20:02:07 +00:00
|
|
|
class TemplateTests(unittest.TestCase):
|
2018-12-07 15:45:35 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.sim_name = 'test_simulation'
|
2018-12-13 20:02:07 +00:00
|
|
|
self.sim_path = '/test_simulation'
|
|
|
|
self.log_path = '/test_simulation/log'
|
2018-12-07 15:45:35 +00:00
|
|
|
|
2018-12-10 16:59:26 +00:00
|
|
|
def test_load_netconvert_template(self):
|
|
|
|
tree = configurator.load_netconvert_template('test.osm', 'test_simulation')
|
|
|
|
self.assertEqual(tree.find('input/osm-files').get('value'), 'test.osm')
|
|
|
|
self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.net.xml')
|
|
|
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.netconvert.log')
|
|
|
|
|
2018-12-13 20:02:07 +00:00
|
|
|
def test_load_sumoconfig_template_default(self):
|
2018-12-10 16:59:26 +00:00
|
|
|
tree = configurator.load_sumoconfig_template(self.sim_name)
|
|
|
|
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
|
|
|
|
self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml')
|
|
|
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
|
|
|
|
|
2018-12-14 18:43:13 +00:00
|
|
|
def test_load_sumoconfig_template_with_polygons(self):
|
|
|
|
tree = configurator.load_sumoconfig_template(self.sim_name, generate_polygons=True)
|
|
|
|
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
|
|
|
|
self.assertEqual(tree.find('input/route-files').get('value'), f'{self.sim_name}.rou.xml')
|
|
|
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
|
|
|
|
self.assertEqual(tree.find('input/additional-files').get('value'), f'{self.sim_name}.poly.xml')
|
|
|
|
|
2018-12-13 20:02:07 +00:00
|
|
|
def test_load_sumoconfig_template_with_routefiles(self):
|
|
|
|
routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml')
|
|
|
|
tree = configurator.load_sumoconfig_template(self.sim_name, routefiles)
|
|
|
|
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
|
|
|
|
self.assertEqual(tree.find('input/route-files').get('value'), ','.join(routefiles))
|
|
|
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.log')
|
|
|
|
|
2019-02-01 10:50:41 +00:00
|
|
|
def test_load_sumoconfig_template_with_seed(self):
|
|
|
|
routefiles = (f'{self.sim_name}.bus.rou.xml', f'{self.sim_name}.passenger.rou.xml')
|
|
|
|
tree = configurator.load_sumoconfig_template(self.sim_name, routefiles, seed=42)
|
|
|
|
self.assertEqual(tree.find('random_number/seed').get('value'), 42)
|
|
|
|
|
2018-12-10 16:59:26 +00:00
|
|
|
def test_load_polyconvert_template(self):
|
|
|
|
tree = configurator.load_polyconvert_template(
|
|
|
|
osm_file=f'{self.sim_name}.osm',
|
|
|
|
type_file='typemap/test.typ.xml',
|
|
|
|
scenario_name=f'{self.sim_name}'
|
|
|
|
)
|
|
|
|
self.assertEqual(tree.find('input/osm-files').get('value'), f'{self.sim_name}.osm')
|
|
|
|
self.assertEqual(tree.find('input/net-file').get('value'), f'{self.sim_name}.net.xml')
|
|
|
|
self.assertEqual(tree.find('input/type-file').get('value'), 'typemap/test.typ.xml')
|
|
|
|
self.assertEqual(tree.find('output/output-file').get('value'), f'{self.sim_name}.poly.xml')
|
|
|
|
self.assertEqual(tree.find('report/log').get('value'), f'{self.sim_name}.polyconvert.log')
|
|
|
|
|
2018-12-13 20:02:07 +00:00
|
|
|
|
|
|
|
class GenerationTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.base_path = tempfile.mkdtemp()
|
|
|
|
self.sim_name = 'test_simulation'
|
|
|
|
self.sim_path = os.path.join(self.base_path, self.sim_name)
|
|
|
|
self.log_path = os.path.join(self.sim_name, 'log')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self.base_path)
|
|
|
|
|
|
|
|
def test_generate_scenario(self):
|
|
|
|
osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
|
2018-12-14 18:43:13 +00:00
|
|
|
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name, generate_polygons=False)
|
|
|
|
self.assert_is_file(os.path.join(self.sim_path, f'{self.sim_name}.net.xml'))
|
|
|
|
|
|
|
|
def test_generate_scenario_with_polygons(self):
|
|
|
|
osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
|
|
|
|
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name, generate_polygons=True)
|
2018-12-13 20:02:07 +00:00
|
|
|
self.assert_is_dir(self.sim_path)
|
|
|
|
generated_files = [
|
|
|
|
f'{self.sim_name}.poly.xml',
|
|
|
|
f'{self.sim_name}.net.xml'
|
|
|
|
]
|
|
|
|
for f in generated_files:
|
|
|
|
self.assert_is_file(os.path.join(self.sim_path, f))
|
|
|
|
|
|
|
|
def test_generate_mobility(self):
|
|
|
|
# The scenario must be generated before the mobility
|
|
|
|
osm_file = os.path.join(SCRIPTDIR, 'sample.osm')
|
|
|
|
trips_file = os.path.join(self.sim_path, f'{self.sim_name}.trips.xml')
|
|
|
|
configurator.generate_scenario(osm_file, self.sim_path, self.sim_name)
|
2019-01-30 14:56:51 +00:00
|
|
|
classes = {'passenger': 10, 'truck': 1}
|
2019-02-01 17:18:07 +00:00
|
|
|
routefiles = configurator.generate_mobility(self.sim_path, self.sim_name, vclasses=classes, end_time=200)
|
2018-12-13 20:02:07 +00:00
|
|
|
|
|
|
|
self.assert_is_file(trips_file)
|
|
|
|
for f in routefiles:
|
|
|
|
self.assert_is_file(os.path.join(self.sim_path, f))
|
|
|
|
|
2018-12-07 15:45:35 +00:00
|
|
|
def assert_exists(self, path):
|
|
|
|
self.assertTrue(os.path.exists(path), msg=f'{path} does not exist')
|
|
|
|
|
|
|
|
def assert_is_file(self, path):
|
|
|
|
self.assert_exists(path)
|
|
|
|
self.assertTrue(os.path.isfile(path), msg=f'{path} is not a file')
|
|
|
|
|
|
|
|
def assert_is_dir(self, path):
|
|
|
|
self.assert_exists(path)
|
|
|
|
self.assertTrue(os.path.isdir(path), msg=f'{path} is not a directory')
|
|
|
|
|
|
|
|
|
2019-02-01 17:00:55 +00:00
|
|
|
class InputTests(unittest.TestCase):
|
|
|
|
def test_commandline(self):
|
|
|
|
options = ['--name', 'test-config', '--path', '/some/path', '--vclass', 'passenger=10', 'truck=1', '--', 'test.osm']
|
|
|
|
actual_conf = configurator.parse_command_line(options)
|
|
|
|
self.assertEqual(actual_conf.name, 'test-config')
|
|
|
|
self.assertEqual(actual_conf.osmfile, 'test.osm')
|
|
|
|
self.assertEqual(actual_conf.path, '/some/path')
|
|
|
|
self.assertEqual(actual_conf.vclasses, {'passenger': '10', 'truck': '1'})
|
|
|
|
|
|
|
|
def test_from_config_file(self):
|
|
|
|
options = """
|
|
|
|
{
|
|
|
|
"name": "test-config",
|
|
|
|
"path": "/some/path",
|
|
|
|
"vclasses": {
|
|
|
|
"passenger": 10,
|
|
|
|
"truck": 1
|
|
|
|
},
|
|
|
|
"osmfile": "test.osm"
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
actual_conf = configurator.parse_json(io.StringIO(options))
|
|
|
|
self.assertEqual(actual_conf.name, 'test-config')
|
|
|
|
self.assertEqual(actual_conf.osmfile, 'test.osm')
|
|
|
|
self.assertEqual(actual_conf.path, '/some/path')
|
|
|
|
self.assertEqual(actual_conf.vclasses, {'passenger': 10, 'truck': 1})
|
|
|
|
|
|
|
|
|
2018-12-07 15:45:35 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|