import java.io.*; import java.lang.reflect.*; import java.util.*; public class Driver { static ModLattice myLattice; static boolean keepAuto; public static void start(ModLattice aLattice) { myLattice = aLattice; keepAuto = true; for (;;) try { printMenu(); String response = getLine("Enter choice: "); actOn(response); } catch(InvalidCommandException ex) { System.out.println(ex.getMessage()); } } public static void printMenu() { System.out.println(); System.out.println("Current number of elements: " + myLattice.elementCount); System.out.println("Current row: " + myLattice.currentRow); System.out.println("Current column: " + myLattice.currentColumn); System.out.println("Current size: " + myLattice.currentSize); System.out.println("Current number of equivalence classes: " + myLattice.myEqRel.countClasses(myLattice.elementCount)); System.out.println(); System.out.println("1. Generate new elements"); System.out.println("2. Compute congruence relation"); System.out.println("3. Impose a relation"); System.out.println("4. Form quotient lattice"); System.out.println("5. Resize"); System.out.println("6. Examine a square in the table"); System.out.println("7. Examine a row in the join table or meet table"); System.out.println("8. Examine an element"); System.out.println("9. Output information to a file"); System.out.println("10. Change settings"); System.out.println("11. Perform exhaustive check"); System.out.println("12. Use other method"); System.out.println("13. Read lattice from file"); System.out.println("0. Quit"); } //printMenu private static String getLine (String prompt) { System.out.print (prompt); String result = new String(); System.out.flush(); try { BufferedReader in = new BufferedReader (new InputStreamReader(System.in)); result = in.readLine(); }catch (IOException ex) { System.err.println("An IOException ocurred: "); } return result; } private static void actOn(String command) throws InvalidCommandException { int i, j, k; ModLattice temp; Element e; boolean opIsJoin; String response; try { i = Integer.parseInt(command); } catch(NumberFormatException ex) { throw new InvalidCommandException("Your command was not recognized."); } switch (i) { case 1: try { j = Integer.parseInt(getLine("How many total elements? ")); if (j > myLattice.currentSize) throw new InvalidCommandException ("Lattice can't hold that many elements; resize first."); myLattice.makeNewElements(j); } catch(NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 2: myLattice.computeCongruence(); break; case 3: try { j = Integer.parseInt(getLine("Enter number of first element: ")); k = Integer.parseInt(getLine("Enter number of second element: ")); if (j < 0 || k < 0 || j >= myLattice.elementCount || k >= myLattice.elementCount) throw new InvalidCommandException("Invalid number."); myLattice.myEqRel.relate(j, k); keepAuto = false; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 4: temp = myLattice.collapse(keepAuto); myLattice = temp; keepAuto = true; break; case 5: try { j = Integer.parseInt(getLine("Enter new size: ")); if (j < myLattice.currentSize) throw new InvalidCommandException("Can't reduce size."); myLattice.resize(j); } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 6: try { j = Integer.parseInt(getLine("Enter row: ")); k = Integer.parseInt(getLine("Enter column: ")); if (j < 0 || k < 0 || j >= myLattice.currentSize || k >= myLattice.currentSize) throw new InvalidCommandException("Invalid number."); if ((e = myLattice.table[j][k]) == null) System.out.println("Square not filled."); else System.out.println(e.myNumber); } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 7: try { j = Integer.parseInt(getLine("Enter 1 for joins, 2 for meets: ")); opIsJoin = (j == 1); k = Integer.parseInt(getLine("Enter number of element to form " + (opIsJoin ? "joins" : "meets") + " with: ")); if (k < 0 || k > myLattice.elementCount) throw new InvalidCommandException("Invalid number."); for (int l = 0; l < myLattice.elementCount; l++) { e = (opIsJoin ? myLattice.join(k, l) : myLattice.meet(k,l)); System.out.print(elementNumber(e)); } getLine("Hit return to continue."); } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 8: try { j = Integer.parseInt(getLine("Enter number of element ")); if (j < 0 || j > myLattice.elementCount) throw new InvalidCommandException("Invalid number."); e = myLattice.elements[j]; System.out.println(); System.out.println("Defining term: " + e.toString()); System.out.println("Expanded: " + e.fullName()); System.out.println("Other terms: "); if (e.altsUsed > 0) for (int l = 0; l < e.altsUsed; l++) System.out.println(e.altTerms[l].toString()); else System.out.println("none"); if (myLattice instanceof SelfDualModLattice) System.out.println("Dual: " + elementNumber(e.dual)); if (e.conjCount > 0) { System.out.println("Conjugates: "); for (int l = 0; l < e.conjCount; l++) System.out.println(elementNumber(e.conjugates[l])); } System.out.println("Equivalence class: "); System.out.print(" "); int [] row = myLattice.myEqRel.classOf(j); for (int kk = 0; kk < row.length; kk++) System.out.print(row[kk] + " "); System.out.println(); System.out.println(); getLine("Hit return to continue."); } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 9: printSubMenu1(); response = getLine("Enter choice: "); actOnSub1(response); break; case 10: printSubMenu2(); response = getLine("Enter choice: "); actOnSub2(response); break; case 11: printSubMenu3(); response = getLine("Enter choice: "); actOnSub3(response); break; case 12: try { Class theClass = Class.forName(getLine("Enter name of class that contains method: ")); Class [] formalParams = { Class.forName("ModLattice") }; Method theMethod = theClass.getMethod(getLine("Enter name of method: "), formalParams); Object [] actualParams = { myLattice }; Object temp2 = theMethod.invoke(null, actualParams); if (temp2 != null) myLattice = (ModLattice) temp2; } catch (ClassNotFoundException ex) { throw new InvalidCommandException("Class not found."); } catch (NoSuchMethodException ex) { throw new InvalidCommandException("Method not found."); } catch (SecurityException ex) { throw new InvalidCommandException("Security exception."); } catch (IllegalAccessException ex) { throw new InvalidCommandException("Method is not accessible."); } catch (IllegalArgumentException ex) { throw new InvalidCommandException("Method has wrong parameter list."); } catch (InvocationTargetException ex) { ex.getTargetException().printStackTrace(System.out); throw new InvalidCommandException("Method generated an exception: " + ex.getMessage()); } catch (ClassCastException ex) { throw new InvalidCommandException("Method returns wrong type."); } catch (NullPointerException ex) { throw new InvalidCommandException("Method must be static."); } getLine("Hit return to continue"); break; case 13: try { response = getLine("Enter filename: "); FileReader theFile = new FileReader(response); BufferedReader myIn = new BufferedReader(theFile); inputAll(myIn); } catch (FileNotFoundException ex) { throw new InvalidCommandException("Invalid filename."); } break; case 0: System.exit(0); break; default: throw new InvalidCommandException("Your command was not recognized."); } //switch } //actOn public static void printSubMenu1() { System.out.println(); System.out.println("1. Output element list"); System.out.println("2. Output join table"); System.out.println("3. Output meet table"); System.out.println("4. Output join/meet table"); System.out.println("5. Output equivalence relation"); System.out.println("6. Output all data"); } public static void actOnSub1(String command) throws InvalidCommandException { int i; PrintStream myOut; try { i = Integer.parseInt(command); } catch(NumberFormatException ex) { throw new InvalidCommandException("Your command was not recognized."); } try { String filename = getLine("Enter filename: "); FileOutputStream myFOS = new FileOutputStream(filename, true); myOut = new PrintStream(myFOS); } catch (FileNotFoundException ex) { throw new InvalidCommandException("Invalid filename."); } catch (IOException ex) { throw new InvalidCommandException("An IOException occurred: " + ex.getMessage()); } switch (i) { case 1: for (int j = 0; j < myLattice.elementCount; j++) { Element e = myLattice.elements[j]; myOut.println(j + " " + e.toString() + " " + e.fullName()); } myOut.println(); break; case 2: for (int j = 0; j < myLattice.elementCount; j++) { myOut.print(j + ": "); for (int k = 0; k < myLattice.elementCount; k++) myOut.print(elementNumber(myLattice.join(j, k))); myOut.println(); } myOut.println(); break; case 3: for (int j = 0; j < myLattice.elementCount; j++) { myOut.print(j + ": "); for (int k = 0; k < myLattice.elementCount; k++) myOut.print(elementNumber(myLattice.meet(j, k))); myOut.println(); } myOut.println(); break; case 4: for (int j = 0; j < myLattice.elementCount; j++) { myOut.print(j + ": "); for (int k = 0; k < myLattice.elementCount; k++) myOut.print(elementNumber(myLattice.table[j][k])); myOut.println(); } myOut.println(); break; case 5: IntCell count = new IntCell(0); int [] repSet = myLattice.myEqRel.repSet(count, myLattice.elementCount); for (int j = 0; j < count.myInt; j++) { myOut.print(" "); int [] row = myLattice.myEqRel.classOf(repSet[j]); for (int k = 0; k < row.length; k++) myOut.print(row[k] + " "); myOut.println(); } myOut.println(); break; case 6: outputAll(myOut); break; default: throw new InvalidCommandException("Your command was not recognized."); } //switch } //actOnSub1 public static void printSubMenu2() { System.out.println(); System.out.println("DEPTH = " + ModLattice.DEPTH); System.out.println("DEPTH2 = " + ModLattice.DEPTH2); System.out.println("M = " + ModLattice.M); System.out.println("N = " + ModLattice.N); System.out.println("ALTS = " + Element.ALTS); System.out.println(); System.out.println("1. Change DEPTH"); System.out.println("2. Change DEPTH2"); System.out.println("3. Change M"); System.out.println("4. Change N"); System.out.println("5. Change ALTS"); } public static void actOnSub2(String command) throws InvalidCommandException { int i, j; try { i = Integer.parseInt(command); } catch(NumberFormatException ex) { throw new InvalidCommandException("Your command was not recognized."); } switch (i) { case 1: try { j = Integer.parseInt(getLine("Enter new DEPTH: ")); if (j < 0) throw new InvalidCommandException("Invalid number."); ModLattice.DEPTH = j; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 2: try { j = Integer.parseInt(getLine("Enter new DEPTH2: ")); if (j < 0) throw new InvalidCommandException("Invalid number."); ModLattice.DEPTH2 = j; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 3: try { j = Integer.parseInt(getLine("Enter new M: ")); if (j < 0) throw new InvalidCommandException("Invalid number."); ModLattice.M = j; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 4: try { j = Integer.parseInt(getLine("Enter new N: ")); if (j < 0) throw new InvalidCommandException("Invalid number."); ModLattice.N = j; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; case 5: try { j = Integer.parseInt(getLine("Enter new ALTS: ")); if (j < 0) throw new InvalidCommandException("Invalid number."); for (int k = 0; k < myLattice.elementCount; k++) { Element e = myLattice.elements[k]; int toCopy = Math.min(j, e.altsUsed); Term [] temp = e.altTerms; e.altTerms = new Term[j]; for (int l = 0; l < toCopy; l++) e.altTerms[l] = temp[l]; e.altsUsed = toCopy; } Element.ALTS = j; } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid number."); } break; default: throw new InvalidCommandException("Your command was not recognized."); } //switch } //actOnSub2 public static void printSubMenu3() { System.out.println(); System.out.println("1. Check join associativity"); System.out.println("2. Check meet associativity"); System.out.println("3. Check join/meet compatibility"); System.out.println("4. Check modularity"); } public static void actOnSub3(String command) throws InvalidCommandException { int i; try { i = Integer.parseInt(command); } catch(NumberFormatException ex) { throw new InvalidCommandException("Your command was not recognized."); } switch (i) { case 1: for (int j = 0; j < myLattice.elementCount; j++) for (int k = 0; k < myLattice.elementCount; k++) for (int l = 0; l < myLattice.elementCount; l++) { Element e1, e2, e3, e4; e1 = myLattice.join(j, k); e3 = myLattice.join(k, l); if (e1 != null && e3 != null) { e2 = myLattice.join(e1.myNumber, l); e4 = myLattice.join(e3.myNumber, j); if (e2 != null && e4 != null && e2 != e4) myLattice.identify(e2, e4); } } break; case 2: for (int j = 0; j < myLattice.elementCount; j++) for (int k = 0; k < myLattice.elementCount; k++) for (int l = 0; l < myLattice.elementCount; l++) { Element e1, e2, e3, e4; e1 = myLattice.meet(j, k); e3 = myLattice.meet(k, l); if (e1 != null && e3 != null) { e2 = myLattice.meet(e1.myNumber, l); e4 = myLattice.meet(e3.myNumber, j); if (e2 != null && e4 != null && e2 != e4) myLattice.identify(e2, e4); } } break; case 3: for (int j = 0; j < myLattice.elementCount; j++) for (int k = 0; k < myLattice.elementCount; k++) { Element e1 = myLattice.join(j, k); Element e2 = myLattice.meet(j, k); Element e3 = myLattice.elements[j]; Element e4 = myLattice.elements[k]; if (e1 == e4 && e2 != null && e2 != e3) myLattice.identify(e2, e3); if (e2 == e3 && e1 != null && e1 != e4) myLattice.identify(e1, e4); } break; case 4: for (int j = 0; j < myLattice.elementCount; j++) for (int l = 0; l < myLattice.elementCount; l++) { Element e5 = myLattice.elements[j]; Element e7 = myLattice.elements[l]; if (myLattice.join(j, l) == e7 || myLattice.meet(j, l) == e5) { Element e1, e2, e3, e4; for (int k = 0; k < myLattice.elementCount; k++) { e1 = myLattice.join(j, k); e3 = myLattice.meet(k, l); if (e1 != null && e3 != null) { e2 = myLattice.meet(e1.myNumber, l); e4 = myLattice.join(j, e3.myNumber); if (e2 != null && e4 != null && e2 != e4) myLattice.identify(e2, e4); } } } //if } //for break; default: throw new InvalidCommandException("Your command was not recognized."); } //switch } //actOnSub3 public static String elementNumber(Element e) { if (e == null) return "n/a "; else return (e.myNumber + " "); } public static String elementNumber2(Element e) { if (e == null) return "n/a"; else return (e.myNumber + ""); } public static Element numberElement(String s) { try { int i = Integer.parseInt(s); return myLattice.elements[i]; } catch (NumberFormatException e) { return null; } } public static void outputAll(PrintStream myOut) { if (myLattice instanceof SelfDualModLattice) myOut.println("SelfDualModLattice"); else myOut.println("ModLattice"); myOut.println(ModLattice.DEPTH); myOut.println(ModLattice.DEPTH2); myOut.println(ModLattice.M); myOut.println(ModLattice.N); myOut.println(Element.ALTS); myOut.println(myLattice.currentSize); myOut.println(myLattice.auto); for (int i = 0; i < myLattice.auto; i++) myOut.println(myLattice.inverse[i]); myOut.println(myLattice.currentRow); myOut.println(myLattice.currentColumn); myOut.println(myLattice.elementCount); for (int i = 0; i < myLattice.elementCount; i++) { Element e = myLattice.elements[i]; myOut.println(elementNumber2(e.dual)); for (int j = 0; j < myLattice.auto; j++) myOut.println(elementNumber2(e.conjugates[j])); myOut.println(e.myTerm.toString()); myOut.println(e.altsUsed); for (int j = 0; j < e.altsUsed; j++) myOut.println(e.altTerms[j].toString()); } for (int i = 0; i < myLattice.elementCount; i++) for (int j = 0; j < myLattice.elementCount; j++) myOut.println(elementNumber2(myLattice.table[i][j])); IntCell count = new IntCell(0); int [] repSet = myLattice.myEqRel.repSet(count, myLattice.elementCount); myOut.println(count.myInt); for (int j = 0; j < count.myInt; j++) { int [] row = myLattice.myEqRel.classOf(repSet[j]); for (int k = 0; k < row.length; k++) myOut.print(row[k] + " "); myOut.println(); } } //outputAll public static void inputAll(BufferedReader myIn) throws InvalidCommandException { String line; boolean selfDual; int currentSize, auto, altsUsed, count; int [] inverse; StringTokenizer st; try { line = myIn.readLine(); if (line.equals("ModLattice")) selfDual = false; else if (line.equals("SelfDualModLattice")) selfDual = true; else throw new InvalidCommandException("Invalid file."); line = myIn.readLine(); ModLattice.DEPTH = Integer.parseInt(line); line = myIn.readLine(); ModLattice.DEPTH2 = Integer.parseInt(line); line = myIn.readLine(); ModLattice.M = Integer.parseInt(line); line = myIn.readLine(); ModLattice.N = Integer.parseInt(line); line = myIn.readLine(); Element.ALTS = Integer.parseInt(line); line = myIn.readLine(); currentSize = Integer.parseInt(line); line = myIn.readLine(); auto = Integer.parseInt(line); inverse = new int[auto]; for (int i = 0; i < auto; i++) { line = myIn.readLine(); inverse[i] = Integer.parseInt(line); } if (selfDual) myLattice = new SelfDualModLattice(currentSize, inverse); else myLattice = new ModLattice(currentSize, inverse); line = myIn.readLine(); myLattice.currentRow = Integer.parseInt(line); line = myIn.readLine(); myLattice.currentColumn = Integer.parseInt(line); line = myIn.readLine(); myLattice.elementCount = Integer.parseInt(line); for (int i = 0; i < myLattice.elementCount; i++) myLattice.elements[i] = new Element(i, auto, myLattice); for (int i = 0; i < myLattice.elementCount; i++) { Element e = myLattice.elements[i]; line = myIn.readLine(); e.dual = numberElement(line); for (int j = 0; j < auto; j++) { line = myIn.readLine(); e.conjugates[j] = numberElement(line); } line = myIn.readLine(); e.myTerm = new Term(line, myLattice); line = myIn.readLine(); e.altsUsed = Integer.parseInt(line); for (int j = 0; j < e.altsUsed; j++) { line = myIn.readLine(); e.altTerms[j] = new Term(line, myLattice); } } //for (i) for (int i = 0; i < myLattice.elementCount; i++) for (int j = 0; j < myLattice.elementCount; j++) { line = myIn.readLine(); myLattice.table[i][j] = numberElement(line); } line = myIn.readLine(); count = Integer.parseInt(line); for (int i = 0; i < count; i++) { line = myIn.readLine(); st = new StringTokenizer(line); int [] row = new int[st.countTokens()]; for (int j = 0; j < row.length; j++) row[j] = Integer.parseInt(st.nextToken()); for (int j = 0; j < row.length; j++) myLattice.myEqRel.classes[row[j]] = row; } } catch (NumberFormatException ex) { throw new InvalidCommandException("Invalid file."); } catch (IOException ex) { throw new InvalidCommandException("An IOException occurred."); } } //inputAll } //class Driver