// cdstring.beh // Author - Binoy Samuel // modified: Binoy : May 26 '98 // This converts graph object to .cd format string //*******************Class UGraph ********************************/ UGraph { traversal toVertEdge(superInitVisitor v) { to {UVertex , UEdge }; } (@ void hashinit(Hashtable vtable, Hashtable etable) { superInitVisitor hcv = new HashCreateVisitor(vtable , etable); toVertEdge(hcv); } @) (@ public String getPreString() { String w = null; /* if(get_preamble() != null) w = "\n(@" + get_preamble().get_code() + "@" + ")\n\n"; if(get_pkg() != null) w = get_pkg().getPreString() + w ;*/ //TODO return w; } @) /** This function converts gcd(graph object) to cd(class dictionary) format input - none output - String corresponding to the cd file */ public String getCdString() to { UConstVertex , UAltVertex , UInterface} { (@ Hashtable vtable; Hashtable etable; @) before UGraph (@ host.idToVertexTable = new Hashtable(); host.idToEdgeTable = new Hashtable(); host.hashinit(host.idToVertexTable , host.idToEdgeTable); // System.out.println("in getCdString"); vtable = host.idToVertexTable ; etable = host.idToEdgeTable ; String pre = host.getPreString(); if (pre != null) return_val = pre ; @) before UConstVertex (@ if(return_val == null) return_val = host.getCdString(vtable , etable); else return_val += host.getCdString(vtable , etable); @) before UAltVertex (@ if(return_val == null) return_val = host.getCdString(vtable , etable); else return_val += host.getCdString(vtable , etable); @) before UInterface (@ if(return_val == null) return_val = host.getCdString(vtable , etable); else return_val += host.getCdString(vtable , etable); @) } } UVertex { traversal toallCdString(cdStringVisitor) { bypassing -> UVertex, vid, UID to *; } (@ String toCdString(cdStringVisitor v) { toallCdString(v); return v.get_return_val(); } @) } UConstVertex { (@ String getCdString(Hashtable vtable, Hashtable etable) { cdStringVisitor v = new ConstVisitor("",vtable , etable); return toCdString(v); } @) } UAltVertex { (@ String getCdString(Hashtable vtable, Hashtable etable) { cdStringVisitor v = new AltVisitor("", vtable , etable ); return super.toCdString(v); } @) } UInterface { (@ String getCdString(Hashtable vtable, Hashtable etable) { cdStringVisitor v = new IntVisitor("",vtable , etable ); return super.toCdString(v); } @) } // now for the visitors which actually produce the output. // abstract visitor. Required for the subclass visitors to work cdStringVisitor { before UVertex (@ @) before UVertexName (@ @) before UAltVertex (@ @) before UConstVertex (@ @) before UConstOrAltVertex (@ @) after { UVertex, UAltVertex, UConstVertex, UConstOrAltVertex } (@ @) before IEdge_List (@ @) before OEdge_List (@ @) before UID (@ @) before JavaCode (@ @) } //**************************ConstVisitor*************************/ // produces construction and repetition productions ConstVisitor { (@ String vname = ""; String parts = ""; String bef = ""; String aft = ""; boolean isrep = false; String srep = ""; String ext = ""; String impl = ""; boolean incoming = true; @) before UVertex //initialize (@ vname = ""; parts = ""; bef = ""; aft = ""; isrep = false; srep = ""; ext = "" ; impl = "" ; incoming = true; //System.out.println("in *************Visitor:: before UVert"); @) before UVertexName (@ //System.out.println("in visitor:UconstVisitor: for - " + host.get_name()); vname = host.get_name().toString(); @) before UConstVertex (@ //System.out.println("in *************Visitor:: before UConstVert"); bef = host.get_beforeSyntax(); aft = host.get_afterSyntax(); @) before IEdge_List (@ incoming = true; @) before OEdge_List (@ incoming = false; @) before UID (@ UEdge ue = (UEdge)etable.get(host); String pname = ""; String tempresult = ""; if(incoming) //extends implements { UVertex uv = (UVertex)vtable.get(ue.get_fromVertex()); pname = uv.get_vertexname().get_name().toString(); if(ue instanceof UExtendEdge) { ext = pname; // no multiple inheritance } else if(ue instanceof UImplEdge) { impl = (impl !=null ? impl : " ") + pname; } } else // parts repetition { boolean opt = false; UVertex uv = (UVertex)vtable.get(ue.get_toVertex()); pname = uv.get_vertexname().get_name().toString(); Integer lower = ((UConstEdge)ue).get_card().get_lower().get_l(); String ebef = ((UConstEdge)ue).get_beforeSyntax(); String eaft = ((UConstEdge)ue).get_afterSyntax(); String ename = ""; if(((UConstEdge)ue).get_edgename()!=null) ename = ((UConstEdge)ue).get_edgename().get_name().toString(); if(lower.equals(new Integer(0))) opt = true; else opt = false; String upper = ""; if(((UConstEdge)ue).get_card().get_upper() != null) { upper = ((UConstEdge)ue).get_card().get_upper().get_u().toString(); if(upper.equals("*")) isrep = true; } tempresult = pname ; if(isrep) { tempresult = (ebef!=null ? ("\""+ebef+"\"") : " ") + tempresult ; tempresult += (eaft!=null ? ("\""+eaft+"\"") : " ") ; tempresult = "{" + tempresult + "}" ; if(opt == false) tempresult = pname + tempresult ; srep = tempresult; } else { tempresult = (ename!=null?("<"+ename+">"):" ") + tempresult; tempresult = (ebef!=null ? ("\""+ebef+"\"") : " ") + tempresult ; tempresult += (eaft!=null ? ("\""+eaft+"\"") : " ") ; if(opt == true) tempresult = "[" + tempresult + "] "; parts = (parts != null ? parts : " ") + tempresult; } } @) after UVertex //concatenate strings (@ if(isrep) { return_val = vname + " ~ " + (bef!=null?("\""+bef+"\" "):" ") + srep + (aft!=null? (" \""+aft+"\""):" ") + ".\n"; } else { return_val = vname + " = " + (bef!=null?("\""+bef+"\""):" ")+ parts; if(ext != "") return_val += " *extends* " + ext ; if(impl != "") return_val += " *implments* " + impl ; return_val += (aft!=null? ("\""+aft+"\""):" ") + ".\n"; } @) } //**************************end ConstVisitor*************************/ //**************************AltVisitor*************************/ // produces alternation productions AltVisitor { (@ String vname = ""; String alts = ""; String parts = ""; String bef = ""; String aft = ""; boolean isrep = false; String srep = ""; String ext = ""; String impl = ""; boolean incoming = true; @) before UVertex //initialize (@ vname = ""; alts = null; parts = ""; bef = ""; aft = ""; isrep = false; srep = ""; ext = "" ; impl = "" ; incoming = true; @) before UVertexName (@ vname = host.get_name().toString(); @) before IEdge_List (@ incoming = true; @) before OEdge_List (@ incoming = false; @) before UID (@ UEdge ue = (UEdge)etable.get(host); String pname = ""; String tempresult = ""; if(incoming) //extends implements { UVertex uv = (UVertex)vtable.get(ue.get_fromVertex()); pname = uv.get_vertexname().get_name().toString(); if(ue instanceof UExtendEdge) { ext = pname; // no multiple inheritance } else if(ue instanceof UImplEdge) { impl = (impl !=null ? impl : " ") + pname; } } else // parts { if (ue instanceof UConstEdge) { boolean opt = false; UVertex uv = (UVertex)vtable.get(ue.get_toVertex()); pname = uv.get_vertexname().get_name().toString(); Integer lower = ((UConstEdge)ue).get_card().get_lower().get_l(); String ebef = ((UConstEdge)ue).get_beforeSyntax(); String eaft = ((UConstEdge)ue).get_afterSyntax(); String ename = ""; if(((UConstEdge)ue).get_edgename()!=null) ename = ((UConstEdge)ue).get_edgename().get_name().toString(); if(lower.equals(new Integer(0))) opt = true; else opt = false; tempresult = pname ; tempresult = (ename!=null?("<"+ename+">"):" ") + tempresult; tempresult = (ebef!=null ? ("\""+ebef+"\"") : " ") + tempresult ; tempresult += (eaft!=null ? ("\""+eaft+"\"") : " ") ; if(opt == true) tempresult = "[" + tempresult + "] "; parts = (parts != null ? parts : " ") + tempresult; } else if (ue instanceof UAltEdge )//instance of UAltEdge { UVertex uv = (UVertex)vtable.get(ue.get_toVertex()); pname = uv.get_vertexname().get_name().toString(); alts = (alts != null ? (alts + " | ") : " ") + pname ; } } @) after UVertex //concatenate strings (@ return_val = vname + " : " + (alts!=null ? alts : ""); return_val += (parts!=null? (" *common* " + parts) : ""); if(ext != "") return_val += " *extends* " + ext ; if(impl != "") return_val += " *implments* " + impl ; return_val += ".\n"; @) } //**************************end AltVisitor*************************/ //**************************IntVisitor*************************/ // produces the interface productions IntVisitor { (@ String vname = ""; String code = null; @) before UVertex //initialize (@ vname = ""; @) before UVertexName (@ vname = host.get_name().toString(); @) before JavaCode (@ code = host.get_code().toString(); @) after UVertex (@ return_val = "*interface* " + vname ; if(code !=null) return_val += " (" + "@ " + code + " @" + ") "; return_val += ".\n"; @) } //**************************end IntVisitor*************************/ Package { traversal toPre(PrintVisitor v) { to * ; } (@ public String getPreString() { StringWriter w = new StringWriter(); toPre(new PrintVisitor(new PrintWriter(w))); return w.toString(); } @) }