/usr/share/sumo/tools/turn-defs/turndefinitions.py is in sumo-tools 0.15.0~dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | #!/usr/bin/python
#-*- encoding: utf8 -*-
""" Operations and classes necessary to work on SUMO turn definitions. """
import connections
import logging
import xml.dom.minidom
LOGGER = logging.getLogger(__name__)
class TurnDefinitions():
""" Represents a connection of turn definitions. """
logger = logging.getLogger(__name__)
def __init__(self):
""" Constructor. """
self.turn_definitions = {}
def add(self, source, destination, probability):
""" Adds a turn definition. If the given turn definition is already
defined (in regard to source and destination), issues
a warning. """
self.logger.debug("Adding turn definition for %s -> %s "
"with probability %f" % (source, destination, probability))
if source not in self.turn_definitions:
self.turn_definitions[source] = {}
if destination not in self.turn_definitions[source]:
self.turn_definitions[source][destination] = 0
self.turn_definitions[source][destination] += probability
if self.turn_definitions[source][destination] > 100:
self.logger.warn("Turn probability overflow: %f; lowered to 100" %
(self.turn_definitions[source][destination]))
self.turn_definitions[source][destination] = 100
def get_sources(self):
""" Returns all of the turn definitions incoming edges. The result
will be sorted in alphabetical. """
sources = self.turn_definitions.keys()
sources.sort()
return sources
def get_destinations(self, source):
""" Returns all of the turn definition's outgoing edges achievable
from given source. The turn_definition_source must
have been added before. The result will be sorted in alphabetical
order."""
destinations = self.turn_definitions[source].keys()
destinations.sort()
return destinations
def get_turning_probability(self,
source,
destination):
""" Returns the turning probability related to the given
turn definition. The source and destination must have
been added before. """
return self.turn_definitions[source][destination]
def __eq__(self, other):
""" Compares this and given object for equality. """
if other is None or other.__class__ is not TurnDefinitions:
self.logger.debug("Checking for equality with "
"non-TurnDefinitions object")
return False
return self.turn_definitions == other.turn_definitions
def from_connections(input_connections):
""" Creates a TurnDefinitions object from given Connections' object. """
LOGGER.info("Creating turn definitions")
turn_definitions = TurnDefinitions()
for source in input_connections.get_sources():
for source_lane in input_connections.get_lanes(source):
for destination in input_connections.get_destinations(source,
source_lane):
weight = input_connections.calculate_destination_weight(source,
source_lane, destination)
LOGGER.debug("Adding connection %s -> %s (%f)" %
(source, destination, weight))
turn_definitions.add(source,
destination,
weight)
return turn_definitions
def to_xml(turn_definitions):
""" Transforms the given TurnDefinitions object into a string
containing a valid SUMO turn-definitions file. """
LOGGER.info("Converting turn definitions to XML")
LOGGER.debug("Turn definitions sources number: %i" %
(len(turn_definitions.get_sources())))
turn_definitions_xml = xml.dom.minidom.Element("turn-defs")
for source in turn_definitions.get_sources():
LOGGER.debug("Converting turn definition with source %s" % (source))
from_edge_element = xml.dom.minidom.Element("fromEdge")
from_edge_element.setAttribute("id", source)
for destination in turn_definitions.get_destinations(source):
probability = turn_definitions.get_turning_probability(source,
destination)
LOGGER.debug("Converting turn definition destination %s "
"with probability %f" % (destination, probability))
to_edge_element = xml.dom.minidom.Element("toEdge")
to_edge_element.setAttribute("id", destination)
to_edge_element.setAttribute("probability", str(probability))
from_edge_element.appendChild(to_edge_element)
turn_definitions_xml.appendChild(from_edge_element)
return turn_definitions_xml.toprettyxml()
import collectinghandler
import unittest
class TurnDefinitionsTestCase(unittest.TestCase):
# pylint: disable=C,R,W,E,F
def setUp(self):
self.turn_definitions = TurnDefinitions()
def tearDown(self):
self.turn_definitions = None
def test_new_turn_definitions_is_empty(self):
self.assertEqual([], self.turn_definitions.get_sources())
def test_get_sources(self):
self.turn_definitions.add("source", "destination", 100)
self.assertEqual(["source"], self.turn_definitions.get_sources())
def test_get_sources_is_sorted(self):
self.turn_definitions.add("source 1", "destination", 100)
self.turn_definitions.add("source 2", "destination", 100)
self.assertEqual(["source 1", "source 2"],
self.turn_definitions.get_sources())
def test_get_destinations(self):
self.turn_definitions.add("source", "destination", 100)
self.assertEqual(["destination"],
self.turn_definitions.get_destinations("source"))
def test_get_destinations_is_sorted(self):
self.turn_definitions.add("source", "destination 1", 100)
self.turn_definitions.add("source", "destination 2", 100)
self.assertEqual(["destination 1", "destination 2"],
self.turn_definitions.get_destinations("source"))
def test_get_turning_probability(self):
self.turn_definitions.add("source", "destination", 100)
self.assertEqual(100,
self.turn_definitions.get_turning_probability("source",
"destination"))
def test_probability_overflow_raises_warning_on_initial_add(self):
collecting_handler = collectinghandler.CollectingHandler()
self.turn_definitions.logger.addHandler(collecting_handler)
self.turn_definitions.add("source", "destination", 101)
self.assertTrue(
"Turn probability overflow: 101.000000; lowered to 100"
in [ record.getMessage()
for record in collecting_handler.log_records ])
def test_probability_overflow_raises_warning_after_addition(self):
collecting_handler = collectinghandler.CollectingHandler()
self.turn_definitions.logger.addHandler(collecting_handler)
self.turn_definitions.add("source", "destination", 90)
self.turn_definitions.add("source", "destination", 11)
self.assertTrue(
"Turn probability overflow: 101.000000; lowered to 100"
in [ record.getMessage()
for record in collecting_handler.log_records ])
class CreateTurnDefinitionsTestCase(unittest.TestCase):
# pylint: disable=C,R,W,E,F
def test_creation_with_0_sources(self):
self.assertEqual(TurnDefinitions(),
from_connections(connections.Connections()))
def test_creation_with_one_source(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_with_two_sources(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
input_connections.add("source 2", "source lane 1", "destination 1")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0)
turn_definitions.add("source 2", "destination 1", 100.0)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_two_destinations_from_two_lanes_overlapping(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
input_connections.add("source 1", "source lane 2", "destination 2")
input_connections.add("source 1", "source lane 2", "destination 1")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0/2/2+100.0/2)
turn_definitions.add("source 1", "destination 2", 100.0/2/2)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_one_destination_from_two_lanes(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
input_connections.add("source 1", "source lane 2", "destination 1")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_with_one_destination_from_one_lane(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_with_two_destinations_from_one_lane(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
input_connections.add("source 1", "source lane 1", "destination 2")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0/2)
turn_definitions.add("source 1", "destination 2", 100.0/2)
self.assertEqual(turn_definitions,
from_connections(input_connections))
def test_creation_with_three_destinations_from_one_lane(self):
input_connections = connections.Connections()
input_connections.add("source 1", "source lane 1", "destination 1")
input_connections.add("source 1", "source lane 1", "destination 2")
input_connections.add("source 1", "source lane 1", "destination 3")
turn_definitions = TurnDefinitions()
turn_definitions.add("source 1", "destination 1", 100.0/3)
turn_definitions.add("source 1", "destination 2", 100.0/3)
turn_definitions.add("source 1", "destination 3", 100.0/3)
self.assertEqual(turn_definitions,
from_connections(input_connections))
if __name__ == "__main__":
unittest.main()
|