//***********************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 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); 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); SaveGraphVisitor sgv = new SaveGraphVisitor(new String()); pv.ofile = of; //PlacementVisitor gplace = new PlacementVisitor(ug); //gplace.anneal(); // ug.toAll(pv); //my printvisitor ug.toAll(sgv); //kedar's printvisitor System.out.println(sgv.get_graphString()); of.close(); newfile.close(); }catch (IOException e){e.printStackTrace();} return ug ; //this to be used by the GUI } @) traversal toAllSubclasses( ClassNameTranspVisitor cntv, EdgeVisitor aev ) { to {Subclass, Part}; } } //********************end class Program *****************************/ //*******************Class UGraph ********************************/ UGraph { traversal toAll( SaveGraphVisitor sgv) { to *; } } //*******************end UGraph class *****************************/ UniqueIDGenerator{ (@ static UID uidgen = new UID(new Integer (0)); public static UID get_NextUnique() { UID olduid = uidgen ; uidgen = new UID (new Integer( uidgen.get_id().intValue() + 1 )); return olduid ; } @) } //*******************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); //magic number why 50? //for want of anything better 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; UID uid = UniqueIDGenerator.get_NextUnique(); //if present , dont care, else introduce a UTerm temporarily. if( this.hTable.containsKey(this.dig_out().get_name())==false) this.hTable.put(this.dig_out().get_name() , new UTerm (this.dig_out().get_name() , UniqueIDGenerator.get_NextUnique() )); @) after ClassDef (@ UID uid = UniqueIDGenerator.get_NextUnique(); //if present and UTerm,get the id, delete and add(with the old id) //else return IEdge_List incoming = null ; OEdge_List outgoing = null ; if( this.hTable.containsKey(this.dig_out().get_name())==true) { if(this.hTable.get( this.dig_out().get_name()).getClass().getName().endsWith("UTerm")) { uid = ((UVertex)this.hTable.get( this.dig_out().get_name() )).get_vid(); incoming = ((UVertex)this.hTable.get( this.dig_out().get_name() )).get_incoming(); outgoing = ((UVertex)this.hTable.get( this.dig_out().get_name() )).get_outgoing(); this.hTable.remove( this.dig_out().get_name()); } else { System.out.println("Possible redefinition of class " + this.dig_out().get_name()); // the UVertex present is either UAltVertex or UConstVertex. The earlier version remains. return ; } } //add the vertex since it is not present. If it were, its been deleted 'cos it was a UTerm. if(this.bFlag == true) { uv = new UConstVertex(this.dig_out().get_name() , uid ); } else { uv = new UAltVertex(this.dig_out().get_name() , uid ); } uv.set_incoming(incoming); uv.set_outgoing(outgoing); //add the vertex into hashtable | the # key being the vertex name. this.hTable.put(this.dig_out().get_name() , uv); @) before Part (@ //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() , UniqueIDGenerator.get_NextUnique())); } UVertex from_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex to_vertex = (UVertex) this.hTable.get( host.get_classspec().get_classname().get_name() ) ; UID from_id = from_vertex.get_vid() ; UID to_id = to_vertex.get_vid(); ue = new UConstEdge( from_id , to_id , host.get_partname().get_name() ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); @) 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; //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() , UniqueIDGenerator.get_NextUnique())); } UVertex from_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex to_vertex = (UVertex) this.hTable.get( host.get_classspec().get_classname().get_name() ) ; UID from_id = from_vertex.get_vid() ; UID to_id = to_vertex.get_vid(); ue = new UAltEdge( from_id , to_id ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); @) 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(); for( ; e.hasMoreElements() ; ) // add vertices to list { e.nextElement(); uv = (UVertex)e2.nextElement(); this.ugraph.add_uvertex(uv); } @) } //***************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)); } } @) //-------------**********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************************************/ //******************UVertex **************************************/ UVertex{ (@ UVertex(Ident name , UID _vid ) { set_vid( _vid); 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) { UID uid = new UID(_uid.get_id()); 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) { UID uid = new UID(_uid.get_id()); 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(UID source_id , UID dest_id , Ident partname ) { super( source_id , dest_id ); if(partname == null) partname = new Ident(" "); set_edgename(new UEdgeName(partname)); } @) } //******************end UConstEdge********************************/ //******************UAltEdge**************************************/ UAltEdge{ (@ UAltEdge(UID source_id , UID dest_id) { super( source_id , dest_id ); } @) } //******************end UAltEdge**********************************/ //******************UEdge**************************************/ UEdge{ (@ UEdge(UID source_id , UID dest_id) { set_eid( UniqueIDGenerator.get_NextUnique() ); set_fromVertex(source_id); set_toVertex(dest_id); } @) } //******************end UEdge**********************************/ //******************UAltVertex************************************/ UAltVertex{ (@ UAltVertex(Ident name , UID _vid ) { super(name , _vid); } @) } //******************end UAltVertex********************************/ //******************UConstVertex**********************************/ UConstVertex{ (@ UConstVertex(Ident name , UID _vid) { super(name , _vid ); } @) } //******************end UConstVertex******************************/ //******************UTerm****************************************/ UTerm{ (@ UTerm(Ident name , UID _vid) { super(name , _vid); } @) } //******************end UTerm*************************************/ //*************PrintVisitor******************************************/ PrintVisitor{ (@ PrintStream ofile; @) before UGraph (@ this.ofile.println (" \n#Class #Dictionary #Graph "); @) before UVertex_List (@ this.ofile.println (" \n#Vertex #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 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 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************************************/ PlacementVisitor { (@ // removed temporarily till a better algorithm is reached. currently just does a random(). @) } //*******************gui code as is from Kedar's work************************ 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)); }*/ @) } ReadEdgeVisitor { before UAltEdge (@ //kedar check here /* Integer edge_x1=host.get_fromVertex().get_position().get_x().get_x(); Integer edge_y1=host.get_fromVertex().get_position().get_y().get_y(); Integer edge_x2=host.get_toVertex().get_position().get_x().get_x(); Integer edge_y2=host.get_toVertex().get_position().get_y().get_y(); System.out.println("added an alternation edge");*/ //Point from = new Point(edge_x1.intValue(), edge_y1.intValue()); //Point to = new Point(edge_x2.intValue(), edge_y2.intValue()); // EdgeInfo edgeinfo = new EdgeInfo(null,null, from, to,fromportno,toportno); //elements.get_alternation().addElement(edgeinfo); @) before UConstEdge (@ //kedar check here String edge_name=host.get_edgename().get_name().toString(); /* Integer edge_x1=host.get_fromVertex().get_position().get_x().get_x(); Integer edge_y1=host.get_fromVertex().get_position().get_y().get_y(); Integer edge_x2=host.get_toVertex().get_position().get_x().get_x(); Integer edge_y2=host.get_toVertex().get_position().get_y().get_y(); */ String card = host.get_card().get_lower().get_l() + (host.get_card().get_upper()==null?"":".."+host.get_card().get_upper().get_u()); System.out.println("added edge"); // Point from = new Point(edge_x1.intValue(), edge_y1.intValue()); //Point to = new Point(edge_x2.intValue(), edge_y2.intValue()); // EdgeInfo edgeinfo = new EdgeInfo(edge_name,card, from, to,fromportno,toportno); //elements.get_construction().addElement(edgeinfo); @) } 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(); System.out.println("added alt vertex"); Point p = new Point(vertex_x.intValue(), vertex_y.intValue()); // VertexInfo vertexinfo = new VertexInfo (vertex_name, p); //elements.get_alternation().addElement(vertexinfo); @) before UConstVertex (@ 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(); System.out.println("added const vertex"); Point p = new Point(vertex_x.intValue(), vertex_y.intValue()); // VertexInfo _vertexinfo = new VertexInfo (vertex_name, p); // elements.get_construction().addElement(_vertexinfo); @) } 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() + "\" "; @) } 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()); } @) }