Commit 8fad5ad4 authored by Jonathan Shahen's avatar Jonathan Shahen

better input method (don't need to create a run configuration); more control...

better input method (don't need to create a run configuration); more control over the ConvertTo classes
parent 76cdbd0d
package mohawk.converter;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Logger;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.lang3.StringUtils;
public class ConverterCUI {
public static final Logger logger = Logger.getLogger("mohawk-converter");
public static void main(String[] args) {
ConverterInstance inst = new ConverterInstance();
ArrayList<String> argv = new ArrayList<String>();
String cmd = "";
Scanner user_input = new Scanner(System.in);
Options options = new Options();
inst.setupOptions(options);
HelpFormatter f = new HelpFormatter();
f.printHelp(120, "mohawk", StringUtils.repeat("-", 80), options, StringUtils.repeat("-", 80), true);
System.out.print("Enter Commandline Argument ('!exit' to run): ");
while (true) {
cmd = user_input.next();
if (cmd.equals("!exit")) {
break;
}
argv.add(cmd);
}
inst.run(argv.toArray(new String[1]));
}
}
......@@ -45,7 +45,7 @@ public class ConverterInstance {
public int run(String[] args) {
try {
Options options = new Options();
setupOptions(options, args);
setupOptions(options);
CommandLineParser cmdParser = new BasicParser();
CommandLine cmd = cmdParser.parse(options, args);
......@@ -95,10 +95,11 @@ public class ConverterInstance {
ConvertToRanise toRanise = new ConvertToRanise();
String raniseStr = toRanise.convert(parser, specFile, true);
if (!raniseStr.startsWith("Error")) {
if (toRanise.lastError == null) {
logger.info("[SUCCESS] Successfully converted to Ranise's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Raines's Input Format; Error code " + raniseStr);
logger.warning("[FAILURE] Failed to convert to Raines's Input Format; Error code "
+ toRanise.lastError);
}
/* Timing */timing.stopTimer("convertToRanise (" + i + ")");
......@@ -115,10 +116,11 @@ public class ConverterInstance {
ConvertToUzun toUzun = new ConvertToUzun();
String uzunStr = toUzun.convert(parser, specFile, true);
if (!uzunStr.startsWith("Error")) {
if (toUzun.lastError == null) {
logger.info("[SUCCESS] Successfully converted to Uzun's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Uzun's Input Format; Error code " + uzunStr);
logger.warning("[FAILURE] Failed to convert to Uzun's Input Format; Error code "
+ toUzun.lastError);
}
/* Timing */timing.stopTimer("convertToUzun (" + i + ")");
......@@ -135,10 +137,11 @@ public class ConverterInstance {
ConvertToMohawk toMohawk = new ConvertToMohawk();
String mohawkStr = toMohawk.convert(parser, specFile, true);
if (!mohawkStr.startsWith("Error")) {
if (toMohawk.lastError == null) {
logger.info("[SUCCESS] Successfully converted to Mohawk's Input Format");
} else {
logger.warning("[FAILURE] Failed to convert to Mohawk's Input Format; Error code " + mohawkStr);
logger.warning("[FAILURE] Failed to convert to Mohawk's Input Format; Error code "
+ toMohawk.lastError);
}
/* Timing */timing.stopTimer("convertToMohawk (" + i + ")");
......@@ -157,11 +160,13 @@ public class ConverterInstance {
}
logger.severe(e.getMessage());
}
logger.info("Converter Instance done running");
return 0;
}
@SuppressWarnings("static-access")
private void setupOptions(Options options, String[] args) {
public void setupOptions(Options options) {
// Add Information Options
options.addOption("help", false, "Print this message");
options.addOption("authors", false, "Prints the authors");
......
......@@ -10,10 +10,17 @@ import java.nio.file.Paths;
import mohawk.converter.generated.MohawkTARBACParser;
public interface ConvertTo {
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile);
public class ConvertTo {
public String convertedStr = null;
public String lastError = null;
public String getFileExtenstion();
public String convert(MohawkTARBACParser p, File f, Boolean writeToFile) {
return null;
}
public String getFileExtenstion() {
return null;
}
// public File getTemplateFile() throws IOException;
......
......@@ -12,7 +12,7 @@ import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToMohawk implements ConvertTo {
public class ConvertToMohawk extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
......@@ -31,7 +31,7 @@ public class ConvertToMohawk implements ConvertTo {
st.add("goalRole", null);
st.add("goalTimeslot", null);
String convertedStr = st.render();
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
......@@ -42,15 +42,16 @@ public class ConvertToMohawk implements ConvertTo {
Files.write(convertedFile.toPath(), convertedStr.getBytes());
}
return convertedStr;
lastError = null;
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.fine(errors.toString());
logger.warning("[ERROR] Unable to convert to Ranise: " + e.getMessage());
return "Error ConvertToRanise.convert.Exception #1";
logger.warning("[ERROR] Unable to convert to Mohawk: " + e.getMessage());
lastError = "Error ConvertToRanise.convert.Exception #1";
}
return convertedStr;
}
}
......@@ -12,7 +12,7 @@ import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToRanise implements ConvertTo {
public class ConvertToRanise extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
......@@ -22,11 +22,11 @@ public class ConvertToRanise implements ConvertTo {
Charset.defaultCharset());
ST st = new ST(template);
st.add("numRoles", p.roleHelper.size());
st.add("numTimeslots", null);
st.add("numTimeslots", p.timeIntervalHelper.size());
st.add("goalRole", null);
st.add("goalTimeslot", null);
String convertedStr = st.render();
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
......@@ -36,16 +36,17 @@ public class ConvertToRanise implements ConvertTo {
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
}
return convertedStr;
lastError = null;
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.fine(errors.toString());
logger.warning("[ERROR] Unable to convert to Ranise: " + e.getMessage());
return "Error ConvertToRanise.convert.Exception #1";
lastError = "Error ConvertToRanise.convert.Exception #1";
}
return convertedStr;
}
@Override
......
package mohawk.converter.to.uzun;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
......@@ -14,7 +15,7 @@ import mohawk.converter.to.ConvertTo;
import org.stringtemplate.v4.ST;
public class ConvertToUzun implements ConvertTo {
public class ConvertToUzun extends ConvertTo {
public static final Logger logger = Logger.getLogger("mohawk-converter");
@Override
......@@ -43,26 +44,39 @@ public class ConvertToUzun implements ConvertTo {
st.add("rules", new UzunRule(r, p.roleHelper, p.timeIntervalHelper));
}
String convertedStr = st.render();
convertedStr = st.render();
if (writeToFile) {
File convertedFile = new File(f.getAbsolutePath() + getFileExtenstion());
logger.info("[SAVE OUT]Saving converted format to file: " + convertedFile.getAbsolutePath());
if (!convertedFile.exists()) {
convertedFile.createNewFile();
}
Files.write(convertedFile.toPath(), convertedStr.getBytes());
} else {
logger.info("[SAVE OUT] Skipping saving this to file");
}
return convertedStr;
lastError = null;
} catch (IOException e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.fine(errors.toString());
logger.warning("[ERROR] Unable to save output to file destination!\n" + convertedStr);
lastError = "Error ConvertToRanise.convert.IOException Unable to save converted string to file";
} catch (Exception e) {
StringWriter errors = new StringWriter();
e.printStackTrace(new PrintWriter(errors));
logger.fine(errors.toString());
logger.warning("[ERROR] Unable to convert to Ranise: " + e.getMessage());
return "Error ConvertToRanise.convert.Exception #1";
logger.warning("[ERROR] Unable to convert to Uzun: " + e.getMessage());
lastError = "Error ConvertToRanise.convert.Exception #1";
}
return convertedStr;
}
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment