chiark / gitweb /
Merge branch 'ceb'
[ypp-sc-tools.db-test.git] / yarrg / yppedia-chart-parser
1 #!/usr/bin/perl
2
3 use strict (qw(vars));
4 use warnings;
5
6 use Graph::Undirected;
7
8 use CommodsDatabase;
9
10 my $widists= Graph::Undirected->new();
11 my $wiarchs= Graph::Undirected->new();
12 my @wiarchlabels;
13 my %wiisland2node;
14 my %winode2island;
15 my %wiisland2arch;
16 my %winode2lines;
17 my %wiccix2arch;
18
19 my $dbdists= Graph::Undirected->new();
20 my %dbisland2arch;
21
22 my %msgcount;
23 sub perr ($$) { print STDERR "$_[0]: $_[1]\n"; $msgcount{$_[0]}++; }
24 sub warning ($) { perr("warning",$_[0]); }
25 sub error   ($) { perr("error",  $_[0]); }
26 sub change  ($) { perr("change", $_[0]); }
27
28 if ($ARGV[0] eq '--debug') {
29     shift @ARGV;
30     open DEBUG, ">&STDOUT" or die $!;
31     select(DEBUG); $|=1;
32 } else {
33     open DEBUG, ">/dev/null" or die $!;
34 }
35 select(STDOUT); $|=1;
36
37 my $parity;
38 sub nn_xy ($$) {
39     my ($x,$y) = @_;
40     my $tp= (0+$x ^ 0+$y) & 1;
41     defined $parity or $parity=$tp;
42     $tp==$parity or warning("line $.: parity error $x,$y is $tp not $parity");
43     my $n= "$_[0],$_[1]";
44     $winode2lines{$n}{$.}++;
45     return $n;
46 }
47
48 sub parse_yppedia_map () {
49     # We don't even bother with tag soup; instead we do line-oriented parsing.
50
51     while (<>) {
52         s/\<--.*--\>//g;
53         s/^\s*//; chomp; s/\s+$//; s/\s+/ /g;
54         s/\<\/?(?:b|em)\>//g;
55         s/\{\{Chart\ style\|[^{}]*\}\}//g;
56         next unless m/\{\{/; # only interested in chart template stuff
57
58         my ($x,$y, $arch,$island,$solid,$dirn);
59         my $nn= sub { return nn_xy($x,$y) };
60     
61         if (($x,$y,$arch) =
62             m/^\{\{ chart\ label \|(\d+)\|(\d+)\| .*
63                     \'\[\[ [^][\']* \| (\S+)\ archipelago \]\]\'*\}\}$/xi) {
64             printf DEBUG "%2d,%-2d arch %s\n", $x,$y,$arch;
65             push @wiarchlabels, [ $x,$y,$arch ];
66         } elsif (($x,$y,$island) =
67             m/^\{\{ chart\ island\ icon \|(\d+)\|(\d+)\|
68                     ([^| ][^|]*[^| ]) \| .*\}\}$/xi) {
69             my $n= $nn->();
70             $wiisland2node{$island}= $n;
71             $winode2island{$n}= $island;
72             $widists->add_vertex($n);
73             $wiarchs->add_vertex($n);
74 #print "\$g->add_vertex('$n');\n";
75             printf DEBUG "%2d,%-2d island %s\n", $x,$y,$island;
76         } elsif (($solid,$x,$y,$dirn) =
77             m/^\{\{ chart\ league((?:\ solid)?) \|(\d+)\|(\d+)\|
78                     ([-\/\\o]) \| .*\}\}$/xi) {
79             next if $dirn eq 'o';
80
81             my ($bx,$by) = ($x,$y);
82             if ($dirn eq '-') { $bx+=2; }
83             elsif ($dirn eq '\\') { $bx++; $by++; }
84             elsif ($dirn eq '/') { $x++; $by++; }
85             else { die; }
86
87             $widists->add_weighted_edge($nn->(), nn_xy($bx,$by), 1);
88             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
89             $wiarchs->add_edge($nn->(), nn_xy($bx,$by)) if $solid;
90 #print "\$g->add_edge('".$nn->()."','".nn_xy($bx,$by)."');\n" if $solid;
91
92             printf DEBUG "%2d,%-2d league %-6s %s\n", $x,$y,
93                 $solid?'solid':'dotted', $dirn;
94         } elsif (
95             m/^\{\{ chart\ head \}\}$/xi
96                  ) {
97             next;
98         } else {
99             warning("line $.: ignoring incomprehensible: $_");
100         }
101     }
102 }
103
104 sub parse_database_map () {
105     my ($row,$sth);
106     $sth= $dbh->prepare('SELECT islandname, archipelago FROM islands');
107     $sth->execute();
108     while ($row= $sth->fetchrow_hashref) {
109         print DEBUG "database-island $row->{'islandname'}".
110                      " $row->{'archipelago'}\n";
111         $dbisland2arch{$row->{'islandname'}}= $row->{'archipelago'};
112     }
113     $sth= $dbh->prepare('SELECT dist, a.islandname a, b.islandname b
114                                 FROM dists
115                                 JOIN islands AS a ON dists.aiid==a.islandid
116                                 JOIN islands AS b ON dists.biid==b.islandid');
117     $sth->execute();
118     while ($row= $sth->fetchrow_hashref) {
119         $dbdists->add_weighted_edge($row->{'a'}, $row->{'b'}, $row->{'dist'});
120     }
121 }                        
122
123 sub process_yppedia_graphs () {
124     # Prune the LP database by eliminating boring intermediate vertices
125     foreach my $delete ($widists->vertices()) {
126         next if exists $winode2island{$delete};
127         my @neigh= $widists->neighbours($delete);
128         next unless @neigh==2;
129 #       my @aneigh= $wiarchs->has_vertex($delete)
130 #           ? $wiarchs->neighbours($delete) : ();
131 #       next unless @aneigh==0 || @aneigh==2;
132         my $weight= 0;
133         map { $weight += $widists->get_edge_weight($delete, $_) } @neigh;
134         $widists->add_weighted_edge(@neigh, $weight);
135         $widists->delete_vertex($delete);
136         printf DEBUG "%-5s elide %5s %-5s %2d\n", $delete, @neigh, $weight;
137     }
138
139     # Check that it's connected.
140     foreach my $cc ($widists->connected_components()) {
141         next if 2*@$cc > $widists->vertices();
142         my $m= "disconnected league point(s):";
143         foreach my $n (@$cc) {
144             $m .= "\n    LP $n, def. yppedia line(s): ".
145                 join(',', sort keys %{ $winode2lines{$n} });
146         }
147         warning($m);
148     }
149
150     # Compute all-pairs-shortest-paths on dist, which is the
151     # actual distances between all LPs.
152     my $wialldists= $widists->APSP_Floyd_Warshall();
153
154     # Assign archipelago labels to groups of islands
155     foreach my $label (@wiarchlabels) {
156         my ($ax,$ay,$arch) = @$label;
157         my $best_ccmulti= -1;
158         my $best_d2= 0;
159         my $best_n;
160 #       print DEBUG "$ax,$ay arch-island-search $arch\n";
161         $ay += 1;  $ax += 2;  # coords are rather to the top left of label
162         foreach my $vertex ($wiarchs->vertices()) {
163             next unless exists $winode2island{$vertex};
164             my $ccix= $wiarchs->connected_component_by_vertex($vertex);
165             my @cc= $wiarchs->connected_component_by_index($ccix);
166             my $ccmulti= @cc > 1;
167             my ($vx,$vy) = split /,/, $vertex;
168             my $d2= ($vx-$ax)*($vx-$ax) + ($vy-$ay)*($vy-$ay);
169             my $cmp= $ccmulti <=> $best_ccmulti
170                 ||   $best_d2 <=> $d2;
171             printf DEBUG "%2d,%-2d arch-island-search %5s d2=%4d ccix=%-2d".
172                          " cc=%2d ccmulti=%d cmp=%-2d %s\n",
173                 $ax,$ay, $vertex, $d2, $ccix, scalar(@cc), $ccmulti, $cmp,
174                 $winode2island{$vertex};
175             next unless $cmp > 0;
176             $best_n=       $vertex;
177             $best_d2=      $d2;
178             $best_ccmulti= $ccmulti;
179         }
180         die 'no island vertices?!' unless defined $best_n;
181         printf DEBUG "%2d,%-2d arch-island-select %-5s d2=%-2d %-10s %s\n",
182             $ax,$ay, $best_n, $best_d2, $arch, $winode2island{$best_n};
183         my $ccix= $wiarchs->connected_component_by_vertex($best_n);
184         my $desc= join "\n", map {
185             my $in= $winode2island{$_};
186             "    LP $_". (defined $in ? ", $in" : "");
187         } sort $wiarchs->connected_component_by_index($ccix);
188
189         if (exists $wiccix2arch{$ccix}) {
190             error("archipelago determination failed, wrongly merged:\n".
191                   "    archipelago $arch\n".
192                   "    archipelago $wiccix2arch{$ccix}\n".
193                   $desc);
194             next;
195         }
196         $wiccix2arch{$ccix}= $arch;
197 #       print "$ccix $arch ::\n$desc\n";
198     }
199
200     # Assign islands not labelled above to archipelagoes.
201     #
202     # We do this by, for each connected component (set of islands
203     # linked by purchaseable charts), searching for the nearest other
204     # connected component which has already been assigned an arch.
205     # `Nearest' means shortest distance of unpurchaseable charts, in
206     # leagues.
207
208     # fixme need some hints
209
210     # we need only consider vertices which weren't `boring intermediate
211     # vertices' (removed during optimisation as being of order 2)
212     my @ccs_useful= map {
213         [ grep { $widists->has_vertex($_) } @$_ ]
214     } $wiarchs->connected_components();
215
216     foreach my $sourceccix (0..$#ccs_useful) {
217         next if defined $wiccix2arch{$sourceccix};
218
219         my $sourcecc= $ccs_useful[$sourceccix];
220         my @islandnodes= grep { $winode2island{$_} } @$sourcecc;
221         next unless @islandnodes; # don't care, then
222
223         my $best_dist= 9999999;
224         my $best_target;
225         foreach my $targetccix (0..$#ccs_useful) {
226             next unless defined $wiccix2arch{$targetccix}; # not helpful
227             my $targetcc= $ccs_useful[$targetccix];
228             foreach my $target (@$targetcc) {
229                 foreach my $source (@$sourcecc) {
230                     my $target_dist= $wialldists->path_length($target,$source);
231                     next if $target_dist >= $best_dist;
232                     $best_dist= $target_dist;
233                     $best_target= $target;
234                 }
235             }
236         }
237 #       die "no possible target ?!" unless defined $best_target;
238 #
239 #       printf DEBUG "
240 #
241 #    foreach my $node (sort keys %winode2island) {
242 #       my $island= $winode2island{$node};
243 #       my $arch= winode2arch($node);
244 #       next if defined $arch;
245 #       my $ccix= $wiarchs->connected_component_by_vertex($node);
246 #       my @cc= $wiarchs->connected_component_by_index($ccix);
247 #       @cc= grep { defined $winode2island{$_} } @cc;
248 #       # We search for the best:
249 #       #      - member of this connected component
250     }
251 }
252
253 sub winode2arch ($) {
254     my ($node) = @_;
255     my $ccix= $wiarchs->connected_component_by_vertex($node);
256     return $wiccix2arch{$ccix};
257 }
258 sub wiisland2arch ($) {
259     my ($island) = @_;
260     my $node= $wiisland2node{$island};
261     die "$island ?" unless defined $node;
262     return winode2arch($node);
263 }
264
265 sub compare_island_lists () {
266     foreach my $island (sort keys %dbisland2arch) {
267         my $node= $wiisland2node{$island};
268         if (!defined $node) {
269             error("would delete island: $island");
270             next;
271         }
272         my $wiarch= winode2arch($node);
273         if (!defined $wiarch) {
274             error("island has no arch: $island");
275             next;
276         }
277         my $dbarch= $dbisland2arch{$island};
278         if ($wiarch ne $dbarch) {
279             change("change archipelago from $dbarch to $wiarch".
280                    " for island $island");
281         }
282     }
283     foreach my $island (sort keys %wiisland2node) {
284         my $dbarch= $dbisland2arch{$island};
285         if (!defined $dbarch) {
286             my $wiarch= wiisland2arch($island);
287             if (!defined $wiarch) {
288                 error("new island has no arch: $island");
289                 next;
290                 # We check arches of non-new islands above
291             }
292             change("new island in $wiarch: $island");
293         }
294     }
295 }
296
297 db_setocean('Midnight');
298 db_connect();
299 parse_yppedia_map();
300 parse_database_map();
301 process_yppedia_graphs();
302 compare_island_lists();