blib/lib/Map/Tube/Beijing.pm

filter: Statementstmt 48.7% 20 / 41 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 7 / 9 Podpod n/a totaltotal 37.5% 27 / 72 CC 12 SCAR 34.8
CC
12
Cov
34%
_remove_alternatives
56.0
_relocate_alternatives
42.0
BEGIN
1.0
CRAP 32.5
↑ Summary
1 # -*- perl -*-
2
3 #
4 # Author: Gisbert W. Selke, TapirSoft Selke & Selke GbR.
5 #
6 # Copyright (C) 2015, 2025--2026 Gisbert W. Selke. All rights reserved.
7 # This package is free software; you can redistribute it and/or
8 # modify it under the same terms as Perl itself.
9 #
10 # Mail: gws@cpan.org
11 #
12
13 package Map::Tube::Beijing;
14 1 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use version 0.77 ( );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 our $VERSION = version->declare('v0.12.8');
20
21 =encoding utf8
22
23 =head1 NAME
24
25 Map::Tube::Beijing - Interface to the Beijing tube map
26
27 =cut
28
29 1 ▶use File::Share ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
32
33 my %nametypes = map { $_ => 1 } qw(alt); # The permissible alternative nametypes. In our case, just 'alt'
34
35 has xml => ( is => 'ro', lazy => 1, default => sub { return dist_file('Map-Tube-Beijing', 'beijing-map.xml') } );
36 has nametype => ( is => 'ro', default => '',
37 isa => sub { die __PACKAGE__ . ": ERROR: Invalid nametype for constructor: '$_[0]'" unless ( ( $_[0] eq '') || exists($nametypes{ $_[0] } ) ) },
38 );
39
40 with 'Map::Tube';
41
42 before _validate_map_structure => sub {
43 $_[1] = _relocate_alternatives( $_[1], '_' . $_[0]->{nametype} ) if ( exists( $_[0]->{nametype}) && ( $_[0]->{nametype} ne '' ) );
44 $_[1] = _remove_alternatives( $_[1] );
45 };
46
47 0 ▶sub _relocate_alternatives {
Subroutine _relocate_alternatives: not called CC=6 SCAR=37.4
48 0 my( $branch, $suffix ) = @_;
49 0 for my $key( keys %{ $branch } ) {
50 0 ▶ if ( ref( $branch->{$key} ) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $branch->{$key} eq 'HASH') { } 0 0 0
elsif (ref $branch->{$key} eq '' and $key eq 'name' . $suffix) { } 0 0 0
elsif (ref $branch->{$key} eq 'ARRAY') { } 0 0 0
51 0 $branch->{$key} = _relocate_alternatives( $branch->{$key}, $suffix );
52 ▶ } elsif ( ( ref( $branch->{$key} ) eq '' ) && ( $key eq ( 'name' . $suffix ) ) ) {
Condition0%0/3
ref $branch->{$key} eq "" and $key eq 'name' . $suffix
!ll&&!rl&&r
000
MC/DC0%0/2
ref $branch->{$key} eq "" and $key eq 'name' . $suffix
ref $branch->{$key} eq "" $key eq 'name' . $suffix
Truth table0%0/3
A and B
A= ref $branch->{$key} eq ""B= $key eq 'name' . $suffix
ABresult
0X0
100
111
53 0 $branch->{'name'} = $branch->{ 'name' . $suffix };
54 } elsif ( ref( $branch->{$key} ) eq 'ARRAY' ) {
55 0 $branch->{$key} = [ map { _relocate_alternatives( $_, $suffix ) } @{ $branch->{$key} } ];
56 }
57 }
58 0 return $branch;
59 }
60
61 0 ▶sub _remove_alternatives {
Subroutine _remove_alternatives: not called CC=7 SCAR=40.3
62 0 my($branch) = @_;
63 0 for my $key( keys %{ $branch } ) {
64 0 ▶ if ( ref( $branch->{$key} ) eq 'HASH' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $branch->{$key} eq 'HASH') { } 0 0 0
elsif (ref $branch->{$key} eq '' and $key eq 'name') { } 0 0 0
elsif (ref $branch->{$key} eq 'ARRAY') { } 0 0 0
65 0 $branch->{$key} = _remove_alternatives( $branch->{$key} );
66 ▶ } elsif ( ( ref( $branch->{$key} ) eq '' ) && ( $key eq 'name' ) ) {
Condition0%0/3
ref $branch->{$key} eq "" and $key eq "name"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $branch->{$key} eq "" and $key eq "name"
ref $branch->{$key} eq "" $key eq "name"
Truth table0%0/3
A and B
A= ref $branch->{$key} eq ""B= $key eq "name"
ABresult
0X0
100
111
67 0 for my $suffix ( keys(%nametypes) ) {
68 0 delete $branch->{ $key . '_' . $suffix };
69 }
70 } elsif ( ref( $branch->{$key} ) eq 'ARRAY' ) {
71 0 $branch->{$key} = [ map { _remove_alternatives($_) } @{ $branch->{$key} } ];
72 }
73 }
74 0 return $branch;
75 }
76
77 =head1 SYNOPSIS
78
79 use Map::Tube::Beijing;
80 my $tube = Map::Tube::Beijing->new( nametype => 'alt' );
81
82 my $route = $tube->get_shortest_route('Yonghegong Lama Temple', 'Chongwenmen')->preferred( );
83
84 print "Route: $route\n";
85
86 =head1 DESCRIPTION
87
88 This module allows to find the shortest route between any two given tube
89 stations in Beijing. All interesting methods are provided by the role
90 L<Map::Tube>.
91
92 =head1 METHODS
93
94 =head2 CONSTRUCTOR
95
96 use Map::Tube::Beijing;
97 my $tube_chin = Map::Tube::Beijing->new( );
98 my $tube_pinyin = Map::Tube::Beijing->new( nametype => 'alt' );
99
100 This will read the tube information from the shared file F<beijing-map.xml>,
101 which is part of the distribution. Without argument, full Chinese characters
102 (simplified) will be used. With the value C<'alt>' for C<nametype>, pinyin
103 transliteration into Western characters will be used. Other values will throw
104 an error.
105
106 =head2 nametype( )
107
108 This yields the nametype that was specified with the constructor call, or '' if none.
109
110
111 =head2 xml( )
112
113 This read-only accessor returns whatever was specified as the XML source at
114 construction.
115
116
117 =head1 MAP DATA FORMAT
118
119 The data format for Map::Tube instances is described in the documentation for L<Map::Tube>.
120 The Beijing map, however, comes either with station and line names in the original Chinese
121 writing or in pinyin, i.e., in Latin alphabet letters that are a rough representation of
122 the pronunciation. To this end, all tags that have a C<name> attribute containing the name
123 in Chinese script also have a C<name_alt> attribute with the pinyin writing. When reading the
124 map data and no C<nametype> is given, all the C<name_alt> attributes are deleted, so that
125 the L<Map::Tube> software sees only a standard data structure. However, if C<nametype=alt>
126 was specified when instantiating L<Map::Data::Beijing>, the C<name_alt> attributes will be
127 copied into the C<name> atributes, and, again, the C<name_alt> attributes themselves are
128 removed.
129
130 This mechanism may also be employed also for other countries/regions where more than one language
131 and/or writing system is used. E.g., for Swiss subway systems it is conceivable to have up to four
132 different languages. C<name> might be used for the French name, C<name_d> for the German name,
133 C<name_i> for Italian, and C<name_r> for Romansh.
134
135 =head1 ERRORS
136
137 If something goes wrong, maybe because the map information file was corrupted,
138 the constructor will die.
139
140 =head1 BUGS
141
142 Please report any bugs or feature requests through the web interface at
143 L<https://github.com/gwselke/Map-Tube-Beijing/issues>. I will be
144 notified and then you'll automatically be notified of progress on your
145 bug when (and if) I make changes.
146
147 =head1 AUTHOR
148
149 Gisbert W. Selke, TapirSoft Selke & Selke GbR.
150
151 =head1 COPYRIGHT AND LICENCE
152
153 The data for the XML file were mainly taken from the appropriate English-language Wikipedia
154 pages. They are CC BY-SA 2.0.
155 The module itself is free software; you may redistribute and/or modify it under
156 the same terms as Perl itself.
157
158 =head1 SEE ALSO
159
160 L<Map::Tube>, L<Map::Tube::GraphViz>.
161
162 =cut
163
164 1;
↑ Summary