summaryrefslogtreecommitdiff
path: root/RGens
diff options
context:
space:
mode:
Diffstat (limited to 'RGens')
-rw-r--r--RGens/REPORT.md34
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/.DS_Storebin0 -> 6148 bytes
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/GrammarReader.java54
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java29
-rw-r--r--RGens/src/main/java/bjc/RGens/parser/ReaderState.java4
-rw-r--r--RGens/src/test/java/bjc/RGens/AppTest.java48
6 files changed, 126 insertions, 43 deletions
diff --git a/RGens/REPORT.md b/RGens/REPORT.md
new file mode 100644
index 0000000..d1c1a7e
--- /dev/null
+++ b/RGens/REPORT.md
@@ -0,0 +1,34 @@
+# Benjamin Culkin
+2015-12-08
+----------------
+
+Assignment #11 Report
+------------------------
+The program here is a slightly adapted version of a random string generator,
+which is itself based off of an example in the book "Multi-paradigm Programming with Leda".
+The easiest way to run it is launch the attached jar-file through the command file,
+then choose the attached .gram file as input, pick the initial rule as <item> from the drop-down
+list and enter any number. It will first print out the rules three times, once before it adds some
+dummy rules to delete, once after the dummy rules have been added, and a third after the dummy
+rules has been deleted. It will then generate text based off of the input.
+
+Collection Details
+-------------------
+The project involves the use of multiple collections, but the main one is the Hashtable inside of
+WeightedGrammar that holds the rules and the cases that belong to them. This is very similiar
+to the HashMap except for some concurrency things that aren't particularly relevant in this
+situation. The basic functionality is simply to look up objects by a key.
+
+This collection has the potential to be useful for a phonebook of some kind,
+where you want to look up people by their phone numbers, or phone numbers by the name.
+
+Resources
+-----------
+The application itself was based heavily off of an example in the above mentioned book, while
+the data for the input file came from a copy of "Diablo II: The Awakening" and its random item
+generation tables.
+
+Source Files
+-------------
+The source file of the main runnable application is the GrammarReaderApp class in bjc.RGens.text,
+while the main class that uses the collection is WeightedGrammar in bjc.utils.gen \ No newline at end of file
diff --git a/RGens/src/main/java/bjc/RGens/parser/.DS_Store b/RGens/src/main/java/bjc/RGens/parser/.DS_Store
new file mode 100644
index 0000000..76d2657
--- /dev/null
+++ b/RGens/src/main/java/bjc/RGens/parser/.DS_Store
Binary files differ
diff --git a/RGens/src/main/java/bjc/RGens/parser/GrammarReader.java b/RGens/src/main/java/bjc/RGens/parser/GrammarReader.java
index e83eede..f6aa383 100644
--- a/RGens/src/main/java/bjc/RGens/parser/GrammarReader.java
+++ b/RGens/src/main/java/bjc/RGens/parser/GrammarReader.java
@@ -93,32 +93,62 @@ public class GrammarReader {
private static void initPragmas() {
pragmaMap = new Hashtable<>();
+ addSubgrammarPragmas();
+
+ pragmaMap.put("debug", GrammarReader::debugGrammar);
+ pragmaMap.put("import-rule", GrammarReader::importRule);
+ pragmaMap.put("initial-rule", GrammarReader::initialRule);
pragmaMap.put("uniform", (stk, rs) -> rs.toggleUniformity());
- pragmaMap.put("subordinate", GrammarReader::subordinateGrammar);
+
+ pragmaMap.put("multi-prefix-with", GrammarReader::multiPrefixRule);
+ pragmaMap.put("multi-suffix-with", GrammarReader::multiSuffixRule);
+
+ pragmaMap.put("prefix-with", GrammarReader::prefixRule);
+ pragmaMap.put("suffix-with", GrammarReader::suffixRule);
+ }
+
+ private static void multiPrefixRule(StringTokenizer stk,
+ ReaderState rs) {
+
+ }
+
+ private static void multiSuffixRule(StringTokenizer stk,
+ ReaderState rs) {
+
+ }
+
+ private static void addSubgrammarPragmas() {
+ pragmaMap.put("edit-sub-grammar", GrammarReader::editSubGrammar);
+ pragmaMap.put("edit-parent", (stk, rs) -> rs.popGrammar());
+
+ pragmaMap.put("load-sub-grammar", GrammarReader::loadSubGrammar);
+
+ pragmaMap.put("new-sub-grammar",
+ (stk, rs) -> rs.pushGrammar(new WeightedGrammar<>()));
+
pragmaMap.put("promote", GrammarReader::promoteGrammar);
+
pragmaMap.put("remove-sub-grammar",
GrammarReader::removeSubGrammar);
pragmaMap.put("remove-rule", GrammarReader::removeRule);
- pragmaMap.put("load-sub-grammar", GrammarReader::loadSubGrammar);
- pragmaMap.put("new-sub-grammar",
- (stk, rs) -> rs.pushGrammar(new WeightedGrammar<>()));
- pragmaMap.put("edit-sub-grammar", GrammarReader::editSubGrammar);
- pragmaMap.put("edit-parent", (stk, rs) -> rs.popGrammar());
+
pragmaMap.put("save-sub-grammar", GrammarReader::saveGrammar);
- pragmaMap.put("debug", GrammarReader::debugGrammar);
- pragmaMap.put("prefix-with", GrammarReader::prefixRule);
- pragmaMap.put("suffix-with", GrammarReader::suffixRule);
- pragmaMap.put("import-rule", GrammarReader::importRule);
+ pragmaMap.put("subordinate", GrammarReader::subordinateGrammar);
}
+ private static void initialRule(StringTokenizer stk, ReaderState rs) {
+ String rName = stk.nextToken();
+
+ rs.setInitialRule(rName);
+ }
private static void importRule(StringTokenizer stk, ReaderState rs) {
String ruleName = stk.nextToken();
String sgName = stk.nextToken();
-
+
rs.getRules().addGrammarAlias(sgName, ruleName);
}
-
+
private static void prefixRule(StringTokenizer stk, ReaderState rs) {
String rName = stk.nextToken();
String prefixToken = stk.nextToken();
diff --git a/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java
index 96f4ed7..5770d55 100644
--- a/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java
+++ b/RGens/src/main/java/bjc/RGens/parser/GrammarReaderApp.java
@@ -3,6 +3,8 @@ package bjc.RGens.parser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
+import java.io.PrintStream;
+
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
@@ -36,15 +38,32 @@ public class GrammarReaderApp {
System.exit(1);
}
- String initRule = SimpleDialogs.getChoice(null,
- "Pick a initial rule",
- "Pick a initial rule to generate choices from",
- wg.ruleNames().stream().sorted().toArray(String[]::new));
+ String initRule = "";
+
+ if (!wg.hasInitRule()) {
+ initRule = SimpleDialogs.getChoice(null, "Pick a initial rule",
+ "Pick a initial rule to generate choices from",
+ wg.ruleNames().stream().sorted()
+ .toArray(String[]::new));
+ } else {
+ initRule = wg.getInitRule();
+ }
int count = SimpleDialogs.getWhole(null,
"Enter number of repititions",
"Enter the number of items to generate from the rule");
+ File outpFile = SimpleFileDialog.getSaveFile(null,
+ "Choose Grammar File");
+
+ PrintStream ps = null;
+
+ try {
+ ps = new PrintStream(outpFile);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ }
+
for (int i = 0; i < count; i++) {
String s = wg.genList(initRule, " ")
.reduceAux(new StringBuilder(),
@@ -53,7 +72,7 @@ public class GrammarReaderApp {
t -> t.toString())
.replaceAll("\\s+", " ");
- System.out.println(s);
+ ps.println(s);
}
}
}
diff --git a/RGens/src/main/java/bjc/RGens/parser/ReaderState.java b/RGens/src/main/java/bjc/RGens/parser/ReaderState.java
index 1487822..3e25741 100644
--- a/RGens/src/main/java/bjc/RGens/parser/ReaderState.java
+++ b/RGens/src/main/java/bjc/RGens/parser/ReaderState.java
@@ -53,4 +53,8 @@ public class ReaderState {
public void toggleUniformity() {
isUniform = !isUniform;
}
+
+ public void setInitialRule(String rName) {
+ wg.peek().setInitRule(rName);
+ }
}
diff --git a/RGens/src/test/java/bjc/RGens/AppTest.java b/RGens/src/test/java/bjc/RGens/AppTest.java
index 25fb563..7620403 100644
--- a/RGens/src/test/java/bjc/RGens/AppTest.java
+++ b/RGens/src/test/java/bjc/RGens/AppTest.java
@@ -7,32 +7,28 @@ import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
-public class AppTest
- extends TestCase
-{
- /**
- * Create the test case
- *
- * @param testName name of the test case
- */
- public AppTest( String testName )
- {
- super( testName );
- }
+public class AppTest extends TestCase {
+ /**
+ * Create the test case
+ *
+ * @param testName
+ * name of the test case
+ */
+ public AppTest(String testName) {
+ super(testName);
+ }
- /**
- * @return the suite of tests being tested
- */
- public static Test suite()
- {
- return new TestSuite( AppTest.class );
- }
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite() {
+ return new TestSuite(AppTest.class);
+ }
- /**
- * Rigourous Test :-)
- */
- public void testApp()
- {
- assertTrue( true );
- }
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp() {
+ assertTrue(true);
+ }
}