Cleaned up structure.

This commit is contained in:
Andrew Lalis 2021-03-09 14:13:52 +01:00
parent 684e47e00c
commit e0229e3cf5
4 changed files with 55 additions and 42 deletions

View File

@ -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<Human, Float> nameWeightMap = fileParser.parseHumanList(cmd.getOptionValue("hl"));
Set<Task> tasks = fileParser.parseTaskList(cmd.getOptionValue("tl"));
String[] previousDistributionPaths = cmd.getOptionValues("prev");
if (previousDistributionPaths == null) previousDistributionPaths = new String[0];
List<Map<Human, Set<Task>>> previousDistributions = fileParser.parsePreviousTaskDistributions(previousDistributionPaths);
long start = System.currentTimeMillis();
Map<Human, Set<Task>> 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);

View File

@ -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);
}

View File

@ -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<Human, Float> nameWeightMap = fileParser.parseHumanList(cmd.getOptionValue("hl"));
Set<Task> tasks = fileParser.parseTaskList(cmd.getOptionValue("tl"));
String[] previousDistributionPaths = cmd.getOptionValues("prev");
if (previousDistributionPaths == null) previousDistributionPaths = new String[0];
List<Map<Human, Set<Task>>> previousDistributions = fileParser.parsePreviousTaskDistributions(previousDistributionPaths);
long start = System.currentTimeMillis();
Map<Human, Set<Task>> 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());
}
}
}

View File

@ -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<Task> tasks = new FileParser().parseTaskList(filePath, regex);
System.out.println("Read " + tasks.size() + " tasks from file.");
String outFilePath = filePath.replaceFirst("\\..*", ".csv");