Search in sources :

Example 11 with InternalRelationship

use of org.eclipse.zest.layouts.dataStructures.InternalRelationship in project archi by archimatetool.

the class TreeLayoutAlgorithm method buildTreeRecursively.

/**
 * Builds a tree of the passed in entity.
 * The entity will pass a weight value to all of its children recursively.
 */
private void buildTreeRecursively(InternalNode layoutEntity, int i, double weight, InternalNode[] entities, final InternalRelationship[] relationships) {
    // No need to do further computation!
    if (layoutEntity == null) {
        return;
    }
    // forest, and its weight value needs to be modified.
    if (markedArr[i]) {
        modifyWeightRecursively(layoutEntity, i, weight, new HashSet(), entities, relationships);
        // No need to do further computation.
        return;
    }
    // Mark this entity, set its weight value and create a new tree node.
    markedArr[i] = true;
    weights[i] = weight;
    // collect the children of this entity and put them in order
    Collection rels = findRelationships(layoutEntity, AS_SOURCE, relationships);
    List children = new ArrayList();
    for (Iterator iter = rels.iterator(); iter.hasNext(); ) {
        InternalRelationship layoutRel = (InternalRelationship) iter.next();
        InternalNode childEntity = layoutRel.getDestination();
        children.add(childEntity);
    }
    if (comparator != null) {
        Collections.sort(children, comparator);
    } else {
        // sort the children by level, then by number of descendents, then by number of children
        // TODO: SLOW
        Collections.sort(children, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                InternalNode node1 = (InternalNode) o1;
                InternalNode node2 = (InternalNode) o2;
                int[] numDescendentsAndLevel1 = new int[2];
                int[] numDescendentsAndLevel2 = new int[2];
                int level1 = numDescendentsAndLevel1[NUM_LEVELS_INDEX];
                int level2 = numDescendentsAndLevel2[NUM_LEVELS_INDEX];
                if (level1 == level2) {
                    getNumDescendentsAndLevel(node1, relationships, numDescendentsAndLevel1);
                    getNumDescendentsAndLevel(node2, relationships, numDescendentsAndLevel2);
                    int numDescendents1 = numDescendentsAndLevel1[NUM_DESCENDENTS_INDEX];
                    int numDescendents2 = numDescendentsAndLevel2[NUM_DESCENDENTS_INDEX];
                    if (numDescendents1 == numDescendents2) {
                        int numChildren1 = getNumChildren(node1, relationships);
                        int numChildren2 = getNumChildren(node1, relationships);
                        return numChildren2 - numChildren1;
                    } else {
                        return numDescendents2 - numDescendents1;
                    }
                } else {
                    return level2 - level1;
                }
            // return getNumChildren(node2, relationships) - getNumChildren(node1, relationships);
            }
        });
    }
    // map children to this parent, and vice versa
    for (Iterator iter = children.iterator(); iter.hasNext(); ) {
        InternalNode childEntity = (InternalNode) iter.next();
        int childEntityIndex = indexOfInternalNode(entities, childEntity);
        if (!childrenLists[i].contains(childEntity)) {
            childrenLists[i].add(childEntity);
        }
        if (!parentLists[childEntityIndex].contains(layoutEntity)) {
            parentLists[childEntityIndex].add(layoutEntity);
        }
    }
    for (Iterator iter = children.iterator(); iter.hasNext(); ) {
        InternalNode childEntity = (InternalNode) iter.next();
        int childEntityIndex = indexOfInternalNode(entities, childEntity);
        buildTreeRecursively(childEntity, childEntityIndex, weight + 1, entities, relationships);
    }
}
Also used : InternalRelationship(org.eclipse.zest.layouts.dataStructures.InternalRelationship) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) InternalNode(org.eclipse.zest.layouts.dataStructures.InternalNode) HashSet(java.util.HashSet) Comparator(java.util.Comparator)

Aggregations

InternalRelationship (org.eclipse.zest.layouts.dataStructures.InternalRelationship)11 InternalNode (org.eclipse.zest.layouts.dataStructures.InternalNode)9 Iterator (java.util.Iterator)5 BasicEntityConstraint (org.eclipse.zest.layouts.constraints.BasicEntityConstraint)5 BendPoint (org.eclipse.zest.layouts.dataStructures.BendPoint)5 DisplayIndependentPoint (org.eclipse.zest.layouts.dataStructures.DisplayIndependentPoint)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)3 List (java.util.List)3 LayoutRelationship (org.eclipse.zest.layouts.LayoutRelationship)2 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Dimension (org.eclipse.draw2d.geometry.Dimension)1 DirectedGraph (org.eclipse.draw2d.graph.DirectedGraph)1 DirectedGraphLayout (org.eclipse.draw2d.graph.DirectedGraphLayout)1 Edge (org.eclipse.draw2d.graph.Edge)1 Node (org.eclipse.draw2d.graph.Node)1 DisplayIndependentRectangle (org.eclipse.zest.layouts.dataStructures.DisplayIndependentRectangle)1