diff --git a/lib/App/MasterUtils.pm b/lib/App/MasterUtils.pm new file mode 100644 index 0000000000000000000000000000000000000000..9742aca608ed32b86a2ce6b03de0535b55a26710 --- /dev/null +++ b/lib/App/MasterUtils.pm @@ -0,0 +1,49 @@ +package MasterUtils; + +use strict; +use warnings; + +BEGIN +{ + unshift @INC, '.'; +} + +use App::Log; +use Data::Printer; + +sub sta_from_id +{ + my %master = %{ $_[0] }; + my $id = $_[1]; + if (!defined $id) { Log::e 'Please specify a station id.'; } + foreach my $k (keys %{ $master{'eki_index'} }) + { + my $v = $master{'eki_index'}->{$k}; + if ($k eq $id) { return $v }; + } + Log::e "Station id $id not found."; +} + +sub get_routes +{ + my %master = %{ $_[0] }; + my $id = $_[1]; + my @res; + + if (!defined $id) { Log::e 'Please specify a station id to get its routes.'; } + my $eki_ref = sta_from_id \%master, $id; + + my %rosens = %{ $master{'rosen'} }; + foreach my $k (keys %rosens) + { + my @ekis = @{ $rosens{$k}->{'eki'} }; + foreach my $eki (@ekis) + { + if ($id eq $eki) { push @res, $k; } + } + } + + @res ? return @res : Log::e "$id has no routes"; +} + +1; diff --git a/src/oud2_parser.pl b/src/oud2_parser.pl index c0836808034d4e8f6172b794c6ffdb03c4566034..947056b52e9b63eb7ac4393f732e91dee51e6183 100644 --- a/src/oud2_parser.pl +++ b/src/oud2_parser.pl @@ -33,6 +33,7 @@ use App::Ts; use App::Ressya; use App::Frame; use App::FrameUtils; +use App::MasterUtils; use App::Dia; # .oud2 grammar @@ -282,7 +283,7 @@ foreach my $frame (values %{ $master{'rosen'} }) # Station occurs twice on the same route (branch line) foreach my $old (values @new_ekis) { - if ($old->{'ekimei'} eq $ekimei) + if ((MasterUtils::sta_from_id \%master, $old)->{'ekimei'} eq $ekimei) { Log::i "$ekimei has already appeared on this route before, assuming branch line"; push @new_ekis, $old; @@ -294,19 +295,24 @@ foreach my $frame (values %{ $master{'rosen'} }) if ($restart) { next; } # TODO Station has already been processed in another route - foreach my $old (@master_ekis) + foreach my $old (keys %{ $master{'eki_index'} }) { - + my $old_ref = MasterUtils::sta_from_id \%master, $old; + if ($old_ref->{'ekimei'} eq $ekimei) + { + Log::i "Found $ekimei in the existing index (first found in " . $old_ref->{'found_in'} . ')'; + Log::i "Assuming this is the same station as the one processed earlier."; + last; + } } my $id = $frame->{'id'} . "_$index"; print "Adding station $ekimei to master frame with id $id\n"; - $master{'eki_index'}{$id} = \%new_struct; - $new_struct{'ekimei'} = $ekimei; $new_struct{'found_in'} = $frame->{'id'}; + $master{'eki_index'}{$id} = \%new_struct; - push @new_ekis, \%new_struct; + push @new_ekis, $id; if ($update_index) { $index++; } } @@ -315,3 +321,5 @@ foreach my $frame (values %{ $master{'rosen'} }) # The order is conserved so there's no need to modify {'jikoku'} in {'dia'} $frame->{'eki'} = \@new_ekis; } + +MasterUtils::get_routes \%master, 'konpoku_10';