Search in sources :

Example 1 with OrderingStrategy

use of org.eclipse.elk.alg.layered.options.OrderingStrategy in project elk by eclipse.

the class ModelOrderNodeComparator method compare.

@Override
public int compare(final LNode n1, final LNode n2) {
    if (!biggerThan.containsKey(n1)) {
        biggerThan.put(n1, new HashSet<>());
    } else if (biggerThan.get(n1).contains(n2)) {
        return 1;
    }
    if (!biggerThan.containsKey(n2)) {
        biggerThan.put(n2, new HashSet<>());
    } else if (biggerThan.get(n2).contains(n1)) {
        return -1;
    }
    if (!smallerThan.containsKey(n1)) {
        smallerThan.put(n1, new HashSet<>());
    } else if (smallerThan.get(n1).contains(n2)) {
        return -1;
    }
    if (!smallerThan.containsKey(n2)) {
        smallerThan.put(n2, new HashSet<>());
    } else if (biggerThan.get(n2).contains(n1)) {
        return 1;
    }
    // This kind of ordering should be preferred, if the order of the edges has priority.
    if (orderingStrategy == OrderingStrategy.PREFER_EDGES || !n1.hasProperty(InternalProperties.MODEL_ORDER) || !n2.hasProperty(InternalProperties.MODEL_ORDER)) {
        // In this case the order of the connected nodes in the previous layer should be respected
        LPort p1SourcePort = n1.getPorts().stream().filter(p -> !p.getIncomingEdges().isEmpty()).findFirst().map(p -> p.getIncomingEdges().get(0).getSource()).orElse(null);
        LPort p2SourcePort = n2.getPorts().stream().filter(p -> !p.getIncomingEdges().isEmpty()).findFirst().map(p -> p.getIncomingEdges().get(0).getSource()).orElse(null);
        // Case both nodes have connections to the previous layer.
        if (p1SourcePort != null && p2SourcePort != null) {
            LNode p1Node = p1SourcePort.getNode();
            LNode p2Node = p2SourcePort.getNode();
            // layer should be used to order them.
            if (p1Node != null && p1Node.equals(p2Node)) {
                // port ordering.
                for (LPort port : p1Node.getPorts()) {
                    if (port.equals(p1SourcePort)) {
                        // Case the port is the one connecting to n1, therefore, n1 has a smaller model order
                        updateBiggerAndSmallerAssociations(n2, n1);
                        return -1;
                    } else if (port.equals(p2SourcePort)) {
                        // Case the port is the one connecting to n2, therefore, n1 has a bigger model order
                        updateBiggerAndSmallerAssociations(n1, n2);
                        return 1;
                    }
                }
                assert (false);
                // Cannot happen, since both nodes have a connection to the previous layer.
                return Integer.compare(getModelOrderFromConnectedEdges(n1), getModelOrderFromConnectedEdges(n2));
            }
            // since the ordering in the previous layer does already reflect it.
            for (LNode previousNode : previousLayer) {
                if (previousNode.equals(p1Node)) {
                    updateBiggerAndSmallerAssociations(n2, n1);
                    return -1;
                } else if (previousNode.equals(p2Node)) {
                    updateBiggerAndSmallerAssociations(n1, n2);
                    return 1;
                }
            }
        }
        // One node has no source port
        if (!n1.hasProperty(InternalProperties.MODEL_ORDER) || !n2.hasProperty(InternalProperties.MODEL_ORDER)) {
            int n1ModelOrder = getModelOrderFromConnectedEdges(n1);
            int n2ModelOrder = getModelOrderFromConnectedEdges(n2);
            if (n1ModelOrder > n2ModelOrder) {
                updateBiggerAndSmallerAssociations(n1, n2);
            } else {
                updateBiggerAndSmallerAssociations(n2, n1);
            }
            return Integer.compare(n1ModelOrder, n2ModelOrder);
        }
    // Fall through case.
    // Both nodes are not connected to the previous layer. Therefore, they must be normal nodes.
    // The model order shall be used to order them.
    }
    // Order nodes by their order in the model.
    int n1ModelOrder = n1.getProperty(InternalProperties.MODEL_ORDER);
    int n2ModelOrder = n2.getProperty(InternalProperties.MODEL_ORDER);
    if (n1ModelOrder > n2ModelOrder) {
        updateBiggerAndSmallerAssociations(n1, n2);
    } else {
        updateBiggerAndSmallerAssociations(n2, n1);
    }
    return Integer.compare(n1ModelOrder, n2ModelOrder);
}
Also used : HashSet(java.util.HashSet) Layer(org.eclipse.elk.alg.layered.graph.Layer) InternalProperties(org.eclipse.elk.alg.layered.options.InternalProperties) LPort(org.eclipse.elk.alg.layered.graph.LPort) OrderingStrategy(org.eclipse.elk.alg.layered.options.OrderingStrategy) HashMap(java.util.HashMap) LNode(org.eclipse.elk.alg.layered.graph.LNode) LEdge(org.eclipse.elk.alg.layered.graph.LEdge) Comparator(java.util.Comparator) LongEdgeOrderingStrategy(org.eclipse.elk.alg.layered.options.LongEdgeOrderingStrategy) LPort(org.eclipse.elk.alg.layered.graph.LPort) LNode(org.eclipse.elk.alg.layered.graph.LNode)

Example 2 with OrderingStrategy

use of org.eclipse.elk.alg.layered.options.OrderingStrategy in project elk by eclipse.

the class LayerSweepCrossingMinimizer method countCurrentNumberOfCrossingsNodePortOrder.

/*
     * We only need to count crossings and violations of node and port order below the current graph and also only
     * if they are marked as to be processed hierarchically.
     */
private double countCurrentNumberOfCrossingsNodePortOrder(final GraphInfoHolder currentGraph) {
    double totalCrossings = 0;
    Deque<GraphInfoHolder> countCrossingsIn = new ArrayDeque<>();
    countCrossingsIn.push(currentGraph);
    while (!countCrossingsIn.isEmpty()) {
        GraphInfoHolder gD = countCrossingsIn.pop();
        double modelOrderInfluence = 0;
        // Additionally add changes to the model order to the number of crossings.
        // The influence of port and node order can be configured.
        OrderingStrategy modelOrderStrategy = currentGraph.lGraph().getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY);
        double crossingCounterNodeInfluence = currentGraph.lGraph().getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_CROSSING_COUNTER_NODE_INFLUENCE);
        double crossingCounterPortInfluence = currentGraph.lGraph().getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_CROSSING_COUNTER_PORT_INFLUENCE);
        if (modelOrderStrategy != OrderingStrategy.NONE) {
            modelOrderInfluence += crossingCounterNodeInfluence * countModelOrderNodeChanges(gD.currentNodeOrder(), modelOrderStrategy);
            modelOrderInfluence += crossingCounterPortInfluence * countModelOrderPortChanges(gD.currentNodeOrder());
        }
        totalCrossings += gD.crossCounter().countAllCrossings(gD.currentNodeOrder()) + modelOrderInfluence;
        for (LGraph childLGraph : gD.childGraphs()) {
            GraphInfoHolder child = graphInfoHolders.get(childLGraph.id);
            if (!child.dontSweepInto()) {
                totalCrossings += countCurrentNumberOfCrossings(child);
            }
        }
    }
    return totalCrossings;
}
Also used : LongEdgeOrderingStrategy(org.eclipse.elk.alg.layered.options.LongEdgeOrderingStrategy) OrderingStrategy(org.eclipse.elk.alg.layered.options.OrderingStrategy) LGraph(org.eclipse.elk.alg.layered.graph.LGraph) ArrayDeque(java.util.ArrayDeque)

Aggregations

LongEdgeOrderingStrategy (org.eclipse.elk.alg.layered.options.LongEdgeOrderingStrategy)2 OrderingStrategy (org.eclipse.elk.alg.layered.options.OrderingStrategy)2 ArrayDeque (java.util.ArrayDeque)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)1 LGraph (org.eclipse.elk.alg.layered.graph.LGraph)1 LNode (org.eclipse.elk.alg.layered.graph.LNode)1 LPort (org.eclipse.elk.alg.layered.graph.LPort)1 Layer (org.eclipse.elk.alg.layered.graph.Layer)1 InternalProperties (org.eclipse.elk.alg.layered.options.InternalProperties)1