// flatten.beh // // This file contains code which implements the Cd_graph flattening procedure. Cd_graph { {{ // Creates the subclass to superclass edges. public void buildParentEdges() { TraversalGraph tg = new TraversalGraph( "from Cd_graph bypassing ParentList to Adjacency", Main.cg ); tg.traverse(this, new BuildParentEdgesVisitor()); } // Flattens the Cd_graph. public void flatten() { this.buildParentEdges(); TraversalGraph tg = new TraversalGraph( "from Cd_graph to-stop Adjacency", Main.cg ); tg.traverse(this, new PullDownVisitor()); } }} } // Visitor class which implements the Cd_graph::buildParentEdges() method. BuildParentEdgesVisitor { {{ public void before(Adjacency a) { // Create TraversalGraph which targets the alternation neighbors. TraversalGraph tg = new TraversalGraph( "from Adjacency bypassing ParentList to Alternat_ns", Main.cg ); Alternat_ns alt = (Alternat_ns) tg.fetch(a); // If alt == null, then this Adjacency is not a superclass, no need // to continue. if (alt != null) { // Iterate through each alternation neighbor. Enumeration e = alt.get_alternat_ns().elements(); while (e.hasMoreElements()) { // sc points to a subclass. Adjacency sc = Main.cdg.search(((Term) e.nextElement()).fetchIdent()); Neighbors_wc wc = (Neighbors_wc) sc.get_ns(); // Add a to sc's list of parents. if (wc.get_parents() != null) { wc.get_parents().get_ps().addElement(a); } else { wc.set_parents(new ParentList()); wc.get_parents().set_ps(new Adjacency_List()); wc.get_parents().get_ps().addElement(a); } } } } }} } // This Visitor pulls down all construction edges to their lowest level in the // class hierarchy. PullDownVisitor { {{ public void before(Adjacency a) { // If a has no superclass defined then there is nothing to do. if (a.fetchParents() != null) { // Create a TraversalGraph which targets the list of // construction edges. TraversalGraph tg = new TraversalGraph( "from * bypassing ParentList to Any_vertex_List", Main.cg ); // The variables myparts and parentparts point to a's // construction edges, and a's parents construction // edges, respectively. Any_vertex_List myparts = (Any_vertex_List) tg.fetch(a); Any_vertex_List parentparts = new Any_vertex_List(); // Iterate through the parents construction edges and add // them to parentparts. ListIterator i = tg.gather(a.fetchParents()).listIterator(); while (i.hasNext()) { Enumeration e = ((Any_vertex_List) i.next()).elements(); while (e.hasMoreElements()) parentparts.addElement((Any_vertex) e.nextElement()); } // Iterate through each parent construction edge and add it // to a. Enumeration e = parentparts.elements(); while (e.hasMoreElements()) { myparts.addElement((Any_vertex) e.nextElement()); } // Construction edges can now be removed from the parent. parentparts.set_first(null); } } }} }