Count/Start.pm

filter: Statementstmt 86.3% 82 / 95 Branchbran 63.6% 14 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 1 totaltotal 83.3% 115 / 138 CC 12 SCAR 8.5
CC
12
Cov
82%
_dofloor
5.7
_ballotset
3.1
_do_majority
2.3
CRAP 2.3
« Count/Method/STAR.pm ↑ Summary Count/Common.pm »
1 package Vote::Count::Start;
2  
3 1 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use feature qw/postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no warnings qw/experimental/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Path::Tiny 0.108;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # use Data::Dumper;
10 # use Vote::Count::Method::CondorcetDropping;
11 1 ▶use Vote::Count;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Vote::Count::ReadBallots 'read_ballots';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION='1.10';
15  
16 =head1 NAME
17  
18 Vote::Count::Start
19  
20 =head1 VERSION 1.10
21  
22 =cut
23  
24 # ABSTRACT: Vote::Count Common Setup
25  
26 =head1 SYNOPSIS
27  
28 use Vote::Count::Start;
29  
30 my $Election = StartElection(
31 BallotFile => $filepath,
32 FloorRule => 'TopCount',
33 FloorValue => 2,
34 LogPath -> '/some/path',
35 ...
36 );
37  
38 $Election->WriteLog();
39  
40 =head1 Description
41  
42 Does common startup steps useful accross methods. Written to avoid a lot of Boiler Plate for the common case of running an election and beginning with a summary of the votes and the winners by the basic simple methods
43  
44 =over
45  
46 * Reads Ballots from a file/path
47  
48 * Calculates and logs Top Count
49  
50 * Calculates and logs Approval
51  
52 * Applies a Floor Rule
53  
54 * Calculatures and logs a Borda Count
55  
56 * Generates a Condorcet Matrix and logs the Win/Loss Summary and the Scores
57  
58 * Conducts IRV (default options) and logs the result
59  
60 * Returns a Vote::Count Object
61  
62 =back
63  
64 =head1 Method StartElection
65  
66 Returns a Vote::Count object performing the above operations.
67  
68 =head2 Parameter BallotSet or BallotFile
69  
70 It is mandatory to provide either a reference to a BallotSet or to provide a BallotFile for ReadBallots to create a BallotSet.
71  
72 =head2 Paramater FloorRule, FloorValue (optional)
73  
74 A FloorRule and optional value (see Vote::Count::Floor). If no FloorRule is provide none will be used.
75  
76 =head2 Other Options
77  
78 Any other option to Vote::Count can just be passed in the arguments list
79  
80 =cut
81  
82 1 ▶use Exporter::Easy ( EXPORT => ['StartElection'] );
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 # checks for ballotfile and updates the ballotset in
85 # args. no return value because %ARGS is passed by reference
86 # and updated directly if needed.
87 ▶sub _ballotset( $ARGS ) {
Subroutine _ballotset: called CC=3 SCAR=11.4
88 2 ▶ if ( $ARGS->{'BallotFile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ARGS->{"BallotFile"}) 1 1 2
89 1 $ARGS->{'BallotSet'} = read_ballots $ARGS->{'BallotFile'};
90 }
91 # If
92 2 ▶ unless ( defined( $ARGS->{'BallotSet'}{'choices'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ARGS->{"BallotSet"}{"choices"}) 0 2 2
93 croak "A Valid BallotSet or BallotFile was not provided "
94 0 . $ARGS->{'BallotFile'} . "\n";
95 }
96 }
97  
98 ▶sub _dofloor ( $self, %ARGS ) {
Subroutine _dofloor: called CC=5 SCAR=17.4
99 2 ▶ unless ( defined $ARGS{'FloorRule'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ARGS{"FloorRule"}) 1 1 2
100 1 return $self->Active();
101 }
102 1 $self->logv(''); # log a blank line.
103 1 my $flr = $ARGS{'FloorRule'};
104 1 my $floorset = {};
105 1 ▶ if ( $flr eq 'TopCount' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($flr eq 'TopCount') { } 0 1 1
elsif ($flr eq 'TCA') { } 0 1 1
elsif ($flr eq 'Approval') { } 1 0 1
106 0 $floorset = $self->TopCountFloor( $ARGS{'FloorValue'} );
107 }
108 elsif ( $flr eq 'TCA' ) {
109 0 $floorset = $self->TCA();
110 }
111 elsif ( $flr eq 'Approval' ) {
112 1 $floorset = $self->ApprovalFloor( $ARGS{'FloorValue'} );
113 }
114 else {
115 0 croak "Undefined Floor rule $flr.\n";
116 }
117 1 $self->logv(''); # add blank line to output
118 1 return $floorset;
119 }
120  
121 ▶sub _do_plurality ( $Election ) {
Subroutine _do_plurality: called CC=2 SCAR=7.5
122 2 my $Plurality = $Election->TopCount();
123 2 $Election->logv(
124 ' ',
125 'Initial Top Count (Plurality)',
126 $Plurality->RankTable()
127 );
128 2 my $PluralityTop = $Plurality->Leader();
129 2 ▶ if ( $PluralityTop->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($PluralityTop->{'winner'}) { } 2 0 2
130 2 $Election->logt( "Plurality Winner: " . $PluralityTop->{'winner'} );
131 2 return $PluralityTop->{'winner'};
132 }
133 else {
134 $Election->logt(
135 0 "Plurality Tie: " . join( ', ', $PluralityTop->{'tied'}->@* ) );
136 0 return '';
137 }
138 }
139  
140 ▶sub _do_approval ( $Election ) {
Subroutine _do_approval: called CC=2 SCAR=7.5
141 2 my $Approval = $Election->Approval();
142 2 $Election->logv( "\nApproval", $Approval->RankTable() );
143 2 my $AWinner = $Approval->Leader();
144 2 ▶ if ( $AWinner->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AWinner->{'winner'}) { } 0 2 2
145 0 $Election->logt( "Approval Winner: " . $AWinner->{'winner'} );
146 0 return $AWinner->{'winner'};
147 }
148 else {
149 $Election->logt(
150 2 "Approval Tie: " . join( ', ', $AWinner->{'tied'}->@* ) );
151 2 return '';
152 }
153 }
154  
155 ▶sub _do_borda ( $Election ) {
Subroutine _do_borda: called CC=2 SCAR=7.5
156 2 my $Borda = $Election->Approval();
157 2 $Election->logv( "\Borda Count", $Borda->RankTable(), );
158 2 my $AWinner = $Borda->Leader();
159 2 ▶ if ( $AWinner->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($AWinner->{'winner'}) { } 0 2 2
160 0 $Election->logt( "Borda Winner: " . $AWinner->{'winner'}, '' );
161 0 return $AWinner->{'winner'};
162 }
163 else {
164 2 $Election->logt( "Borda Tie: " . join( ', ', $AWinner->{'tied'}->@* ),
165 '' );
166 2 return '';
167 }
168 }
169  
170 ▶sub _do_majority( $Election) {
Subroutine _do_majority: called CC=2 SCAR=8.4
171 2 my $majority = $Election->TopCountMajority();
172 2 ▶ if ( $majority->{'winner'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($majority->{'winner'}) { } 0 2 2
173 0 $Election->logv( "Majority Winner: " . $majority->{'winner'} );
174 0 return $majority->{'winner'};
175 }
176 2 else { return ''; }
177 }
178  
179 ▶sub _do_matrix( $Election) {
Subroutine _do_matrix: called CC=2 SCAR=6.9
180 2 my $matrix = $Election->PairMatrix();
181 2 $Election->logv(
182 "Pairing Results:",
183 $matrix->MatrixTable(),
184 "\nSmith Set: " . join( ', ', sort( keys $matrix->SmithSet()->%* ) )
185 );
186 2 ▶ if ( $matrix->CondorcetWinner() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matrix->CondorcetWinner) { } 1 1 2
187 1 $Election->logv( "Condoret Winner: " . $matrix->CondorcetWinner() );
188 1 return $matrix->CondorcetWinner();
189 }
190 1 else { return '' }
191 }
192  
193 ▶sub _do_irv ( $Election, $floorset ) {
Subroutine _do_irv: called CC=1 SCAR=0.4
194 2 ▶ my $IRVResult = try { $Election->RunIRV() }
Subroutine __ANON__: called CC=1 SCAR=0.0
195 2 ▶ catch { croak "RunIRV exploded" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
196 }
197  
198 ▶sub StartElection ( %ARGS ) {
Subroutine StartElection: called CC=1 SCAR=0.0
Pod undocumented
199 2 my $winners = {};
200 2 _ballotset( \%ARGS );
201 2 my $Election = Vote::Count->new(%ARGS);
202 2 $winners->{'plurality'} = _do_plurality($Election);
203 2 $winners->{'approval'} = _do_approval($Election);
204 2 my $floorset = _dofloor( $Election, %ARGS );
205 2 $Election->SetActive($floorset);
206 2 $winners->{'majority'} = _do_majority($Election);
207 2 $winners->{'borda'} = _do_borda($Election);
208 2 $winners->{'condorcet'} = _do_matrix($Election);
209 2 $winners->{'irv'} = _do_irv( $Election, $floorset );
210 # todo generate a summary from the winners hash.
211 2 $Election->{'startdata'} = $winners;
212 # Active gets modified from default, so reset to floorset
213 2 $Election->SetActive($floorset);
214 2 return ($Election);
215 }
216  
217 #FOOTER
218  
219 =pod
220  
221 BUG TRACKER
222  
223 L<https://github.com/brainbuz/Vote-Count/issues>
224  
225 AUTHOR
226  
227 John Karr (BRAINBUZ) brainbuz@cpan.org
228  
229 CONTRIBUTORS
230  
231 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
232  
233 LICENSE
234  
235 This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
236  
237 =cut
238  
« Count/Method/STAR.pm ↑ Summary Count/Common.pm »