package EDU.neu.ccs.demeter.tools.apstudio.graphedit; import java.awt.*; import java.io.*; import java.util.*; import EDU.neu.ccs.demeter.*; import EDU.neu.ccs.demeter.common.tg.*; import EDU.neu.ccs.demeter.*; class EdgeVisitor extends UniversalVisitor { protected EdgeDistinctionVisitor edv; public EdgeDistinctionVisitor get_edv() { return edv; } public void set_edv(EdgeDistinctionVisitor new_edv) { edv = new_edv; } protected ClassNameTranspVisitor cntv; public ClassNameTranspVisitor get_cntv() { return cntv; } public void set_cntv(ClassNameTranspVisitor new_cntv) { cntv = new_cntv; } protected UGraph ugraph; public UGraph get_ugraph() { return ugraph; } public void set_ugraph(UGraph new_ugraph) { ugraph = new_ugraph; } EdgeVisitor() { super(); } public EdgeVisitor(EdgeDistinctionVisitor edv, ClassNameTranspVisitor cntv, UGraph ugraph) { super(); set_edv(edv); set_cntv(cntv); set_ugraph(ugraph); } /** 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. boolean bFirstUID; boolean bParse ; //this one for individual classwise parse/noparse boolean bBlockParse ; //parse/noparse for a block boolean bClassDef ; //set when a classdef is encountered in traversal String class_before; String class_after; String edge_before; String edge_after; String var_syntax; boolean tripflag; //true = after atleast one part/subclass public void before_parameters(ParamClassName source, ClassName_Commalist dest) { throw new ParameterisationException("Detected parameterized classes"); } public void before_actual_parameters(ClassSpec source, ClassSpec_Commalist dest) { throw new ParameterisationException("Detected parameterized classes"); } public void before(Program host) { bFirstUID = false ; bParse = true ; bBlockParse = true ; bClassDef = false ; } public void before(DoParse host) { if(bClassDef) bParse = true ; else bBlockParse = true ; } public void before(DontParse host) { if(bClassDef) bParse = false ; else bBlockParse = false ; } public void before(ClassDef host) { this.bFlag = true; //TRUE means const vertex ; FALSE means its alt vertex // set to TRUE before each vertex:classdef this.bOptFlag = false; this.bClassDef = true; bParse = bBlockParse ; 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 UTermConstVertex (this.dig_out().get_name() , UniqueIDGenerator.get_NextUnique() )); class_before = null; class_after = null; edge_before = null; edge_after = null; var_syntax = null; tripflag = false; } public void after(ClassDef host) { 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 ; // System.out.println("after classdef -" + this.dig_out().get_name()); if( this.hTable.containsKey(this.dig_out().get_name())==true) { // System.out.println("already in hashtable as "+this.hTable.get( this.dig_out().get_name()).getClass().getName() ); if(this.hTable.get( this.dig_out().get_name()).getClass().getName().endsWith("UTermConstVertex")) { // System.out.println("UT true"); 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("UT false"); String errMesg ="Possible redefinition of class " + this.dig_out().get_name(); System.out.println(errMesg); // the UVertex present is either UAltVertex or UConstVertex. The earlier version remains. this.bClassDef = false; //kedar throw a redefinition exception here throw new UnsupportedSyntaxException(errMesg); // 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 , bParse); class_after = var_syntax; ((UConstVertex)uv).set_beforeSyntax(class_before); ((UConstVertex)uv).set_afterSyntax(class_after);} else { uv = new UAltVertex(this.dig_out().get_name() , uid , bParse); } uv.set_incoming(incoming); uv.set_outgoing(outgoing); ScopeIdentifier_List sl = host.getClassMods(); if(!sl.isEmpty()) ((UConstOrAltVertex)uv).set_keywords(sl); else ((UConstOrAltVertex)uv).set_keywords(null); //add the vertex into hashtable | the # key being the vertex name. this.hTable.put(this.dig_out().get_name() , uv); if(! this.bFirstUID) { this.get_ugraph().set_firstuid(uv.get_vid()); bFirstUID = true; } this.bClassDef = false; } public void before(PlainSyntax host) { if(var_syntax != null) var_syntax += host.get_string(); else var_syntax = host.get_string(); } public void before(Part host) { //add vertex to hashtable if not already present // System.out.println("in Part" + host.get_classspec().get_classname().get_name()); if( this.hTable.containsKey( host.get_classspec().get_classname().get_name())==false) { // System.out.println("in Part(new) -"); this.hTable.put( host.get_classspec().get_classname().get_name() , new UTermConstVertex(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(); if(host.get_partname() == null) { host.set_partname(new PartName(new Ident((host.get_classspec()).get_classname().get_name().toString().toLowerCase()))); } ue = new UConstEdge( from_id , to_id , new Ident(host.get_partname().toString()) ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); if( tripflag || bOptFlag ) edge_before = var_syntax; else class_before = var_syntax; } public void after(Part host) { 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); var_syntax = null; tripflag = true; ((UConstEdge)ue).set_beforeSyntax(edge_before); PartModifier_List pl = host.getPartMods(); if(!pl.isEmpty()) ((UConstEdge)ue).set_keywords(pl); else ((UConstEdge)ue).set_keywords(null); } public void before(Subclass host) { 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 UTermConstVertex(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()); } public void after(Subclass host) { this.ugraph.add_uedge(ue); tripflag = true; } public void before(Superclass host) { //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 UTermConstVertex(host.get_classspec().get_classname().get_name() , UniqueIDGenerator.get_NextUnique())); } UVertex to_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex from_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 UExtendEdge( from_id , to_id ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); } public void before(Interface host) { //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 UTermConstVertex(host.get_classspec().get_classname().get_name() , UniqueIDGenerator.get_NextUnique())); } UVertex to_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex from_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 UImplEdge( from_id , to_id ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); } public void after(Superclass host) { this.ugraph.add_uedge(ue); //tripflag = true; } public void after(Interface host) { this.ugraph.add_uedge(ue); //tripflag = true; } public void before(OptionalPart host) { this.bOptFlag = true; if(!tripflag) class_before = var_syntax; else { edge_after = var_syntax; if(edge_after != null) //if prev edge was alt, then syntax==null ((UConstEdge)ue).set_afterSyntax(edge_after); } var_syntax = null; } public void after(OptionalPart host) { this.bOptFlag = false; edge_after= var_syntax; var_syntax = null; ((UConstEdge)ue).set_afterSyntax(edge_after); // System.out.println("bef str - "+ ((UConstEdge)ue).get_beforeSyntax()+" aft str -" + ((UConstEdge)ue).get_afterSyntax()); } public void before(RepeatedPart host) { Name first = null; Name inner = null; RepClassSpecVisitor csv = new RepClassSpecVisitor(first , inner); host.toClassSpec(csv); first = csv.get_first(); inner = csv.get_inner(); Cardinality crdn; if( first != null) { if( first.equals(inner) ) { //ue:: cardinality 1..* crdn = new Cardinality(new Lower(new Integer(1)) ,new Upper( new String("*"))); } else { //ue::cardinality 1:first //ue::cardinality 0..*:inner //first if(this.hTable.containsKey(first) == false) { this.hTable.put(first , new UTermConstVertex(first , UniqueIDGenerator.get_NextUnique())); } UVertex from_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex to_vertex = (UVertex) this.hTable.get( first ) ; UID from_id = from_vertex.get_vid() ; UID to_id = to_vertex.get_vid(); ue = new UConstEdge( from_id , to_id , null ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); crdn = new Cardinality(); crdn.set_lower(new Lower(new Integer(1))); ((UConstEdge)ue).set_card(crdn); this.ugraph.add_uedge(ue); //inner crdn = new Cardinality(new Lower(new Integer(0)) ,new Upper( new String("*"))); } } else { //ue::cardinality 0..* crdn = new Cardinality(new Lower(new Integer(0)) ,new Upper( new String("*"))); } if(inner != null) { if(this.hTable.containsKey(inner) == false) { this.hTable.put(inner , new UTermConstVertex(inner , UniqueIDGenerator.get_NextUnique())); } UVertex from_vertex = (UVertex) this.hTable.get(this.dig_out().get_name() ) ; UVertex to_vertex = (UVertex) this.hTable.get( inner ) ; UID from_id = from_vertex.get_vid() ; UID to_id = to_vertex.get_vid(); ue = new UConstEdge( from_id , to_id , null ); from_vertex.add_outgoing( ue.get_eid()); to_vertex.add_incoming( ue.get_eid()); ((UConstEdge)ue).set_card(crdn); class_before = var_syntax; host.setSyntax((UConstEdge)ue); this.ugraph.add_uedge(ue); } } public void after(RepeatedPart host) { var_syntax = null; } public void before(PackageDirective host) { this.ugraph.set_pkg(host.get_pkg()); } public void before(LocalImports host) { this.ugraph.set_imports(host.get_imports()); } public void after(Program host) { Enumeration e = this.hTable.keys(), e2 = this.hTable.elements(); for( ; e.hasMoreElements() ; ) // add vertices to list { e.nextElement(); uv = (UVertex)e2.nextElement(); if(uv.get_incoming() != null) if(uv.get_incoming().isEmpty()) uv.set_incoming(null); if(uv.get_outgoing() != null) if(uv.get_outgoing().isEmpty()) uv.set_outgoing(null); this.ugraph.add_uvertex(uv); } } void universal_trv0_bef(UniversalVisitor _v_) { super.universal_trv0_bef(_v_); } void universal_trv0_aft(UniversalVisitor _v_) { super.universal_trv0_aft(_v_); } void universal_trv0(UniversalVisitor _v_) { universal_trv0_bef(_v_); edv.universal_trv0(_v_); cntv.universal_trv0(_v_); ugraph.universal_trv0(_v_); super.universal_trv0(_v_); universal_trv0_aft(_v_); } public void toClassName(ClassNameRetVisitor cnrv) { toClassName_EdgeVisitor_trv(cnrv); } void toClassName_EdgeVisitor_trv_bef(ClassNameRetVisitor cnrv) { } void toClassName_EdgeVisitor_trv_aft(ClassNameRetVisitor cnrv) { } void toClassName_EdgeVisitor_trv(ClassNameRetVisitor cnrv) { toClassName_EdgeVisitor_trv_bef(cnrv); cntv.toClassName_EdgeVisitor_trv(cnrv); toClassName_EdgeVisitor_trv_aft(cnrv); } }