Commits

Anonymous committed b647615

layouting resolved

Comments (0)

Files changed (8)

src/nl/tudelft/lime/layout/ClusterEdgeCreator.java

-/*
- * Created on Jul 14, 2004
- */
-package nl.tudelft.lime.layout;
-
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
-import org.eclipse.draw2d.graph.DirectedGraph;
-import org.eclipse.draw2d.graph.Edge;
-import org.eclipse.draw2d.graph.EdgeList;
-import org.eclipse.draw2d.graph.Node;
-import org.eclipse.draw2d.graph.NodeList;
-
-
-/**
- * Creates dummy edges between nodes, to be used with NodeJoiningDirectedGraphLayout
- * @author Phil Zoio
- */
-public class ClusterEdgeCreator {
-    //sets up maximum depth of recursion to set up initial cluster list
-    private static final int INITIAL_RECURSION_DEPTH = 3;
-    NodeList nodeList;
-    EdgeList edgeList;
-    DirectedGraph graph;
-    List edgesAdded;
-    List encountered = new ArrayList();
-    List clusters = new ArrayList();
-    Cluster currentCluster = null;
-
-    /**
-     * @param graph
-     */
-    public ClusterEdgeCreator() {
-        super();
-    }
-
-    public void visit(DirectedGraph graph) {
-        try {
-            this.graph = graph;
-            this.nodeList = graph.nodes;
-            this.edgeList = graph.edges;
-            edgesAdded = new ArrayList();
-
-            //iterate through all of the nodes in the node list
-            for (Iterator iter = nodeList.iterator(); iter.hasNext();) {
-                Node node = (Node) iter.next();
-
-                //check whether we have already come across this node
-                if (!encountered.contains(node)) {
-                    //create a new cluster for this node
-                    currentCluster = new Cluster();
-                    clusters.add(currentCluster);
-                    encountered.add(node);
-                    currentCluster.set.add(node);
-
-                    // recursively add any other nodes reachable from it
-                    int depth = INITIAL_RECURSION_DEPTH;
-                    recursivelyAddToCluster(node, depth);
-                }
-            }
-
-            for (Iterator iter = clusters.iterator(); iter.hasNext();) {
-                Cluster cluster = (Cluster) iter.next();
-            }
-
-            coalesceRemainingClusters();
-
-            joinClusters();
-        } catch (RuntimeException e) {
-            e.printStackTrace();
-            throw e;
-        }
-    }
-
-    /**
-     * If recursion fails to join all the remaining
-     */
-    private void coalesceRemainingClusters() {
-    }
-
-    /**
-     * Joins the clusters together
-     */
-    private void joinClusters() {
-        if (clusters.size() > 1) {
-            Node sourceNode = null;
-            Node targetNode = null;
-
-            //add an edge from each successive cluster to next
-            for (Iterator iter = clusters.iterator(); iter.hasNext();) {
-                Cluster cluster = (Cluster) iter.next();
-
-                if (sourceNode != null) {
-                    //use first node in set as target node
-                    targetNode = (Node) cluster.set.get(0);
-                    newDummyEdge(sourceNode, targetNode);
-                }
-
-                //set up source node for the next iteration using last node in
-                // set
-                sourceNode = (Node) cluster.set.get(cluster.set.size() - 1);
-            }
-        }
-    }
-
-    private void recursivelyAddToCluster(Node node, int depth) {
-        if (depth > 3) {
-            return;
-        } else {
-            depth++;
-
-            EdgeList incoming = node.incoming;
-
-            for (Iterator iter = incoming.iterator(); iter.hasNext();) {
-                Edge edge = (Edge) iter.next();
-                Node incomingNode = edge.source;
-
-                if (!encountered.contains(incomingNode)) {
-                    encountered.add(incomingNode);
-                    currentCluster.set.add(incomingNode);
-                    recursivelyAddToCluster(incomingNode, depth);
-                }
-            }
-
-            EdgeList outgoing = node.outgoing;
-
-            for (Iterator iter = outgoing.iterator(); iter.hasNext();) {
-                Edge edge = (Edge) iter.next();
-                Node outgoingNode = edge.target;
-
-                if (!encountered.contains(outgoingNode)) {
-                    encountered.add(outgoingNode);
-                    currentCluster.set.add(outgoingNode);
-                    recursivelyAddToCluster(outgoingNode, depth);
-                }
-            }
-        }
-    }
-
-    /**
-     * creates a new dummy edge to be used in the graph
-     */
-    private Edge newDummyEdge(Node sourceNode, Node targetNode) {
-        boolean addedEdge;
-        DummyEdgePart edgePart = new DummyEdgePart();
-        Edge edge = new Edge(edgePart, sourceNode, targetNode);
-        edge.weight = 2;
-
-        //add the new edge to the edge list
-        edgeList.add(edge);
-
-        targetNode = sourceNode;
-        addedEdge = true;
-
-        return edge;
-    }
-
-    /**
-     * Very thin wrapper around List
-     */
-    private class Cluster {
-        List set = new ArrayList();
-
-        public String toString() {
-            return set.toString();
-        }
-    }
-}

src/nl/tudelft/lime/layout/DummyEdgeCreator.java

 
         int nodeCount = nodeList.size();
 
-        boolean addedEdge = false;
-
         //if node count is only one then we don't have to worry about whether
         // the nodes are connected
         if (nodeCount > 1) {
-            for (Iterator iter = nodeList.iterator(); iter.hasNext();) {
-                Node sourceNode = (Node) iter.next();
+        	for (Object node : nodeList){
+                Node sourceNode = (Node) node;
 
                 //we will need to set up a dummy relationship for any table not
                 // in one already

src/nl/tudelft/lime/layout/LimeDiagramDirectedGraphLayoutVisitor.java

 import nl.tudelft.lime.edit.editpart.LimeDiagramEditPart;
 import nl.tudelft.lime.edit.editpart.LimeEditPart;
 import nl.tudelft.lime.edit.editpart.PortEditPart;
+import nl.tudelft.lime.layout.insidecomponent.InsideComponentClusterBasedGraphLayout;
+import nl.tudelft.lime.layout.toplevel.ClusterBasedGraphLayout;
 
 import org.eclipse.draw2d.AbsoluteBendpoint;
 import org.eclipse.draw2d.Connection;
             } catch (Exception e) {
                 e.printStackTrace();
             }
-
-            new NodeJoiningDirectedGraphLayout().visit(graph);
+            new ClusterBasedGraphLayout().visit(graph);
             applyResults(diagram, partToNodesMap);
         }
     }
         List<LimeEditPart> children = (List<LimeEditPart>) component.getChildren();
 
         //this is the recursive part
-        //        for (LimeEditPart child : children) {
-        //            if (child instanceof ComponentEditPart) {
-        //                layoutComponentContents(mainParent, child);
-        //            }
-        //        }
+                for (LimeEditPart child : children) {
+                    if (child instanceof ComponentEditPart) {
+                        layoutComponentContents(mainParent, child);
+                    }
+                }
 
         //and this is the important part which layouts a component,
         //the rule is that first we check if items are connected together,
             //then draw the edges for the 
             addEdges(mainParent, getPorts(component), componentGraph,
                 componentPartsMap, processedEdges);
-            new NodeJoiningDirectedGraphLayout().visit(componentGraph);
+            new InsideComponentClusterBasedGraphLayout().visit(componentGraph);
             applyChildrenResults(component, componentPartsMap);
         }
     }

src/nl/tudelft/lime/layout/NodeJoiningDirectedGraphLayout.java

-package nl.tudelft.lime.layout;
-
-import org.eclipse.draw2d.graph.DirectedGraph;
-import org.eclipse.draw2d.graph.DirectedGraphLayout;
-
-
-/**
- * this graph layout, layouts the graph only if it is acyclic
- * Provide the class an acyclic graph
- *
- */
-public class NodeJoiningDirectedGraphLayout extends DirectedGraphLayout {
-    /**
-     * Lays out the given graph
-     * @param graph the graph to layout
-     */
-    public void visit(DirectedGraph graph) {
-        //add dummy edges so that graph does not fall over because some nodes
-        // are not in relationships
-        new DummyEdgeCreator().visit(graph);
-
-        // create edges to join any isolated clusters
-        new ClusterEdgeCreator().visit(graph);
-        super.visit(graph);
-    }
-}

src/nl/tudelft/lime/layout/insidecomponent/InsideComponentClusterBasedGraphLayout.java

+package nl.tudelft.lime.layout.insidecomponent;
+
+import org.eclipse.draw2d.graph.DirectedGraph;
+import org.eclipse.draw2d.graph.DirectedGraphLayout;
+
+
+/**
+ * this graph layout, layouts the graph only if it is acyclic
+ * Provide the class an acyclic graph
+ *
+ */
+public class InsideComponentClusterBasedGraphLayout extends DirectedGraphLayout {
+    /**
+     * Lays out the given graph
+     * @param graph the graph to layout
+     */
+    public void visit(DirectedGraph graph) {
+        //add dummy edges so that graph does not fall over because some nodes
+        // are not in relationships
+        //new DummyEdgeCreator().visit(graph);
+
+        // create edges to join any isolated clusters
+        new InsideComponentClusterEdgeCreator().visit(graph);
+        super.visit(graph);
+    }
+}

src/nl/tudelft/lime/layout/insidecomponent/InsideComponentClusterEdgeCreator.java

+package nl.tudelft.lime.layout.insidecomponent;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import nl.tudelft.lime.edit.editpart.ComponentEditPart;
+import nl.tudelft.lime.edit.editpart.LimeCodeFragmentEditPart;
+import nl.tudelft.lime.edit.editpart.PortEditPart;
+import nl.tudelft.lime.layout.DummyEdgePart;
+import nl.tudelft.lime.model.port.InputPort;
+
+import org.eclipse.draw2d.graph.DirectedGraph;
+import org.eclipse.draw2d.graph.Edge;
+import org.eclipse.draw2d.graph.EdgeList;
+import org.eclipse.draw2d.graph.Node;
+import org.eclipse.draw2d.graph.NodeList;
+
+
+/**
+ * Creates dummy edges between nodes, to be used with NodeJoiningDirectedGraphLayout
+ * from the code by Phil Zoio
+ */
+public class InsideComponentClusterEdgeCreator {
+    //sets up maximum depth of recursion to set up initial cluster list
+//    private static final int INITIAL_RECURSION_DEPTH = 3;
+    NodeList nodeList;
+    EdgeList edgeList;
+    DirectedGraph graph;
+    List<Edge> edgesAdded;
+    List<Node> encountered = new ArrayList<Node>();
+    List<Cluster> clusters = new ArrayList<Cluster>();
+    //holds a map for the created clusters based on the name
+    //prefix of the components added
+    Cluster inputPortCluster = new Cluster();
+    Cluster embeddedComponentsCluster = new Cluster();
+    Cluster outputPortCluster = new Cluster();
+    
+    Cluster currentCluster = null;
+
+    /**
+     * @param graph
+     */
+    public InsideComponentClusterEdgeCreator() {
+        super();
+        clusters.add(inputPortCluster);
+        clusters.add(embeddedComponentsCluster);
+        clusters.add(outputPortCluster);
+    }
+
+    public void visit(DirectedGraph graph) {
+        try {
+            this.graph = graph;
+            this.nodeList = graph.nodes;
+            edgesAdded = new ArrayList<Edge>();
+            this.edgeList = graph.edges;
+
+            //iterate through all of the nodes in the node list
+            // in this level each component should be grouped in 
+            //three clusters
+            //first input cluster
+            //second component cluster
+            //third output cluster
+            for (Object item : nodeList){
+                Node node = (Node) item;
+
+                //check whether we have already come across this node
+                if (!encountered.contains(node)) {
+                    //create a new cluster for this node
+            		//if it is componentEditPart do somthing else
+                	//first consider the case 
+                	String nodePrefix = "";
+                	if ((node.data instanceof ComponentEditPart) || (node.data instanceof LimeCodeFragmentEditPart)) 
+                		embeddedComponentsCluster.set.add(node);
+                	else if (node.data instanceof PortEditPart){
+                		PortEditPart portEditPart = (PortEditPart)node.data;
+                		if (portEditPart.getModel() instanceof InputPort)
+                			inputPortCluster.set.add(node);
+                		else
+                			outputPortCluster.set.add(node);
+                	}
+                    //inform this object that we have met the node
+                    encountered.add(node);
+                }
+            }
+            //we don't use it by now
+            coalesceRemainingClusters();
+            //now connect all the clusters
+            joinClusters();
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
+    /**
+     * If recursion fails to join all the remaining
+     */
+    private void coalesceRemainingClusters() {
+    }
+
+    /**
+     * Joins the clusters together
+     */
+    private void joinClusters() {
+        if (clusters.size() > 1) {
+            Node sourceNode = null;
+            //add an edge from each successive cluster to next
+            for (Cluster cluster : clusters){
+            	if (cluster.set.size() != 0){
+	                if (sourceNode != null) {
+	                    //use first node in set as target node
+		                //for a beetterlayout there should be edges from a single element of the cluster
+		                //to all the next
+	                	
+	                	for (Node targetNode : cluster.set){
+	                		newDummyEdge(sourceNode, targetNode);
+	                	}
+	                }
+	                //set up source node for the next iteration using last node in
+	                // set
+	                sourceNode = (Node) cluster.set.get(cluster.set.size() - 1);
+            	}
+            }
+        }
+    }
+    /**
+     * Our clustering heuristic is to add the ones with the similar name to one cluster
+     * @param node
+     * @param depth
+     */
+/*    @Deprecated
+    private void recursivelyAddToCluster(Node node, int depth) {
+        if (depth > 4) {
+            return;
+        } else {
+            depth++;
+            //add whoever which is connected as an input to this node to this cluster
+            
+            for (Object item : node.incoming){
+                Edge edge = (Edge) item;
+                Node incomingNode = edge.source;
+
+                if (areOfTheSameFamily(node.data, incomingNode) && !encountered.contains(incomingNode)){
+                    encountered.add(incomingNode);
+                    currentCluster.set.add(incomingNode);
+                    recursivelyAddToCluster(incomingNode, depth);
+                }
+            }
+
+            //add whoever which is connected as an outputto this node to this cluster
+
+            for (Object item : node.outgoing){
+                Edge edge = (Edge) item;
+                Node outgoingNode = edge.target;
+
+                if (areOfTheSameFamily(node.data, outgoingNode.data) && !encountered.contains(outgoingNode)){
+                    encountered.add(outgoingNode);
+                    currentCluster.set.add(outgoingNode);
+                    recursivelyAddToCluster(outgoingNode, depth);
+                }
+            }
+        }
+    }
+*/    
+
+	/**
+     * creates a new dummy edge to be used in the graph
+     */
+    private Edge newDummyEdge(Node sourceNode, Node targetNode) {
+//    	boolean addedEdge;
+        DummyEdgePart edgePart = new DummyEdgePart();
+        Edge edge = new Edge(edgePart, sourceNode, targetNode);
+        edge.weight = 10;
+
+        //add the new edge to the edge list
+        edgeList.add(edge);
+
+        targetNode = sourceNode;
+//        addedEdge = true;
+
+        return edge;
+    }
+
+    /**
+     * Very thin wrapper around List
+     */
+    private class Cluster {
+        List<Node> set = new ArrayList<Node>();
+
+        public String toString() {
+            return set.toString();
+        }
+    }
+}

src/nl/tudelft/lime/layout/toplevel/ClusterBasedGraphLayout.java

+package nl.tudelft.lime.layout.toplevel;
+
+import org.eclipse.draw2d.graph.DirectedGraph;
+import org.eclipse.draw2d.graph.DirectedGraphLayout;
+
+
+/**
+ * this graph layout, layouts the graph only if it is acyclic
+ * Provide the class an acyclic graph
+ *
+ */
+public class ClusterBasedGraphLayout extends DirectedGraphLayout {
+    /**
+     * Lays out the given graph
+     * @param graph the graph to layout
+     */
+    public void visit(DirectedGraph graph) {
+        //add dummy edges so that graph does not fall over because some nodes
+        // are not in relationships
+        //new DummyEdgeCreator().visit(graph);
+
+        // create edges to join any isolated clusters
+        new ClusterEdgeCreator().visit(graph);
+        super.visit(graph);
+    }
+}

src/nl/tudelft/lime/layout/toplevel/ClusterEdgeCreator.java

+/*
+ * Created on Jul 14, 2004
+ */
+package nl.tudelft.lime.layout.toplevel;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import nl.tudelft.lime.edit.editpart.ComponentEditPart;
+import nl.tudelft.lime.layout.DummyEdgePart;
+
+import org.eclipse.draw2d.graph.DirectedGraph;
+import org.eclipse.draw2d.graph.Edge;
+import org.eclipse.draw2d.graph.EdgeList;
+import org.eclipse.draw2d.graph.Node;
+import org.eclipse.draw2d.graph.NodeList;
+
+
+/**
+ * Creates dummy edges between nodes, to be used with NodeJoiningDirectedGraphLayout
+ * @author Phil Zoio
+ */
+public class ClusterEdgeCreator {
+    //sets up maximum depth of recursion to set up initial cluster list
+//    private static final int INITIAL_RECURSION_DEPTH = 3;
+    NodeList nodeList;
+    EdgeList edgeList;
+    DirectedGraph graph;
+    List<Edge> edgesAdded;
+    List<Node> encountered = new ArrayList<Node>();
+    List<Cluster> clusters = new ArrayList<Cluster>();
+    //holds a map for the created clusters based on the name
+    //prefix of the components added
+    Map<String, Cluster> clusterMap = new HashMap<String, Cluster>();
+    Cluster currentCluster = null;
+
+    /**
+     * @param graph
+     */
+    public ClusterEdgeCreator() {
+        super();
+    }
+
+    public void visit(DirectedGraph graph) {
+        try {
+            this.graph = graph;
+            this.nodeList = graph.nodes;
+            edgesAdded = new ArrayList<Edge>();
+            this.edgeList = graph.edges;
+
+            //iterate through all of the nodes in the node list
+            for (Object item : nodeList){
+                Node node = (Node) item;
+
+                //check whether we have already come across this node
+                if (!encountered.contains(node)) {
+                    //create a new cluster for this node
+            		//if it is componentEditPart do somthing else
+                	//first consider the case 
+                	String nodePrefix = "";
+                	if (node.data instanceof ComponentEditPart)
+                		nodePrefix = ((ComponentEditPart) node.data).getLimeDiagram().getName().split("[0-9]+")[0];
+                	currentCluster = clusterMap.get(nodePrefix);
+                    if (currentCluster == null){
+                    	currentCluster = new Cluster();
+                    	if (nodePrefix != null && !"".equals(nodePrefix))
+                    		clusterMap.put(nodePrefix, currentCluster);
+                        clusters.add(currentCluster);
+                    }
+                    //inform this object that we have met the node
+                    encountered.add(node);
+                    //add it to the cluster
+                    currentCluster.set.add(node);
+                    //it is not needed for top-level components
+//                    int depth = INITIAL_RECURSION_DEPTH;
+//                    recursivelyAddToCluster(node, depth);
+                }
+            }
+            //we don't use it by now
+            coalesceRemainingClusters();
+            //now connect all the clusters
+            joinClusters();
+        } catch (RuntimeException e) {
+            e.printStackTrace();
+            throw e;
+        }
+    }
+
+    /**
+     * If recursion fails to join all the remaining
+     */
+    private void coalesceRemainingClusters() {
+    }
+
+    /**
+     * Joins the clusters together
+     */
+    private void joinClusters() {
+        if (clusters.size() > 1) {
+            Node sourceNode = null;
+            Node targetNode = null;
+
+            //add an edge from each successive cluster to next
+            for (Cluster cluster : clusters){
+                if (sourceNode != null) {
+                    //use first node in set as target node
+                    targetNode = (Node) cluster.set.get(0);
+                    newDummyEdge(sourceNode, targetNode);
+                }
+
+                //set up source node for the next iteration using last node in
+                // set
+                sourceNode = (Node) cluster.set.get(cluster.set.size() - 1);
+            }
+        }
+    }
+    /**
+     * Our clustering heuristic is to add the ones with the similar name to one cluster
+     * @param node
+     * @param depth
+     */
+/*    @Deprecated
+    private void recursivelyAddToCluster(Node node, int depth) {
+        if (depth > 4) {
+            return;
+        } else {
+            depth++;
+            //add whoever which is connected as an input to this node to this cluster
+            
+            for (Object item : node.incoming){
+                Edge edge = (Edge) item;
+                Node incomingNode = edge.source;
+
+                if (areOfTheSameFamily(node.data, incomingNode) && !encountered.contains(incomingNode)){
+                    encountered.add(incomingNode);
+                    currentCluster.set.add(incomingNode);
+                    recursivelyAddToCluster(incomingNode, depth);
+                }
+            }
+
+            //add whoever which is connected as an outputto this node to this cluster
+
+            for (Object item : node.outgoing){
+                Edge edge = (Edge) item;
+                Node outgoingNode = edge.target;
+
+                if (areOfTheSameFamily(node.data, outgoingNode.data) && !encountered.contains(outgoingNode)){
+                    encountered.add(outgoingNode);
+                    currentCluster.set.add(outgoingNode);
+                    recursivelyAddToCluster(outgoingNode, depth);
+                }
+            }
+        }
+    }
+*/    
+
+	/**
+     * creates a new dummy edge to be used in the graph
+     */
+    private Edge newDummyEdge(Node sourceNode, Node targetNode) {
+//    	boolean addedEdge;
+        DummyEdgePart edgePart = new DummyEdgePart();
+        Edge edge = new Edge(edgePart, sourceNode, targetNode);
+        edge.weight = 2;
+
+        //add the new edge to the edge list
+        edgeList.add(edge);
+
+        targetNode = sourceNode;
+//        addedEdge = true;
+
+        return edge;
+    }
+
+    /**
+     * Very thin wrapper around List
+     */
+    private class Cluster {
+        List<Node> set = new ArrayList<Node>();
+
+        public String toString() {
+            return set.toString();
+        }
+    }
+}