//***********************Entry Point into appln ************************/ Program { (@ /** Prints all edges in a given class graph. A program to show second order visitors. Requires a visitor to traverse a visitor. */ public static void main(String args[]) throws Exception { Program p = parse(System.in); p.print_edges(); } @) (@ public void 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); String filename = new String("temp.out"); //System.out.println("Enter filename to save the print visitor in - "); //System.in.read(filename); System.out.println("Saving in file : " + filename); try{ FileOutputStream newfile = new FileOutputStream(filename); PrintStream of = new PrintStream(newfile); PrintVisitor pv = new PrintVisitor(ug); pv.ofile = of; ug.toAll(pv); of.close(); newfile.close(); }catch (IOException e){e.printStackTrace();} } @) traversal toAllSubclasses( ClassNameTranspVisitor cntv, EdgeVisitor aev ) { to {Subclass, Part}; } } //********************end class Program *****************************/ //*******************Class UGraph ********************************/ UGraph { traversal toAll( PrintVisitor pv) { to *; } } //*******************end UGraph class *****************************/ //*******************Visitors Galore ********************************/ //**ClassNameTranspVisitor gets the source classname */ ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @) } ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @) } //**Distinguishes the alternation and construction edges */ EdgeDistinctionVisitor { } //*****************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); 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) { if(this.hTable.get( this.dig_out().get_name()).getClass().getName().equals("UTerm")) { this.hTable.remove( this.dig_out().get_name()); } else { System.out.println("Possible redefinition of class " + this.dig_out().get_name()); 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.ugraph.add_uvertex(uv); //add vertex name //into hashtable 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; @) before Program (@ // System.out.println("\n***before Program***\n "); @) after Program (@ //System.out.println("\n***after Program***\n"); Enumeration e = this.hTable.keys(), e2 = this.hTable.elements(); for( int count =1 ; e.hasMoreElements() ; count++) //assign unique id# to vertices and add to list { //System.out.print("# " + count +" "); //System.out.println("*"+ ); e.nextElement(); uv = (UVertex)e2.nextElement(); uv.set_id(new UID(new Integer(count))); //System.out.println("the vertex id #"+ uv.get_id().get_id()); 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(); int count = 1; UID uid; 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***********************************/ //******************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)); } } @) } //******************end UGraph************************************/ //******************UVertex **************************************/ UVertex{ (@ UVertex(Ident name) { set_id(new UID(new Integer(0))); set_vertexname(new UVertexName(name)); set_position(new Coordinates(new X(new Integer(1)),new Y(new Integer(1)))); 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)); super.set_fromPortNo(new PortNo(new Integer(1))); super.set_toPortNo(new PortNo(new Integer(1))); } @) } //******************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 super.set_fromPortNo(new PortNo(new Integer(1))); super.set_toPortNo(new PortNo(new Integer(1))); } @) } //******************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*************************************/ //******************UEdgeName*************************************/ UEdgeName{ (@/* UEdgeName(Ident name) { set_name(name); } */ @) } //******************end UEdgeName*********************************/ //******************UVertexName***********************************/ UVertexName{ (@/* UVertexName(Ident name) { set_name(name); } */@) } //******************end UVertexName*******************************/ //*************PrintVisitor******************************************/ PrintVisitor{ (@ PrintStream ofile; @) before UGraph (@ this.ofile.println (" \nClass Dictionary Graph "); @) before UVertex_List (@ this.ofile.println (" \nVertex List "); @) after UVertex_List (@ this.ofile.println ("\n\n "); @) before IEdge_List (@ this.ofile.print (" Incoming "); @) before OEdge_List (@ this.ofile.print (" Outgoing "); @) before UConstVertex (@ this.ofile.print ("\n\t ConstVertex "); @) before UAltVertex (@ this.ofile.print ("\n\t AltVertex "); @) before UTerm (@ this.ofile.print ("\n\t TermVertex "); @) before UEdge_List (@ this.ofile.println ("Edge List "); @) after UEdge_List (@ this.ofile.println ("\n\n "); @) before Coordinates_List (@ /*this.ofile.print (" Bend Points ");*/ @) before UAltEdge (@ this.ofile.print ("\n\t AltEdge "); @) before UConstEdge (@ this.ofile.print ("\n\t ConstEdge "); @) before Lower (@ this.ofile.print (host.get_l() ); @) before Upper (@ this.ofile.print (" .. \"" + host.get_u() + "\" "); @) before PortNo (@ this.ofile.print (" PortNo " + host.get_integer()+" "); @) before UID (@ this.ofile.print (" "+host.get_id()+" "); @) before UEdgeName (@ this.ofile.print (" "+host.get_name()+" "); @) before UVertexName (@ this.ofile.print (" "+host.get_name()+ " "); @) before Coordinates (@ this.ofile.print (" { "); @) after Coordinates (@ this.ofile.print (" } "); @) before X (@ this.ofile.print (host.get_x()+" "); @) before Y (@ this.ofile.print(host.get_y()); @) } //**************end PrintVisitor************************************/