| 1 | package App::War; | ||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 11 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 11 | ▶ | use Graph; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 11 | ▶ | use List::Util 'shuffle'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 11 | our $VERSION = 0.05; | |||||||||||
| 8 | |||||||||||||
| 9 | =pod | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 NAME | ||||||||||||
| 12 | |||||||||||||
| 13 | App::War - turn one big decision into many small decisions | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | use App::War; | ||||||||||||
| 18 | my $war = App::War->new; | ||||||||||||
| 19 | $war->items(qw/ this that the-other that-too /); | ||||||||||||
| 20 | $war->init; | ||||||||||||
| 21 | $war->rank; | ||||||||||||
| 22 | print $war->report; | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||
| 25 | |||||||||||||
| 26 | How do you go about ranking a number of items? One way to do it is to | ||||||||||||
| 27 | compare the objects two at a time until a clear winner can be established. | ||||||||||||
| 28 | |||||||||||||
| 29 | This module does just that, using a topological sort to establish a unique | ||||||||||||
| 30 | ordering of all the "combatants" in the "war". | ||||||||||||
| 31 | |||||||||||||
| 32 | This module is modeled loosely after L<http://kittenwar.com/>, a | ||||||||||||
| 33 | crowdsourced web application for determining the cutest kitten in the | ||||||||||||
| 34 | universe. | ||||||||||||
| 35 | |||||||||||||
| 36 | =head1 METHODS | ||||||||||||
| 37 | |||||||||||||
| 38 | =head2 App::War->new() | ||||||||||||
| 39 | |||||||||||||
| 40 | Constructs a new war object. | ||||||||||||
| 41 | |||||||||||||
| 42 | =cut | ||||||||||||
| 43 | |||||||||||||
| 44 | 8 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 45 | 8 | my $class = shift; | |||||||||||
| 46 | 8 | my $self = bless { @_ }, $class; | |||||||||||
| 47 | 8 | return $self; | |||||||||||
| 48 | } | ||||||||||||
| 49 | |||||||||||||
| 50 | =head2 $war->run | ||||||||||||
| 51 | |||||||||||||
| 52 | Starts the war. | ||||||||||||
| 53 | |||||||||||||
| 54 | =cut | ||||||||||||
| 55 | |||||||||||||
| 56 | 1 | ▶ | sub run { | ||||||||||
|
Subroutine
run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 57 | 1 | my $self = shift; | |||||||||||
| 58 | 1 | $self->init; | |||||||||||
| 59 | 1 | $self->rank; | |||||||||||
| 60 | } | ||||||||||||
| 61 | |||||||||||||
| 62 | =head2 $war->init | ||||||||||||
| 63 | |||||||||||||
| 64 | Uses the content of C<< $self->items >> to initialize a graph containing | ||||||||||||
| 65 | only vertices, one per item. | ||||||||||||
| 66 | |||||||||||||
| 67 | =cut | ||||||||||||
| 68 | |||||||||||||
| 69 | # NOTE: calling '$self->graph->add_vertex' breaks in strange | ||||||||||||
| 70 | # and mysterious ways. Why does this fix it? | ||||||||||||
| 71 | |||||||||||||
| 72 | 6 | ▶ | sub init { | ||||||||||
|
Subroutine
init: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 73 | 6 | my $self = shift; | |||||||||||
| 74 | 6 | my @items = $self->items; | |||||||||||
| 75 | 6 | $self->_info("Ranking items: @items"); | |||||||||||
| 76 | 6 | my $g = $self->graph; | |||||||||||
| 77 | 6 | for my $i (0 .. $#items) { | |||||||||||
| 78 | # Why does this not work? | ||||||||||||
| 79 | # $self->graph->add_vertex($i); | ||||||||||||
| 80 | 22 | $g->add_vertex($i); | |||||||||||
| 81 | } | ||||||||||||
| 82 | 6 | return $self; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | =head2 $war->report | ||||||||||||
| 86 | |||||||||||||
| 87 | Returns the current state of the war graph as a multiline string. | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | 3 | ▶ | sub report { | ||||||||||
|
Subroutine
report: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 92 | 3 | my $self = shift; | |||||||||||
| 93 | 3 | my @out; | |||||||||||
| 94 | 3 | push @out, "graph: @{[ $self->graph ]}\n"; | |||||||||||
| 95 | 3 | my @items = $self->items; | |||||||||||
| 96 | 3 | my @ts = map { $items[$_] } $self->graph->topological_sort; | |||||||||||
| 97 | 3 | push @out, "sort: @ts\n"; | |||||||||||
| 98 | 3 | return join q(), @out; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 $war->graph | ||||||||||||
| 102 | |||||||||||||
| 103 | Returns the graph object that stores the user choices. | ||||||||||||
| 104 | |||||||||||||
| 105 | =cut | ||||||||||||
| 106 | |||||||||||||
| 107 | 56 | ▶ | sub graph { | ||||||||||
|
Subroutine
graph: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 108 | 56 | my $self = shift; | |||||||||||
| 109 | 56 | ▶ | unless (exists $self->{graph}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 6 | $self->{graph} = Graph->new(directed => 1); | |||||||||||
| 111 | } | ||||||||||||
| 112 | 56 | return $self->{graph}; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | =head2 $war->items | ||||||||||||
| 116 | |||||||||||||
| 117 | Get/set the items to be ranked. It's a bad idea to modify this once the | ||||||||||||
| 118 | war has started. | ||||||||||||
| 119 | |||||||||||||
| 120 | =cut | ||||||||||||
| 121 | |||||||||||||
| 122 | 18 | ▶ | sub items { | ||||||||||
|
Subroutine
items: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 123 | 18 | my $self = shift; | |||||||||||
| 124 | 18 | ▶ | $self->{items} ||= []; | ||||||||||
|
Condition100%2/2
$self->{"items"} ||= []
MC/DC100%1/1
$self->{"items"} ||= []
$self->{"items"}
Truth table100%2/2
A
A= $self->{"items"}
| |||||||||||||
| 125 | 18 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | 2 | $self->{items} = [shuffle @_]; | |||||||||||
| 127 | } | ||||||||||||
| 128 | 18 | return @{ $self->{items} }; | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | =head2 $war->rank | ||||||||||||
| 132 | |||||||||||||
| 133 | Starts the process of uniquely ordering the graph vertices. This method | ||||||||||||
| 134 | calls method C<tsort_not_unique> until it returns false, I<i.e.> we have a | ||||||||||||
| 135 | unique topo sort. | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | 2 | ▶ | sub rank { | ||||||||||
|
Subroutine
rank: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 140 | 2 | my $self = shift; | |||||||||||
| 141 | 2 | ▶ | while (my $v = $self->tsort_not_unique) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 7 | $self->compare($v->[0], $v->[1]); | |||||||||||
| 143 | } | ||||||||||||
| 144 | 2 | return $self; | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | =head2 $war->tsort_not_unique | ||||||||||||
| 148 | |||||||||||||
| 149 | This method returns a true value (more on this later) if the graph | ||||||||||||
| 150 | currently lacks a unique topo sort. If the graph B<has> a unique sort, the | ||||||||||||
| 151 | "war" is over, and results should be reported. | ||||||||||||
| 152 | |||||||||||||
| 153 | If the graph B<lacks> a unique topological sort, this method returns an | ||||||||||||
| 154 | arrayref containing a pair of vertices that have an ambiguous ordering. | ||||||||||||
| 155 | From L<http://en.wikipedia.org/wiki/Topological_sorting>: | ||||||||||||
| 156 | |||||||||||||
| 157 | =over 4 | ||||||||||||
| 158 | |||||||||||||
| 159 | If a topological sort has the property that all pairs of consecutive | ||||||||||||
| 160 | vertices in the sorted order are connected by edges, then these edges form | ||||||||||||
| 161 | a directed Hamiltonian path in the DAG. If a Hamiltonian path exists, the | ||||||||||||
| 162 | topological sort order is unique; no other order respects the edges of the | ||||||||||||
| 163 | path. | ||||||||||||
| 164 | |||||||||||||
| 165 | =back | ||||||||||||
| 166 | |||||||||||||
| 167 | This property of the topological sort is used to ensure that we have a | ||||||||||||
| 168 | unique ordering of the "combatants" in our "war". | ||||||||||||
| 169 | |||||||||||||
| 170 | =cut | ||||||||||||
| 171 | |||||||||||||
| 172 | 11 | ▶ | sub tsort_not_unique { | ||||||||||
|
Subroutine
tsort_not_unique: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 173 | 11 | my $self = shift; | |||||||||||
| 174 | |||||||||||||
| 175 | # search for unordered items by calculating the topological sort and | ||||||||||||
| 176 | # verifying that adjacent items are connected by a directed edge | ||||||||||||
| 177 | |||||||||||||
| 178 | 11 | my @ts = $self->graph->topological_sort; | |||||||||||
| 179 | |||||||||||||
| 180 | 11 | for my $i (0 .. $#ts - 1) { | |||||||||||
| 181 | 19 | my ($u,$v) = @ts[$i,$i+1]; | |||||||||||
| 182 | 19 | ▶ | if (!$self->graph->has_edge($u,$v)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 8 | return [$u,$v]; | |||||||||||
| 184 | } | ||||||||||||
| 185 | } | ||||||||||||
| 186 | 3 | return 0; | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | =head2 $war->compare($index1,$index2) | ||||||||||||
| 190 | |||||||||||||
| 191 | Handles user interaction choosing one of two alternatives. Arguments | ||||||||||||
| 192 | C<$index1> and C<$index2> are indexes into the internal array of items to | ||||||||||||
| 193 | be ranked, and indicate the two items that need to have their rank | ||||||||||||
| 194 | disambiguated. | ||||||||||||
| 195 | |||||||||||||
| 196 | =cut | ||||||||||||
| 197 | |||||||||||||
| 198 | 5 | ▶ | sub compare { | ||||||||||
|
Subroutine
compare: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 199 | 5 | my ($self,@x) = @_; | |||||||||||
| 200 | 5 | my @items = $self->items; | |||||||||||
| 201 | 5 | my $response = $self->_get_response(@items[@x]); | |||||||||||
| 202 | 5 | ▶ | if ($response =~ /1/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 203 | 2 | $self->graph->add_edge($x[0],$x[1]); | |||||||||||
| 204 | } | ||||||||||||
| 205 | else { | ||||||||||||
| 206 | 3 | $self->graph->add_edge($x[1],$x[0]); | |||||||||||
| 207 | } | ||||||||||||
| 208 | } | ||||||||||||
| 209 | |||||||||||||
| 210 | 0 | ▶ | sub _get_response { | ||||||||||
|
Subroutine
_get_response: not called CC=1 SCAR=6.9
| |||||||||||||
| 211 | 0 | my ($self,@items) = @_; | |||||||||||
| 212 | 0 | print "Choose one of the following:\n"; | |||||||||||
| 213 | 0 | print "<1> $items[0]\n"; | |||||||||||
| 214 | 0 | print "<2> $items[1]\n"; | |||||||||||
| 215 | 0 | (my $resp = <STDIN>) =~ y/12//cd; | |||||||||||
| 216 | 0 | return $resp; | |||||||||||
| 217 | } | ||||||||||||
| 218 | |||||||||||||
| 219 | 8 | ▶ | sub _info { | ||||||||||
|
Subroutine
_info: called CC=2 SCAR=6.9
| |||||||||||||
| 220 | 8 | my $self = shift; | |||||||||||
| 221 | 8 | ▶ | if ($self->{verbose}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 222 | 1 | warn "@_\n"; | |||||||||||
| 223 | } | ||||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||
| 227 | |||||||||||||
| 228 | John Trammell, C<< <johntrammell@gmail.com> >> | ||||||||||||
| 229 | |||||||||||||
| 230 | =head1 BUGS | ||||||||||||
| 231 | |||||||||||||
| 232 | Please report any bugs or feature requests to C<bug-app-war at | ||||||||||||
| 233 | rt.cpan.org>, or through the web interface at | ||||||||||||
| 234 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-War>. I will be | ||||||||||||
| 235 | notified, and then you'll automatically be notified of progress on your bug | ||||||||||||
| 236 | as I make changes. | ||||||||||||
| 237 | |||||||||||||
| 238 | =head1 SUPPORT | ||||||||||||
| 239 | |||||||||||||
| 240 | You can find documentation for this module with the perldoc command. | ||||||||||||
| 241 | |||||||||||||
| 242 | perldoc App::War | ||||||||||||
| 243 | |||||||||||||
| 244 | Your operating system may also have installed a manual page for this | ||||||||||||
| 245 | module; it would likely be available via the command | ||||||||||||
| 246 | |||||||||||||
| 247 | man war | ||||||||||||
| 248 | |||||||||||||
| 249 | You can also look for information at: | ||||||||||||
| 250 | |||||||||||||
| 251 | =over 4 | ||||||||||||
| 252 | |||||||||||||
| 253 | =item * GitHub | ||||||||||||
| 254 | |||||||||||||
| 255 | L<http://github.com/trammell/app-war> | ||||||||||||
| 256 | |||||||||||||
| 257 | =item * RT: CPAN's request tracker | ||||||||||||
| 258 | |||||||||||||
| 259 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=App-War> | ||||||||||||
| 260 | |||||||||||||
| 261 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||
| 262 | |||||||||||||
| 263 | L<http://annocpan.org/dist/App-War> | ||||||||||||
| 264 | |||||||||||||
| 265 | =item * CPAN Ratings | ||||||||||||
| 266 | |||||||||||||
| 267 | L<http://cpanratings.perl.org/d/App-War> | ||||||||||||
| 268 | |||||||||||||
| 269 | =item * Search CPAN | ||||||||||||
| 270 | |||||||||||||
| 271 | L<http://search.cpan.org/dist/App-War/> | ||||||||||||
| 272 | |||||||||||||
| 273 | =back | ||||||||||||
| 274 | |||||||||||||
| 275 | =head1 COPYRIGHT & LICENSE | ||||||||||||
| 276 | |||||||||||||
| 277 | Copyright 2009 John Trammell, all rights reserved. | ||||||||||||
| 278 | |||||||||||||
| 279 | This program is free software; you can redistribute it and/or modify it | ||||||||||||
| 280 | under the same terms as Perl itself. | ||||||||||||
| 281 | |||||||||||||
| 282 | =cut | ||||||||||||
| 283 | |||||||||||||
| 284 | 11 | 'BOOYA!'; | |||||||||||
| 285 | |||||||||||||