chiark / gitweb /
openstvoutput24compare; wip
[appendix-a6.git] / normalise
index 9703a3a96cdd22247e3d0c637bafb8feebae5779..0af8f19623d021aa3d9c34f912fc3be0584fc908 100755 (executable)
--- a/normalise
+++ b/normalise
@@ -14,22 +14,36 @@ use strict;
 
 our @options;
 our %candidates; # $candidates{CAND}{Desc}, {Opts}[]
+our %candref; # $candref{CAND} => msg of why needed
 our @ballots;
 
 my $candvoter_re = '\w+';
+my $cands_re = '\w+(?,\w+)*';
 my $opt_re = '\w+(?:=\S*)?';
 
 sub badinput ($) {
     die "bad input: $_[0]";
 }
 
-sub normalise_opts_list ($) {
-    my ($os) = @_;
+sub normalise_opts_list ($$) {
+    # $ctx is one of Election Candidate Ballot
+    my ($os,$ctx) = @_;
     $os //= '';
     my @o;
     foreach my $o (split /\s+/, $os) {
        if ($o =~ m/^\w+$/) {
            push @o, $&;
+       } elsif ($o =~ s/^_[Tt]ie\=//) {
+           $o =~ m/^($cands_re)([<>])($cands_re)$/
+               or badinput "bad value \`$_' for tie option";
+           my ($l,$op,$r) = ($1,$2,$3);
+           ($l,$op,$r) = ($r,'>',$l) if $op eq '<';
+           $candref{$_} = "tie break spec" foreach $o =~ m/\w+/g;
+           $l = join ',', sort split /\,/, $l;
+           $r = join ',', sort split /\,/, $r;
+           $l =~ m/\b$_\b/ and badinput "reflexive tie"
+               foreach split /\,/, $r;
+           push @o, "$l$op$r";
        } elsif ($o =~ m/^\w+\=\S+$/) {
            push @o, $&;
        } elsif ($o !~ m/\S/) {
@@ -40,9 +54,9 @@ sub normalise_opts_list ($) {
     return @o;
 }
 
-sub normalise_opts ($) {
-    my ($os) = @_;
-    my @o = normalise_opts_list $os;
+sub normalise_opts ($$) {
+    my ($os,$ctx) = @_;
+    my @o = normalise_opts_list $os, $ctx;
     return " | @o";
 }
 
@@ -81,12 +95,12 @@ while (<>) {
     s/^\s+//;
     s/\s+$//;
     if (m/^\|/) {
-       push @options, normalise_opts_list $';
+       push @options, normalise_opts_list $', 'Election';
     } elsif (m/^($candvoter_re?)\s*=\s*([^|]+?)\s*\|(.*)?$/o) {
        use Data::Dumper;
 print STDERR Dumper($1,$2,$3);
        my ($cand,$desc,$opts) = ($1,$2,$3);
-       push @{ $candidates{$cand}{Opts} }, normalise_opts $opts;
+       push @{ $candidates{$cand}{Opts} }, normalise_opts $opts, 'Candidate';
        setcanddesc $cand, $desc;
     } elsif (m/^($candvoter_re?)?\s*\:([^|]*)(?:\|(.*))?$/) {
        my ($voter,$opts) = ($1,$3);
@@ -101,7 +115,7 @@ print STDERR Dumper($1,$2,$3);
                badinput "bad vote preference \`$p'";
            }
        }
-       push @ballots, "$voter : @p".normalise_opts $opts;
+       push @ballots, "$voter : @p".normalise_opts $opts, 'Ballot';
     } elsif (m/^\.$/) {
     } else {
        badinput "unknown line format \`$_'";
@@ -110,6 +124,11 @@ print STDERR Dumper($1,$2,$3);
 
 print "| @options\n" or die $!;
 
+foreach my $cand (sort keys %candref) {
+    badinput "missing candidate $cand (ref. by $candref{$cand}"
+       unless defined $candidates{$cand};
+}
+
 foreach my $cand (sort keys %candidates) {
     my $c = $candidates{$cand};
     $c->{Desc} //= $cand;