scenario_parser.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # Copyright (c) 2019 Intel Corporation
  3. #
  4. # This work is licensed under the terms of the MIT license.
  5. # For a copy, see <https://opensource.org/licenses/MIT>.
  6. """
  7. This module provides access to a scenario configuration parser
  8. """
  9. import glob
  10. import os
  11. import xml.etree.ElementTree as ET
  12. from srunner.scenarioconfigs.scenario_configuration import ScenarioConfiguration, ActorConfigurationData
  13. from srunner.scenarioconfigs.route_scenario_configuration import RouteConfiguration
  14. class ScenarioConfigurationParser(object):
  15. """
  16. Pure static class providing access to parser methods for scenario configuration files (*.xml)
  17. """
  18. @staticmethod
  19. def parse_scenario_configuration(scenario_name, config_file_name):
  20. """
  21. Parse all scenario configuration files at srunner/examples and the additional
  22. config files, providing a list of ScenarioConfigurations @return
  23. If scenario_name starts with "group:" all scenarios that
  24. have that type are parsed and returned. Otherwise only the
  25. scenario that matches the scenario_name is parsed and returned.
  26. """
  27. list_of_config_files = glob.glob("{}/srunner/examples/*.xml".format(os.getenv('SCENARIO_RUNNER_ROOT', "./")))
  28. if config_file_name != '':
  29. list_of_config_files.append(config_file_name)
  30. single_scenario_only = True
  31. if scenario_name.startswith("group:"):
  32. single_scenario_only = False
  33. scenario_name = scenario_name[6:]
  34. scenario_configurations = []
  35. for file_name in list_of_config_files:
  36. tree = ET.parse(file_name)
  37. for scenario in tree.iter("scenario"):
  38. scenario_config_name = scenario.attrib.get('name', None)
  39. scenario_config_type = scenario.attrib.get('type', None)
  40. if single_scenario_only:
  41. # Check the scenario is the correct one
  42. if scenario_config_name != scenario_name:
  43. continue
  44. else:
  45. # Check the scenario is of the correct type
  46. if scenario_config_type != scenario_name:
  47. continue
  48. new_config = ScenarioConfiguration()
  49. new_config.town = scenario.attrib.get('town', None)
  50. new_config.name = scenario_config_name
  51. new_config.type = scenario_config_type
  52. new_config.other_actors = []
  53. new_config.ego_vehicles = []
  54. new_config.trigger_points = []
  55. for weather in scenario.iter("weather"):
  56. new_config.weather.cloudiness = float(weather.attrib.get("cloudiness", 0))
  57. new_config.weather.precipitation = float(weather.attrib.get("precipitation", 0))
  58. new_config.weather.precipitation_deposits = float(weather.attrib.get("precipitation_deposits", 0))
  59. new_config.weather.wind_intensity = float(weather.attrib.get("wind_intensity", 0.35))
  60. new_config.weather.sun_azimuth_angle = float(weather.attrib.get("sun_azimuth_angle", 0.0))
  61. new_config.weather.sun_altitude_angle = float(weather.attrib.get("sun_altitude_angle", 15.0))
  62. new_config.weather.fog_density = float(weather.attrib.get("fog_density", 0.0))
  63. new_config.weather.fog_distance = float(weather.attrib.get("fog_distance", 0.0))
  64. new_config.weather.wetness = float(weather.attrib.get("wetness", 0.0))
  65. for ego_vehicle in scenario.iter("ego_vehicle"):
  66. new_config.ego_vehicles.append(ActorConfigurationData.parse_from_node(ego_vehicle, 'hero'))
  67. new_config.trigger_points.append(new_config.ego_vehicles[-1].transform)
  68. for route in scenario.iter("route"):
  69. route_conf = RouteConfiguration()
  70. route_conf.parse_xml(route)
  71. new_config.route = route_conf
  72. for other_actor in scenario.iter("other_actor"):
  73. new_config.other_actors.append(ActorConfigurationData.parse_from_node(other_actor, 'scenario'))
  74. scenario_configurations.append(new_config)
  75. return scenario_configurations
  76. @staticmethod
  77. def get_list_of_scenarios(config_file_name):
  78. """
  79. Parse *all* config files and provide a list with all scenarios @return
  80. """
  81. list_of_config_files = glob.glob("{}/srunner/examples/*.xml".format(os.getenv('SCENARIO_RUNNER_ROOT', "./")))
  82. list_of_config_files += glob.glob("{}/srunner/examples/*.xosc".format(os.getenv('SCENARIO_RUNNER_ROOT', "./")))
  83. if config_file_name != '':
  84. list_of_config_files.append(config_file_name)
  85. scenarios = []
  86. for file_name in list_of_config_files:
  87. if ".xosc" in file_name:
  88. tree = ET.parse(file_name)
  89. scenarios.append("{} (OpenSCENARIO)".format(tree.find("FileHeader").attrib.get('description', None)))
  90. else:
  91. tree = ET.parse(file_name)
  92. for scenario in tree.iter("scenario"):
  93. scenarios.append(scenario.attrib.get('name', None))
  94. return scenarios