summaryrefslogtreecommitdiff
path: root/src/stv.adb
blob: 4f8ef5aba59cb83f5850fc8959cf3478b5fb4085 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269


with GNAT.Command_Line;
with GNAT.Strings;
with Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Directories;
with Ada.Strings.Unbounded;
with Simple_Time;
with CSV;
with Candidates.Containers;
with Preferences;
with Bundles.Containers;
with Election;


procedure STV is


    package ACom renames Ada.Command_Line;
    package GCom renames GNAT.Command_Line;
    package GStr renames GNAT.Strings;
    package File renames Ada.Directories;
    package SU renames Ada.Strings.Unbounded;


    use type File.File_Kind;
    use type SU.Unbounded_String;
    use type Simple_Time.Time;


    Config : GCom.Command_Line_Configuration;


    Further_Help : String := "Try ""stv --help"" for more information.";


    --  I'm not fond of accesses to string accesses
    Verbose         : aliased Boolean;
    Version         : aliased Boolean;
    Help            : aliased Boolean;
    Candidate_File  : aliased GStr.String_Access;
    Preference_File : aliased GStr.String_Access;
    Output_Dir      : aliased GStr.String_Access;
    Number_To_Elect : aliased Integer;
    State_String    : aliased GStr.String_Access;


    State : Candidates.State_Name;


    Start_Time, Finish_Time : Simple_Time.Time;


    Main_Log, Log_Msg : SU.Unbounded_String;
    Log_File : File_Type;


    Candidate_Data : Candidates.Containers.Candidate_Map;
    Above_Ballot : Candidates.Containers.Above_Line_Ballot;
    Below_Ballot : Candidates.Containers.Below_Line_Ballot;


begin


    GCom.Define_Switch
           (Config => Config, Output      => Verbose'Access,
            Switch => "-v",   Long_Switch => "--verbose",
            Help   => "chatty output on stderr");

    GCom.Define_Switch
           (Config => Config, Output      => Version'Access,
            Switch => "-V",   Long_Switch => "--version",
            Help   => "show version number");

    --  technically not required, but included so
    --  it will show up in the help output
    GCom.Define_Switch
           (Config => Config, Output      => Help'Access,
            Switch => "-h",   Long_Switch => "--help",
            Help   => "show this help information");

    GCom.Define_Switch
           (Config => Config, Output      => Candidate_File'Access,
            Switch => "-c:",  Long_Switch => "--candidates=",
            Help   => ".csv file containing AEC candidate data");

    GCom.Define_Switch
           (Config => Config, Output      => Preference_File'Access,
            Switch => "-p:",  Long_Switch => "--preference=",
            Help   => ".csv file containing AEC formal preferences");

    GCom.Define_Switch
           (Config => Config, Output      => Output_Dir'Access,
            Switch => "-o:",  Long_Switch => "--outdir=",
            Help   => "new directory to output count logging");

    GCom.Define_Switch
           (Config => Config, Output      => Number_To_Elect'Access,
            Switch => "-e:",  Long_Switch => "--elect=",
            Help   => "number of candidates to elect");

    GCom.Define_Switch
           (Config => Config, Output      => State_String'Access,
            Switch => "-s:",  Long_Switch => "--state=",
            Help   => "state or territory the data corresponds to");


    GCom.Set_Usage
           (Config => Config,
            Usage  => "[switches]",
            Help   => "Note that the -c, -p, -o, -e, -s options are all" & ASCII.LF &
                        "required for normal operation." & ASCII.LF);


    --  parse options
    begin
        GCom.Getopt (Config);
    exception
        when GCom.Exit_From_Command_Line =>
            ACom.Set_Exit_Status (ACom.Failure);
            return;
        when GCom.Invalid_Switch =>
            ACom.Set_Exit_Status (ACom.Failure);
            return;
    end;


    --  version display functionality
    if Version then
        Put_Line ("Australian STV Counter v0.2");
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;


    --  check candidate data option is valid
    if Candidate_File.all = "" then
        Put_Line ("Candidate data file not provided." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;
    if not File.Exists (Candidate_File.all) then
        Put_Line ("Candidate data file does not exist." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;
    if File.Kind (Candidate_File.all) /= File.Ordinary_File then
        Put_Line ("Candidate data file name appears to refer to a directory or special file." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;


    --  check preference data option is valid
    if Preference_File.all = "" then
        Put_Line ("Preference data file not provided." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;
    if not File.Exists (Preference_File.all) then
        Put_Line ("Preference data file does not exist." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;
    if File.Kind (Preference_File.all) /= File.Ordinary_File then
        Put_Line ("Preference data file name appears to refer to a directory or special file." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;


    --  check output directory option is valid
    if Output_Dir.all = "" then
        Put_Line ("Output logging directory not provided." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;
    if File.Exists (Output_Dir.all) then
        Put_Line ("Output logging directory already exists." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;


    --  check number to elect option is valid
    if Number_To_Elect < 1 then
        Put_Line ("Number of candidates to be elected too low." & ASCII.LF & Further_Help);
        ACom.Set_Exit_Status (ACom.Failure);
        return;
    end if;


    --  check state option is valid
    begin
        State := Candidates.State_Name'Value (State_String.all);
    exception
        when Constraint_Error =>
            Put_Line ("Invalid State/Territory or State/Territory not provided." & ASCII.LF & Further_Help);
            ACom.Set_Exit_Status (ACom.Failure);
            return;
    end;


    --  set up logging
    File.Create_Directory (Output_Dir.all);
    Start_Time := Simple_Time.Now;
    Main_Log := SU.To_Unbounded_String (Output_Dir.all & "/" & "log.txt");
    Log_Msg := SU.To_Unbounded_String ("Started election count at " & Simple_Time.To_String (Start_Time));
    Create (Log_File, Append_File, SU.To_String (Main_Log));
    Put_Line (Log_File, SU.To_String (Log_Msg));
    Close (Log_File);
    if Verbose then
        Put_Line (Standard_Error, SU.To_String (Log_Msg));
    end if;


    --  read in candidate data, which is necessary for further setup
    if Verbose then
        Put_Line (Standard_Error, "Reading candidate data...");
    end if;
    Candidates.Containers.Read_Candidates (Candidate_File.all, State, Candidate_Data);
    Candidates.Containers.Generate_Ballots (Candidate_Data, Above_Ballot, Below_Ballot);


    --  set up and run the election singleton
    declare
        package Given_Prefs is new Preferences
               (Pref_Size    => Integer (Below_Ballot.Length),
                Above_Ballot => Above_Ballot,
                Below_Ballot => Below_Ballot);

        package Vote_Bundles is new Bundles
               (Given_Prefs => Given_Prefs);

        package Vote_Bundle_Containers is new Vote_Bundles.Containers;

        package This_Election is new Election
               (Given_Bundles     => Vote_Bundles,
                Bundle_Containers => Vote_Bundle_Containers);
    begin
        This_Election.Setup
               (Candidate_Data  => Candidate_Data,
                Preference_File => Preference_File.all,
                Output_Dir      => Output_Dir.all,
                Main_Logfile    => "log.txt",
                Number_To_Elect => Number_To_Elect,
                Is_Verbose      => Verbose);
        This_Election.Run;
    end;


    --  finish up logging
    Finish_Time := Simple_Time.Now;
    Log_Msg := SU.To_Unbounded_String
           ("Finished election count at " & Simple_Time.To_String (Finish_Time) & ASCII.LF &
            Duration'Image (Finish_Time - Start_Time) & " seconds elapsed." & ASCII.LF);
    Open (Log_File, Append_File, SU.To_String (Main_Log));
    Put_Line (Log_File, SU.To_String (Log_Msg));
    Close (Log_File);
    if Verbose then
        Put_Line (Standard_Error, SU.To_String (Log_Msg));
    end if;


end STV;