// *******************Visitors Galore ******************************** // PART - 1 // *******************Visitors Galore ******************************** //#****************** PlacementVisitor ************************************************* PlacementVisitor { (@ private int maxedges ; private int maxvertices ; private Hashtable vTable ; private Hashtable eTable ; void anneal_init() { Nonempty_UEdge_List elist; Nonempty_UVertex_List vlist; UVertex uv; UEdge ue; //--------------what follows would look better in the constructor //---------------but i didnt want to touch generated constructors maxedges = this.ugraph.get_edges().get_first().get_it().get_id().get_id().intValue(); maxvertices = this.ugraph.get_vertices().get_first().get_it().get_id().get_id().intValue(); vTable = new Hashtable(maxvertices); eTable = new Hashtable(maxedges); //----------------------------------------------- vlist = this.ugraph.get_vertices().get_first(); while(vlist != null) { uv = vlist.get_it(); vTable.put(uv.get_id().get_id() , uv); vlist = vlist.get_next(); } elist = this.ugraph.get_edges().get_first(); while(elist != null) { ue = elist.get_it(); eTable.put(ue.get_id().get_id() , ue); elist = elist.get_next(); } } void anneal() { anneal_init(); int temp = 1000; double rate = 0.80 ; UID id1 = new UID(); UID id2 = new UID(); double prob = 0; double rndm = 0; int cost; for ( ;temp > 0 ; temp = (int) (((double)temp)*rate)) { do { id1.set_id(new Integer((int) Math.ceil( Math.random() * maxvertices ))); id2.set_id(new Integer((int) Math.ceil( Math.random() * maxvertices ))); }while ( id1.get_id().intValue() == id2.get_id().intValue() ); UVertex v1 = (UVertex)this.vTable.get(id1.get_id()); UVertex v2 = (UVertex)this.vTable.get(id2.get_id()); cost = cost_of_swap( v1 , v2 ); if(cost > 0) { prob = Math.exp(-(cost*cost)/temp); rndm = Math.random(); } if(cost <= 0 | rndm < prob ) { swap_vertex_pos(v1 , v2); //increse # of swaps // System.out.println("Swapping vertex at temp = " + temp); } } } int vertex_cost(UVertex v) { int x1 , x2; int y1 , y2; double length = 0; UVertex v2; x1 = v.get_position().get_x().get_x().intValue(); y1 = v.get_position().get_y().get_y().intValue(); Nonempty_IEdge_List inlist; Nonempty_OEdge_List outlist ; UID uid; inlist = v.get_incoming().get_first(); while(inlist != null) { uid = inlist.get_it(); v2 = (UVertex)vTable.get(uid.get_id()); x2 = v2.get_position().get_x().get_x().intValue(); y2 = v2.get_position().get_y().get_y().intValue(); length += Math. sqrt(x1*x2 - y1*y2); inlist = inlist.get_next(); } outlist = v.get_outgoing().get_first(); while(outlist != null) { uid = outlist.get_it(); v2 = (UVertex)vTable.get(uid.get_id()); x2 = v2.get_position().get_x().get_x().intValue(); y2 = v2.get_position().get_y().get_y().intValue(); length += Math. sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)); outlist = outlist.get_next(); } // System.out.println("vertex cost " + length); return (int)length; } int graph_cost() { return 0; } void swap_vertex_pos(UVertex v1 , UVertex v2) { Coordinates pos; pos = v1.get_position(); v1.set_position(v2.get_position()); v2.set_position(pos); } int cost_of_swap(UVertex v1 , UVertex v2) { int cost1 , cost2; cost1= vertex_cost(v1) + vertex_cost(v2); swap_vertex_pos(v1,v2); cost2= vertex_cost(v1) + vertex_cost(v2); swap_vertex_pos(v1,v2); return (cost2 - cost1) ; } @) } //#****************** PlacementVisitor ************************************************* //#*****************ClassNameTranspVisitor*************************** ClassNameTranspVisitor // It gets the source classname { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @) } //#*****************ClassNameTranspVisitor**************************** //#*****************ClassNameRetVisitor******************************* ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @) } //#*****************ClassNameRetVisitor******************************* //#*****************EdgeVisitor************************************** EdgeVisitor { traversal toClassName( ClassNameRetVisitor cnrv ) { bypassing -> *,parameters,* to ClassName;} (@ /** Retrieve the class name from the visitor. Requires a traversal from the visitor class which itself needs a visitor. */ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } Hashtable hTable = new Hashtable(50); //magic number why 50? UVertex uv; //create a UVertex* in the visitor //creating instances before ClassDef UEdge ue; //create a UEdge* in the visitor //creating instances before Part & Subclass boolean bFlag ; //to diff alt and const vertex. //false = alt vertex. boolean bOptFlag; //for diff cardinality.(optional = 0). //true = optional part. @) before ClassDef (@ this.bFlag = true; //TRUE means const vertex ; FALSE means its alt vertex // set to TRUE before each vertex this.bOptFlag = false; @) after ClassDef (@ //if present and UTerm, delete and add //else return if( this.hTable.containsKey(this.dig_out().get_name())==true) { System.out.println("**vector contains**"+this.hTable.get(this.dig_out().get_name()).getClass().getName()); if(this.hTable.get( this.dig_out().get_name()).getClass().getName().endsWith("UTerm")) { this.hTable.remove( this.dig_out().get_name()); } else { System.out.println("**Possible redefinition of class " + this.dig_out().get_name()); this.hTable.remove( this.dig_out().get_name()); //keep only the latest definition return ; } } //add the vertex since it is not present. if(this.bFlag == true) { uv = new UConstVertex(this.dig_out().get_name()); } else { uv = new UAltVertex(this.dig_out().get_name()); } this.hTable.put(this.dig_out().get_name() , uv); @) before Part (@ ue = new UConstEdge(this.dig_out().get_name(), host.get_classspec().get_classname().get_name() , host.get_partname().get_name() ); //add source, dest, partname //add vertex to hashtable if not already present if( this.hTable.containsKey( host.get_classspec().get_classname().get_name())==false) { this.hTable.put( host.get_classspec().get_classname().get_name() , new UTerm(host.get_classspec().get_classname().get_name() )); } @) after Part (@ Cardinality crdn; if(this.bOptFlag == true) { crdn = new Cardinality(new Lower(new Integer(0)) ,new Upper( new String("1"))); } else { crdn = new Cardinality(); crdn.set_lower(new Lower(new Integer(1))); } ((UConstEdge)ue).set_card(crdn); this.ugraph.add_uedge(ue); @) before Subclass (@ this.bFlag = false; ue = new UAltEdge(this.dig_out().get_name(), host.get_classspec().get_classname().get_name() ); // add source, dest //add vertex to hashtable if not already present if( this.hTable.containsKey( host.get_classspec().get_classname().get_name())==false) { this.hTable.put( host.get_classspec().get_classname().get_name() , new UTerm(host.get_classspec().get_classname().get_name() )); } @) after Subclass (@ this.ugraph.add_uedge(ue); @) before OptionalPart (@ this.bOptFlag = true; @) after OptionalPart (@ this.bOptFlag = false; @) after Program (@ Enumeration e = this.hTable.keys(), e2 = this.hTable.elements(); int count ; for( count =1 ; e.hasMoreElements() ; count++) //assign unique id# to vertices and add to list { e.nextElement(); uv = (UVertex)e2.nextElement(); uv.set_id(new UID(new Integer(count))); this.ugraph.add_uvertex(uv); } //assign edges unique id# and let all edges reflect // the right type of to & from Vertex Nonempty_UEdge_List list; list = this.ugraph.get_edges().get_first(); UID uid; count = 1; while(list != null) { ue = list.get_it(); //-------------actual code uid = new UID(new Integer(count)); ue.set_id( uid ); //set uids uv =(UVertex)this.hTable.get( ue.get_fromVertex().get_vertexname().get_name() ); ue.set_fromVertex( uv ); //update fromvertex uv.add_outgoing( uid ); uv =(UVertex)this.hTable.get( ue.get_toVertex().get_vertexname().get_name() ); ue.set_toVertex( uv ); //update tovertex uv.add_incoming( uid ); //------------- list = list.get_next(); count++; } @) } //***************end EdgeVisitor***********************************/ // Kedar's visitors ==> //#****************** ReadEdgeVisitor ************************************************* ReadEdgeVisitor { before UAltEdge (@ UID from=host.get_fromVertex().get_id(); UID to=host.get_toVertex().get_id(); UID uid=host.get_id(); EdgeInfo edgeinfo = new EdgeInfo(null,null, from, to,uid); elements.get_alternation().addElement(edgeinfo); @) before UConstEdge (@ String edge_name=host.get_edgename().get_name().toString(); String card = host.get_card().get_lower().get_l() + (host.get_card().get_upper()==null?"":".."+host.get_card().get_upper().get_u()); UID from=host.get_fromVertex().get_id(); UID to=host.get_toVertex().get_id(); UID uid=host.get_id(); EdgeInfo edgeinfo = new EdgeInfo(edge_name,card, from, to,uid); elements.get_construction().addElement(edgeinfo); @) } //#****************** ReadEdgeVisitor ************************************************* //#****************** ReadVertexVisitor ************************************************* ReadVertexVisitor { before UAltVertex (@ String vertex_name=host.get_vertexname().get_name().toString(); Integer vertex_x=host.get_position().get_x().get_x(); Integer vertex_y=host.get_position().get_y().get_y(); Point p = new Point(vertex_x.intValue(), vertex_y.intValue()); UID uid=host.get_id(); VertexInfo vertexinfo = new VertexInfo (vertex_name, p,uid); elements.get_alternation().addElement(vertexinfo); @) before {UConstVertex,UTerm } (@ String vertex_name=host.get_vertexname().get_name().toString(); Integer vertex_x=host.get_position().get_x().get_x(); Integer vertex_y=host.get_position().get_y().get_y(); Point p = new Point(vertex_x.intValue(), vertex_y.intValue()); UID uid=host.get_id(); VertexInfo _vertexinfo = new VertexInfo (vertex_name, p,uid); elements.get_construction().addElement(_vertexinfo); @) } //#****************** ReadVertexVisitor ************************************************* //#****************** SaveGraphVisitor ************************************************* SaveGraphVisitor { before UGraph (@ graphString = graphString + "#Class #Dictionary #Graph\n"; @) before UVertex_List (@graphString = graphString + "\n#Vertex #List\n"; @) before UEdge_List (@graphString = graphString + "\n#Edge #List\n"; @) before IEdge_List (@ graphString = graphString + "#Incoming "; @) before OEdge_List (@ graphString = graphString + "#Outgoing "; @) before UConstVertex (@ graphString = graphString +"\n #ConstVertex "; @) before UAltVertex (@ graphString = graphString + "\n #AltVertex "; @) before UTerm (@ graphString = graphString +"\n #TermVertex "; @) before UAltEdge (@ graphString = graphString + "\n #AltEdge "; @) before UConstEdge (@ graphString = graphString + "\n #ConstEdge "; @) before UEdgeName (@ graphString = graphString + host.get_name() +" "; @) before UVertexName (@ graphString = graphString + host.get_name() +" "; @) before Coordinates (@ graphString = graphString + " { "; @) after Coordinates (@ graphString = graphString + " } "; @) before X (@ graphString = graphString + host.get_x() +" "; @) before Y (@ graphString = graphString + host.get_y() +" "; @) before UID (@ graphString = graphString + host.get_id() +" "; @) before Lower (@ graphString = graphString + host.get_l() +" "; @) before Upper (@ graphString = graphString + ".." + "\"" + host.get_u() + "\" "; @) } //#****************** SaveGraphVisitor ************************************************* // *******************Classes with pure java code and Traversals************************ // PART - 2 // *******************Classes with pure java code and Traversals************************ //******************UVertex **************************************/ UVertex{ (@ UVertex(Ident name) { set_id(new UID(new Integer(0))); set_vertexname(new UVertexName(name)); //magic numbers 600 & 400 = screen x y max set_position(new Coordinates(new X(new Integer((int)(600 * Math.random()))),new Y(new Integer((int)(400 * Math.random()))))); set_incoming(new IEdge_List()); set_outgoing(new OEdge_List()); } //add a incoming UID into the UVetex::IEdge_List public void add_incoming(UID uid) { if(get_incoming() != null ) { Nonempty_IEdge_List newlist = new Nonempty_IEdge_List ( uid , get_incoming().get_first()); get_incoming().set_first(newlist); } else { Nonempty_IEdge_List newlist = new Nonempty_IEdge_List( uid , null); set_incoming(new IEdge_List(newlist)); } } //add a outgoing UID into the UVertex::OEdge_List public void add_outgoing(UID uid) { if(get_outgoing() != null ) { Nonempty_OEdge_List newlist = new Nonempty_OEdge_List ( uid , get_outgoing().get_first()); get_outgoing().set_first(newlist); } else { Nonempty_OEdge_List newlist = new Nonempty_OEdge_List( uid , null); set_outgoing(new OEdge_List(newlist)); } } @) } //******************end UVertex **********************************/ //******************UConstEdge************************************/ UConstEdge{ (@ UConstEdge(Ident source , Ident dest , Ident partname ) { if(partname == null) partname = new Ident(" "); super.set_fromVertex((UVertex)new UConstVertex(source)); super.set_toVertex((UVertex)new UConstVertex(dest)); set_edgename(new UEdgeName(partname)); } @) } //******************end UConstEdge********************************/ //******************UAltEdge**************************************/ UAltEdge{ (@ UAltEdge(Ident source , Ident dest) { super.set_fromVertex((UVertex)new UAltVertex(source)); super.set_toVertex((UVertex)new UConstVertex(dest)); //hey, the dest can be alt } @) } //******************end UAltEdge**********************************/ //******************UAltVertex************************************/ UAltVertex{ (@ UAltVertex(Ident name) { super(name); } @) } //******************end UAltVertex********************************/ //******************UConstVertex**********************************/ UConstVertex{ (@ UConstVertex(Ident name) { super(name); } @) } //******************end UConstVertex******************************/ //******************UTerm****************************************/ UTerm{ (@ UTerm(Ident name) { super(name); } @) } //******************end UTerm*************************************/ //***********************Entry Point into appln ************************/ Program { (@ public UGraph print_edges () { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); UGraph ug = new UGraph(); EdgeDistinctionVisitor edv = new EdgeDistinctionVisitor(); EdgeVisitor aev = new EdgeVisitor(edv , cntv , ug); this.toAllSubclasses(cntv,aev); return ug ; //this to be used by the GUI } @) traversal toAllSubclasses(ClassNameTranspVisitor cntv,EdgeVisitor aev) { to {Subclass, Part}; } } //********************end class Program *****************************/ //******************UGraph****************************************/ UGraph { (@ //add a UVertex into the UVertex_List public void add_uvertex(UVertex uvertex) { if(get_vertices() != null ) { Nonempty_UVertex_List newlist = new Nonempty_UVertex_List( uvertex , get_vertices().get_first()); get_vertices().set_first(newlist); //get a UVertexList, get a Nonempty_UvertexList , replace // old one with new one, it=uvertex , next = old_Ne_V_L } else { Nonempty_UVertex_List newlist = new Nonempty_UVertex_List( uvertex , null); set_vertices(new UVertex_List(newlist)); } } //add a UEdge into the UEdge_Lis public void add_uedge(UEdge uedge) { if(get_edges() != null ) { Nonempty_UEdge_List newlist = new Nonempty_UEdge_List( uedge , get_edges().get_first()); get_edges().set_first(newlist); //get a UEdgeList, get its Nonempty_UEdgeList , replace // old one with new one, it=uedge , next = old_Ne_V_L } else { // System.out.println("NULL edges"); Nonempty_UEdge_List newlist = new Nonempty_UEdge_List( uedge , null); set_edges(new UEdge_List(newlist)); } } @) //-------------**********kedar's traversals traversal ReadAllVertices(ReadVertexVisitor rvv) { bypassing -> *,edges,* to { UConstVertex, UAltVertex, UTerm}; } (@ public VertexContainer GetAllVertices() { ReadVertexVisitor rvv = new ReadVertexVisitor( new VertexContainer(new Vector(),new Vector(),new Vector())); this.ReadAllVertices(rvv); return rvv.get_elements(); } @) traversal ReadAllEdges(ReadEdgeVisitor rev) { bypassing -> *,vertices,* to { UConstEdge, UAltEdge}; } (@ public EdgeContainer GetAllEdges() { ReadEdgeVisitor rev= new ReadEdgeVisitor( new EdgeContainer(new Vector(),new Vector())); this.ReadAllEdges(rev); return rev.get_elements(); } @) traversal saveGraph(SaveGraphVisitor sgv) { to *; } (@ public String GetGraphString() { SaveGraphVisitor sgv= new SaveGraphVisitor(new String()); this.saveGraph(sgv); return sgv.get_graphString(); } @) } //******************end UGraph************************************/ //******************* kedar's classes ************************ VertexContainer //all this because jack doesnt respect predefinned classes { (@ private Vector construction; public Vector get_construction() { return construction; } public Vector set_construction(Vector construction) { Vector old_construction = this.construction; this.construction = construction; return old_construction; } private Vector alternation; public Vector get_alternation() { return alternation; } public Vector set_alternation(Vector alternation) { Vector old_alternation = this.alternation; this.alternation = alternation; return old_alternation; } private Vector termination; public Vector get_termination() { return termination; } public Vector set_termination(Vector termination) { Vector old_termination = this.termination; this.termination = termination; return old_termination; } /* VertexContainer() { }*/ public VertexContainer(Vector construction , Vector alternation , Vector termination ) { super(); set_construction(construction); set_alternation(alternation); set_termination(termination); } /* public static VertexContainer parse(java.io.InputStream in) throws Exception { Parser p = new Parser(in); return p.VertexContainer(); } public static VertexContainer parse(String str) throws Exception { return parse(new java.io.StringBufferInputStream(str)); } */ @) } EdgeContainer //all this just because jack doesnt respect predefined classes { (@ private Vector construction; public Vector get_construction() { return construction; } public Vector set_construction(Vector construction) { Vector old_construction = this.construction; this.construction = construction; return old_construction; } private Vector alternation; public Vector get_alternation() { return alternation; } public Vector set_alternation(Vector alternation) { Vector old_alternation = this.alternation; this.alternation = alternation; return old_alternation; } /*EdgeContainer() { }*/ public EdgeContainer(Vector construction , Vector alternation ) { super(); set_construction(construction); set_alternation(alternation); } /*public static EdgeContainer parse(java.io.InputStream in) throws Exception { Parser p = new Parser(in); return p.EdgeContainer(); } public static EdgeContainer parse(String str) throws Exception { return parse(new java.io.StringBufferInputStream(str)); }*/ @) } Main { (@ public static void main(String args[]) throws Exception { FileInputStream fi=new FileInputStream("demdraw.input"); UGraph graph = UGraph.parse(fi); graph.GetAllVertices(); graph.GetAllEdges(); System.out.println(graph.GetGraphString()); } @) }