summaryrefslogtreecommitdiff
path: root/bin/umlauf-to-dot
blob: 47a766cc84cb29db253031db0ff4735cc44bb789 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env perl

use strict;
use warnings;
use 5.020;
use utf8;

use File::Slurp qw(read_file);
use JSON;
use List::Util qw(uniq);

sub show_usage {
	my ($exit_code) = @_;

	say STDERR
"Usage: umlauf-to-dot <wagenreihung.json> [line number] [limit] | dot -Tpng > umlauf.png";

	exit $exit_code;
}

sub contained_in {
	my ( $value, @list ) = @_;

	for my $elem (@list) {
		if ( $elem eq $value ) {
			return 1;
		}
	}
	return;
}

sub format_train {
	my ( $train_no, $train ) = @_;

	return sprintf( '"%s %d\n%s\n%s"',
		$train->{type} // $train->{raw} // 'Zug',
		$train_no,
		$train->{route}{start} // $train->{route}{preStart} // '?',
		$train->{route}{end}   // $train->{route}{postEnd}  // '?' );
}

binmode( STDOUT, ':encoding(utf-8)' );

if ( @ARGV < 1 ) {
	show_usage(1);
}

my ( $file, $line, $limit ) = @ARGV;

my $file_content = read_file($file);
my $json         = JSON->new->utf8->decode($file_content);

my $map = $json->{train};

sub build_cycle {
	my ( $line, $cycle_id ) = @_;
	my @queue = ( [ $line, 0 ] );
	my @train_numbers;

	while (@queue) {
		my ( $train_number, $distance ) = @{ pop @queue };
		push( @train_numbers, $train_number );

		if ( $limit and $distance > $limit ) {
			next;
		}

		my @candidates;

		my $train = $map->{$train_number};
		if ( my $c = $train->{cycle}{$cycle_id} ) {
			push( @candidates, @{ $c->{from} // [] } );
			push( @candidates, @{ $c->{to}   // [] } );
		}

		@candidates = uniq @candidates;
		@candidates
		  = grep { not contained_in( $_, @train_numbers, @queue ) } @candidates;

		if (@candidates) {
			printf( "# handle %s -> push %s\n",
				$train_number, join( " ", @candidates ) );
		}

		push( @queue, map { [ $_, $distance + 1 ] } @candidates );
	}

	my @output;

	for my $train_number (@train_numbers) {
		my $train = $map->{$train_number};
		if ( my $c = $train->{cycle}{$cycle_id} ) {
			for my $from ( @{ $c->{from} // [] } ) {
				push(
					@output,
					sprintf( "%s -> %s;",
						format_train( $from,         $map->{$from} ),
						format_train( $train_number, $map->{$train_number} ) )
				);
			}
			for my $to ( @{ $c->{to} // [] } ) {
				push(
					@output,
					sprintf( "%s -> %s;",
						format_train( $train_number, $map->{$train_number} ),
						format_train( $to,           $map->{$to} ) )
				);
			}
		}
		if ( $train_number != $line ) {
			push(
				@output,
				sprintf( "%s [shape=box];",
					format_train( $train_number, $map->{$train_number} ) )
			);
		}
	}

	return @output;
}

my @cycle_ids;

push( @cycle_ids, keys %{ $map->{$line}{cycle} // {} } );

say "digraph Umlauf {";

for my $cycle_id (@cycle_ids) {
	say join( "\n", uniq build_cycle( $line, $cycle_id ) );
}

printf( "%s [style=bold];\n", format_train( $line, $map->{$line} ) );

say "}"