Count/Common.pm

filter: Statementstmt 98.5% 68 / 69 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 75.0% 9 / 12 totaltotal 95.8% 114 / 119 CC 12 SCAR 8.0
CC
12
Cov
98%
VotesActive
5.1
_buildmatrix
3.0
_defaultactive
3.0
CRAP 2.2
« Count/Start.pm ↑ Summary Count/Log.pm »
1 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 39 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::Common;
6 39 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 39 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Role shared by Count and Matrix for common functionality. See Vote::Count Documentation.
15  
16 39 our $VERSION='2.02';
17  
18 =head1 NAME
19  
20 Vote::Count::Common
21  
22 =head1 VERSION 2.02
23  
24 =head1 Synopsis
25  
26 This Role is consumed by Vote::Count and Vote::Count::Matrix. It provides common methods for the Active Set.
27  
28 =cut
29  
30 39 has 'BallotSet' => ( is => 'ro', isa => 'HashRef', required => 1 );
31  
32 39 has 'Active' => (
33 is => 'ro',
34 isa => 'HashRef',
35 lazy => 1,
36 builder => '_defaultactive',
37 );
38  
39 39 has 'VoteValue' => (
40 is => 'ro',
41 isa => 'Int',
42 default => 1,
43 );
44  
45 39 has 'WithdrawalList' => (
46 is => 'rw',
47 isa => 'Str',
48 required => 0,
49 );
50  
51 39 has 'PairMatrix' => (
52 is => 'ro',
53 isa => 'Object',
54 lazy => 1,
55 builder => '_buildmatrix',
56 );
57  
58 ▶sub _buildmatrix ($self) {
Subroutine _buildmatrix: called CC=3 SCAR=11.0
59 58 ▶ my $tiebreak =
Branch: 1 branch
BranchTrueFalseTotal
defined $self->TieBreakMethod ? : 45 13 58
60 defined( $self->TieBreakMethod() )
61 ? $self->TieBreakMethod()
62 : 'none';
63 58 my %args = (
64 BallotSet => $self->BallotSet(),
65 Active => $self->Active(),
66 TieBreakMethod => $tiebreak,
67 LogTo => $self->LogTo() . '_matrix',
68 );
69 58 ▶ $args{'PrecedenceFile'} = $self->PrecedenceFile() if $self->PrecedenceFile();
Branch: 1 branch
BranchTrueFalseTotal
if $self->PrecedenceFile 12 46 58
70 58 $args{'TieBreakerFallBackPrecedence'} = $self->TieBreakerFallBackPrecedence();
71 58 return Vote::Count::Matrix->new( %args );
72 }
73  
74 ▶sub UpdatePairMatrix ($self) {
Subroutine UpdatePairMatrix: called CC=1 SCAR=0.0
Pod documented
75 3 $self->{'PairMatrix'} = $self->_buildmatrix();
76 }
77  
78 ▶sub GetChoices ( $self ) {
Subroutine GetChoices: called CC=1 SCAR=0.0
Pod undocumented
79 60 return sort keys( $self->BallotSet()->{'choices'}->%* );
80 }
81  
82 ▶sub _defaultactive ( $self ) {
Subroutine _defaultactive: called CC=3 SCAR=11.0
83 154 my $active = dclone $self->BallotSet()->{'choices'} ;
84 154 ▶ if ( $self->WithdrawalList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->WithdrawalList) 2 152 154
85 2 for my $w (path( $self->WithdrawalList )->lines({ chomp => 1})) {
86 8 delete $active->{$w};
87 }
88 }
89 154 return $active;
90 }
91  
92 ▶sub SetActive ( $self, $active ) {
Subroutine SetActive: called CC=2 SCAR=6.9
Pod documented
93 # Force deref
94 35 $self->{'Active'} = dclone $active;
95 # if there is a child PairMatrix, update it too.
96 35 ▶ if ( defined $self->{'PairMatrix'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"PairMatrix"}) 8 27 35
97 8 $self->{'PairMatrix'}{'Active'} = $self->{'Active'};
98 }
99 }
100  
101 ▶sub ResetActive ( $self ) {
Subroutine ResetActive: called CC=1 SCAR=0.0
Pod documented
102 1 $self->{'Active'} = $self->_defaultactive();
103 }
104  
105 # I was typing the equivalent too often. made a method.
106 ▶sub SetActiveFromArrayRef ( $self, $active ) {
Subroutine SetActiveFromArrayRef: called CC=1 SCAR=0.0
Pod documented
107 7 $self->SetActive( { map { $_ => 1 } $active->@* } );
108 }
109  
110 ▶sub GetActive ( $self ) {
Subroutine GetActive: called CC=1 SCAR=0.0
Pod documented
111 # Force deref
112 231 my $active = $self->Active();
113 231 return dclone $active;
114 }
115  
116 # this deref also happens a lot
117 ▶sub GetActiveList( $self ) {
Subroutine GetActiveList: called CC=1 SCAR=0.0
Pod documented
118 206 return ( sort( keys( $self->Active->%* ) ) );
119 }
120  
121 ▶sub Defeat ( $self, $choice ) {
Subroutine Defeat: called CC=1 SCAR=0.0
Pod documented
122 13 delete $self->{'Active'}{$choice};
123 }
124  
125 ▶sub VotesCast ( $self ) {
Subroutine VotesCast: called CC=1 SCAR=0.0
Pod documented
126 44 return $self->BallotSet()->{'votescast'};
127 }
128  
129 ▶sub VotesActive ( $self ) {
Subroutine VotesActive: called CC=5 SCAR=16.2
Pod documented
130 6 ▶ unless ( $self->BallotSet()->{'options'}{'rcv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->BallotSet->{"options"}{"rcv"}) 0 6 6
131 0 die "VotesActive Method only supports rcv";
132 }
133 6 my $set = $self->BallotSet();
134 6 my $active = $self->Active();
135 6 my $activeCount = 0;
136 LOOPVOTESACTIVE:
137 6 for my $B ( values $set->{ballots}->%* ) {
138 36 for my $V ( $B->{'votes'}->@* ) {
139 39 ▶ if ( defined $active->{$V} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $active->{$V}) 33 6 39
140 33 $activeCount += $B->{'count'};
141 33 next LOOPVOTESACTIVE;
142 }
143 }
144 }
145 6 return $activeCount;
146 }
147  
148 ▶sub BallotSetType ( $self ) {
Subroutine BallotSetType: called CC=3 SCAR=11.0
Pod undocumented
149 48 ▶ if ( $self->BallotSet()->{'options'}{'rcv'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->BallotSet->{'options'}{'rcv'}) { } 42 6 48
elsif ($self->BallotSet->{'options'}{'range'}) { } 5 1 6
150 42 return 'rcv';
151 }
152 elsif ( $self->BallotSet()->{'options'}{'range'} ) {
153 5 return 'range';
154 }
155 else {
156 1 die "BallotSetType is undefined or unknown type.";
157 }
158 }
159  
160 ▶sub GetBallots ( $self ) {
Subroutine GetBallots: called CC=1 SCAR=0.0
Pod undocumented
161 96 return $self->BallotSet()->{'ballots'};
162 }
163  
164 39 1;
165  
166 =head1 Usage
167  
168 This role is consumed by Vote::Count and Vote::Count::Matrix, providing a common set of functions to all Vote::Count objects.
169  
170 =head1 new
171  
172 The only required parameter is BallotSet. The BallotSet is provided by L<Vote::Count::ReadBallots>, you may place the BallotSet in a variable or more typically read it from within the new method.
173  
174 use Vote::Count;
175 use Vote::Count::ReadBallots;
176 my $Election = Vote::Count->new( BallotSet => read_ballots( $ballotfile ) );
177  
178 =head1 Optional Paramters to Vote::Count
179  
180 =head2 Active
181  
182 Sets the Active Set at creation. See L<Active Sets>.
183  
184 =head2 VoteValue
185  
186 Sets a VoteValue for use in weighted systems like STV. The default value is 1. Approval and TopCount are aware of VoteValue for RCV ballots.
187  
188 =head2 LogTo
189  
190 Sets a path and Naming pattern for writing logs with the WriteLogs method. If LogTo is not provided the default location is I</tmp/votecount>.
191  
192 'LogTo' => '/logging_path/election_name'
193  
194 See L<Vote::Count::Log> for more informationand and additional log path options.
195  
196 =head1 Logging Methods
197  
198 Vote::Count writes 3 logs: B<t>erse/brief, B<v>erbose, and B<d>etail. The three logging methods I<logt>, I<logv>, and I<logd> are used to record messages to those respective logs. The WriteLogs method is used to write the logs to disk.
199  
200 For more detail, see L<Vote::Count::Log>.
201  
202 =head1 Active Sets
203  
204 Active sets are a Hash Reference where the keys represent the active choices and the value is true. The VoteCount Object contains an Active Set which can be Accessed via the Active() method which will return a reference to the Active Set (changing the reference will change the active set). The GetActive and SetActive methods break the reference links. GetActiveList returns the Active Set as a sorted list.
205  
206 Many Components will take an argument for $activeset or default to the current Active set of the Vote::Count object, which is defaulted to the Choices defined in the BallotSet.
207  
208 =head2 Active
209  
210 Get Active Set as HashRef to the active set. Changing the new HashRef will change the internal Active Set, GetActive is often preferable as it will return a HashRef that is a copy instead. Active may be used as an optional parameter to L<new|"Optional Paramters to Vote::Count">.
211  
212 =head2 GetActive
213  
214 Returns a hashref containing a copy of the Active Set.
215  
216 =head2 GetActiveList
217  
218 Returns a simple array of the members of the Active Set.
219  
220 =head2 ResetActive
221  
222 Sets the Active Set to the full choices list of the BallotSet.
223  
224 =head2 SetActive
225  
226 Sets the Active Set to provided HashRef. The values to the hashref should evaluate as True.
227  
228 =head2 SetActiveFromArrayRef
229  
230 Same as SetActive except it takes an ArrayRef of the choices to be set as Active.
231  
232 =head1 Vote::Count Methods
233  
234 Most of these are provided by the Role Common and available directly in both Matrix objects and Vote::Count Objects. Vote::Count objects create a child Matrix object: PairMatrix.
235  
236 =head2 BallotSet
237  
238 Get BallotSet
239  
240 =head2 PairMatrix
241  
242 Get a Matrix Object for the Active Set. Generated and cached on the first request.
243  
244 =head2 UpdatePairMatrix
245  
246 Regenerate and cache Matrix with current Active Set.
247  
248 =head2 VotesCast
249  
250 Returns the number of votes cast.
251  
252 =head2 VotesActive
253  
254 Returns the number of non-exhausted ballots based on the current Active Set.
255  
256 =head2 new
257  
258 Has the following Attributes:
259  
260 =head2 WithdrawalList
261  
262 A text file containing choices 1 per line that are withdrawn. Use when a choice may be included in the ballots but should be treated as not-present. Removing a choice from the choices list in a Ballot File will generate an exception from ReadBallots if it appears on any Ballots. Withdrawing a choice will exclude it from the Active Set if it is present in the Ballots.
263  
264 =head2 Choices
265  
266 Returns an array of all of the Choices in the Ballot Set.
267  
268 =head2 GetActiveList
269  
270 Returns a simple array of the members of the Active Set.
271  
272 =head2 ResetActive
273  
274 Sets the Active Set to the full choices list of the BallotSet.
275  
276 =head2 SetActive
277  
278 Sets the Active Set to provided HashRef. The values to the hashref should evaluate as True.
279  
280 =head2 SetActiveFromArrayRef
281  
282 Same as SetActive except it takes an ArrayRef of the choices to be set as Active.
283  
284 =head2 Defeat
285  
286 Remove $choice from current Active List.
287  
288 $Election->Defeat( $choice );
289 $Election->logv( "Defeated $choice");
290  
291 =cut
292  
293 #FOOTER
294  
295 =pod
296  
297 BUG TRACKER
298  
299 L<https://github.com/brainbuz/Vote-Count/issues>
300  
301 AUTHOR
302  
303 John Karr (BRAINBUZ) brainbuz@cpan.org
304  
305 CONTRIBUTORS
306  
307 Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
308  
309 LICENSE
310  
311 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>.
312  
313 SUPPORT
314  
315 This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
316  
317 =cut
318  
« Count/Start.pm ↑ Summary Count/Log.pm »