Metro.pm

filter: Statementstmt 70.5% 55 / 78 Branchbran 31.2% 5 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 78.9% 15 / 19 Podpod 50.0% 2 / 4 totaltotal 62.2% 79 / 127 CC 11 SCAR 16.3
CC
11
Cov
62%
__ANON__
13.8
decamelize
6.0
parse
6.0
CRAP 5.1
« Metro/Graph/Station.pm ↑ Summary Metro/Graph/Connection.pm »
1 11 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Map::Metro;
6  
7 # ABSTRACT: Public transport graphing
8 11 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 11 our $VERSION = '0.2404';
10  
11 11 ▶use Map::Metro::Elk;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Module::Pluggable search_path => ['Map::Metro::Plugin::Map'], require => 1, sub_name => 'system_maps';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Types::Standard qw/ArrayRef Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 ▶use Map::Metro::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶use Map::Metro::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 11 has map => (
21 is => 'ro',
22 traits => ['Array'],
23 isa => ArrayRef,
24 predicate => 1,
25 handles => {
26 get_map => 'get',
27 },
28 );
29 has mapclasses => (
30 is => 'ro',
31 traits => ['Array'],
32 isa => ArrayRef,
33 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 11 handles => {
35 add_mapclass => 'push',
36 get_mapclass => 'get',
37 },
38 );
39 has hooks => (
40 is => 'ro',
41 isa => ArrayRef[ Str ],
42 traits => ['Array'],
43 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 11 handles => {
45 all_hooks => 'elements',
46 hook_count => 'count',
47 },
48 );
49 11 has _plugin_ns => (
50 is => 'ro',
51 isa => Str,
52 default => 'Plugin::Map',
53 init_arg => undef,
54 );
55  
56 around BUILDARGS => sub {
57 1 ▶ my ($orig, $class, @args) = @_;
Subroutine __ANON__: called CC=7 SCAR=26.3
58 1 my %args;
59 1 ▶ if(scalar @args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @args == 1) { } 1 0 1
elsif (scalar @args % 2 != 0) { } 0 0 0
60 1 $args{'map'} = shift @args;
61 }
62 elsif(scalar @args % 2 != 0) {
63 0 my $map = shift @args;
64 0 %args = @args;
65 0 $args{'map'} = $map;
66 }
67 else {
68 0 %args = @args;
69 }
70  
71 1 ▶ if(exists $args{'map'} && !ArrayRef->check($args{'map'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"map"} and not &ArrayRef()->check($args{"map"})) 1 0 1
Condition33%1/3
exists $args{"map"} and not &ArrayRef()->check($args{"map"})
!ll&&!rl&&r
001
MC/DC0%0/2
exists $args{"map"} and not &ArrayRef()->check($args{"map"})
exists $args{"map"} not &ArrayRef()->check($args{"map"})
Truth table33%1/3
A and not(B)
A= exists $args{"map"}B= not &ArrayRef()->check($args{"map"})
ABresult
0X0
100
111
72 1 $args{'map'} = [$args{'map'}];
73 }
74 1 ▶ if(exists $args{'hooks'} && !ArrayRef->check($args{'hooks'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"hooks"} and not &ArrayRef()->check($args{"hooks"})) 0 1 1
Condition33%1/3
exists $args{"hooks"} and not &ArrayRef()->check($args{"hooks"})
!ll&&!rl&&r
100
MC/DC0%0/2
exists $args{"hooks"} and not &ArrayRef()->check($args{"hooks"})
exists $args{"hooks"} not &ArrayRef()->check($args{"hooks"})
Truth table33%1/3
A and not(B)
A= exists $args{"hooks"}B= not &ArrayRef()->check($args{"hooks"})
ABresult
0X0
100
111
75 0 $args{'hooks'} = [$args{'hooks'}];
76 }
77  
78 1 return $class->$orig(%args);
79 11 };
80  
81 sub BUILD {
82 1 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=13.3
Pod undocumented
83 1 my @args = @_;
84  
85 1 ▶ if($self->has_map) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_map) 1 0 1
86 1 my @system_maps = map { s{^Map::Metro::Plugin::Map::}{}; $_ } $self->system_maps;
87 1 ▶ if(any { $_ eq $self->get_map(0) } @system_maps) {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ eq $self->get_map(0); } , @system_maps)) { } 0 1 1
88 0 my $mapclass = 'Map::Metro::Plugin::Map::'.$self->get_map(0);
89 0 my $mapobj = $mapclass->new(hooks => $self->hooks);
90 0 $self->add_mapclass($mapobj);
91 }
92 else {
93 1 ▶ try { die no_such_map mapname => $self->get_map(0) } catch { die $_->desc };
Subroutine __ANON__: called CC=1 SCAR=0.0
94 }
95 }
96 }
97  
98 # Borrowed from Mojo::Util
99 sub decamelize {
100 0 ▶ my $self = shift;
Subroutine decamelize: not called CC=2 SCAR=17.9
Pod undocumented
101 0 my $string = shift;
102  
103 0 ▶ return $string if $string !~ m{[A-Z]};
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[A-Z]/ 0 0 0
104 return join '_' => map {
105 0 join ('_' => map { lc } grep { length } split m{([A-Z]{1}[^A-Z]*)})
106 } split '::' => $string;
107 }
108  
109 sub parse {
110 0 ▶ my $self = shift;
Subroutine parse: not called CC=2 SCAR=17.9
Pod documented
111 0 my %args = @_;
112  
113 return Map::Metro::Graph->new(filepath => $self->get_mapclass(0)->maplocation,
114 do_undiacritic => $self->get_mapclass(0)->do_undiacritic,
115 wanted_hook_plugins => [$self->all_hooks],
116 0 ▶ exists $args{'override_line_change_weight'} ? (override_line_change_weight => $args{'override_line_change_weight'}) : (),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'override_line_change_weight'} ? : 0 0 0
117 )->parse;
118 }
119  
120 sub available_maps {
121 0 ▶ my $self = shift;
Subroutine available_maps: not called CC=1 SCAR=6.9
Pod documented
122 0 return sort $self->system_maps;
123 }
124  
125 11 1;
126  
127 __END__
128  
129 =pod
130  
131 =encoding utf-8
132  
133 =head1 NAME
134  
135 Map::Metro - Public transport graphing
136  
137  
138  
139 =begin html
140  
141 <p>
142 <img src="https://img.shields.io/badge/perl-5.10+-blue.svg" alt="Requires Perl 5.10+" />
143 <a href="https://travis-ci.org/Csson/p5-Map-Metro"><img src="https://api.travis-ci.org/Csson/p5-Map-Metro.svg?branch=master" alt="Travis status" /></a>
144 <a href="http://cpants.cpanauthors.org/dist/Map-Metro-0.2404"><img src="https://badgedepot.code301.com/badge/kwalitee/Map-Metro/0.2404" alt="Distribution kwalitee" /></a>
145 <a href="http://matrix.cpantesters.org/?dist=Map-Metro%200.2404"><img src="https://badgedepot.code301.com/badge/cpantesters/Map-Metro/0.2404" alt="CPAN Testers result" /></a>
146 <img src="https://img.shields.io/badge/coverage-72.9%-red.svg" alt="coverage 72.9%" />
147 </p>
148  
149 =end html
150  
151 =head1 VERSION
152  
153 Version 0.2404, released 2016-04-30.
154  
155 =head1 SYNOPSIS
156  
157 # Install a map
158 $ cpanm Map::Metro::Plugin::Map::Stockholm
159  
160 # And then
161 my $graph = Map::Metro->new('Stockholm', hooks => ['PrettyPrinter'])->parse;
162  
163 my $routing = $graph->routing_for('Universitetet', 'Kista');
164  
165 # or in a terminal
166 $ map-metro.pl route Stockholm Universitetet Kista
167  
168 prints
169  
170 From Universitetet to Kista
171 ===========================
172  
173 -- Route 1 (cost 15) ----------
174 [ T14 ] Universitetet
175 [ T14 ] Tekniska högskolan
176 [ T14 ] Stadion
177 [ T14 ] Östermalmstorg
178 [ T14 ] T-Centralen
179 [ * T11 ] T-Centralen
180 [ T11 ] Rådhuset
181 [ T11 ] Fridhemsplan
182 [ T11 ] Stadshagen
183 [ T11 ] Västra skogen
184 [ T11 ] Solna centrum
185 [ T11 ] Näckrosen
186 [ T11 ] Hallonbergen
187 [ T11 ] Kista
188  
189 T11 Blue line
190 T14 Red line
191  
192 *: Transfer to other line
193 +: Transfer to other station
194  
195 =head1 DESCRIPTION
196  
197 The purpose of this distribution is to find the shortest L<unique|/"What is a unique path?"> route/routes between two stations in a transport network.
198  
199 See L<Task::MapMetro::Maps> for a list of released maps.
200  
201 =head2 Methods
202  
203 =head3 new($city, hooks => [])
204  
205 B<C<$city>>
206  
207 The name of the city you want to search connections in. Mandatory, unless you are only going to call L</"available_maps">.
208  
209 B<C<$hooks>>
210  
211 Array reference of L<Hooks|Map::Metro::Hook> that listens for events.
212  
213 =head3 parse()
214  
215 Returns a L<Map::Metro::Graph> object containing the entire graph.
216  
217 =head3 available_maps()
218  
219 Returns an array reference containing the names of all Map::Metro maps installed on the system.
220  
221 =head2 What is a unique path?
222  
223 The following rules are a guideline:
224  
225 If the starting station and finishing station...
226  
227 ...are on the same line there will be no changes to other lines.
228  
229 ...shares multiple lines (e.g., both stations are on both line 2 and 4), each line constitutes a route.
230  
231 ...are on different lines, line changes will take place at suitable station(s). There is no guarantee that the same stations will be chosen for line changes between searches, if there are more than one suitable station to make a change at.
232  
233 =head1 MORE INFORMATION
234  
235 L<Map::Metro::Graph> - How to use graph object.
236  
237 L<Map::Metro::Plugin::Map> - How to make your own maps.
238  
239 L<Map::Metro::Hook> - How to extend Map::Metro via hooks/events.
240  
241 L<Map::Metro::Cmd> - A guide to the command line application.
242  
243 L<Map::Metro::Graph::Connection> - Defines a MMG::Connection.
244  
245 L<Map::Metro::Graph::Line> - Defines a MMG::Line.
246  
247 L<Map::Metro::Graph::LineStation> - Defines a MMG::LineStation.
248  
249 L<Map::Metro::Graph::Route> - Defines a MMG::Route.
250  
251 L<Map::Metro::Graph::Routing> - Defines a MMG::Routing.
252  
253 L<Map::Metro::Graph::Segment> - Defines a MMG::Segment.
254  
255 L<Map::Metro::Graph::Station> - Defines a MMG::Station.
256  
257 L<Map::Metro::Graph::Step> - Defines a MMG::Step.
258  
259 L<Map::Metro::Graph::Transfer> - Defines a MMG::Transfer.
260  
261 =head2 Hierarchy
262  
263 The following is a conceptual overview of the various parts of a graph:
264  
265 At first, the map file is parsed. The four types of information (stations, transfers, lines and segments) are translated
266 into their respective objects.
267  
268 Next, lines and stations are put together into L<LineStations|Map::Metro::Graph::LineStation>. Every two adjacent LineStations
269 are put into two L<Connections|Map::Metro::Graph::Connection> (one for each direction).
270  
271 Now the network is complete, and it is time to start traversing it.
272  
273 Once a request to search for paths between two stations is given, we first search for the starting L<Station|Map::Metro::Graph::Station> given either a
274 station id or station name. Then we find all L<LineStations|Map::Metro::Graph::LineStation> for that station.
275  
276 Then we do the same for the destination station.
277  
278 And then we walk through the network, from L<LineStation|Map::Metro::Graph::LineStation> to L<LineStation|Map::Metro::Graph::LineStation>, finding their L<Connections|Map::Metro::Graph::Connection>
279 and turning them into L<Steps|Map::Metro::Graph::Step>, which we then add to the L<Route|Map::Metro::Graph::Route>.
280  
281 All L<Routes|Map::Metro::Graph::Route> between the two L<Stations|Map::Metro::Graph::Station> are then put into a L<Routing|Map::Metro::Graph::Routing>, which is returned to the user.
282  
283 =head1 PERFORMANCE
284  
285 Since 0.2200 performance is less of an issue than it used to be, but it could still be improved. Prior to that version the entire network was analyzed up-front. This is unnecessary when searching one (or a few) routes. For long-running applications it is still possible to pre-calculate all paths, see L<asps|Map::Metro::Graph/"asps()">.
286  
287 It is also possible to run the backend to some commands in a server, see L<App::Map::Metro>.
288  
289 =head1 STATUS
290  
291 This is somewhat experimental. I don't expect that the map file format will I<break>, but it might be
292 extended. Only the documented api should be used, though breaking changes might occur.
293  
294 For all maps in the Map::Metro::Plugin::Map namespace (unless noted):
295  
296 =over 4
297  
298 =item *
299  
300 These maps are not an official source. Use accordingly.
301  
302 =item *
303  
304 There should be a note regarding what routes the map covers.
305  
306 =back
307  
308 =head1 COMPATIBILITY
309  
310 Under Perl 5.16 or greater, C<fc> will be used instead of C<lc> for some string comparisons. Depending on the map definition
311 this might lead to some maps not working properly on pre-5.16 Perls.
312  
313 Prior to version 0.2400, C<Map::Metro> required at least Perl 5.16.
314  
315 =head1 Map::Metro or Map::Tube?
316  
317 L<Map::Tube> is the main alternative to C<Map::Metro>. They both have their strong and weak points.
318  
319 =over 4
320  
321 =item *
322  
323 Map::Tube is faster.
324  
325 =item *
326  
327 Map::Tube is more stable: It has been on Cpan for a long time, and is under active development.
328  
329 =item *
330  
331 Map::Metro has (in my opinion) a better map format.
332  
333 =item *
334  
335 Map::Metro supports eg. transfers between stations.
336  
337 =item *
338  
339 See L<Task::MapMetro::Maps> and L<Task::Map::Tube> for available maps.
340  
341 =item *
342  
343 It is possible to convert Map::Metro maps into Map::Tube maps using L<map-metro.pl|Map::Metro::Cmd/"map-metro.pl metro_to_tube $city">.
344  
345 =back
346  
347 =head1 SEE ALSO
348  
349 =over 4
350  
351 =item *
352  
353 L<Task::MapMetro::Maps> - Available maps
354  
355 =item *
356  
357 L<Map::Tube> - An alternative
358  
359 =back
360  
361 =head1 SOURCE
362  
363 L<https://github.com/Csson/p5-Map-Metro>
364  
365 =head1 HOMEPAGE
366  
367 L<https://metacpan.org/release/Map-Metro>
368  
369 =head1 AUTHOR
370  
371 Erik Carlsson <info@code301.com>
372  
373 =head1 COPYRIGHT AND LICENSE
374  
375 This software is copyright (c) 2016 by Erik Carlsson.
376  
377 This is free software; you can redistribute it and/or modify it under
378 the same terms as the Perl 5 programming language system itself.
379  
380 =cut
« Metro/Graph/Station.pm ↑ Summary Metro/Graph/Connection.pm »