/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: GMLEditor.java Description: This is a class that allows for the addition and removal of objects, arrows, and relations to a given gml representation of a category */ class GMLEditor //class GMLEditor definition { IniSettings ini = new IniSettings(); void addObject(Category cat) //Adds a given object to the given gml //represenation of a category { //Create string to hold gml code String gml = cat.gml; //Create temporary string String temp = new String(); String temp2 = new String(); //Start and end indices of //node being removed int startIndex = 0; int endIndex = 0; //Information about the node ... String node = "node"; int nodeLength = 4; String idLabel = "id "; //Information about brackets ... int oBracket = 0; int cBracket = 0; //Search gml for "relations" node for (int i=0; i < gml.length()-nodeLength; i++) { //If node is found... if (node.equals(gml.substring(i, i+nodeLength))) { startIndex = i; //startIndex is start of node i++; //While the node isn't completely searched... while ((oBracket != cBracket) || (oBracket == 0)) { if (gml.charAt(i) == '[') oBracket++; if (gml.charAt(i) == ']') { cBracket++; endIndex = i+1; //endIndex is end of node + 1 } //decrement higher indexed nodes by 1 if (idLabel.equals(gml.substring(i, i+idLabel.length()))) { String idNum = new String(); int start = i + idLabel.length(); while (gml.charAt(i) != '\n') i++; int end = i; if (Integer.parseInt(gml.substring(start, end)) == cat.num_objects-1) { gml = gml.substring(0, start) + (Integer.parseInt(gml.substring(start, end))+1) + gml.substring(end); } } i++; } //Reinitialize local variables oBracket = cBracket = 0; //removeNode = false; } } //Remove "graph [\n directed 1" from beginning of GML code temp = gml.substring(startIndex); temp2 = gml.substring(0, startIndex); //Add in text for a new node(ie category object) gml = temp2+ "node [\nid " + (cat.num_objects-1) + "\nlabel \"" + cat.obj[cat.num_objects-1] + "\"\ngraphics [\nImage [\nType \"\"\nLocation \"\"\n]\ncenter [\nx " + (((int) (Math.random() * 350)) - 175) + "\ny " + (((int) (Math.random() * 325)) - 125) + "\nz 0.0\n]\nwidth 20.0\nheight 20.0\ndepth 20.0\n]\nvgj [\nlabelPosition \"in\"\nshape \"Oval\"\n]\n]\n" + temp; //******************NOTE****************** //cat.num_objects-1, the index of the new object //can not be used as the node id because //when a gml representation is originally //created, the node storing the relations //is given the id cat.num_objects, which means //all new objects should increment their //id by one. //**************************************** //save gml with object added cat.gml = gml; } void addArrow(Category cat) //Adds a given arrow to the given gml //represenation of a category { //Create string to hold gml code String gml = cat.gml; int count = 0; //Remove closing bracket from gml code gml = gml.substring(0, gml.length()-2); int i= cat.num_arrows-1; String arrows = cat.arrow[cat.num_arrows-1]; //For all of the arrows determine if there already //exists arrows between the current source and destination for (int j = 0; j < cat.num_arrows; j++) { //If such an arrow exists, append its name to the string "arrows" if ((i != j) && (cat.arr[i][0] == cat.arr[j][0]) && (cat.arr[i][1] == cat.arr[j][1])) { //Increment the count count++; arrows = arrows.concat("," + cat.arrow[j]); } } //Develop gml code for arrow String curr = "edge [\n"; curr = curr + "linestyle \"solid\"\n"; curr = curr + "label \"" + /*cat.arrow[i]*/arrows + "\"\n"; curr = curr + "source " + cat.arr[i][0] + "\n"; curr = curr + "target " + cat.arr[i][1] + "\n"; //If domain = codomain if (cat.arr[i][0] == cat.arr[i][1]) { /* int p1_x = (int)graph_.getNodeFromId(cat.arr[i][0]).getPosition().x; int p1_y = (int)graph_.getNodeFromId(cat.arr[i][0]).getPosition().y; int p2_x = (int)graph_.getNodeFromId(cat.arr[i][1]).getPosition().x; int p2_y = (int)graph_.getNodeFromId(cat.arr[i][1]).getPosition().y; curr = curr + "graphics [\n "; curr = curr + "Point [\nx " + p1_x + "\ny " + p1_y + "\nz 0.0\n]"; curr = curr + "Point [\nx " + p2_x + "\ny " + p2_y + "\nz 0.0\n]\n]"; */ } //Add closing "]" to text representation gml = gml + curr + "\n]\n]\n"; //save text representation in gml string of current category cat.gml = gml; //Display Category //????????? } void addRelation(Category cat, String newRelation) //Adds a given relation to the given gml //represenation of a category { //Information about node with relations in it... String relations = "relations\\n"; int relationsLength = 11; //Create string to hold gml code String gml = new String(cat.gml); //Create temporary string String temp = new String(); String temp2 = new String(); //Search gml for "relations\\n" for (int i=0; i < gml.length()-relationsLength; i++) { //gmlText.append(gml.substring(i, i+relationsLength) + " = " + relations + "\n\n"); if (relations.equals(gml.substring(i, i+relationsLength))) { //Save new gml in category file cat.gml = gml.substring(0, i+relationsLength) + newRelation + ", " + gml.substring(i+relationsLength, gml.length()); return; } } // if there is no "relations" node in the graph, then create one here: //Remove "graph [\n directed 1" from beginning of GML code temp = gml.substring(19); temp2 = gml.substring(0, 19); //Add in text for a new node(ie category object) //gml = temp2 + "node [\nid 7 \nlabel \"test object\"\ngraphics [\nImage [\nType \"\"\nLocation \"\"\n]\ncenter [\nx " + (((int) (Math.random() * 350)) - 175) + "\ny " + (((int) (Math.random() * 325)) - 125) + "\nz 0.0\n]\nwidth 20.0\nheight 20.0\ndepth 20.0\n]\nvgj [\nlabelPosition \"in\"\nshape \"Oval\"\n]\n]\n" + temp; gml = temp2 + "\nnode [\nid "+ cat.num_objects + "\nlabel \"relations\\n" + newRelation + "\"\ngraphics [\nImage [\nType \"\"\nLocation \"\"\n]\ncenter [\nx " + (((int) (Math.random() * 350)) - 175) + "\ny " + (((int) (Math.random() * 325)) - 125) + "\nz 0.0\n]\nwidth 20.0\nheight 20.0\ndepth 20.0\n]\nvgj [\nlabelPosition \"in\"\nshape \"Oval\"\n]\n]\n" + temp; //Save new gml in category file cat.gml = gml; } void removeObject(Category cat, String object) //Removes a given object from the given gml //represenation of a category { //Start and end indices of //node being removed int startIndex = 0; int endIndex = 0; //Variable to determine if current node //should be removed boolean removeNode = false; boolean removeNode2 = false; //Information about the node ... String node = "node"; int nodeLength = 4; String nodeLabel = "label \""+ object + "\""; int labelLength = 8 + object.length(); String idLabel = "id "; //Information about brackets ... int oBracket = 0; int cBracket = 0; //Create string to hold gml code String gml = cat.gml; //Search gml for "node" for (int i=0; i < gml.length()-nodeLength; i++) { //If node is found... if (node.equals(gml.substring(i, i+nodeLength))) { startIndex = i; //startIndex is start of node i++; //While the node isn't completely searched... while ((oBracket != cBracket) || (oBracket == 0)) { if (gml.charAt(i) == '[') oBracket++; if (gml.charAt(i) == ']') { cBracket++; endIndex = i+1; //endIndex is end of node + 1 } //If node to be removed hasn't been found if (removeNode == false) { //If label is found if (nodeLabel.equals(gml.substring(i, i+labelLength))) { removeNode = true; removeNode2 = true; } } //If node has been found, decrement all other nodes //found by 1 else { //decrement higher indexed nodes by 1 if (idLabel.equals(gml.substring(i, i+idLabel.length()))) { String idNum = new String(); int start = i + idLabel.length(); while (gml.charAt(i) != '\n') i++; int end = i; gml = gml.substring(0, start) + (Integer.parseInt(gml.substring(start, end))-1) + gml.substring(end); } } i++; } //If current node is the node to be removed... if (removeNode2 == true) { gml = gml.substring(0, startIndex) + gml.substring(endIndex); removeNode2 = false; i = startIndex; } //Reinitialize local variables oBracket = cBracket = 0; } } cat.gml = gml; //Since node ids have changed, the source and //target node ids have to be updated on the edges if (cat.num_arrows > 0) removeArrow(cat); } void removeArrow(Category cat) //Removes a given arrow from the given gml //represenation of a category { // Add your handling code here: String edge = "edge"; int oBracket = 0; int cBracket = 0; //Create string to hold gml code String gml = cat.gml; //Truncate edge portion of gml code int i=0; while ((!edge.equals(gml.substring(i, i+4))) && ((oBracket != cBracket) || (oBracket == 0))) { if (gml.charAt(i) == '[') oBracket++; if (gml.charAt(i) == ']') cBracket++; i++; } cat.gml = gml.substring(0, i); //Add new edge portion of gml code //based on current edges for (i=0; i< cat.num_arrows; i++) { String arrows = cat.arrow[i]; int count = 0; for (int j = 0; j < cat.num_arrows; j++) { if ((i != j) && (cat.arr[i][0] == cat.arr[j][0]) && (cat.arr[i][1] == cat.arr[j][1])) { count++; arrows = arrows.concat("," + cat.arrow[j]); } } String curr = "edge [\n"; curr = curr + "linestyle \"solid\"\n"; curr = curr + "label \"" + arrows + "\"\n"; curr = curr + "source " + cat.arr[i][0] + "\n"; curr = curr + "target " + cat.arr[i][1] + "\n]"; cat.gml = cat.gml + curr; } //Add closing "]" to text representation cat.gml = cat.gml + "\n]\n"; } void removeRelation(Category cat) //Removes a given relation from the given gml //represenation of a category { //int startIndex = 0; //int endIndex = 0; //boolean removeNode = false; //Information about start of node ... String node = "node"; int nodeLength = 4; //Information about brackets ... int oBracket = 0; int cBracket = 0; //Information about node that stores relations... String relationLabel = "label \"relation"; int labelLength = 15; int startLabel =0; int endLabel = 0; //Create string to hold gml code String gml = cat.gml; //Search gml for "node" for (int i=0; i < gml.length()-labelLength; i++) { if (node.equals(gml.substring(i, i+nodeLength))) { i++; //For current node... while ((oBracket != cBracket) || (oBracket == 0)) { if (gml.charAt(i) == '[') oBracket++; if (gml.charAt(i) == ']') cBracket++; //If current node stores relations... if (relationLabel.equals(gml.substring(i, i+labelLength))) { // System.out.println(gml.substring(i, i+labelLength)); startLabel = i; i = i+labelLength+1; while (gml.charAt(i) != '\"') i++; endLabel = i; String rels = new String(); //print_rel(cat, rels); if (cat.num_relations == 0) { cat.gml = gml.substring(0, startLabel) + "label \"relations: none" + gml.substring(endLabel); // System.out.println(cat.gml); return; //removeObject(cat, "relations"); } else { cat.gml = gml.substring(0, startLabel) + "label \"" + cat.relnode_label() + gml.substring(endLabel); return; } //Search label of relations node for relation to //be removed /*for (int j=startLabel;j < endLabel; j++) { //If relation is to be removed... if (relation.equals(gml.substring(j, j+relation.length()))) { cat.gml = gml.substring(0, j) + gml.substring(j+relation.length()+1); } } */ } i++; } oBracket = cBracket = 0; } } } }