"""
**Scenario Module**
This module contains descriptions that stablish a traffic scenario. A traffic scenario for SymuVia is regularly described by a simulation object that points towards properties of the simulation file in this case an XML.
"""
# ============================================================================
# STANDARD IMPORTS
# ============================================================================
from pathlib import Path
from datetime import datetime
import os
from lxml import etree
# ============================================================================
# INTERNAL IMPORTS
# ============================================================================
from ensemble.input.scenario import Scenario
from ensemble.tools.exceptions import (
EnsembleAPIWarning,
EnsembleAPILoadFileError,
EnsembleAPILoadLibraryError,
)
from ensemble.tools import constants as ct
# ============================================================================
# CLASS AND DEFINITIONS
# ============================================================================
[docs]class SymuviaScenario(Scenario):
"""
Scenario class for Vissim
"""
[docs] def load_xml_tree(self) -> None:
"""Load XML file_name"""
# TODO: Add validation with DTD
tree = etree.parse(self.filename())
root = tree.getroot()
self.xmltree = root
[docs] def get_simulation_parameters(self) -> tuple:
"""Get simulation parameters
:return: tuple with XML dictionary containing parameters
:rtype: tuple
"""
branch_tree = "SIMULATIONS"
sim_params = self.xmltree.xpath(branch_tree)[0].getchildren()
return tuple(par.attrib for par in sim_params)
[docs] def get_network_endpoints(self) -> tuple:
"""Get networks endpoint names
:return: tuple containing endpoint names
:rtype: tuple
"""
branch_tree = "TRAFICS/TRAFIC/EXTREMITES"
end_points = self.xmltree.xpath(branch_tree)[0].getchildren()
return tuple(ep.attrib["id"] for ep in end_points)
[docs] def get_network_links(self) -> tuple:
"""Get network link names
:return: tuple containing link names
:rtype: tuple
"""
branch_tree = "TRAFICS/TRAFIC/TRONCONS"
links = self.xmltree.xpath(branch_tree)[0].getchildren()
return tuple(ep.attrib["id"] for ep in links)
[docs] def get_simulation_steps(self, simid: int = 0) -> range:
"""Get simulation steps for an simulation. specify the simulation id via an integer value
:param simid: simulation id , defaults to 0
:type simid: int, optional
:return:
:rtype: range
"""
t1 = datetime.strptime(
self.get_simulation_parameters()[simid].get("debut"), ct.HOUR_FORMAT
)
t2 = datetime.strptime(
self.get_simulation_parameters()[simid].get("fin"), ct.HOUR_FORMAT
)
t = t2 - t1
n = t.seconds / float(self.get_simulation_parameters()[simid].get("pasdetemps"))
return range(int(n))
[docs] def filename(self, encoding: str = "UTF8"):
"""
This method returns the value of encoding of the simulation scenario under consideration
:param encoding: enconder UTF8, defaults to None
:type encoding: string, optional
:return: Full path of scenario
:rtype: string
"""
if encoding == "UTF8":
return self.scn_file.encode(encoding)
return self.scn_file