diff --git a/src/oud2_parser.pl b/src/oud2_parser.pl index b3b6e6f96798188759dc79380609436b5f3bdf12..ac8c7fdf7535d6f6d484c13575c7036f1588f28e 100644 --- a/src/oud2_parser.pl +++ b/src/oud2_parser.pl @@ -26,7 +26,9 @@ use Data::Printer; use File::BOM; use File::Basename; use Getopt::Long; +use List::Util qw(any); +use App::Log; use App::Ts; use App::Ressya; use App::Frame; @@ -211,6 +213,8 @@ opendir my $dir, $trains_path or die "Error opening $trains_path"; my @trains = readdir $dir; closedir $dir; +# Parsing step. +# Build "temporary" frames for each diagram. foreach my $file (@trains) { next if ($file =~ /^\.+$/); @@ -240,13 +244,71 @@ foreach my $file (@trains) print "\n"; my %frame = %{ ${$recce->value()} }; + $frame{'id'} = $basename; Frame::frame_info \%frame; - # p %frame; $master{$basename} = \%frame; - # FrameUtils::hassya_hyou \%frame, 11; - close $dia_file; } + +# Processing step 1. +# Bring stations back to the master frame and use unique IDs in children frames. +# Assume stations with the same name are identical. +# TODO cases where different stations have the same names +# TODO cases where the same station has two distinct names +# > maybe use a null footpath? +# TODO overall, find more generic criteria for matching two similar stations +my @master_ekis; +foreach my $frame (values %master) +{ + my @old_ekis = @{ $frame->{'eki'} }; + my @new_ekis; + my $index = 0; + my $update_index = 1; + for (my $i = 0; $i < $#old_ekis; $i++) + { + $update_index = 1; + + my %new_struct; + my $ekimei = $old_ekis[$i]; + my @prev = @old_ekis[0..$i-1]; + my $restart = 0; + + # Run all similarity checks first + # Station occurs twice on the same route (branch line) + foreach my $old (@new_ekis) + { + if ($old->{'ekimei'} eq $ekimei) + { + Log::i "$ekimei has already appeared on this route before, assuming branch line"; + push @new_ekis, $old; + $restart = 1; + last; + } + } + + if ($restart) { next; } + + # TODO Station has already been processed in another route + foreach my $old (@master_ekis) + { + + } + + my $id = $frame->{'id'} . "_$index"; + print "Adding station $ekimei to master frame with id $id\n"; + + $new_struct{'ekimei'} = $ekimei; + $new_struct{'found_in'} = $frame->{'id'}; + + push @new_ekis, \%new_struct; + + if ($update_index) { $index++; } + } + + push @master_ekis, @new_ekis; +} + +p @master_ekis;