// Mihran Shahinian & Genci Xhaxhka COM 1205 // Checking that all Alternatives of Alternation class are correctly defined // Checking that all alternatives are defined as either construction // classes or alternation classes that will eventually be defined by construction classes ... // Checking Inheritance Cycles ClassGraph { (@ protected Vector visited = new Vector() ; @) (@ public void init_check() { ClassNameTranspVisitor cntv = new ClassNameTranspVisitor(); EdgeVisitor aev = new EdgeVisitor(cntv,new Vector()); this.toAllSubclasses(cntv,aev); findChildren (aev.get_tree()); checkCycles (); } public void checkCycles () { for (int i=0;i 0) { //error! System.out.println("This path:" + szpath + " has cycles."); } else { //ok System.out.println("This path:" + szpath + " is OK."); } } } } public int foundACycle (String path, String name) { StringTokenizer sttok = new StringTokenizer (path, "<-"); int icycles = 0; while (sttok.hasMoreTokens()) { String sztest = sttok.nextToken(); if (name.equals(sztest)) { icycles++; } } return (icycles); } public int searchVisited (String name) { int ifound = -1; int ivisitedsize = visited.size(); int ivisited = 0; while (ifound == -1 && ivisited < ivisitedsize) { Node node = (Node)visited.elementAt (ivisited); if (name.equals(node.getName())) { ifound = ivisited; } ivisited++; } return (ifound); } public void findChildren (Vector vsource) { Node parent; Node child; int isrcsize = vsource.size(); int isource = 0; int iparent = -1; while (isource < isrcsize) { // Get the parent node String szparent = (String)vsource.elementAt(isource); //is the a super node?... iparent = searchVisited (szparent); if (iparent == -1) { iparent = visited.size(); parent = new Node (szparent); visited.addElement (parent); // System.out.println("Pushing Base Node " + vsource.elementAt(isource)); } else { parent = (Node)visited.elementAt(iparent); // System.out.println("Found parent " + vsource.elementAt(isource)); } //set the childrenflag... parent.setHasChildren (true); //skip the stupid : isource += 2; String szchild = (String)vsource.elementAt(isource); child = new Node (szchild); child.setParent(iparent); visited.addElement (child); // System.out.println(vsource.elementAt(isource) + " is a child..."); isource++; } } @) traversal toAllSubclasses( ClassNameTranspVisitor cntv, EdgeVisitor aev) { to {Subclass, Part}; } (@ void visit_cgraph() { AltCheckVisitor acv = new AltCheckVisitor(); this.through_alt(acv); } @) traversal through_alt(AltCheckVisitor acv) { bypassing { ConstructionClass, RepetitionClass } to ClassName ; } (@ NameContainer findAlternatives(){ RightSide_ClassVisitor rcv = new RightSide_ClassVisitor(new NameContainer()); this.all_alternatives( rcv ); return rcv.get_right_side(); } @) (@ NameContainer findDemClasses(){ LeftSide_ClassVisitor lcv = new LeftSide_ClassVisitor (new NameContainer()); this.allClassDefinitions( lcv ); return lcv.get_left_side(); } @) (@ void check_alternatives( NameContainer DemClasses, NameContainer Alternatives) { // Checking that a left side i.e Consruction classes contain // Alternatives of alternation class. First we make a new copy // of a vector which contains alternatives. Every time we match // alternative in the DemClasses vector we remove that element // from our copy. At the end temp vector contains alternatives // which are not defined as construction classes. Vector temp = (Vector)Alternatives.clone(); // Make a copy for (int i = Alternatives.size()-1 ; i >=0; i--) { for (int j = 0 ; j " + temp.elementAt(i)+"()... "); } } @) traversal all_alternatives( RightSide_ClassVisitor rcv ) { // P2 bypassing {ConstructionClass , RepetitionClass} to ClassName; } traversal allClassDefinitions( LeftSide_ClassVisitor lcv ) { // P2 bypassing {Subclass_Barlist, RepetitionClass} to ClassName; } } LeftSide_ClassVisitor { // P2 before -> ClassDef, *, ParamClassName (@ this.get_left_side(). addElement( dest.get_classname().get_name()); @) } RightSide_ClassVisitor { // P2 before Subclass (@ // Law of Demeter violated this.get_right_side().addElement( host.get_classspec().get_classname().get_name()); @) } ParamClassName { // Extended to visit Left Side of Alternation statement (@ ParamContainer goleft() { // Returns Vector containing left side parameters NameCollector pp = new NameCollector(new ParamContainer()); this.allParams(pp); return pp.get_cparams(); } @) traversal allParams(NameCollector pp) {to ClassName;} } Node { (@ protected int iParent; protected String szName; protected boolean bhasChildren; public Node (String name) { szName = name; iParent = -1; bhasChildren = false; } public void setParent (int parent) { iParent = parent; } public void setHasChildren (boolean bchildren) { bhasChildren = bchildren; } public int getParent () { return (iParent); } public String getName () { return (szName); } public boolean hasChildren () { return (bhasChildren); } @) } Subclass { // Extended to visit Right Side of Alternation statement (@ ParamContainer goright() { NameCollector rs = new NameCollector(new ParamContainer()); this.actParams(rs); return rs.get_cparams(); } @) traversal actParams(NameCollector rs) {to ClassName;} } NameCollector { // visitor which adds the specified class names to vector before ClassName (@ this.get_cparams().addElement(host.get_name()); @) } ClassNameTranspVisitor { before ClassDef (@ this.set_pcn(host.get_paramclassname()); @)} ClassNameRetVisitor { before ClassName (@ this.set_cn(host); @)} EdgeVisitor { traversal toClassName( ClassNameRetVisitor cnrv) { bypassing -> *,parameters,* to ClassName;} (@ public ClassName dig_out() { ClassNameRetVisitor cnrv = new ClassNameRetVisitor(); this.toClassName(cnrv); return cnrv.get_cn(); } @) before Subclass (@ tree.addElement(this.dig_out().get_name().toString()); tree.addElement(":"); tree.addElement(host.get_classspec().get_classname().get_name().toString()); @) } // end inheritance visitors AltCheckVisitor { // Main Visitor which goes through alternation class before ParamClassName (@ lside = host.goleft(); // might be 1 (just the classname) or more. @) before Subclass (@ rside = host.goright(); // get them // might be 1 (just the classname) or more. if (lside.size() != rside.size()) { System.out.println ( " Error: Check the number of arguments => " +lside.firstElement().toString() +"()" + " : " + rside.firstElement().toString() + "()..."); } else { // we know they're the same size Boolean passarg = new Boolean(true); for (int j = 1; j < lside.size(); j++) { // start from second element. if (! lside.elementAt(j).toString().equals(rside.elementAt(j).toString())) passarg = Boolean.valueOf("False"); } if (passarg.booleanValue()) System.out.print(" "+ lside.firstElement().toString() + "() : " + rside.firstElement().toString() + "() passed ...\n"); else System.out.println(" Error: Icorrect Order of arguments => " + lside.firstElement().toString() +"()" + " : " + rside.firstElement().toString() + "() ..." ); } @) } Main { (@ static public void main(String args[]) throws Exception { System.out.println("\n\t J - S E M - C H E C K 2 \n"); ClassGraph a = ClassGraph.parse(System.in); System.out.println("\n Checking that every alternative of a \n " +"parameterized alternation class has \n " +"all its formal parameters in the same order ... \n"); a.visit_cgraph (); System.out.println("\n Checking that all alternatives are defined as \n " +"either construction classes or alternation classes that \n " +"will eventually be defined by construction classes ... \n" +"but can't defined as repetition classes \n"); a.check_alternatives(a.findDemClasses(),a.findAlternatives()); // P2 System.out.println (" Checking Inheritance Cycles ... \n"); a.init_check(); System.out.println("\n "); } @) }