summaryrefslogtreecommitdiff
path: root/src/main/FarmShell.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/FarmShell.java')
-rw-r--r--src/main/FarmShell.java458
1 files changed, 241 insertions, 217 deletions
diff --git a/src/main/FarmShell.java b/src/main/FarmShell.java
index 6bf350e..1f2835c 100644
--- a/src/main/FarmShell.java
+++ b/src/main/FarmShell.java
@@ -1,217 +1,241 @@
-package main;
-
-import database.ConnectionBuilder;
-import java.io.IOException;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.NoSuchElementException;
-import java.util.Scanner;
-
-/**
- *
- * @author s123188
- */
-public class FarmShell {
-
- /**
- * A scanner for the stdin.
- */
- private final Scanner scanner = new Scanner(System.in);
-
- private Analyzor cached_analyzor;
- private final ConnectionBuilder dbConnectionBuilder;
-
- FarmShell(ConnectionBuilder dbConnectionBuilder) {
- this.dbConnectionBuilder = dbConnectionBuilder;
- }
-
- private void printPrompt() {
- System.out.print("$ ");
- }
-
- private Analyzor getAnalyzor() throws SQLException {
- if (cached_analyzor == null) {
- Connection dbCon = dbConnectionBuilder.create();
- cached_analyzor = new Analyzor(dbCon);
- }
- return cached_analyzor;
- }
-
- /**
- * Processes commands from stdin until the exit command is received or EOF.
- */
- public void process_forever() {
- System.err.println("Entering interactive shell, type 'help' for help "
- + "or 'exit' to leave. '.' repeats the previous interactive "
- + "command.");
- // print prompt for reading first command
- printPrompt();
- String lastLine = "";
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine().trim();
- // repeat last command
- if (line.equals(".")) {
- line = lastLine;
- }
- if (!execute(line)) {
- // requested to terminate
- break;
- }
- if (!line.isEmpty()) {
- lastLine = line;
- }
- // print prompt for reading next line
- printPrompt();
- }
- }
-
- /**
- * Execute a single commands.
- *
- * @param cmd A single line of the command.
- * @return Whether to continue or exit the application.
- */
- public boolean execute(String cmd) {
- String[] args = cmd.trim().split("\\s+", 2);
- if (!args[0].isEmpty()) {
- // non-empty command, let's see whether it makes sense?
- return execute(args);
- }
- return true;
- }
-
- /**
- * Executes a command with optional parameters.
- *
- * @param args An array with the first argument containing the command with
- * optional parameters in following arguments.
- * @return true if more commands are allowed to be executed, false
- * otherwise.
- */
- public boolean execute(String[] args) {
- try {
- Command command = Command.fromString(args[0]);
- String[] params = Arrays.copyOfRange(args, 1, args.length);
- execute(command, params);
- } catch (IllegalArgumentException ex) {
- System.err.println(ex.getMessage());
- } catch (IOException ex) {
- System.err.println("Command " + args[0] + " failed with " + ex);
- ex.printStackTrace();
- } catch (NoSuchElementException ex) {
- if ("EXIT NOW".equals(ex.getMessage())) {
- // thrown by the "exit" command to signal exit
- return false;
- } else {
- System.err.println("ZOMG SOMETHIGN FAILED: " + ex.getMessage());
- ex.printStackTrace();
- }
- } catch (SQLException ex) {
- System.err.println("such " + ex);
- }
- // another satisfied customer, next!
- return true;
- }
-
- private void execute(Command command, String[] params) throws SQLException, IOException {
- if (params.length < command.getParamCount()) {
- throw new IllegalArgumentException("Expected "
- + command.getParamCount() + " parameters, got only "
- + params.length);
- }
- switch (command) {
- case filterbots:
- System.out.println("not yet implemented");
- break;
- case sentiment:
- // if there is no query, update all unrated items.
- if (params.length > 0) {
- getAnalyzor().sentimentAnalysis(params[0]);
- } else {
- getAnalyzor().sentimentAnalysis("");
- }
- break;
- case wordcloud:
- getAnalyzor().makeWordCloud(params[0]);
- break;
- case timezone:
- getAnalyzor().timezone(params[0]);
- case disco:
- getAnalyzor().disco(params[0]);
- break;
- case getBrands:
- getAnalyzor().getBrands();
- break;
- case help:
- for (String line : HELP) {
- System.out.println(line);
- }
- for (Command cmd : Command.values()) {
- System.out.printf(" %-10s", cmd.name());
- if (!cmd.getDescription().isEmpty()) {
- System.out.print(" " + cmd.getDescription());
- }
- if (cmd.getParamCount() == 1) {
- System.out.print(" (1 arg)");
- } else if (cmd.getParamCount() > 1) {
- System.out.printf(" (%d args)", cmd.getParamCount());
- }
- System.out.println();
- }
- break;
- case exit:
- throw new NoSuchElementException("EXIT NOW");
- default:
- throw new AssertionError(command.name());
- }
- }
-
- enum Command {
-
- filterbots("marks all users as bot or not", 1),
- sentiment("analyzes all tweets on brand positivity (optional arg: tweet/brand selection query)"),
- wordcloud("makes a wordcloud of the text of the tweets", 1),
- getBrands("fills the database with the brands of a tweet"),
- timezone("makes a map per brand for the users", 1),
- disco("makes a outputfile for disco", 1),
- exit("Returns to shell"),
- help("Get help");
-
- private final String description;
- private final int paramCount;
-
- Command(String description) {
- this.description = description;
- this.paramCount = 0;
- }
-
- Command(String description, int paramCount) {
- this.description = description;
- this.paramCount = paramCount;
- }
-
- public String getDescription() {
- return description;
- }
-
- public int getParamCount() {
- return paramCount;
- }
-
- public static Command fromString(String command) {
- for (Command cmd : values()) {
- if (cmd.name().equals(command)) {
- return cmd;
- }
- }
- throw new IllegalArgumentException("Unrecognized command. Hint: help");
- }
- };
-
- private final String[] HELP = new String[]{
- "Interactive TweetShell",
- "",
- "Available commands:"
- };
-}
+package main;
+
+import database.ConnectionBuilder;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.NoSuchElementException;
+import java.util.Scanner;
+
+/**
+ *
+ * @author s123188
+ */
+public class FarmShell {
+
+ /**
+ * A scanner for the stdin.
+ */
+ private final Scanner scanner = new Scanner(System.in);
+
+ private Analyzor cached_analyzor;
+ private final ConnectionBuilder dbConnectionBuilder;
+
+ FarmShell(ConnectionBuilder dbConnectionBuilder) {
+ this.dbConnectionBuilder = dbConnectionBuilder;
+ }
+
+ private void printPrompt() {
+ System.out.print("$ ");
+ }
+
+ private Analyzor getAnalyzor() throws SQLException {
+ if (cached_analyzor == null) {
+ Connection dbCon = dbConnectionBuilder.create();
+ cached_analyzor = new Analyzor(dbCon);
+ }
+ return cached_analyzor;
+ }
+
+ /**
+ * Processes commands from stdin until the exit command is received or EOF.
+ */
+ public void process_forever() {
+ System.err.println("Entering interactive shell, type 'help' for help "
+ + "or 'exit' to leave. '.' repeats the previous interactive "
+ + "command.");
+ // print prompt for reading first command
+ printPrompt();
+ String lastLine = "";
+ while (scanner.hasNextLine()) {
+ String line = scanner.nextLine().trim();
+ // repeat last command
+ if (line.equals(".")) {
+ line = lastLine;
+ }
+ if (!execute(line)) {
+ // requested to terminate
+ break;
+ }
+ if (!line.isEmpty()) {
+ lastLine = line;
+ }
+ // print prompt for reading next line
+ printPrompt();
+ }
+ }
+
+ /**
+ * Execute a single commands.
+ *
+ * @param cmd A single line of the command.
+ * @return Whether to continue or exit the application.
+ */
+ public boolean execute(String cmd) {
+ String[] args = cmd.trim().split("\\s+", 2);
+ if (!args[0].isEmpty()) {
+ // non-empty command, let's see whether it makes sense?
+ return execute(args);
+ }
+ return true;
+ }
+
+ /**
+ * Executes a command with optional parameters.
+ *
+ * @param args An array with the first argument containing the command with
+ * optional parameters in following arguments.
+ * @return true if more commands are allowed to be executed, false
+ * otherwise.
+ */
+ public boolean execute(String[] args) {
+ try {
+ Command command = Command.fromString(args[0]);
+ String[] params = Arrays.copyOfRange(args, 1, args.length);
+ execute(command, params);
+ } catch (IllegalArgumentException ex) {
+ System.err.println(ex.getMessage());
+ } catch (IOException ex) {
+ System.err.println("Command " + args[0] + " failed with " + ex);
+ ex.printStackTrace();
+ } catch (NoSuchElementException ex) {
+ if ("EXIT NOW".equals(ex.getMessage())) {
+ // thrown by the "exit" command to signal exit
+ return false;
+ } else {
+ System.err.println("ZOMG SOMETHIGN FAILED: " + ex.getMessage());
+ ex.printStackTrace();
+ }
+ } catch (SQLException ex) {
+ System.err.println("such " + ex);
+ }
+ // another satisfied customer, next!
+ return true;
+ }
+
+ private void execute(Command command, String[] params) throws SQLException, IOException {
+ if (params.length < command.getParamCount()) {
+ throw new IllegalArgumentException("Expected "
+ + command.getParamCount() + " parameters, got only "
+ + params.length);
+ }
+ switch (command) {
+ case filterbots:
+ System.out.println("not yet implemented");
+ break;
+ case sentiment:
+ // if there is no query, update all unrated items.
+ if (params.length > 0) {
+ getAnalyzor().sentimentAnalysis(params[0]);
+ } else {
+ getAnalyzor().sentimentAnalysis("");
+ }
+ break;
+ case wordcloud:
+ getAnalyzor().makeWordCloud(params[0]);
+ break;
+ case timezone:
+ getAnalyzor().timezone(params[0]);
+ case disco:
+ getAnalyzor().disco(params[0]);
+ break;
+ case getBrands:
+ String trimmed = params[0].trim();
+ String bool = trimmed;
+ String query = null;
+
+ int index = trimmed.indexOf(" ");
+
+ if (index > -1) {
+ bool = trimmed.substring(0, index);
+ query = trimmed.substring(index + 1, trimmed.length());
+ }
+
+ boolean reset = false;
+ if (bool.equals("true")) {
+ reset = true;
+ } else if (bool.equals("false")){
+ reset = false;
+ } else {
+ throw new IllegalArgumentException("getBrands: expected boolean, got " + params[0]);
+ }
+
+ if (query != null) {
+ getAnalyzor().getBrands(query, reset);
+ } else {
+ getAnalyzor().getBrands("", reset);
+ }
+ break;
+ case help:
+ for (String line : HELP) {
+ System.out.println(line);
+ }
+ for (Command cmd : Command.values()) {
+ System.out.printf(" %-10s", cmd.name());
+ if (!cmd.getDescription().isEmpty()) {
+ System.out.print(" " + cmd.getDescription());
+ }
+ if (cmd.getParamCount() == 1) {
+ System.out.print(" (1 arg)");
+ } else if (cmd.getParamCount() > 1) {
+ System.out.printf(" (%d args)", cmd.getParamCount());
+ }
+ System.out.println();
+ }
+ break;
+ case exit:
+ throw new NoSuchElementException("EXIT NOW");
+ default:
+ throw new AssertionError(command.name());
+ }
+ }
+
+ enum Command {
+
+ filterbots("marks all users as bot or not", 1),
+ sentiment("analyzes all tweets on brand positivity (optional arg: tweet/brand selection query)"),
+ wordcloud("makes a wordcloud of the text of the tweets", 1),
+ getBrands("fills the database with the brands of a tweet, arg: bool indicating whether to reset mentionsbrand (optional arg: tweet selection query", 1),
+ timezone("makes a map per brand for the users", 1),
+ disco("makes a outputfile for disco", 1),
+ exit("Returns to shell"),
+ help("Get help");
+
+ private final String description;
+ private final int paramCount;
+
+ Command(String description) {
+ this.description = description;
+ this.paramCount = 0;
+ }
+
+ Command(String description, int paramCount) {
+ this.description = description;
+ this.paramCount = paramCount;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public int getParamCount() {
+ return paramCount;
+ }
+
+ public static Command fromString(String command) {
+ for (Command cmd : values()) {
+ if (cmd.name().equals(command)) {
+ return cmd;
+ }
+ }
+ throw new IllegalArgumentException("Unrecognized command. Hint: help");
+ }
+ };
+
+ private final String[] HELP = new String[]{
+ "Interactive TweetShell",
+ "",
+ "Available commands:"
+ };
+}