From cfd69d5a555bc4d5e7df21d266454a13ac617531 Mon Sep 17 00:00:00 2001 From: Julia McCauley Date: Wed, 31 Mar 2021 14:58:57 -0400 Subject: set up basic repl/command structure for ease of testing backend --- src/main/java/edu/brown/cs/student/stars/Main.java | 62 ---------------------- src/main/java/edu/brown/cs/student/term/Main.java | 54 +++++++++++++++++++ .../edu/brown/cs/student/term/repl/Command.java | 19 +++++++ .../java/edu/brown/cs/student/term/repl/REPL.java | 59 ++++++++++++++++++++ 4 files changed, 132 insertions(+), 62 deletions(-) delete mode 100644 src/main/java/edu/brown/cs/student/stars/Main.java create mode 100644 src/main/java/edu/brown/cs/student/term/Main.java create mode 100644 src/main/java/edu/brown/cs/student/term/repl/Command.java create mode 100644 src/main/java/edu/brown/cs/student/term/repl/REPL.java (limited to 'src/main/java/edu/brown') diff --git a/src/main/java/edu/brown/cs/student/stars/Main.java b/src/main/java/edu/brown/cs/student/stars/Main.java deleted file mode 100644 index 61127b5..0000000 --- a/src/main/java/edu/brown/cs/student/stars/Main.java +++ /dev/null @@ -1,62 +0,0 @@ -package edu.brown.cs.student.stars; - -import java.io.File; -import java.io.IOException; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Map; - -import joptsimple.OptionParser; -import joptsimple.OptionSet; -import spark.ExceptionHandler; -import spark.ModelAndView; -import spark.Request; -import spark.Response; -import spark.Spark; -import spark.TemplateViewRoute; -import spark.template.freemarker.FreeMarkerEngine; - -import com.google.common.collect.ImmutableMap; - -import freemarker.template.Configuration; - -/** - * The Main class of our project. This is where execution begins. - * - */ -public final class Main { - - private static final int DEFAULT_PORT = 4567; - - /** - * The initial method called when execution begins. - * - * @param args - * An array of command line arguments - */ - public static void main(String[] args) { - new Main(args).run(); - } - - private String[] args; - - private Main(String[] args) { - this.args = args; - } - - private void run() { - // Parse command line arguments - OptionParser parser = new OptionParser(); - parser.accepts("gui"); - parser.accepts("port").withRequiredArg().ofType(Integer.class) - .defaultsTo(DEFAULT_PORT); - OptionSet options = parser.parse(args); - - if (options.has("gui")) { - //do a gui type thing - //runSparkServer((int) options.valueOf("port")); - } - - // TODO: Process commands in a REPL - } -} \ No newline at end of file diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java new file mode 100644 index 0000000..c436b2e --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/Main.java @@ -0,0 +1,54 @@ +package edu.brown.cs.student.term; + +import edu.brown.cs.student.term.repl.Command; +import edu.brown.cs.student.term.repl.REPL; +import joptsimple.OptionParser; +import joptsimple.OptionSet; + +import java.util.HashMap; + +/** + * The Main class of our project. This is where execution begins. + * + */ +public final class Main { + + private static final int DEFAULT_PORT = 4567; + + /** + * The initial method called when execution begins. + * + * @param args + * An array of command line arguments + */ + public static void main(String[] args) { + new Main(args).run(); + } + + private String[] args; + + private Main(String[] args) { + this.args = args; + } + + private void run() { + // Parse command line arguments + OptionParser parser = new OptionParser(); + parser.accepts("gui"); + parser.accepts("port").withRequiredArg().ofType(Integer.class) + .defaultsTo(DEFAULT_PORT); + OptionSet options = parser.parse(args); + + if (options.has("gui")) { + //do a gui type thing + //runSparkServer((int) options.valueOf("port")); + } + + HashMap commandHashMap = new HashMap<>(); + /** add commands to map here! */ + REPL repl = new REPL(commandHashMap); + repl.runREPL(); + + // TODO: Process commands in a REPL + } +} \ No newline at end of file diff --git a/src/main/java/edu/brown/cs/student/term/repl/Command.java b/src/main/java/edu/brown/cs/student/term/repl/Command.java new file mode 100644 index 0000000..f8b0b04 --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/repl/Command.java @@ -0,0 +1,19 @@ +package edu.brown.cs.student.term.repl; + +/** + * Interface implemented by all five StarsCommand commands. + * Implemented by StarsCommand, NaiveNeighbors, NaiveRadiusCommand, RadiusCommand, NeighborsCommand + */ +public interface Command { + /** + * Main run method for every command. + * @param args arguments for the command + */ + String run(String[] args); + + /** + * Used to print command output to GUI. + * @return String representing neighbors found by NeighborsCommand and RadiusCommand commands + */ + String toString(); +} diff --git a/src/main/java/edu/brown/cs/student/term/repl/REPL.java b/src/main/java/edu/brown/cs/student/term/repl/REPL.java new file mode 100644 index 0000000..0be7e3f --- /dev/null +++ b/src/main/java/edu/brown/cs/student/term/repl/REPL.java @@ -0,0 +1,59 @@ +package edu.brown.cs.student.term.repl; +import edu.brown.cs.student.term.repl.Command; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +/** + * REPL Class maps input to proper class and function for that command. + */ +public class REPL { + + private Map commands; + + /** + * Constructor for a REPL object. + */ + public REPL(HashMap userCommands) { + commands = userCommands; + } + + /** + * Gets the current map of commands the REPL supports. + * @return the command map + */ + public Map getReplCommandMap() { + return commands; } + + /** + * Reads user input, maps it to the correct command, and continues. + */ + public void runREPL() { + BufferedReader commandHandler = new BufferedReader(new InputStreamReader(System.in)); + try { + String command = commandHandler.readLine(); + while (command != null) { + String[] commandPieces = command.split("\\s+(?=([^\"]*\"[^\"]*\")*[^\"]*$)"); + + if (commands.containsKey(commandPieces[0])) { + Command desiredCommand = commands.get(commandPieces[0]); + + String[] arguments = Arrays.copyOfRange(commandPieces, 1, commandPieces.length); + + desiredCommand.run(arguments); + } else { + System.out.println("ERROR: Sorry, command not recognized! Please try again."); + } + + command = commandHandler.readLine(); + } + } catch (IOException e) { + //might wanna change this later + System.out.println("ERROR: Could not locate specified file!"); + } + } +} -- cgit v1.2.3-70-g09d2