/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: EqualityComposites.java Description: This class implements a frame that tests paths in a given category for equality. */ //import statements import java.awt.*; import java.awt.event.*; public class EqualityComposites extends Frame { IniSettings ini = new IniSettings(); Category cat = new Category(); CategoryList l = new CategoryList(); int check_enter = 0; //Objects in the Graphical User Interface(GUI) Button exit = new Button(); Panel button_panel = new Panel(); BorderLayout borderLayout1 = new BorderLayout(); Button equalityButton = new Button(); Button okButton = new Button(); Label label2 = new Label(); Label label1 = new Label(); TextField path1Field = new TextField(); TextField categoryName = new TextField(); MainWindow edit; Label label3 = new Label(); TextField path2Field = new TextField(); Label label4 = new Label(); TextArea resultArea = new TextArea("", 10, 10, TextArea.SCROLLBARS_VERTICAL_ONLY); int p1[] = new int[ini.getMAXWORD()]; int p2[] = new int[ini.getMAXWORD()]; int redp1[] = new int[ini.getMAXWORD()]; int redp2[] = new int[ini.getMAXWORD()]; String path1 = new String(); String path2 = new String(); Label label5 = new Label(); GridBagLayout gridBagLayout1 = new GridBagLayout(); GBConstraintsEditor g = new GBConstraintsEditor(); public EqualityComposites(CategoryList ltemp, MainWindow ed) //Default Constructor with the list of categories as a parameter { l = ltemp; edit = ed; edit.enableMenus(false); try { jbInit(); } catch (Exception e) { e.printStackTrace(); } } public boolean handleEvent(Event e) { if ((e.id == Event.WINDOW_DESTROY) || (e.id == Event.ACTION_EVENT)) { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources return true; } else if (e.key == Event.ENTER) { if (check_enter == 0) check_enter++; else { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources } return true; } return super.handleEvent(e); } public boolean action(Event e, Object o) { if ((e.target == exit) || (e.key == Event.ENTER)) { edit.enableMenus(true); hide(); //hide frame dispose(); //free resources return true; } return false; } private void jbInit() throws Exception //This function intializes the GUI { this.setTitle("GDCT: Equality of Composites"); this.setBackground(new Color(192,192,192)); equalityButton.setLabel("Equality ?"); equalityButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { equalityButton_actionPerformed(e); } }); okButton.setLabel("Close"); label2.setText("Category Name:"); label1.setText("First Path:"); this.setLayout(gridBagLayout1); resultArea.setBackground(Color.white); categoryName.setBackground(Color.white); this.add(equalityButton, g.getConstraints(4, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(9, 0, 0, 0), 78, 8)); this.add(okButton, g.getConstraints(4, 4, 1, 1, 0.0, 0.0 ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(6, 0, 5, 0), 114, 8)); this.add(label2, g.getConstraints(0, 0, 4, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 10, 0, 0), -8, 0)); this.add(label1, g.getConstraints(0, 1, 2, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(10, 10, 0, 0), 0, 0)); this.add(categoryName, g.getConstraints(4, 0, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(9, 6, 0, 0), 121, 0)); this.add(path1Field, g.getConstraints(4, 1, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 6, 0, 0), 121, 1)); this.add(label3, g.getConstraints(0, 2, 3, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11, 10, 0, 0), 0, 0)); this.add(path2Field, g.getConstraints(4, 2, 1, 1, 1.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(9, 8, 0, 0), 122, 0)); this.add(label4, g.getConstraints(5, 0, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 28, 0, 0), -8, -2)); this.add(resultArea, g.getConstraints(5, 1, 2, 4, 1.0, 1.0 ,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 29, 8, 6), 141, -27)); this.add(label5, g.getConstraints(0, 3, 1, 1, 0.0, 0.0 ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(14, 10, 0, 0), 0, 0)); CategoryNode current = l.head; categoryName.setEditable(false); categoryName.setText(edit.categoryChoice.getSelectedItem()); String name = edit.categoryChoice.getSelectedItem(); cat = l.getNode(name); label5.setText("Controls:"); path2Field.setBackground(Color.white); path1Field.setBackground(Color.white); label4.setText("Results:"); label3.setText("Second Path:"); resultArea.setEditable(false); } void equalityButton_actionPerformed(ActionEvent e) //Function called when OK button is pressed. { equality_test(); } void equality_test() //This function determines if the two paths specified by the user //are equal or not equal. { boolean success = false; //If paths p1 and p2 are valid paths if (ask_for_paths()) { //Reduce both paths redp1 = reduce(redp1, p1, cat); redp2 = reduce(redp2, p2, cat); //If the two reduced paths are equals if ((equals(redp1, redp2)) )//&& ((cat.arr[p1[0]][1] == cat.arr[p2[0]][1]) || (p1[0] == -1) || (p2[0] == -1))) { resultArea.append(path1 + " and " + path2 + " are equal.\n"); } else { resultArea.append(path1 + " and " + path2 + " are NOT equal.\n"); } } } boolean ask_for_paths() //This method reads in the two paths from path1Field and path2Field that //have been entered by the user and converts them to arrays of integers //terminated by -1 and then stores them in p1 and p2. { String first = new String(); //path 1 string String second = new String(); //path 2 string //Intitialize paths to -3 which means invalid p1[0] = p2[0] = -3; //Get First Path path1 = first = path1Field.getText(); path1Field.setText(""); //Check First Path for Valid Arrows if (check_string(first, cat)) { //Convert path string to path of integers p1 = string_to_path(first, cat); //Check path for valid domains and codomains if (!check_path(p1,cat)) { p1[0] = -3; return(false); } //Get Second Path path2 = second = path2Field.getText(); path2Field.setText(""); //Check Second Path for Valid Arrows if (check_string(second, cat)) { //Convert path string to path of integers p2 = string_to_path(second, cat); //copy_path(p2, string_to_path(second, A)); //Check path for valid domains and codomains if (!check_path(p2,cat)) { p2[0] = -3; return(false); } } } if ((p1[0] == -3) || (p2[0] == -3)) return(false); return(true); } public int[] copy_path(int dest[], int source[]) /* Parameters: dest: an array of integers which will be set to equal source source: an array of integers which is copied into dest Purpose: To copy one array of integers to another array of integers Design: Copies source into dest */ { int i; int temp[] = new int [ini.getMAXWORD()]; for(i=0;i<=path_len(source);i++) { temp[i] = source[i]; } return(temp); } public String path_to_string(int path[], Category A) /* Parameters: path: the path to be converted to a string(function does not change path) A: a category that contains the arrows of the path Purpose: To convert an array of integers, which represent arrows, terminated by -1 into a path which is a string of characters which are arrows and the string is terminated by \0. Design: Returns a string which is the path represented by the integers in the integer array */ { int i; String temp = new String(); //If path is an identity arrow return "1" if (path_len(path) == 0) { return("1"); } else { for(i=0;i rhs is always true for (j=spot;j