Ccodepackage Com
Ccodepackage Com
electionpredictor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import com.electionpredictor.file.FileHandler;
import com.electionpredictor.instance.Election;
import com.electionpredictor.instance.Instance;
import com.electionpredictor.instance.PartyID;
import com.electionpredictor.predictors.ElectionRange;
import com.electionpredictor.predictors.PartyRange;
import com.electionpredictor.predictors.Prediction;
import com.electionpredictor.ui.Application;
/**
* Get the ball rolling!
*
* @author Niels Stchedroff
*/
public class KickOff
{
private static final String AP = "ap";
private static final String F = "f";
private static final String L = "l";
private static Options mOptions;
private static final String P1 = "p1";
private static final String P2 = "p2";
private static final int PARTY_RANGE_ARGUMENT_COUNT = 4;
/**
* @param args
* @throws IOException
*/
public static void main(final String[] args) throws IOException
{
if ((args == null) || (args.length == 0))
{
Application.runApplication();
}
else
{
KickOff.runFromCommandLine(args);
}
}
Package com 5
if (inst == null) { throw new IllegalArgumentException("Instance is null!"); }
try
{
partyName = PartyID.valueOf(option.getValues()[0]);
}
catch (final IllegalArgumentException exp)
{
throw new IllegalArgumentException("Option " + option.getOpt() + " must
have a valid party - value was " + option.getValues()[0], exp);
}
try
{
min = Double.valueOf(option.getValues()[1]);
}
catch (final NumberFormatException numExp)
{
throw new NumberFormatException("Party " + partyName + " must have a
valid number for minimum support! '" + option.getValues()[1]
+ "' is not valid.");
}
try
{
max = Double.valueOf(option.getValues()[2]);
}
catch (final NumberFormatException numExp2)
{
throw new NumberFormatException("Party " + partyName + " must have a
valid number for maximum support! '" + option.getValues()[2]
+ "' is not valid.");
}
try
{
intervals = Integer.valueOf(option.getValues()[3]);
}
catch (final NumberFormatException numExp2)
{
throw new NumberFormatException("Party " + partyName + " must have a
valid number for intervals! '" + option.getValues()[3]
+ "' is not valid.");
Package com 5
}
if (min < 0) { throw new IllegalArgumentException("Party " + partyName + " must have
a positive minimum support!"); }
if (max < min) { throw new IllegalArgumentException("Party " + partyName + " must
have a max that is greater than min!"); }
if ((min > 100) || (max > 100)) { throw new IllegalArgumentException("Party " +
partyName + " must have max and imn that are less than 100%!"); }
return result;
}
try
{
partyName = PartyID.valueOf(option.getValues()[0]);
}
catch (final IllegalArgumentException exp)
{
throw new IllegalArgumentException("Option " + option.getOpt() + " must
have a valid party - value was " + option.getValues()[0], exp);
}
return partyName;
}
try
{
Package com 5
final CommandLine cmd = parser.parse(KickOff.mOptions, args);
final Option[] options = cmd.getOptions();
if (option.getOpt().equalsIgnoreCase(KickOff.P2))
{
partyB = KickOff.buildRangeFromOption(instance, option);
}
if (option.getOpt().equalsIgnoreCase(KickOff.AP))
{
adjustmentParty = KickOff.getPartyFromOption(option);
}
if (option.getOpt().equalsIgnoreCase(KickOff.F))
{
filePath = option.getValue();
}
if (option.getOpt().equalsIgnoreCase(KickOff.L))
{
dataDirectory = option.getValue();
}
}
results.add(0,
Prediction.displayPredictionHeader(instance.getPartyStore().getPartyList()));
FileHandler.writeDataToFile(filePath, results);
}
catch (final ParseException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
/**
Package com 5
* Construct the arguments for the command line
*/
private static void setupArguments()
{
// create Options object
KickOff.mOptions = new Options();
KickOff.mOptions.addOption(OptionBuilder.hasArgs(KickOff.PARTY_RANGE_ARGUMENT_COUNT
).isRequired().withValueSeparator(',').withDescription(
"party 1 name").create(KickOff.P1));
KickOff.mOptions.addOption(OptionBuilder.hasArgs(KickOff.PARTY_RANGE_ARGUMENT_COUNT
).isRequired().withValueSeparator(',').withDescription(
"party 2 name").create(KickOff.P2));
KickOff.mOptions.addOption(OptionBuilder.hasArgs(1).isRequired().withValueSeparator(',').withDescript
ion("adjument party name").create(
KickOff.AP));
KickOff.mOptions.addOption(OptionBuilder.hasArgs(1).isRequired().withValueSeparator(',').withDescript
ion("output file name")
.create(KickOff.F));
KickOff.mOptions.addOption(OptionBuilder.hasArgs(1).isRequired().withValueSeparator(',').withDescript
ion("last election").create(KickOff.L));
}
Package com 5