microsoft / automatic-graph-layout

A set of tools for graph layout and viewing
Other
1.35k stars 302 forks source link

Constraints with Clusters #318

Open Brains opened 2 years ago

Brains commented 2 years ago

Explored this during #317. Decided to save the progress for future despite don't know how to complete this PR.

#

Messing with SugiyamaLayoutSettings.Transformation was kinda overcomplicated for such trivial goal in #317. A better solution would be to fix LayerConstraints to make them work with Nodes inside Clusters (and maybe even Clusters themselves).

Exploration has shown next sequence of usage

SugiyamaLayoutSettings
    🡇
InitialLayoutByCluster
    🡇
InitialLayoutByCluster.GetComponents
// Creates a shallow copy of the root cluster and divides into GeometryGraphs
// each of which is a connected component with respect to edges internal to root.
    🡇
InitialLayoutByCluster.ShallowNodeCopyDictionary
// Copy the cluster's child Nodes and Clusters as nodes and return a mapping of original to copy.
// The reverse mapping (copy to original) is available via the copy's UserData

GetComponents wraps each node into new Node(). This is the reason of the bug since constraints are added against original Nodes

graph.LayerConstraints.AddSameLayerNeighbors(
    graph.FindNode("47"),
    graph.FindNode("58"));

then used by https://github.com/microsoft/automatic-graph-layout/blob/d20210ecfc62fc08a17ab17fd934201b5c6652ea/GraphLayout/MSAGL/Layout/Layered/HorizontalConstraintsForSugiyama.cs#L144-L150 so the method https://github.com/microsoft/automatic-graph-layout/blob/d20210ecfc62fc08a17ab17fd934201b5c6652ea/GraphLayout/MSAGL/Layout/Layered/HorizontalConstraintsForSugiyama.cs#L98-L102 always returns -1 since node is an original Node while nodeIdToIndex was built against wrapped Nodes (in GetComponents). As result, lists of constraints are always empty.

Brains commented 2 years ago

After the fix 5ee1a3fdd802857003f49876773930c648ab095b, method NodeIndex finds required node and returns its index. Of course the fix is just to demonstrate the error since it does not handle non wrapped Nodes at all.

Unfortunatelly, an error occurs later in https://github.com/microsoft/automatic-graph-layout/blob/47d30d63d73847492c796028a87fe08a6e7347a6/GraphLayout/MSAGL/Layout/Layered/Ordering/ConstrainedOrdering.cs#L254-L272 image

Tried to figure out why but no luck so far. Layers are too complicated, a lot of different indexes, arrays, counters and lookups. Maybe you know how to this can be resolved.

Brains commented 2 years ago

Also noticed that playing with Subgraphs configuration in WpfApplicationSample often leads to same IndexOutOfRangeException (before committed 5ee1a3fdd802857003f49876773930c648ab095b) https://github.com/microsoft/automatic-graph-layout/blob/47d30d63d73847492c796028a87fe08a6e7347a6/GraphLayout/MSAGL/Layout/Layered/Balancing.cs#L210