| 1 | #!/usr/bin/env perl | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use App::War; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use Getopt::Long; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Pod::Usage; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | our $VERSION = $App::War::VERSION; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =pod | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | war - break one big decision into lots of little decisions | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | war [--help] [--man] [--version] | ||||||||||||||||||||
| 20 | war [--graph] --file sourcefile | ||||||||||||||||||||
| 21 | war [--graph] alt1 alt2 alt3 ... | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Imagine you're at a department store, trying to choose one china pattern | ||||||||||||||||||||
| 26 | out of a dozen or more alternatives. How do you do it? If none stands out | ||||||||||||||||||||
| 27 | as the clear winner, you might apply a process of elimination to your | ||||||||||||||||||||
| 28 | choices, until a winner emerges. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | This application applies a process similar to that described above: first, | ||||||||||||||||||||
| 31 | the set of alternatives is defined. Then pairs of alternatives are | ||||||||||||||||||||
| 32 | presented to the user, who chooses one "winner" from each pair. The | ||||||||||||||||||||
| 33 | process continues until an unambiguous ordering of the alternatives is | ||||||||||||||||||||
| 34 | known. | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =head1 OPTIONS | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | =over 4 | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | =item B<--file> filename, B<-f> filename | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | Reads the list of alternatives from file C<filename>, a text file | ||||||||||||||||||||
| 43 | containing the items to be ranked. Blank lines are ignored, as well as | ||||||||||||||||||||
| 44 | lines starting with the pound (C<#>) character. | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | =item B<--graph>, B<-g> | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | When the results are shown to the user, also display a textual | ||||||||||||||||||||
| 49 | representation of the preference graph. | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =item B<--help>, B<-h>, B<-?> | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | Display simple command-line options. | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | =item B<--man> | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | Display detailed script information. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =item B<--version>, B<-v> | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Shows the current script version. | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | =back | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 EXAMPLE | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | To choose our favorite bug, we could do the following: | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | % war aphid bee cricket | ||||||||||||||||||||
| 70 | Choose one of the following: | ||||||||||||||||||||
| 71 | <1> aphid | ||||||||||||||||||||
| 72 | <2> bee | ||||||||||||||||||||
| 73 | 2 | ||||||||||||||||||||
| 74 | Choose one of the following: | ||||||||||||||||||||
| 75 | <1> cricket | ||||||||||||||||||||
| 76 | <2> bee | ||||||||||||||||||||
| 77 | 1 | ||||||||||||||||||||
| 78 | ===== final preference order ===== | ||||||||||||||||||||
| 79 | cricket | ||||||||||||||||||||
| 80 | bee | ||||||||||||||||||||
| 81 | aphid | ||||||||||||||||||||
| 82 | % | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 SEE ALSO | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =over 4 | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =item L<App::War> | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | Information on access to the source code, bug tracking, and smoke testing | ||||||||||||||||||||
| 91 | can be found in the L<App::War> documentation. | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | =item L<http://en.wikipedia.org/wiki/Paired_comparison> | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =item L<http://en.wikipedia.org/wiki/Law_of_comparative_judgment> | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | =item L<http://en.wikipedia.org/wiki/Analytic_Hierarchy_Process> | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =item L<http://www.kittenwar.com> | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head1 AUTHOR | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | John Trammell, C<< <johntrammell@gmail.com> >> | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 1 | our %OPT = (); | |||||||||||||||||||
| 110 | 1 | my @OPTIONS = qw/ file|f=s graph|g help|h|? man version|v /; | |||||||||||||||||||
| 111 | 1 | ▶ | GetOptions(\%OPT,@OPTIONS) || pod2usage; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 0 | ▶ | if ($OPT{version}) { die "Using App::War v$VERSION\n" } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 0 | ▶ | if ($OPT{man}) { pod2usage(-verbose => 2); } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 0 | ▶ | if ($OPT{help}) { pod2usage(-exitval => 0); } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | # get items | ||||||||||||||||||||
| 117 | 1 | my @items = _items(); | |||||||||||||||||||
| 118 | 1 | ▶ | unless (@items) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | 1 | pod2usage(-msg => 'No items specified.', -verbose => 0, -exitval => 0); | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # fight the war | ||||||||||||||||||||
| 123 | 0 | my $war = App::War->new(items => \@items)->run; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | # print the results | ||||||||||||||||||||
| 126 | 0 | report($war); | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | # read the items from the file specified on the command line | ||||||||||||||||||||
| 129 | 1 | ▶ | sub _items { | ||||||||||||||||||
|
Subroutine
_items: called CC=4 SCAR=20.4
| |||||||||||||||||||||
| 130 | 1 | ▶ | return @ARGV if @ARGV; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 1 | ▶ | open(my $h,q(<),$OPT{file}) or die $!; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 0 | ▶ | return grep { /\S/ && !/^#/ } map { chomp; $_ } <$h>; | ||||||||||||||||||
|
Condition0%0/3
/\S/ && !/^#/
MC/DC0%0/2
/\S/ && !/^#/
/\S/
!/^#/
Truth table0%0/3
A && not(B)
A= /\S/B= !/^#/
| |||||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | ▶ | sub report { | ||||||||||||||||||
|
Subroutine
report: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 136 | 0 | my $war = shift; | |||||||||||||||||||
| 137 | 0 | ▶ | if ($OPT{graph}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 138 | 0 | print "graph: @{[ $war->graph ]}\n"; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 0 | my @items = $war->items; | |||||||||||||||||||
| 141 | 0 | print "===== final preference order =====\n"; | |||||||||||||||||||
| 142 | 0 | for (map { $items[$_] } $war->graph->topological_sort) { | |||||||||||||||||||
| 143 | 0 | print $_, "\n"; | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||