def test_set_label(self): lg = LabeledGraph() lg.set_edge('v1', 'v2', 5) lg.set_edge('v2', 'v3', 10) self.assertEqual(5, lg.get_edge('v1', 'v2')) self.assertEqual(10, lg.get_edge('v2', 'v3'))
def test_remove_vertex(self): lg = LabeledGraph() lg.set_edge('t1', 's1', 2) lg.set_edge('t1', 's2', 3) lg.remove_vertex('t1') exp_vertexes = ['s1', 's2'] self.assertEqual(set(lg.vertexes()), set(exp_vertexes)) self.assertEqual(None, lg.get_edge('t1', 's1')) self.assertEqual(None, lg.get_edge('t1', 's2'))
class ExtendableMap: """ Map to save information about locations, their coordinates and lengths of paths between them. """ def __init__(self): self.clear() def clear(self): self.links = LabeledGraph() self.location_positions = {} def add_location(self, location): self.links.add_vertex(location) def get_locations(self): return self.links.vertexes() def is_location(self, location): return location in self.links.vertexes() def get_locations_linked_to(self, location): return self.links.get_successors(location) def get_distance(self, from_location, to_location): return self.links.get_edge(from_location, to_location) def add_unidirectional_link(self, from_location, to_location, distance): self.links.set_edge(from_location, to_location, distance) def add_bidirectional_link(self, from_location, to_location, distance): self.links.set_edge(from_location, to_location, distance) self.links.set_edge(to_location, from_location, distance) def remove_unidirectional_link(self, from_location, to_location): self.links.remove_edge(from_location, to_location) def remove_bidirectional_link(self, from_location, to_location): self.links.remove_edge(from_location, to_location) self.links.remove_edge(to_location, from_location) def set_position(self, location, pos): self.location_positions[location] = pos def get_position(self, location): return self.location_positions[location] def set_dist_and_dir_to_ref_location(self, location, dist, dir): coordinates = Point2D(-sin(dir * math.pi / 180) * dist, cos(dir * math.pi / 180) * dist) self.links.add_vertex(location) self.location_positions[location] = coordinates