diff options
| author | student <student@student-OptiPlex-9020> | 2017-03-17 10:49:27 -0400 |
|---|---|---|
| committer | student <student@student-OptiPlex-9020> | 2017-03-17 10:49:27 -0400 |
| commit | 0ea49dd4a52358f053c9be7138c392b16de05899 (patch) | |
| tree | 802e275aaf279480ee8626136f56bfa1fbab6845 /RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java | |
| parent | 36cf3a0f0604ef43ce838ff6e9a7fc4e7c299522 (diff) | |
Move things around, and start on new parser.
Diffstat (limited to 'RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java')
| -rw-r--r-- | RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java b/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java new file mode 100644 index 0000000..b3beb81 --- /dev/null +++ b/RGens/src/main/java/bjc/rgens/parser/GrammarReaderApp.java @@ -0,0 +1,93 @@ +package bjc.rgens.parser; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintStream; + +import javax.swing.UIManager; +import javax.swing.UnsupportedLookAndFeelException; + +import bjc.utils.funcutils.ListUtils; +import bjc.utils.gen.WeightedGrammar; +import bjc.utils.gui.SimpleDialogs; +import bjc.utils.gui.awt.SimpleFileDialog; + +/** + * App that reads a grammar from a file and generates results + * + * @author ben + * + */ +public class GrammarReaderApp { + /** + * Main method of class + * + * @param args + * CLI args + */ + public static void main(String[] args) { + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (ClassNotFoundException + | InstantiationException + | IllegalAccessException + | UnsupportedLookAndFeelException e) { + e.printStackTrace(); + System.exit(1); + } + + doSingleFile(); + } + + private static void doSingleFile() { + File gramFile = SimpleFileDialog.getOpenFile(null, "Choose Grammar File", ".gram"); + + WeightedGrammar<String> grammar = null; + + try { + grammar = RBGrammarReader.fromPath(gramFile.toPath()); + } catch (IOException ioex) { + ioex.printStackTrace(); + + System.exit(1); + } + + String initRule = ""; + + if (!grammar.hasInitialRule()) { + grammar.getRuleNames().sort((leftString, rightString) -> { + return leftString.compareTo(rightString); + }); + + initRule = SimpleDialogs.getChoice(null, + "Pick a initial rule", + "Pick a initial rule to generate choices from", + grammar.getRuleNames().toArray(new String[0])); + } else { + initRule = grammar.getInitialRule(); + } + + int count = SimpleDialogs.getWhole(null, + "Enter number of repetitions", + "Enter the number of items to generate from the rule"); + + File outputFile = SimpleFileDialog.getSaveFile(null, "Choose Grammar File"); + + PrintStream outputStream = null; + + try { + outputStream = new PrintStream(outputFile); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + for (int i = 0; i < count; i++) { + String ruleResult = ListUtils.collapseTokens(grammar.generateListValues(initRule, " ")); + + outputStream.println(ruleResult.replaceAll("\\s+", " ")); + } + + outputStream.close(); + } +} |
