From e0229e3cf57dd132d02c04931c41ff73b8859794 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Tue, 9 Mar 2021 14:13:52 +0100 Subject: [PATCH] Cleaned up structure. --- .../HumanTaskDistributor.java | 42 +++---------------- .../commands/Command.java | 4 +- .../commands/DistributeTasks.java | 41 ++++++++++++++++++ .../commands/PrepareTasksList.java | 10 +++-- 4 files changed, 55 insertions(+), 42 deletions(-) create mode 100644 src/main/java/nl/andrewlalis/human_task_distributor/commands/DistributeTasks.java diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java b/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java index 6a9242a..5ffcdb8 100644 --- a/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java +++ b/src/main/java/nl/andrewlalis/human_task_distributor/HumanTaskDistributor.java @@ -1,13 +1,10 @@ package nl.andrewlalis.human_task_distributor; +import nl.andrewlalis.human_task_distributor.commands.DistributeTasks; import nl.andrewlalis.human_task_distributor.commands.PrepareTasksList; import org.apache.commons.cli.*; import org.apache.commons.csv.CSVFormat; -import java.util.List; -import java.util.Map; -import java.util.Set; - public class HumanTaskDistributor { public static final CSVFormat CSV_FORMAT = CSVFormat.RFC4180; @@ -15,44 +12,15 @@ public class HumanTaskDistributor { final Options options = getOptions(); CommandLineParser cmdParser = new DefaultParser(); try { - - FileParser fileParser = new FileParser(); - FileWriter fileWriter = new FileWriter(); CommandLine cmd = cmdParser.parse(options, args); if (cmd.hasOption("ptl")) { - String[] values = cmd.getOptionValues("ptl"); - new PrepareTasksList().execute(values); - return; + new PrepareTasksList().execute(cmd); + } else if (cmd.hasOption("hl") && cmd.hasOption("tl")) { + new DistributeTasks().execute(cmd); } - - if (!cmd.hasOption("hl") || !cmd.hasOption("tl")) { - throw new IllegalArgumentException("When not preparing a tasks-list, hl and tl are required."); - } - - Map nameWeightMap = fileParser.parseHumanList(cmd.getOptionValue("hl")); - Set tasks = fileParser.parseTaskList(cmd.getOptionValue("tl")); - String[] previousDistributionPaths = cmd.getOptionValues("prev"); - if (previousDistributionPaths == null) previousDistributionPaths = new String[0]; - List>> previousDistributions = fileParser.parsePreviousTaskDistributions(previousDistributionPaths); - - long start = System.currentTimeMillis(); - Map> taskDistributions = new Distributor().generateDistribution(nameWeightMap, tasks, previousDistributions); - long durationMillis = System.currentTimeMillis() - start; - System.out.printf( - "Completed distribution of %d tasks to %d people in %d ms.%n", - tasks.size(), - taskDistributions.keySet().size(), - durationMillis - ); - - // Write to a file. - final String filePath = cmd.hasOption("o") ? cmd.getOptionValue("o") : "distribution.csv"; - fileWriter.write(taskDistributions, filePath); - System.out.println("Wrote task distribution data to " + filePath); - + throw new IllegalArgumentException("Invalid command."); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); - e.printStackTrace(); HelpFormatter hf = new HelpFormatter(); hf.printHelp("HumanTaskDistributor", options); System.exit(1); diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java b/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java index dcaadd8..88a43d1 100644 --- a/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java +++ b/src/main/java/nl/andrewlalis/human_task_distributor/commands/Command.java @@ -1,6 +1,8 @@ package nl.andrewlalis.human_task_distributor.commands; +import org.apache.commons.cli.CommandLine; + public interface Command { - void execute(String[] args); + void execute(CommandLine cmd); } diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/commands/DistributeTasks.java b/src/main/java/nl/andrewlalis/human_task_distributor/commands/DistributeTasks.java new file mode 100644 index 0000000..6e88b95 --- /dev/null +++ b/src/main/java/nl/andrewlalis/human_task_distributor/commands/DistributeTasks.java @@ -0,0 +1,41 @@ +package nl.andrewlalis.human_task_distributor.commands; + +import nl.andrewlalis.human_task_distributor.*; +import org.apache.commons.cli.CommandLine; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class DistributeTasks implements Command { + @Override + public void execute(CommandLine cmd) { + final FileParser fileParser = new FileParser(); + final FileWriter fileWriter = new FileWriter(); + Map nameWeightMap = fileParser.parseHumanList(cmd.getOptionValue("hl")); + Set tasks = fileParser.parseTaskList(cmd.getOptionValue("tl")); + String[] previousDistributionPaths = cmd.getOptionValues("prev"); + if (previousDistributionPaths == null) previousDistributionPaths = new String[0]; + List>> previousDistributions = fileParser.parsePreviousTaskDistributions(previousDistributionPaths); + + long start = System.currentTimeMillis(); + Map> taskDistributions = new Distributor().generateDistribution(nameWeightMap, tasks, previousDistributions); + long durationMillis = System.currentTimeMillis() - start; + System.out.printf( + "Created distribution of %d tasks to %d people in %d ms.%n", + tasks.size(), + taskDistributions.keySet().size(), + durationMillis + ); + + // Write to a file. + final String filePath = cmd.hasOption("o") ? cmd.getOptionValue("o") : "distribution.csv"; + try { + fileWriter.write(taskDistributions, filePath); + System.out.println("Wrote task distribution data to " + filePath); + } catch (IOException e) { + System.err.println("Couldn't write to file: " + e.getMessage()); + } + } +} diff --git a/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java b/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java index 6cdb940..6d1e000 100644 --- a/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java +++ b/src/main/java/nl/andrewlalis/human_task_distributor/commands/PrepareTasksList.java @@ -3,18 +3,20 @@ package nl.andrewlalis.human_task_distributor.commands; import nl.andrewlalis.human_task_distributor.FileParser; import nl.andrewlalis.human_task_distributor.FileWriter; import nl.andrewlalis.human_task_distributor.Task; +import org.apache.commons.cli.CommandLine; import java.io.IOException; import java.util.Set; public class PrepareTasksList implements Command { @Override - public void execute(String[] args) { - if (args.length != 2) { + public void execute(CommandLine cmd) { + String[] values = cmd.getOptionValues("ptl"); + if (values.length != 2) { throw new IllegalArgumentException("Expected exactly 2 parameters for ptl arg."); } - String filePath = args[0].trim(); - String regex = args[1].trim(); + String filePath = values[0].trim(); + String regex = values[1].trim(); Set tasks = new FileParser().parseTaskList(filePath, regex); System.out.println("Read " + tasks.size() + " tasks from file."); String outFilePath = filePath.replaceFirst("\\..*", ".csv");