| 1 | package Map::Tube; | ||
| 2 | |||
| 3 | 18 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 18 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 18 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | our $VERSION = qv('v5.1.1'); | ||
| 8 | our $AUTHORITY = 'cpan:MANWAR'; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Map::Tube - Lightweight Routing Framework. | ||
| 13 | |||
| 14 | =head1 VERSION | ||
| 15 | |||
| 16 | Version v5.1.1 | ||
| 17 | |||
| 18 | =cut | ||
| 19 | |||
| 20 | 18 | ▶ | use Unicode::Normalize; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 18 | ▶ | use Encode qw(decode_utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | |||
| 23 | 18 | ▶ | use v5.14; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 0 | ▶ | use XML::Twig; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | use Data::Dumper; | ||
| 26 | use Map::Tube::Node; | ||
| 27 | use Map::Tube::Line; | ||
| 28 | use Map::Tube::Table; | ||
| 29 | use Map::Tube::Route; | ||
| 30 | use Map::Tube::Pluggable; | ||
| 31 | use Map::Tube::Exception::MissingMapData; | ||
| 32 | use Map::Tube::Exception::MissingStationName; | ||
| 33 | use Map::Tube::Exception::InvalidStationName; | ||
| 34 | use Map::Tube::Exception::MissingStationId; | ||
| 35 | use Map::Tube::Exception::InvalidStationId; | ||
| 36 | use Map::Tube::Exception::InvalidStationLineId; | ||
| 37 | use Map::Tube::Exception::MissingLineId; | ||
| 38 | use Map::Tube::Exception::InvalidLineId; | ||
| 39 | use Map::Tube::Exception::MissingLineName; | ||
| 40 | use Map::Tube::Exception::InvalidLineName; | ||
| 41 | use Map::Tube::Exception::InvalidLineColor; | ||
| 42 | use Map::Tube::Exception::FoundMultiLinedStation; | ||
| 43 | use Map::Tube::Exception::FoundMultiLinkedStation; | ||
| 44 | use Map::Tube::Exception::FoundSelfLinkedStation; | ||
| 45 | use Map::Tube::Exception::DuplicateStationId; | ||
| 46 | use Map::Tube::Exception::DuplicateStationName; | ||
| 47 | use Map::Tube::Exception::MissingPluginGraph; | ||
| 48 | use Map::Tube::Exception::MissingPluginFormatter; | ||
| 49 | use Map::Tube::Exception::MissingPluginFuzzyFind; | ||
| 50 | use Map::Tube::Exception::MalformedMapData; | ||
| 51 | use Map::Tube::Exception::InvalidLineStructure; | ||
| 52 | use Map::Tube::Exception::InvalidStationStructure; | ||
| 53 | use Map::Tube::Exception::RouteNotFound; | ||
| 54 | use Map::Tube::Utils qw(to_perl is_same trim common_lines get_method_map is_valid_color); | ||
| 55 | use Map::Tube::Types qw(Routes Tables Lines NodeMap LineMap Color); | ||
| 56 | |||
| 57 | use Moo::Role; | ||
| 58 | use Role::Tiny qw(); | ||
| 59 | use Types::Common::Numeric qw(PositiveOrZeroNum); | ||
| 60 | use namespace::autoclean; | ||
| 61 | |||
| 62 | =encoding utf8 | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | The core module defined as a Role (Moo) to process the map data. It provides the | ||
| 67 | interface to find the shortest route in terms of stoppage between two nodes. Also | ||
| 68 | you can get all possible routes between two given nodes. | ||
| 69 | |||
| 70 | The routing algorithm is line-change-aware: when two candidate routes have the | ||
| 71 | same number of stops, the one requiring fewer line changes is preferred. Each | ||
| 72 | line change incurs a small fractional penalty (0.5) on top of the normal per-hop | ||
| 73 | cost of 1, so hop count always dominates but unnecessary changes are avoided. | ||
| 74 | |||
| 75 | If you are keen to know the internals of L<Map::Tube> then please follow the note | ||
| 76 | documented in L<Map::Tube::Cookbook>. | ||
| 77 | |||
| 78 | =head1 MAP LEADER BOARD | ||
| 79 | |||
| 80 | +----------------------+-----------+------------------------------------------+ | ||
| 81 | | Author | PAUSE ID | Map Count (City) | | ||
| 82 | +----------------------+-----------+------------------------------------------+ | ||
| 83 | | Michal Josef Spacek | SKIM | 22 (Bucharest, Budapest, Dnipropetrovsk, | | ||
| 84 | | | | Kazan, Kharkiv, Kiev, KualaLumpur, | | ||
| 85 | | | | Malaga, Minsk, Moscow, Nanjing, | | ||
| 86 | | | | NizhnyNovgorod, Novosibirsk, Prague, | | ||
| 87 | | | | SaintPetersburg, Samara, Singapore, | | ||
| 88 | | | | Sofia, Tbilisi, Vienna, Warsaw, | | ||
| 89 | | | | Yekaterinburg) | | ||
| 90 | | | | | | ||
| 91 | | Gisbert W Selke | GWS | 17 (Beijing, Brussels, Chicago, Glasgow, | | ||
| 92 | | | | Hamburg, Hannover, KoelnBonn, Lyon, | | ||
| 93 | | | | Muenchen, Napoli, Oslo, Paris, RheinRuhr,| | ||
| 94 | | | | San Francisco, Stockholm, Stuttgart, | | ||
| 95 | | | | Toulouse) | | ||
| 96 | | | | | | ||
| 97 | | Mohammad Sajid Anwar | MANWAR | 8 (Barcelona, Delhi, Kolkatta, Leipzig, | | ||
| 98 | | | | London, Madrid, NYC, Tokyo) | | ||
| 99 | | | | | | ||
| 100 | | FUNG Cheok Yin | CYFUNG | 1 (Hong Kong) | | ||
| 101 | | | | | | ||
| 102 | | Peter Harrison | EARLYBEAN | 1 (Sydney) | | ||
| 103 | | | | | | ||
| 104 | | Errietta Kostala | ERRIETTA | 1 (Athens) | | ||
| 105 | | | | | | ||
| 106 | | Giuseppe Di Terlizzi | GDT | 1 (Rome) | | ||
| 107 | | | | | | ||
| 108 | | Marco Fontani | MFONTANI | 1 (Milan) | | ||
| 109 | | | | | | ||
| 110 | | Renee Baecker | RENEEB | 1 (Frankfurt) | | ||
| 111 | | | | | | ||
| 112 | | Soren Lund | SLU | 1 (Copenhagen) | | ||
| 113 | | | | | | ||
| 114 | | Slaven Rezic | SREZIC | 1 (Berlin) | | ||
| 115 | | | | | | ||
| 116 | | Stefan Limbacher | STELIM | 1 (Nuremberg) | | ||
| 117 | | | | | | ||
| 118 | | Vitali Peil | VPEIL | 1 (Bielefeld) | | ||
| 119 | +----------------------+-----------+------------------------------------------+ | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | has [qw(name name_to_id plugins _active_link _other_links _link_lines _line_stations _line_station_index _common_lines)] => (is => 'rw'); | ||
| 124 | has experimental => (is => 'ro', default => sub { 0 }); | ||
| 125 | has nodes => (is => 'rw', isa => NodeMap); | ||
| 126 | has lines => (is => 'rw', isa => Lines ); | ||
| 127 | has tables => (is => 'rw', isa => Tables ); | ||
| 128 | has routes => (is => 'rw', isa => Routes ); | ||
| 129 | has _lines => (is => 'rw', isa => LineMap); | ||
| 130 | has bgcolor => (is => 'rw', isa => Color ); | ||
| 131 | has line_change_penalty => (is => 'rw', isa => PositiveOrZeroNum, default => 0.5 ); | ||
| 132 | |||
| 133 | our $AUTOLOAD; | ||
| 134 | our $PLUGINS = { | ||
| 135 | 'Map::Tube::Plugin::Graph' => 1, | ||
| 136 | 'Map::Tube::Plugin::FuzzyFind' => 1, | ||
| 137 | }; | ||
| 138 | |||
| 139 | sub AUTOLOAD { | ||
| 140 | |||
| 141 | my $name = $AUTOLOAD; | ||
| 142 | $name =~ s/.*://; | ||
| 143 | |||
| 144 | my @caller = caller(0); | ||
| 145 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 146 | |||
| 147 | my $method_map = get_method_map(); | ||
| 148 | if (exists $method_map->{$name}) { | ||
| 149 | my $module = $method_map->{$name}->{module}; | ||
| 150 | my $exception = $method_map->{$name}->{exception}; | ||
| 151 | $exception->throw({ | ||
| 152 | method => "${module}::${name}", | ||
| 153 | message => "ERROR: Missing plugin $module.", | ||
| 154 | filename => $caller[1], | ||
| 155 | line_number => $caller[2] }); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | sub BUILD { | ||
| 160 | my ($self) = @_; | ||
| 161 | |||
| 162 | # Handle lazy attributes. | ||
| 163 | my @attributes = (keys %{Moo->_constructor_maker_for(ref($self))->all_attribute_specs}); | ||
| 164 | unless ((grep /^xml$/, @attributes) || (grep /^json$/, @attributes)) { | ||
| 165 | die "ERROR: Can't apply Map::Tube role, missing 'xml' or 'json'."; | ||
| 166 | } | ||
| 167 | |||
| 168 | $self->_init_map; | ||
| 169 | $self->_load_plugins; | ||
| 170 | } | ||
| 171 | |||
| 172 | =head1 SYNOPSIS | ||
| 173 | |||
| 174 | =head2 Common Usage | ||
| 175 | |||
| 176 | use strict; use warnings; | ||
| 177 | use Map::Tube::London; | ||
| 178 | |||
| 179 | my $map = Map::Tube::London->new; | ||
| 180 | print $map->get_shortest_route('Baker Street', 'Euston Square'), "\n"; | ||
| 181 | |||
| 182 | You should expect the result like below: | ||
| 183 | |||
| 184 | Baker Street (Circle, Hammersmith & City, Bakerloo, Metropolitan, Jubilee), Great Portland Street (Circle, Hammersmith & City, Metropolitan), Euston Square (Circle, Hammersmith & City, Metropolitan) | ||
| 185 | |||
| 186 | Alternatively, | ||
| 187 | |||
| 188 | use strict; use warnings; | ||
| 189 | use Map::Tube::London; | ||
| 190 | |||
| 191 | my $map = Map::Tube::London->new; | ||
| 192 | print $map->get_shortest_route('Baker Street', 'Euston Square')->preferred, "\n"; | ||
| 193 | |||
| 194 | You should now expect the result like below: | ||
| 195 | |||
| 196 | Baker Street (Circle, Hammersmith & City, Metropolitan), Great Portland Street (Circle, Hammersmith & City, Metropolitan), Euston Square (Circle, Hammersmith & City, Metropolitan) | ||
| 197 | |||
| 198 | =head1 METHODS | ||
| 199 | |||
| 200 | =head2 get_shortest_routes($from, $to) | ||
| 201 | |||
| 202 | This requires C<$from> and C<$to> station names parameters. It returns an object | ||
| 203 | of type L<Map::Tube::Route>. On error it throws an exception of type L<Map::Tube::Exception>. | ||
| 204 | |||
| 205 | =cut | ||
| 206 | |||
| 207 | sub get_shortest_route { | ||
| 208 | my ($self, $from, $to) = @_; | ||
| 209 | |||
| 210 | ($from, $to) = | ||
| 211 | $self->_validate_input('get_shortest_route', $from, $to); | ||
| 212 | |||
| 213 | my $_from = $self->get_node_by_id($from); | ||
| 214 | my $_to = $self->get_node_by_id($to); | ||
| 215 | |||
| 216 | $self->_capture_common_lines($_from, $_to); | ||
| 217 | |||
| 218 | my $reverse = 0; | ||
| 219 | |||
| 220 | # Found common lines between start and end nodes. | ||
| 221 | if (@{$self->{_common_lines}}) { | ||
| 222 | my $_common_line = $self->{_common_lines}->[0]; | ||
| 223 | my $from_index = $self->{_line_station_index} | ||
| 224 | ->{uc($_common_line)} | ||
| 225 | ->{$_from->id}; | ||
| 226 | my $to_index = $self->{_line_station_index} | ||
| 227 | ->{uc($_common_line)} | ||
| 228 | ->{$_to->id}; | ||
| 229 | |||
| 230 | # TODO: This cause issue with unidirection route with station index. | ||
| 231 | # $reverse = 1 if (defined $from_index | ||
| 232 | # && defined $to_index | ||
| 233 | # && ($from_index < $to_index)); | ||
| 234 | } | ||
| 235 | |||
| 236 | if ($reverse) { | ||
| 237 | ($from, $to) = ($to, $from); | ||
| 238 | ($_from, $_to) = ($_to, $_from); | ||
| 239 | } | ||
| 240 | |||
| 241 | $self->_get_shortest_route($from); | ||
| 242 | |||
| 243 | my $nodes = []; | ||
| 244 | while (defined($to) && !(is_same($from, $to))) { | ||
| 245 | push @$nodes, $self->get_node_by_id($to); | ||
| 246 | $to = $self->_get_path($to); | ||
| 247 | } | ||
| 248 | |||
| 249 | push @$nodes, $_from; | ||
| 250 | |||
| 251 | my $_nodes; | ||
| 252 | |||
| 253 | if ($reverse) { | ||
| 254 | $_nodes = [ @$nodes ]; | ||
| 255 | } | ||
| 256 | else { | ||
| 257 | $_nodes = [ reverse(@$nodes) ]; | ||
| 258 | } | ||
| 259 | |||
| 260 | my $start = $_nodes->[0]; | ||
| 261 | my $end = $_nodes->[-1]; | ||
| 262 | |||
| 263 | # If you end up with just 2 nodes i.e. start and end, then | ||
| 264 | # double check, if start is directly linked to end node. | ||
| 265 | if (scalar(@$_nodes) == 2) { | ||
| 266 | my $start_name = $start->name; | ||
| 267 | my $end_name = $end->name; | ||
| 268 | unless ($self->_is_directly_linked($start_name, $end_name)) { | ||
| 269 | my @caller = caller(0); | ||
| 270 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 271 | Map::Tube::Exception::RouteNotFound->throw({ | ||
| 272 | method => __PACKAGE__."::get_shortest_route", | ||
| 273 | message => "ERROR: Route not found from [$start_name] to [$end_name].", | ||
| 274 | filename => $caller[1], | ||
| 275 | line_number => $caller[2] }); | ||
| 276 | } | ||
| 277 | } | ||
| 278 | |||
| 279 | return Map::Tube::Route->new( | ||
| 280 | { from => $start, | ||
| 281 | to => $end, | ||
| 282 | nodes => $_nodes, | ||
| 283 | link_lines => $self->_link_lines, | ||
| 284 | } | ||
| 285 | ); | ||
| 286 | } | ||
| 287 | |||
| 288 | =head2 get_all_routes($from, $to) *** EXPERIMENTAL *** | ||
| 289 | |||
| 290 | This requires C<$from> and C<$to> station names as parameters. It returns a ref to a | ||
| 291 | list of objects of type L<Map::Tube::Route>. On error it throws an exception of type | ||
| 292 | L<Map::Tube::Exception>. | ||
| 293 | |||
| 294 | Be careful when using this against a large map. You may encounter a warning similar to | ||
| 295 | this one when running against London map: | ||
| 296 | |||
| 297 | Deep recursion on subroutine "Map::Tube::_get_all_routes" | ||
| 298 | |||
| 299 | However, for comparatively smaller maps, like below, it is happy to give all routes. | ||
| 300 | |||
| 301 | A(1) ---- B(2) | ||
| 302 | / \ | ||
| 303 | C(3) -------- F(6) --- G(7) ---- H(8) | ||
| 304 | \ / | ||
| 305 | D(4) ---- E(5) | ||
| 306 | |||
| 307 | =cut | ||
| 308 | |||
| 309 | sub get_all_routes { | ||
| 310 | my ($self, $from, $to) = @_; | ||
| 311 | |||
| 312 | ($from, $to) = | ||
| 313 | $self->_validate_input('get_all_routes', $from, $to); | ||
| 314 | |||
| 315 | return $self->_get_all_routes([ $from ], $to); | ||
| 316 | } | ||
| 317 | |||
| 318 | =head2 name() | ||
| 319 | |||
| 320 | Returns the map name. | ||
| 321 | |||
| 322 | =head2 get_node_by_id($node_id) | ||
| 323 | |||
| 324 | Returns an object of type L<Map::Tube::Node> for a given C<$node_id>. | ||
| 325 | |||
| 326 | =cut | ||
| 327 | |||
| 328 | sub get_node_by_id { | ||
| 329 | my ($self, $id) = @_; | ||
| 330 | |||
| 331 | my @caller = caller(0); | ||
| 332 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 333 | Map::Tube::Exception::MissingStationId->throw({ | ||
| 334 | method => __PACKAGE__."::get_node_by_id", | ||
| 335 | message => "ERROR: Missing Station ID.", | ||
| 336 | filename => $caller[1], | ||
| 337 | line_number => $caller[2] }) unless defined $id; | ||
| 338 | |||
| 339 | my $node = $self->{nodes}->{$id}; | ||
| 340 | Map::Tube::Exception::InvalidStationId->throw({ | ||
| 341 | method => __PACKAGE__."::get_node_by_id", | ||
| 342 | message => "ERROR: Invalid Station ID [$id].", | ||
| 343 | filename => $caller[1], | ||
| 344 | line_number => $caller[2] }) unless defined $node; | ||
| 345 | |||
| 346 | # Check if the node name appears more than once with different id. | ||
| 347 | my @nodes = $self->_get_node_id($node->name); | ||
| 348 | return $node if (scalar(@nodes) == 1); | ||
| 349 | |||
| 350 | my $lines = {}; | ||
| 351 | foreach my $l (@{$node->line}) { | ||
| 352 | $lines->{$l->name} = $l if defined $l->name; | ||
| 353 | } | ||
| 354 | foreach my $i (@nodes) { | ||
| 355 | foreach my $j (@{$self->{nodes}->{$i}->line}) { | ||
| 356 | $lines->{$j->name} = $j if defined $j->name; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | $node->line([ values %$lines ]); | ||
| 360 | |||
| 361 | return $node; | ||
| 362 | } | ||
| 363 | |||
| 364 | =head2 get_node_by_name($node_name) | ||
| 365 | |||
| 366 | Returns an object of type L<Map::Tube::Node> for a given C<$node_name>. | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub get_node_by_name { | ||
| 371 | my ($self, $name) = @_; | ||
| 372 | |||
| 373 | my @caller = caller(0); | ||
| 374 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 375 | Map::Tube::Exception::MissingStationName->throw({ | ||
| 376 | method => __PACKAGE__."::get_node_by_name", | ||
| 377 | message => "ERROR: Missing Station Name.", | ||
| 378 | filename => $caller[1], | ||
| 379 | line_number => $caller[2] }) unless defined $name; | ||
| 380 | |||
| 381 | my $id = $self->_get_node_id($name); | ||
| 382 | Map::Tube::Exception::InvalidStationName->throw({ | ||
| 383 | method => __PACKAGE__."::get_node_by_name", | ||
| 384 | message => "ERROR: Invalid Station Name [$name].", | ||
| 385 | filename => $caller[1], | ||
| 386 | line_number => $caller[2] }) unless defined $id; | ||
| 387 | |||
| 388 | return $self->get_node_by_id($id); | ||
| 389 | } | ||
| 390 | |||
| 391 | =head2 get_line_by_id($line_id) | ||
| 392 | |||
| 393 | Returns an object of type L<Map::Tube::Line>. | ||
| 394 | |||
| 395 | =cut | ||
| 396 | |||
| 397 | sub get_line_by_id { | ||
| 398 | my ($self, $id) = @_; | ||
| 399 | |||
| 400 | my @caller = caller(0); | ||
| 401 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 402 | Map::Tube::Exception::MissingLineId->throw({ | ||
| 403 | method => __PACKAGE__."::get_line_by_id", | ||
| 404 | message => "ERROR: Missing Line ID.", | ||
| 405 | filename => $caller[1], | ||
| 406 | line_number => $caller[2] }) unless defined $id; | ||
| 407 | |||
| 408 | my $line = $self->_get_line_object_by_id($id); | ||
| 409 | Map::Tube::Exception::InvalidLineId->throw({ | ||
| 410 | method => __PACKAGE__."::get_line_by_id", | ||
| 411 | message => "ERROR: Invalid Line ID [$id].", | ||
| 412 | filename => $caller[1], | ||
| 413 | line_number => $caller[2] }) unless defined $line; | ||
| 414 | |||
| 415 | return $line; | ||
| 416 | } | ||
| 417 | |||
| 418 | =head2 get_line_by_name($line_name) | ||
| 419 | |||
| 420 | Returns an object of type L<Map::Tube::Line>. | ||
| 421 | |||
| 422 | =cut | ||
| 423 | |||
| 424 | sub get_line_by_name { | ||
| 425 | my ($self, $name) = @_; | ||
| 426 | |||
| 427 | my @caller = caller(0); | ||
| 428 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 429 | Map::Tube::Exception::MissingLineName->throw({ | ||
| 430 | method => __PACKAGE__."::get_line_by_name", | ||
| 431 | message => "ERROR: Missing Line Name.", | ||
| 432 | filename => $caller[1], | ||
| 433 | line_number => $caller[2] }) unless defined $name; | ||
| 434 | |||
| 435 | my $line = $self->_get_line_object_by_name($name); | ||
| 436 | $name = _decode_utf8_if_needed($name); | ||
| 437 | Map::Tube::Exception::InvalidLineName->throw({ | ||
| 438 | method => __PACKAGE__."::get_line_by_name", | ||
| 439 | message => "ERROR: Invalid Line Name [$name].", | ||
| 440 | filename => $caller[1], | ||
| 441 | line_number => $caller[2] }) unless defined $line; | ||
| 442 | |||
| 443 | return $line; | ||
| 444 | } | ||
| 445 | |||
| 446 | =head2 get_lines() | ||
| 447 | |||
| 448 | Returns a ref to a list of objects of type L<Map::Tube::Line>, containing | ||
| 449 | all lines in the map (but excluding "other links"). | ||
| 450 | |||
| 451 | =cut | ||
| 452 | |||
| 453 | sub get_lines { | ||
| 454 | my ($self) = @_; | ||
| 455 | |||
| 456 | my $lines = []; | ||
| 457 | my $other_links = $self->_other_links; | ||
| 458 | foreach (@{$self->{lines}}) { | ||
| 459 | next if exists $other_links->{uc($_->id)}; | ||
| 460 | push @$lines, $_ if defined $_->name; | ||
| 461 | } | ||
| 462 | |||
| 463 | return $lines; | ||
| 464 | } | ||
| 465 | |||
| 466 | =head2 get_stations($line_name) | ||
| 467 | |||
| 468 | Returns a ref to a list of objects of type L<Map::Tube::Node> for the line called C<$line_name>. | ||
| 469 | If C<$line_name> is missing, it will return all stations in the map. | ||
| 470 | |||
| 471 | =cut | ||
| 472 | |||
| 473 | sub get_stations { | ||
| 474 | my ($self, $line_name) = @_; | ||
| 475 | |||
| 476 | my $lines = []; | ||
| 477 | my $stations = []; | ||
| 478 | my $seen = {}; | ||
| 479 | |||
| 480 | if (defined $line_name) { | ||
| 481 | my @caller = caller(0); | ||
| 482 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 483 | |||
| 484 | my $line = $self->_get_line_object_by_name($line_name); | ||
| 485 | Map::Tube::Exception::InvalidLineName->throw({ | ||
| 486 | method => __PACKAGE__."::get_stations", | ||
| 487 | message => "ERROR: Invalid Line Name [$line_name].", | ||
| 488 | filename => $caller[1], | ||
| 489 | line_number => $caller[2] }) | ||
| 490 | unless defined $line; | ||
| 491 | |||
| 492 | $lines = [ $self->_get_line_object_by_name($line_name) ]; | ||
| 493 | } | ||
| 494 | else { | ||
| 495 | $lines = $self->get_lines; | ||
| 496 | } | ||
| 497 | |||
| 498 | foreach my $line (@$lines) { | ||
| 499 | foreach my $station (@{$line->{stations}}) { | ||
| 500 | unless (exists $seen->{$station->id}) { | ||
| 501 | push @$stations, $self->get_node_by_id($station->id); | ||
| 502 | $seen->{$station->id} = 1; | ||
| 503 | } | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | return $stations; | ||
| 508 | } | ||
| 509 | |||
| 510 | =head2 get_next_stations($station_name) | ||
| 511 | |||
| 512 | Returns a ref to the list of next stations from the given C<$station_name> as objects | ||
| 513 | of type L<Map::Tube::Node>. | ||
| 514 | |||
| 515 | =cut | ||
| 516 | |||
| 517 | sub get_next_stations { | ||
| 518 | my ($self, $station_name, $line_name) = @_; | ||
| 519 | |||
| 520 | my @caller = caller(0); | ||
| 521 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 522 | Map::Tube::Exception::MissingStationName->throw({ | ||
| 523 | method => __PACKAGE__."::get_next_stations", | ||
| 524 | message => "ERROR: Missing Station Name.", | ||
| 525 | filename => $caller[1], | ||
| 526 | line_number => $caller[2] }) unless defined $station_name; | ||
| 527 | |||
| 528 | my $node_id = $self->_get_node_id($station_name); | ||
| 529 | Map::Tube::Exception::InvalidStationName->throw({ | ||
| 530 | method => __PACKAGE__."::get_next_stations", | ||
| 531 | message => "ERROR: Invalid Station Name [$station_name].", | ||
| 532 | filename => $caller[1], | ||
| 533 | line_number => $caller[2] }) unless defined $node_id; | ||
| 534 | |||
| 535 | # Optional: restrict results to stations reachable from this one on a | ||
| 536 | # specific line. This takes any per-edge line restriction (set via the | ||
| 537 | # "A:x" style link syntax) into account, so a link that physically only | ||
| 538 | # runs on line x will correctly be excluded when asking about line y, | ||
| 539 | # even though both stations are nominally on both lines. | ||
| 540 | my $_line_id; | ||
| 541 | if (defined $line_name) { | ||
| 542 | my $_line = $self->_get_line_object_by_name($line_name); | ||
| 543 | Map::Tube::Exception::InvalidLineName->throw({ | ||
| 544 | method => __PACKAGE__."::get_next_stations", | ||
| 545 | message => "ERROR: Invalid Line Name [$line_name].", | ||
| 546 | filename => $caller[1], | ||
| 547 | line_number => $caller[2] }) unless defined $_line; | ||
| 548 | $_line_id = uc($_line->id); | ||
| 549 | } | ||
| 550 | |||
| 551 | my $nodes = []; | ||
| 552 | my $node = $self->get_node_by_id($node_id); | ||
| 553 | |||
| 554 | if (defined $_line_id) { | ||
| 555 | my %_node_lines = map { uc($_->{id}) => 1 } @{$node->{line}}; | ||
| 556 | # If the starting station isn't even served by this line, it has | ||
| 557 | # no neighbors on it, full stop. | ||
| 558 | return $nodes unless $_node_lines{$_line_id}; | ||
| 559 | } | ||
| 560 | |||
| 561 | foreach my $link (split /,/,$node->{link}) { | ||
| 562 | my $_next_node = $self->get_node_by_id($link); | ||
| 563 | |||
| 564 | if (defined $_line_id) { | ||
| 565 | my %_next_lines = map { uc($_->{id}) => 1 } @{$_next_node->{line}}; | ||
| 566 | |||
| 567 | my $_node_restrictions = $self->{_link_lines}->{uc($node_id)}; | ||
| 568 | my $_restriction = defined($_node_restrictions) ? $_node_restrictions->{uc($link)} : undef; | ||
| 569 | if (defined $_restriction) { | ||
| 570 | my %_allowed = map { uc($_) => 1 } @$_restriction; | ||
| 571 | next unless $_allowed{$_line_id}; | ||
| 572 | } | ||
| 573 | next unless $_next_lines{$_line_id}; | ||
| 574 | } | ||
| 575 | |||
| 576 | push @$nodes, $_next_node; | ||
| 577 | } | ||
| 578 | |||
| 579 | return $nodes; | ||
| 580 | } | ||
| 581 | |||
| 582 | =head2 get_linked_stations($station_name, $line_name) | ||
| 583 | |||
| 584 | Returns a ref to the list of the names of the linked stations from the given | ||
| 585 | C<$station_name> as strings. | ||
| 586 | |||
| 587 | The C<$line_name> param is optional. If passed, only stations linked via that | ||
| 588 | specific line are returned -- this respects any per-edge line restriction set | ||
| 589 | via the "A:x" style link syntax, so a link that only physically runs on one | ||
| 590 | line will not be listed as reachable via another line the two stations | ||
| 591 | otherwise share. | ||
| 592 | |||
| 593 | =cut | ||
| 594 | |||
| 595 | sub get_linked_stations { | ||
| 596 | my ($self, $station_name, $line_name) = @_; | ||
| 597 | |||
| 598 | my $nodes = $self->get_next_stations($station_name, $line_name); | ||
| 599 | my $linked_stations = []; | ||
| 600 | foreach my $node (@$nodes) { | ||
| 601 | push @$linked_stations, $node->name; | ||
| 602 | } | ||
| 603 | |||
| 604 | return $linked_stations; | ||
| 605 | } | ||
| 606 | |||
| 607 | =head2 bgcolor($color) | ||
| 608 | |||
| 609 | Set the background color for the map. Returns the new value. If called | ||
| 610 | without a value, returns the unchanged previous value. Setting this is | ||
| 611 | optional. Please set it before making a call to method | ||
| 612 | L<Map::Tube::Plugin::Graph/"as_image($line_name)">. If not set, it will | ||
| 613 | try to guess and may not be as good as you would expect.The C<$color> | ||
| 614 | can be a simply a color name or hash code (C<#RRGGBB>). | ||
| 615 | |||
| 616 | =head2 line_change_penalty($penalty) | ||
| 617 | |||
| 618 | When calculating shortest routes, each change from one line to another incurs | ||
| 619 | a penalty which is added to the number of hops on the route. The default penalty | ||
| 620 | is 0.5. This method can be used to query the current value or to set it to a new | ||
| 621 | value, which must be at least 0. (Values of 1 or higher probably do not make much | ||
| 622 | sense, since the number of hops should be the dominating factor.) If you just care | ||
| 623 | about the nnumber of hops but not for changes, set it to 0. | ||
| 624 | |||
| 625 | =cut | ||
| 626 | |||
| 627 | # | ||
| 628 | # | ||
| 629 | # DO NOT MAKE IT PUBLIC | ||
| 630 | |||
| 631 | sub get_map_data { | ||
| 632 | my ($self, $caller, $method) = @_; | ||
| 633 | |||
| 634 | my $data; | ||
| 635 | my $xml = $self->xml; | ||
| 636 | if ($xml ne '') { | ||
| 637 | eval { | ||
| 638 | $data = XML::Twig->new->parsefile($xml)->simplify(keyattr => 'stations', forcearray => 0); | ||
| 639 | # Handle if there is only one line. | ||
| 640 | my $lines = $data->{lines}->{line}; | ||
| 641 | if (ref($lines) eq 'HASH') { | ||
| 642 | $data->{lines}->{line} = [ $lines ]; | ||
| 643 | } | ||
| 644 | }; | ||
| 645 | unless ($@) { | ||
| 646 | $self->_validate_map_structure($data, $caller); | ||
| 647 | return $data; | ||
| 648 | } | ||
| 649 | |||
| 650 | $@ =~ s/\s+at\s+\S*?\.pm\s+line\s+.*$//; | ||
| 651 | Map::Tube::Exception::MalformedMapData->throw({ | ||
| 652 | method => $method, | ||
| 653 | message => "ERROR: Malformed Map Data ($xml): $@", | ||
| 654 | filename => $caller->[1], | ||
| 655 | line_number => $caller->[2] }); | ||
| 656 | } | ||
| 657 | else { | ||
| 658 | my $json = $self->json; | ||
| 659 | if ($json ne '') { | ||
| 660 | eval { $data = to_perl($json) }; | ||
| 661 | unless ($@) { | ||
| 662 | $self->_validate_map_structure($data, $caller); | ||
| 663 | return $data; | ||
| 664 | } | ||
| 665 | |||
| 666 | $@ =~ s/\s+at\s+\S*?\.pm\s+line\s+.*$//; | ||
| 667 | Map::Tube::Exception::MalformedMapData->throw({ | ||
| 668 | method => $method, | ||
| 669 | message => "ERROR: Malformed Map Data ($json): $@", | ||
| 670 | filename => $caller->[1], | ||
| 671 | line_number => $caller->[2] }); | ||
| 672 | } | ||
| 673 | else { | ||
| 674 | if (!defined $caller) { | ||
| 675 | $method = __PACKAGE__.'::get_map_data'; | ||
| 676 | my @_caller = caller(0); | ||
| 677 | @_caller = caller(2) if $_caller[3] eq '(eval)'; | ||
| 678 | $caller = \@_caller; | ||
| 679 | } | ||
| 680 | |||
| 681 | Map::Tube::Exception::MissingMapData->throw({ | ||
| 682 | method => $method, | ||
| 683 | message => "ERROR: Missing Map Data.", | ||
| 684 | filename => $caller->[1], | ||
| 685 | line_number => $caller->[2] }); | ||
| 686 | } | ||
| 687 | } | ||
| 688 | } | ||
| 689 | |||
| 690 | =head1 PLUGINS | ||
| 691 | |||
| 692 | =head2 L<Map::Tube::Plugin::Graph> | ||
| 693 | |||
| 694 | The L<Map::Tube::Plugin::Graph> plugin adds support to generate the entire map | ||
| 695 | or a map for a particular line as a base64 encoded string of a PNG image. | ||
| 696 | For C<Map::Tube> v3.54 or above, you can set the background color explicitly. | ||
| 697 | |||
| 698 | use strict; use warnings; | ||
| 699 | use MIME::Base64; | ||
| 700 | use Map::Tube::London; | ||
| 701 | |||
| 702 | my $tube = Map::Tube::London->new; | ||
| 703 | |||
| 704 | # Optionally, you can override the default background color. | ||
| 705 | $tube->bgcolor("gray"); | ||
| 706 | |||
| 707 | # Entire map image | ||
| 708 | my $name = $tube->name; | ||
| 709 | open(my $MAP_IMAGE, ">", "$name.png") | ||
| 710 | or die "ERROR: Can't open [$name.png]: $!"; | ||
| 711 | binmode($MAP_IMAGE); | ||
| 712 | print $MAP_IMAGE decode_base64($tube->as_image); | ||
| 713 | close($MAP_IMAGE); | ||
| 714 | |||
| 715 | # Just a particular line map image | ||
| 716 | my $line = 'Bakerloo'; | ||
| 717 | open(my $LINE_IMAGE, ">", "$line.png") | ||
| 718 | or die "ERROR: Can't open [$line.png]: $!"; | ||
| 719 | binmode($LINE_IMAGE); | ||
| 720 | print $LINE_IMAGE decode_base64($tube->as_image($line)); | ||
| 721 | close($LINE_IMAGE); | ||
| 722 | |||
| 723 | Please refer to the L<documentation|Map::Tube::Plugin::Graph> for more details. | ||
| 724 | |||
| 725 | =head2 L<Map::Tube::Plugin::Formatter> | ||
| 726 | |||
| 727 | The L<Map::Tube::Plugin::Formatter> plugin adds support to format objects | ||
| 728 | supported by the plugin. | ||
| 729 | |||
| 730 | use strict; use warnings; | ||
| 731 | use Map::Tube::London; | ||
| 732 | |||
| 733 | my $tube = Map::Tube::London->new; | ||
| 734 | |||
| 735 | my $node = $tube->get_node_by_name('Baker Street'); | ||
| 736 | print $node->to_xml, "\n\n"; | ||
| 737 | print $node->to_json, "\n\n"; | ||
| 738 | print $node->to_yaml, "\n\n"; | ||
| 739 | print $node->to_string, "\n\n"; | ||
| 740 | |||
| 741 | my $line = $tube->get_line_by_name('Metropolitan'); | ||
| 742 | print $line->to_xml, "\n\n"; | ||
| 743 | print $line->to_json, "\n\n"; | ||
| 744 | print $line->to_yaml, "\n\n"; | ||
| 745 | print $line->to_string, "\n\n"; | ||
| 746 | |||
| 747 | my $route = $tube->get_shortest_route('Baker Street', 'Wembley Park'); | ||
| 748 | print $route->to_xml, "\n\n"; | ||
| 749 | print $route->to_json, "\n\n"; | ||
| 750 | print $route->to_yaml, "\n\n"; | ||
| 751 | print $route->to_string,"\n\n"; | ||
| 752 | |||
| 753 | Please refer to the L<documentation|Map::Tube::Plugin::Formatter> for more info. | ||
| 754 | |||
| 755 | =head2 L<Map::Tube::Plugin::FuzzyFind> | ||
| 756 | |||
| 757 | Gisbert W. Selke built the add-on for L<Map::Tube> to find stations and lines by | ||
| 758 | name, possibly partly or inexactly specified. The module is a Moo role which gets | ||
| 759 | plugged into the Map::Tube::* family automatically once it is installed. | ||
| 760 | |||
| 761 | use strict; use warnings; | ||
| 762 | use Map::Tube::London; | ||
| 763 | |||
| 764 | my $tube = Map::Tube::London->new(); | ||
| 765 | |||
| 766 | print 'line matches exactly: ', scalar($tube->fuzzy_find(search => 'erloo', objects => 'lines')), "\n"; | ||
| 767 | print 'line contains : ', scalar($tube->fuzzy_find(search => 'erloo', objects => 'lines', method => 'in')), "\n"; | ||
| 768 | |||
| 769 | Please refer to the L<documentation|Map::Tube::Plugin::FuzzyFind> for more info. | ||
| 770 | |||
| 771 | =head1 MAP DATA FORMAT | ||
| 772 | |||
| 773 | Map data can be represented in JSON or XML format. The preferred format is JSON. | ||
| 774 | C<Map::Tube v3.23> or above comes with a handy script C<map-data-converter>, that | ||
| 775 | can be used to change the data format of an existing map data.Below is how we can | ||
| 776 | represent the sample map: | ||
| 777 | |||
| 778 | A(1) ---- B(2) | ||
| 779 | / \ | ||
| 780 | C(3) -------- F(6) --- G(7) ---- H(8) | ||
| 781 | \ / | ||
| 782 | D(4) ---- E(5) | ||
| 783 | |||
| 784 | =head2 JSON | ||
| 785 | |||
| 786 | { | ||
| 787 | "name" : "sample map", | ||
| 788 | "lines" : { | ||
| 789 | "line" : [ | ||
| 790 | { "id" : "A", "name" : "A", "color" : "red" }, | ||
| 791 | { "id" : "B", "name" : "B", "color" : "#FFFF00" } | ||
| 792 | ] | ||
| 793 | }, | ||
| 794 | "stations" : { | ||
| 795 | "station" : [ | ||
| 796 | { "id" : "A1", "name" : "A1", "line" : "A", "link" : "B2,C3" }, | ||
| 797 | { "id" : "B2", "name" : "B2", "line" : "A", "link" : "A1,F6" }, | ||
| 798 | { "id" : "C3", "name" : "C3", "line" : "A,B", "link" : "A1,D4,F6" }, | ||
| 799 | { "id" : "D4", "name" : "D4", "line" : "A,B", "link" : "C3,E5" }, | ||
| 800 | { "id" : "E5", "name" : "E5", "line" : "B", "link" : "D4,F6" }, | ||
| 801 | { "id" : "F6", "name" : "F6", "line" : "B", "link" : "B2,C3,E5" }, | ||
| 802 | { "id" : "G7", "name" : "G7", "line" : "B", "link" : "F6,H8" }, | ||
| 803 | { "id" : "H8", "name" : "H8", "line" : "B", "link" : "G7" } | ||
| 804 | ] | ||
| 805 | } | ||
| 806 | } | ||
| 807 | |||
| 808 | =head2 XML | ||
| 809 | |||
| 810 | <?xml version="1.0" encoding="UTF-8"?> | ||
| 811 | <tube name="sample map"> | ||
| 812 | <lines> | ||
| 813 | <line id="A" name="A" color="red" /> | ||
| 814 | <line id="B" name="B" color="#FFFF00"/> | ||
| 815 | </lines> | ||
| 816 | <stations> | ||
| 817 | <station id="A1" name="A1" line="A" link="B2,C3" /> | ||
| 818 | <station id="B2" name="B2" line="A" link="A1,F6" /> | ||
| 819 | <station id="C3" name="C3" line="A,B" link="A1,D4,F6"/> | ||
| 820 | <station id="D4" name="D4" line="A,B" link="C3,E5" /> | ||
| 821 | <station id="E5" name="E5" line="B" link="D4,F6" /> | ||
| 822 | <station id="F6" name="F6" line="B" link="B2,C3,E5"/> | ||
| 823 | <station id="G7" name="G7" line="B" link="F6,H8" /> | ||
| 824 | <station id="H8" name="H8" line="B" link="G7" /> | ||
| 825 | </stations> | ||
| 826 | </tube> | ||
| 827 | |||
| 828 | =head1 MAP VALIDATION | ||
| 829 | |||
| 830 | =head2 DATA VALIDATION | ||
| 831 | |||
| 832 | The package L<Test::Map::Tube> can easily be used to validate raw map data. Anyone | ||
| 833 | building a new map using L<Map::Tube> is advised to have a unit test as a part of | ||
| 834 | their distribution. Just like in L<Map::Tube::London> package, there is a unit test | ||
| 835 | somewhat like below: | ||
| 836 | |||
| 837 | use strict; use warnings; | ||
| 838 | use Test::More; | ||
| 839 | use Map::Tube::London; | ||
| 840 | use Test::Map::Tube; | ||
| 841 | ok_map(Map::Tube::London->new); | ||
| 842 | |||
| 843 | =head2 FUNCTIONAL VALIDATION | ||
| 844 | |||
| 845 | The package L<Test::Map::Tube> can easily be used to validate map | ||
| 846 | basic functions provided by L<Map::Tube>. | ||
| 847 | |||
| 848 | use strict; use warnings; | ||
| 849 | use Test::More; | ||
| 850 | use Test::Map::Tube; | ||
| 851 | use Map::Tube::London; | ||
| 852 | ok_map_functions(Map::Tube::London->new); | ||
| 853 | |||
| 854 | It can also easily be used to validate map routing functions provided | ||
| 855 | by L<Map::Tube>: | ||
| 856 | |||
| 857 | use strict; use warnings; | ||
| 858 | use Test::More; | ||
| 859 | use Test::Map::Tube; | ||
| 860 | use Map::Tube::London; | ||
| 861 | my $map = Map::Tube::London->new; | ||
| 862 | my @routes = ( | ||
| 863 | "Route 1|Tower Gateway|Aldgate|Tower Gateway,Tower Hill,Aldgate", | ||
| 864 | "Route 2|Liverpool Street|Monument|Liverpool Street,Bank,Monument", | ||
| 865 | ); | ||
| 866 | ok_map_routes($map, \@routes); | ||
| 867 | |||
| 868 | =cut | ||
| 869 | |||
| 870 | # | ||
| 871 | # | ||
| 872 | # PRIVATE METHODS | ||
| 873 | |||
| 874 | sub _get_shortest_route { | ||
| 875 | my ($self, $from) = @_; | ||
| 876 | |||
| 877 | # Each line change adds this fractional penalty on top of the normal hop | ||
| 878 | # cost of 1. Must be < 1 so hop count always dominates, but large enough | ||
| 879 | # to distinguish zero-change routes from one-change routes. | ||
| 880 | |||
| 881 | $self->_init_table; | ||
| 882 | $self->_set_length($from, 0); | ||
| 883 | $self->_set_path($from, $from); | ||
| 884 | |||
| 885 | my $all_nodes = $self->{nodes}; | ||
| 886 | |||
| 887 | # Seed the start node with all of its own lines as the active lines. | ||
| 888 | if (defined $all_nodes->{$from}) { | ||
| 889 | my @ids = map { $_->{id} } @{$all_nodes->{$from}->{line}}; | ||
| 890 | $self->_set_current_lines($from, \@ids); | ||
| 891 | } | ||
| 892 | |||
| 893 | # BFS queue. We still use a plain FIFO queue (not a priority queue) so | ||
| 894 | # that the traversal order, and therefore tie-breaking, matches the | ||
| 895 | # original BFS as closely as possible. The only change is that the cost | ||
| 896 | # stored per node is penalty-weighted (fractional), so a same-hop path | ||
| 897 | # with zero line changes beats one with a change. | ||
| 898 | # | ||
| 899 | # "queued" replaces the old $seen: a node is added to the queue at most | ||
| 900 | # once per cost improvement. Unlike the original $seen, a node can be | ||
| 901 | # re-queued if a cheaper (penalty-aware) path is later discovered. | ||
| 902 | my $queued = {}; | ||
| 903 | my @queue = ($from); | ||
| 904 | $queued->{$from} = 1; | ||
| 905 | |||
| 906 | while (@queue) { | ||
| 907 | my $u = shift @queue; | ||
| 908 | my $u_cost = $self->_get_length($u); | ||
| 909 | my $u_node = $all_nodes->{$u}; | ||
| 910 | next unless defined $u_node; | ||
| 911 | |||
| 912 | $self->_set_active_links($u_node); | ||
| 913 | |||
| 914 | my %active_set = map { $_ => 1 } @{$self->_get_current_lines($u)}; | ||
| 915 | |||
| 916 | foreach my $v (split /\,/, $u_node->{link}) { | ||
| 917 | next if ($v eq $u); | ||
| 918 | |||
| 919 | my $v_node = $all_nodes->{$v}; | ||
| 920 | next unless defined $v_node; | ||
| 921 | |||
| 922 | my @v_line_ids = map { $_->{id} } @{$v_node->{line}}; | ||
| 923 | |||
| 924 | # If this specific edge (u -> v) is restricted to a subset of | ||
| 925 | # lines, only those lines are eligible to "continue" through it. | ||
| 926 | my $_u_restrictions = $self->{_link_lines}->{uc($u)}; | ||
| 927 | my $_restriction = defined($_u_restrictions) ? $_u_restrictions->{uc($v)} : undef; | ||
| 928 | if (defined $_restriction) { | ||
| 929 | my %_allowed = map { $_ => 1 } @$_restriction; | ||
| 930 | @v_line_ids = grep { $_allowed{uc($_)} } @v_line_ids; | ||
| 931 | } | ||
| 932 | |||
| 933 | my @continuing = grep { $active_set{$_} } @v_line_ids; | ||
| 934 | |||
| 935 | my $penalty = (@continuing == 0) ? $self->line_change_penalty : 0; | ||
| 936 | my $new_cost = $u_cost + 1 + $penalty; | ||
| 937 | |||
| 938 | # Update if unvisited or cheaper. "Unvisited" is path==undef | ||
| 939 | # (avoids the 0-cost ambiguity of the start node). | ||
| 940 | my $old_cost = $self->_get_length($v); | ||
| 941 | my $visited = defined $self->{tables}->{$v}->{path}; | ||
| 942 | |||
| 943 | if (!$visited || $new_cost < $old_cost) { | ||
| 944 | $self->_set_length($v, $new_cost); | ||
| 945 | $self->_set_path($v, $u); | ||
| 946 | my @next = @continuing ? @continuing | ||
| 947 | : (defined $_restriction ? @v_line_ids : $self->_capture_common_lines($u_node, $v_node)); | ||
| 948 | $self->_set_current_lines($v, \@next); | ||
| 949 | |||
| 950 | # Re-queue only if not already waiting in the queue. | ||
| 951 | unless ($queued->{$v}) { | ||
| 952 | push @queue, $v; | ||
| 953 | $queued->{$v} = 1; | ||
| 954 | } | ||
| 955 | } | ||
| 956 | } | ||
| 957 | |||
| 958 | # Allow re-queuing this node in future if a cheaper path is found. | ||
| 959 | delete $queued->{$u}; | ||
| 960 | } | ||
| 961 | } | ||
| 962 | |||
| 963 | sub _get_all_routes { | ||
| 964 | my ($self, $visited, $to) = @_; | ||
| 965 | |||
| 966 | my $last = $visited->[-1]; | ||
| 967 | my $nodes = $self->get_node_by_id($last)->link; | ||
| 968 | foreach my $id (split /\,/, $nodes) { | ||
| 969 | next if _is_visited($id, $visited); | ||
| 970 | |||
| 971 | if (is_same($id, $to)) { | ||
| 972 | push @$visited, $id; | ||
| 973 | $self->_set_routes($visited); | ||
| 974 | pop @$visited; | ||
| 975 | last; | ||
| 976 | } | ||
| 977 | } | ||
| 978 | |||
| 979 | foreach my $id (split /\,/, $nodes) { | ||
| 980 | next if (_is_visited($id, $visited) || is_same($id, $to)); | ||
| 981 | |||
| 982 | push @$visited, $id; | ||
| 983 | $self->_get_all_routes($visited, $to); | ||
| 984 | pop @$visited; | ||
| 985 | } | ||
| 986 | |||
| 987 | return $self->{routes}; | ||
| 988 | } | ||
| 989 | |||
| 990 | sub _map_node_name { | ||
| 991 | my ($self, $name, $id) = @_; | ||
| 992 | |||
| 993 | $self->{name_to_id}->{NFC(uc($name))} = $id; | ||
| 994 | } | ||
| 995 | |||
| 996 | sub _validate_input { | ||
| 997 | my ($self, $method, $from, $to) = @_; | ||
| 998 | |||
| 999 | my @caller = caller(0); | ||
| 1000 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 1001 | |||
| 1002 | Map::Tube::Exception::MissingStationName->throw({ | ||
| 1003 | method => __PACKAGE__."::$method", | ||
| 1004 | message => "ERROR: Missing Station Name.", | ||
| 1005 | filename => $caller[1], | ||
| 1006 | line_number => $caller[2] }) | ||
| 1007 | unless (defined($from) && defined($to)); | ||
| 1008 | |||
| 1009 | $from = trim($from); | ||
| 1010 | my $_from = $self->get_node_by_name($from); | ||
| 1011 | |||
| 1012 | $to = trim($to); | ||
| 1013 | my $_to = $self->get_node_by_name($to); | ||
| 1014 | |||
| 1015 | return ($_from->{id}, $_to->{id}); | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub _xml_data { | ||
| 1019 | my ($self) = @_; | ||
| 1020 | |||
| 1021 | return $self->get_map_data; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | sub _init_map { | ||
| 1025 | my ($self) = @_; | ||
| 1026 | |||
| 1027 | my $_lines = {}; | ||
| 1028 | my $lines = {}; | ||
| 1029 | my $nodes = {}; | ||
| 1030 | my $tables = {}; | ||
| 1031 | my $_other_links = {}; | ||
| 1032 | my $_link_lines = {}; | ||
| 1033 | my $_seen_nodes = {}; | ||
| 1034 | |||
| 1035 | my @caller = caller(0); | ||
| 1036 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 1037 | |||
| 1038 | my $method = __PACKAGE__."::_init_map"; | ||
| 1039 | my $data = $self->get_map_data(\@caller, $method); | ||
| 1040 | $self->{name} = $data->{name}; | ||
| 1041 | |||
| 1042 | # Pull all lines detail first | ||
| 1043 | # So that we can check if a station has a valid line attached. | ||
| 1044 | my @lines; | ||
| 1045 | if (exists $data->{lines} && exists $data->{lines}->{line}) { | ||
| 1046 | @lines = (ref $data->{lines}->{line} eq 'HASH') | ||
| 1047 | ? ($data->{lines}->{line}) | ||
| 1048 | : @{$data->{lines}->{line}}; | ||
| 1049 | } | ||
| 1050 | my $master_line_data = {}; | ||
| 1051 | foreach (@lines) { | ||
| 1052 | $master_line_data->{NFC(lc $_->{id})} = 1; | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | my $has_station_index = {}; | ||
| 1056 | foreach my $station (@{$data->{stations}->{station}}) { | ||
| 1057 | my $id = $station->{id}; | ||
| 1058 | |||
| 1059 | # Support per-link line restrictions in the 'link' attribute, e.g. | ||
| 1060 | # link="A:x,C" means the link to station A only runs on line 'x', | ||
| 1061 | # while the link to C is unrestricted (all lines common between the | ||
| 1062 | # two stations apply, as before). Multiple restricted lines for a | ||
| 1063 | # single link are pipe-separated, e.g. link="A:x|y,C". | ||
| 1064 | # This is fully backward compatible: a plain "A,C" with no colon is | ||
| 1065 | # untouched and behaves exactly as it always has. | ||
| 1066 | if (defined $station->{link} && $station->{link} =~ /\:/) { | ||
| 1067 | my @_clean_links = (); | ||
| 1068 | my $_restrictions_for_station = {}; | ||
| 1069 | foreach my $_raw_link (split /\,/, $station->{link}) { | ||
| 1070 | if ($_raw_link =~ /\:/) { | ||
| 1071 | my ($_target, $_line_list) = split /\:/, $_raw_link, 2; | ||
| 1072 | my @_restricted_lines = split /\|/, $_line_list; | ||
| 1073 | foreach my $_rline (@_restricted_lines) { | ||
| 1074 | my $_decoded_rline = _decode_utf8_if_needed($_rline); | ||
| 1075 | unless (exists $master_line_data->{NFC(lc $_decoded_rline)}) { | ||
| 1076 | Map::Tube::Exception::InvalidStationLineId->throw({ | ||
| 1077 | method => $method, | ||
| 1078 | message => "ERROR: Invalid restricted line [$_rline] for link to [$_target] at station [$id].", | ||
| 1079 | filename => $caller[1], | ||
| 1080 | line_number => $caller[2] }); | ||
| 1081 | } | ||
| 1082 | } | ||
| 1083 | $_restrictions_for_station->{uc($_target)} = [ map { uc(_decode_utf8_if_needed($_)) } @_restricted_lines ]; | ||
| 1084 | push @_clean_links, $_target; | ||
| 1085 | } | ||
| 1086 | else { | ||
| 1087 | push @_clean_links, $_raw_link; | ||
| 1088 | } | ||
| 1089 | } | ||
| 1090 | $station->{link} = join(",", @_clean_links); | ||
| 1091 | $_link_lines->{uc($id)} = $_restrictions_for_station; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | Map::Tube::Exception::DuplicateStationId->throw({ | ||
| 1095 | method => $method, | ||
| 1096 | message => "ERROR: Duplicate Station ID [$id].", | ||
| 1097 | filename => $caller[1], | ||
| 1098 | line_number => $caller[2] }) if (exists $_seen_nodes->{$id}); | ||
| 1099 | |||
| 1100 | $_seen_nodes->{$id} = 1; | ||
| 1101 | my $name = $station->{name}; | ||
| 1102 | $name = _decode_utf8_if_needed($name); | ||
| 1103 | Map::Tube::Exception::DuplicateStationName->throw({ | ||
| 1104 | method => $method, | ||
| 1105 | message => "ERROR: Duplicate Station Name [$name].", | ||
| 1106 | filename => $caller[1], | ||
| 1107 | line_number => $caller[2] }) if (defined $self->{name_to_id}->{NFC(uc($name))}); | ||
| 1108 | |||
| 1109 | $self->_map_node_name($name, $id); | ||
| 1110 | $tables->{$id} = Map::Tube::Table->new({ id => $id }); | ||
| 1111 | |||
| 1112 | my $_station_lines = []; | ||
| 1113 | foreach my $_line (split /\,/, $station->{line}) { | ||
| 1114 | if ($_line =~ /\:/) { | ||
| 1115 | $_line = $self->_capture_line_station($_line, $id); | ||
| 1116 | $has_station_index->{$_line} = 1; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | $_line = _decode_utf8_if_needed($_line); | ||
| 1120 | if (!exists $master_line_data->{NFC(lc $_line)}) { | ||
| 1121 | Map::Tube::Exception::InvalidStationLineId->throw({ | ||
| 1122 | method => $method, | ||
| 1123 | message => "ERROR: Invalid line [$_line] for station [$name].", | ||
| 1124 | filename => $caller[1], | ||
| 1125 | line_number => $caller[2] }); | ||
| 1126 | } | ||
| 1127 | |||
| 1128 | my $uc_line = uc($_line); | ||
| 1129 | my $line = $lines->{$uc_line}; | ||
| 1130 | $line = Map::Tube::Line->new({ id => $_line }) unless defined $line; | ||
| 1131 | $_lines->{$uc_line} = $line; | ||
| 1132 | $lines->{$uc_line} = $line; | ||
| 1133 | push @$_station_lines, $line; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | if (exists $station->{other_link} && defined $station->{other_link}) { | ||
| 1137 | my @link_nodes = (); | ||
| 1138 | foreach my $_entry (split /\,/, $station->{other_link}) { | ||
| 1139 | my ($_link_type, $_nodes) = split /\:/, $_entry, 2; | ||
| 1140 | my $uc_link_type = uc($_link_type); | ||
| 1141 | my $line = $lines->{$uc_link_type}; | ||
| 1142 | $line = Map::Tube::Line->new({ id => $_link_type, name => $_link_type }) unless defined $line; | ||
| 1143 | $_lines->{$uc_link_type} = $line; | ||
| 1144 | $lines->{$uc_link_type} = $line; | ||
| 1145 | $_other_links->{$uc_link_type} = 1; | ||
| 1146 | |||
| 1147 | push @$_station_lines, $line; | ||
| 1148 | push @link_nodes, (split /\|/, $_nodes); | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | $station->{link} .= "," if $station->{link} ne ""; | ||
| 1152 | $station->{link} .= join(",", @link_nodes); | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | $station->{line} = $_station_lines; | ||
| 1156 | my $node = Map::Tube::Node->new($station); | ||
| 1157 | $nodes->{$id} = $node; | ||
| 1158 | |||
| 1159 | foreach my $line (@{$_station_lines}) { | ||
| 1160 | next if exists $has_station_index->{$line->id}; | ||
| 1161 | push @{$line->{stations}}, $node; | ||
| 1162 | } | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | foreach my $_line (@lines) { | ||
| 1166 | my $uc_line = uc($_line->{id}); | ||
| 1167 | my $line = $_lines->{$uc_line}; | ||
| 1168 | if (defined $line) { | ||
| 1169 | $line->{name} = $_line->{name}; | ||
| 1170 | $line->{color} = $_line->{color}; | ||
| 1171 | if ($has_station_index) { | ||
| 1172 | foreach (sort { $a <=> $b } keys %{$self->{_line_stations}->{$uc_line}}) { | ||
| 1173 | my $station_id = $self->{_line_stations}->{$uc_line}->{$_}; | ||
| 1174 | $line->add_station($nodes->{$station_id}); | ||
| 1175 | } | ||
| 1176 | } | ||
| 1177 | $_lines->{$uc_line} = $line; | ||
| 1178 | } | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | $self->_order_station_lines($nodes); | ||
| 1182 | |||
| 1183 | $self->lines([ values %$lines ]); | ||
| 1184 | $self->_lines($_lines); | ||
| 1185 | $self->_other_links($_other_links); | ||
| 1186 | $self->_link_lines($_link_lines); | ||
| 1187 | |||
| 1188 | # Populate Node links as ref to Node object | ||
| 1189 | foreach my $node_id (keys %$nodes) { | ||
| 1190 | my $node_obj = $nodes->{$node_id}; | ||
| 1191 | foreach my $link (split /\,/,$node_obj->{link}) { | ||
| 1192 | push @{$node_obj->{links}}, $nodes->{$link}; | ||
| 1193 | } | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | $self->nodes($nodes); | ||
| 1197 | $self->tables($tables); | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | sub _decode_utf8_if_needed { | ||
| 1201 | my ($data) = @_; | ||
| 1202 | |||
| 1203 | if (!utf8::is_utf8($data) && utf8::valid($data)) { | ||
| 1204 | $data = decode_utf8($data); | ||
| 1205 | } | ||
| 1206 | |||
| 1207 | return $data; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | sub _is_directly_linked { | ||
| 1211 | my ($self, $start_station, $end_station) = @_; | ||
| 1212 | my $linked_stations = $self->get_linked_stations($start_station); | ||
| 1213 | |||
| 1214 | return grep { $_ eq $end_station } @$linked_stations; | ||
| 1215 | } | ||
| 1216 | |||
| 1217 | sub _init_table { | ||
| 1218 | my ($self) = @_; | ||
| 1219 | |||
| 1220 | foreach my $id (keys %{$self->{tables}}) { | ||
| 1221 | $self->{tables}->{$id}->{path} = undef; | ||
| 1222 | $self->{tables}->{$id}->{length} = 0; | ||
| 1223 | $self->{tables}->{$id}->{current_lines} = undef; | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | $self->{_active_links} = undef; | ||
| 1227 | } | ||
| 1228 | |||
| 1229 | sub _load_plugins { | ||
| 1230 | my ($self) = @_; | ||
| 1231 | |||
| 1232 | $self->{plugins} = [ Map::Tube::Pluggable::plugins ]; | ||
| 1233 | foreach my $plugin (@{$self->plugins}) { | ||
| 1234 | # Only bother applying the approved plugins. | ||
| 1235 | next unless (exists $PLUGINS->{$plugin}); | ||
| 1236 | Role::Tiny->apply_roles_to_object($self, $plugin); | ||
| 1237 | } | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | sub _capture_common_lines { | ||
| 1241 | my ($self, $from, $to) = @_; | ||
| 1242 | |||
| 1243 | my $from_lines = [ map { $_->id } @{$from->line} ]; | ||
| 1244 | my $to_lines = [ map { $_->id } @{$to->line} ]; | ||
| 1245 | |||
| 1246 | $self->{_common_lines} = [ common_lines($from_lines, $to_lines) ]; | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | sub _get_common_lines { | ||
| 1250 | my ($nodes, $active_links) = @_; | ||
| 1251 | |||
| 1252 | my %_unique_links = (); | ||
| 1253 | foreach (@{$active_links}) { | ||
| 1254 | $_unique_links{$_} = 1; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | my %_common_lines = (); | ||
| 1258 | foreach my $_link (keys %_unique_links) { | ||
| 1259 | foreach my $_link_line (@{$nodes->{$_link}->line}) { | ||
| 1260 | $_common_lines{$_link_line->id} = 1; | ||
| 1261 | } | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | return (keys %_common_lines); | ||
| 1265 | } | ||
| 1266 | |||
| 1267 | sub _get_next_link { | ||
| 1268 | my ($self, $from, $seen, $links) = @_; | ||
| 1269 | |||
| 1270 | my $nodes = $self->{nodes}; | ||
| 1271 | my $active_links = $self->{_active_links}; | ||
| 1272 | |||
| 1273 | # Revert changes as it breaks routing (v3.65) | ||
| 1274 | # Commit id: 49273fd2a0baaedbfc1d02ef7 | ||
| 1275 | # ------------------------------------------- | ||
| 1276 | # my @common_lines = _get_common_lines( | ||
| 1277 | # $nodes, [ @{$active_links->[0]}, @{$active_links->[1]} ] | ||
| 1278 | # ); | ||
| 1279 | |||
| 1280 | my @common_lines = common_lines($active_links->[0], $active_links->[1]); | ||
| 1281 | |||
| 1282 | if ($self->{experimental} && scalar(@{$self->{_common_lines}})) { | ||
| 1283 | @common_lines = (@{$self->{_common_lines}}, @common_lines); | ||
| 1284 | } | ||
| 1285 | |||
| 1286 | my $link = undef; | ||
| 1287 | foreach my $_link (@$links) { | ||
| 1288 | return (0, $_link) if ((exists $seen->{$_link}) || ($from eq $_link)); | ||
| 1289 | |||
| 1290 | my $node = $nodes->{$_link}; | ||
| 1291 | next unless defined $node; | ||
| 1292 | |||
| 1293 | my @lines = (); | ||
| 1294 | foreach (@{$node->{line}}) { push @lines, $_->{id}; } | ||
| 1295 | |||
| 1296 | my @common = common_lines(\@common_lines, \@lines); | ||
| 1297 | return (1, $_link) if (scalar(@common) > 0); | ||
| 1298 | |||
| 1299 | $link = $_link; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | return (defined($link), $link); | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | sub _set_active_links { | ||
| 1306 | my ($self, $node) = @_; | ||
| 1307 | |||
| 1308 | my $active_links = $self->{_active_links}; | ||
| 1309 | my $links = [ split /\,/, $node->{link} ]; | ||
| 1310 | |||
| 1311 | if (defined $active_links) { | ||
| 1312 | shift @$active_links; | ||
| 1313 | push @$active_links, $links; | ||
| 1314 | } | ||
| 1315 | else { | ||
| 1316 | push @$active_links, $links; | ||
| 1317 | push @$active_links, $links; | ||
| 1318 | } | ||
| 1319 | |||
| 1320 | $self->{_active_links} = $active_links; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | sub _validate_map_structure { | ||
| 1324 | my ($self, $data, $caller) = @_; | ||
| 1325 | |||
| 1326 | unless (exists $data->{lines} | ||
| 1327 | && exists $data->{lines}->{line} | ||
| 1328 | && @{$data->{lines}->{line}}) { | ||
| 1329 | Map::Tube::Exception::InvalidLineStructure->throw({ | ||
| 1330 | method => __PACKAGE__."::_validate_map_structure", | ||
| 1331 | message => "ERROR: Invalid line structure in map data.", | ||
| 1332 | filename => $caller->[1], | ||
| 1333 | line_number => $caller->[2] }); | ||
| 1334 | } | ||
| 1335 | |||
| 1336 | unless (exists $data->{stations} | ||
| 1337 | && exists $data->{stations}->{station} | ||
| 1338 | && @{$data->{stations}->{station}}) { | ||
| 1339 | Map::Tube::Exception::InvalidStationStructure->throw({ | ||
| 1340 | method => __PACKAGE__."::_validate_map_structure", | ||
| 1341 | message => "ERROR: Invalid station structure in map data.", | ||
| 1342 | filename => $caller->[1], | ||
| 1343 | line_number => $caller->[2] }); | ||
| 1344 | } | ||
| 1345 | } | ||
| 1346 | |||
| 1347 | sub _capture_line_station { | ||
| 1348 | my ($self, $line, $station_id) = @_; | ||
| 1349 | |||
| 1350 | my ($line_id, $sequence) = split /\:/, $line, 2; | ||
| 1351 | $self->{_line_stations}->{uc($line_id)}->{$sequence} = $station_id; | ||
| 1352 | $self->{_line_station_index}->{uc($line_id)}->{$station_id} = $sequence; | ||
| 1353 | |||
| 1354 | return $line_id; | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | sub _validate_multi_lined_nodes { | ||
| 1358 | my ($self, $caller, $node, $id) = @_; | ||
| 1359 | |||
| 1360 | my %lines = (); | ||
| 1361 | foreach (@{$node->{line}}) { $lines{$_->{id}}++; } | ||
| 1362 | |||
| 1363 | my $max_link = 1; | ||
| 1364 | foreach (keys %lines) { | ||
| 1365 | $max_link = $lines{$_} if ($max_link < $lines{$_}); | ||
| 1366 | } | ||
| 1367 | |||
| 1368 | if ($max_link > 1) { | ||
| 1369 | my $message = sprintf("ERROR: %s has multiple lines %s,", | ||
| 1370 | $id, join( ',', grep { $lines{$_} > 1 } keys %lines)); | ||
| 1371 | |||
| 1372 | Map::Tube::Exception::FoundMultiLinedStation->throw({ | ||
| 1373 | method => __PACKAGE__."::_validate_map_data", | ||
| 1374 | message => $message, | ||
| 1375 | filename => $caller->[1], | ||
| 1376 | line_number => $caller->[2] }); | ||
| 1377 | } | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | sub _set_routes { | ||
| 1381 | my ($self, $routes) = @_; | ||
| 1382 | |||
| 1383 | my $_routes = []; | ||
| 1384 | my $nodes = $self->{nodes}; | ||
| 1385 | foreach my $id (@$routes) { | ||
| 1386 | push @$_routes, $nodes->{$id}; | ||
| 1387 | } | ||
| 1388 | |||
| 1389 | my $from = $_routes->[0]; | ||
| 1390 | my $to = $_routes->[-1]; | ||
| 1391 | my $route = Map::Tube::Route->new({ from => $from, to => $to, nodes => $_routes, link_lines => $self->_link_lines }); | ||
| 1392 | push @{$self->{routes}}, $route; | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | sub _get_path { | ||
| 1396 | my ($self, $id) = @_; | ||
| 1397 | |||
| 1398 | return $self->{tables}->{$id}->{path}; | ||
| 1399 | } | ||
| 1400 | |||
| 1401 | sub _set_path { | ||
| 1402 | my ($self, $id, $node_id) = @_; | ||
| 1403 | |||
| 1404 | return unless (defined $id && defined $node_id); | ||
| 1405 | $self->{tables}->{$id}->{path} = $node_id; | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | sub _get_length { | ||
| 1409 | my ($self, $id) = @_; | ||
| 1410 | |||
| 1411 | return 0 unless (defined $id && defined $self->{tables}->{$id}); | ||
| 1412 | return $self->{tables}->{$id}->{length}; | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | sub _set_length { | ||
| 1416 | my ($self, $id, $value) = @_; | ||
| 1417 | |||
| 1418 | return unless (defined $id && defined $value); | ||
| 1419 | $self->{tables}->{$id}->{length} = $value; | ||
| 1420 | } | ||
| 1421 | |||
| 1422 | # Track the set of line IDs that were active when we arrived at a node. | ||
| 1423 | # This lets _get_shortest_route penalise line changes. | ||
| 1424 | sub _get_current_lines { | ||
| 1425 | my ($self, $id) = @_; | ||
| 1426 | |||
| 1427 | return [] unless (defined $id && defined $self->{tables}->{$id}); | ||
| 1428 | return $self->{tables}->{$id}->{current_lines} // []; | ||
| 1429 | } | ||
| 1430 | |||
| 1431 | sub _set_current_lines { | ||
| 1432 | my ($self, $id, $lines_ref) = @_; | ||
| 1433 | |||
| 1434 | return unless (defined $id && defined $lines_ref); | ||
| 1435 | $self->{tables}->{$id}->{current_lines} = $lines_ref; | ||
| 1436 | } | ||
| 1437 | |||
| 1438 | sub _get_table { | ||
| 1439 | my ($self, $id) = @_; | ||
| 1440 | |||
| 1441 | return $self->{tables}->{$id}; | ||
| 1442 | } | ||
| 1443 | |||
| 1444 | sub _get_node_id { | ||
| 1445 | my ($self, $name) = @_; | ||
| 1446 | |||
| 1447 | return unless defined $name; | ||
| 1448 | $name =~ s/^\s+//; | ||
| 1449 | $name =~ s/\s+$//; | ||
| 1450 | $name = _decode_utf8_if_needed($name); | ||
| 1451 | $name = NFC(uc($name)); | ||
| 1452 | return $self->{name_to_id}->{$name}; | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | sub _get_line_object_by_name { | ||
| 1456 | my ($self, $name) = @_; | ||
| 1457 | |||
| 1458 | $name = _decode_utf8_if_needed($name); | ||
| 1459 | $name = NFC(uc($name)); | ||
| 1460 | foreach my $line_id (keys %{$self->{_lines}}) { | ||
| 1461 | my $line = $self->{_lines}->{$line_id}; | ||
| 1462 | if (defined $line && defined $line->name) { | ||
| 1463 | return $line if ($name eq uc($line->name)); | ||
| 1464 | } | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | return; | ||
| 1468 | } | ||
| 1469 | |||
| 1470 | sub _get_line_object_by_id { | ||
| 1471 | my ($self, $id) = @_; | ||
| 1472 | |||
| 1473 | $id = uc($id); | ||
| 1474 | foreach my $line_id (keys %{$self->{_lines}}) { | ||
| 1475 | |||
| 1476 | my $line = $self->{_lines}->{$line_id}; | ||
| 1477 | if (defined $line && defined $line->name) { | ||
| 1478 | return $line if ($id eq uc($line->id)); | ||
| 1479 | } | ||
| 1480 | } | ||
| 1481 | |||
| 1482 | return; | ||
| 1483 | } | ||
| 1484 | |||
| 1485 | sub _order_station_lines { | ||
| 1486 | my ($self, $nodes) = @_; | ||
| 1487 | |||
| 1488 | return unless scalar(keys %$nodes); | ||
| 1489 | |||
| 1490 | foreach my $node (keys %$nodes) { | ||
| 1491 | my $_lines_h = {}; | ||
| 1492 | foreach (@{$nodes->{$node}->{line}}) { | ||
| 1493 | $_lines_h->{$_->id} = $_ if defined $_->name; | ||
| 1494 | } | ||
| 1495 | my $_lines_a = []; | ||
| 1496 | foreach (sort keys %$_lines_h) { | ||
| 1497 | push @$_lines_a, $_lines_h->{$_}; | ||
| 1498 | } | ||
| 1499 | $nodes->{$node}->line($_lines_a); | ||
| 1500 | } | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | sub _is_visited { | ||
| 1504 | my ($id, $list) = @_; | ||
| 1505 | |||
| 1506 | foreach (@$list) { | ||
| 1507 | return 1 if is_same($_, $id); | ||
| 1508 | } | ||
| 1509 | |||
| 1510 | return 0; | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | =head1 AUTHOR | ||
| 1514 | |||
| 1515 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||
| 1516 | |||
| 1517 | =head1 REPOSITORY | ||
| 1518 | |||
| 1519 | L<https://github.com/manwar/Map-Tube> | ||
| 1520 | |||
| 1521 | =head1 SEE ALSO | ||
| 1522 | |||
| 1523 | =over 2 | ||
| 1524 | |||
| 1525 | =item * L<Map::Tube::CLI> | ||
| 1526 | |||
| 1527 | =item * L<Map::Tube::API> | ||
| 1528 | |||
| 1529 | =item * L<Map::Tube::Server> | ||
| 1530 | |||
| 1531 | =item * L<Map::Metro> | ||
| 1532 | |||
| 1533 | =back | ||
| 1534 | |||
| 1535 | =head1 CONTRIBUTORS | ||
| 1536 | |||
| 1537 | =over 2 | ||
| 1538 | |||
| 1539 | =item * Michal Špaček, C<< <skim at cpan.org> >> | ||
| 1540 | |||
| 1541 | =item * Slaven Rezic, C<< <srezic at cpan.org> >> | ||
| 1542 | |||
| 1543 | =item * Gisbert W. Selke, C<< <gws at cpan.org> >> | ||
| 1544 | |||
| 1545 | =item * Toby Inkster, C<< <tobyink at cpan.org> >> | ||
| 1546 | |||
| 1547 | =item * Ed J, C<< <etj at cpan.org> >> | ||
| 1548 | |||
| 1549 | =back | ||
| 1550 | |||
| 1551 | =head1 BUGS | ||
| 1552 | |||
| 1553 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube/issues>. | ||
| 1554 | I will be notified and then you'll automatically be notified of progress on your | ||
| 1555 | bug as I make changes. | ||
| 1556 | |||
| 1557 | =head1 SUPPORT | ||
| 1558 | |||
| 1559 | You can find documentation for this module with the perldoc command. | ||
| 1560 | |||
| 1561 | perldoc Map::Tube | ||
| 1562 | |||
| 1563 | You can also look for information at: | ||
| 1564 | |||
| 1565 | =over 4 | ||
| 1566 | |||
| 1567 | =item * BUG Report | ||
| 1568 | |||
| 1569 | L<https://github.com/manwar/Map-Tube/issues> | ||
| 1570 | |||
| 1571 | =item * Search MetaCPAN | ||
| 1572 | |||
| 1573 | L<https://metacpan.org/dist/Map-Tube/> | ||
| 1574 | |||
| 1575 | =back | ||
| 1576 | |||
| 1577 | =head1 LICENSE AND COPYRIGHT | ||
| 1578 | |||
| 1579 | Copyright (C) 2010 - 2026 Mohammad Sajid Anwar. | ||
| 1580 | |||
| 1581 | This program is free software; you can redistribute it and / or modify it under | ||
| 1582 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||
| 1583 | license at: | ||
| 1584 | |||
| 1585 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 1586 | |||
| 1587 | Any use, modification, and distribution of the Standard or Modified Versions is | ||
| 1588 | governed by this Artistic License.By using, modifying or distributing the Package, | ||
| 1589 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||
| 1590 | not accept this license. | ||
| 1591 | |||
| 1592 | If your Modified Version has been derived from a Modified Version made by someone | ||
| 1593 | other than you,you are nevertheless required to ensure that your Modified Version | ||
| 1594 | complies with the requirements of this license. | ||
| 1595 | |||
| 1596 | This license does not grant you the right to use any trademark, service mark, | ||
| 1597 | tradename, or logo of the Copyright Holder. | ||
| 1598 | |||
| 1599 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||
| 1600 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||
| 1601 | Package with respect to any patent claims licensable by the Copyright Holder that | ||
| 1602 | are necessarily infringed by the Package. If you institute patent litigation | ||
| 1603 | (including a cross-claim or counterclaim) against any party alleging that the | ||
| 1604 | Package constitutes direct or contributory patent infringement,then this Artistic | ||
| 1605 | License to you shall terminate on the date that such litigation is filed. | ||
| 1606 | |||
| 1607 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||
| 1608 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||
| 1609 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||
| 1610 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||
| 1611 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||
| 1612 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||
| 1613 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 1614 | |||
| 1615 | =cut | ||
| 1616 | |||
| 1617 | 1; # End of Map::Tube | ||