blib/lib/Map/Tube/Brussels.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) 2025 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::Brussels;
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.2.1');
20
21 =encoding utf8
22
23 =head1 NAME
24
25 Map::Tube::Brussels - Interface to the Brussels 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-Brussels', 'brussels-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 sub _relocate_alternatives {
48 0 ▶ my( $branch, $suffix ) = @_;
Subroutine _relocate_alternatives: not called CC=6 SCAR=37.4
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 sub _remove_alternatives {
62 0 ▶ my($branch) = @_;
Subroutine _remove_alternatives: not called CC=7 SCAR=40.3
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::Brussels;
80
81 my $tube_nl = Map::Tube::Brussels->new( nametype => 'alt' );
82 my $route_nl = $tube_nl->get_shortest_route('Delacroix', 'Zuidstation')->preferred( );
83 print "Route: $route_nl\n";
84
85 my $tube_fr = Map::Tube::Brussels->new( );
86 my $route_fr = $tube_fr->get_shortest_route('Delacroix', 'Gare du Midi')->preferred( );
87 print "Route: $route_fr\n";
88
89 =head1 DESCRIPTION
90
91 This module allows to find the shortest route between any two given tube
92 stations in Brussels. All interesting methods are provided by the role
93 L<Map::Tube>.
94
95 =head1 METHODS
96
97 =head2 CONSTRUCTOR
98
99 use Map::Tube::Brussels;
100 my $tube_fr = Map::Tube::Brussels->new( );
101 my $tube_nl = Map::Tube::Brussels->new( nametype => 'alt' );
102
103 This will read the tube information from the shared file
104 F<brussels-map.xml>, which is part of the distribution. Without
105 argument, French-language place names will be used. With the value
106 C<'alt>' for C<nametype>, Dutch-language place names will be used.
107 Other values will throw an error.
108
109
110 =head2 nametype( )
111
112 This yields the nametype that was specified with the constructor call, or '' if none.
113
114
115 =head2 xml( )
116
117 This read-only accessor returns whatever was specified as the XML source at
118 construction.
119
120
121 =head1 MAP DATA FORMAT
122
123 The data format for Map::Tube instances is described in the
124 documentation for L<Map::Tube>. The Brussels map, however, comes either
125 with station and line names in the French language (spoken in Wallonia)
126 or in the Dutch language (spoken in Flanders). All line and station
127 tags have the French name in the C<name> attribute. For tags where two
128 names differ there is an additional C<name_alt> attribute having the
129 Dutch name. When reading the map data and no C<nametype> is given, all
130 the C<name_alt> attributes are deleted, so that the L<Map::Tube>
131 software sees only a standard data structure. However, if
132 C<nametype=alt> was specified when instantiating L<Map::Data::Brussels>,
133 the C<name_alt> attributes will be copied into the C<name> atributes,
134 and, again, the C<name_alt> attributes themselves are removed.
135
136
137 =head1 ERRORS
138
139 If something goes wrong, maybe because the map information file was corrupted,
140 the constructor will die.
141
142 Some sources have a slightly different view on which stations exist on which lines.
143 While the differences are few, they should be resolved by direct evidence. Hints are
144 welcome.
145
146 =head1 AUTHOR
147
148 Gisbert W. Selke, TapirSoft Selke & Selke GbR.
149
150 =head1 COPYRIGHT AND LICENCE
151
152 The data for the XML file were mainly taken from the appropriate French and Dutch Wikipedia
153 pages. They are CC BY-SA 2.0. For additional verification and names, openstreetmap.org was
154 consulted.
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