private void ConfigureGrouping()
        {
            IOptionItem groupingItem = Handler.GetItemByName("GROUPING.GROUP_LAYOUT_POLICY");

            switch ((string)groupingItem.Value)
            {
            case IGNORE_GROUPS:
                preStage = new HideGroupsStage();
                organic.PrependStage(preStage);
                break;

            case LAYOUT_GROUPS:
                //do nothing...
                break;

            case FIX_GROUP_BOUNDS:
                IDataProvider groupDP = CurrentLayoutGraph.GetDataProvider(GroupingKeys.GroupDpKey);
                if (groupDP != null)
                {
                    groupNodeContentDP = Maps.CreateHashedNodeMap();
                    foreach (Node node in CurrentLayoutGraph.Nodes)
                    {
                        if (groupDP.GetBool(node))
                        {
                            groupNodeContentDP.Set(node, GroupNodeMode.FixBounds);
                        }
                    }
                    CurrentLayoutGraph.AddDataProvider(OrganicLayout.GroupNodeModeDpKey, groupNodeContentDP);
                }
                break;

            case FIX_GROUP_CONTENTS:
                groupDP = CurrentLayoutGraph.GetDataProvider(GroupingKeys.GroupDpKey);
                if (groupDP != null)
                {
                    groupNodeContentDP = Maps.CreateHashedNodeMap();
                    foreach (Node node in CurrentLayoutGraph.Nodes)
                    {
                        if (groupDP.GetBool(node))
                        {
                            groupNodeContentDP.Set(node, GroupNodeMode.FixContents);
                        }
                    }
                    CurrentLayoutGraph.AddDataProvider(OrganicLayout.GroupNodeModeDpKey, groupNodeContentDP);
                }
                break;
            }
        }
        protected override void OptimizeAfterSequencing(IComparer <object> inEdgeOrder, IComparer <object> outEdgeOrder, LayoutGraph graph, ILayers layers,
                                                        ILayoutDataProvider ldp, IItemFactory itemFactory)
        {
            edge2LaneCrossing  = Maps.CreateHashedEdgeMap();
            node2LaneAlignment = Maps.CreateHashedNodeMap();

            var criticalEdges = Maps.CreateHashedEdgeMap();

            // determine whether an edge crosses a swim lane border and if so in which direction
            foreach (var edge in graph.Edges)
            {
                var originalEdge = GetOriginalEdge(edge, ldp);

                // now we have a 'real' edge with valid valid source and target nodes
                var          originalSourceId = GetLaneId(originalEdge.Source, ldp);
                var          originalTargetId = GetLaneId(originalEdge.Target, ldp);
                LaneCrossing crossing         = LaneCrossing.None;
                if (originalSourceId != originalTargetId)
                {
                    // check if we need to flip the sides because edge and original edge have different directions
                    var flipSides = edge.Source != originalEdge.Source;
                    var sourceId  = flipSides ? originalTargetId : originalSourceId;
                    var targetId  = flipSides ? originalSourceId : originalTargetId;

                    crossing = sourceId > targetId ? LaneCrossing.ToWest : LaneCrossing.ToEast;
                }
                edge2LaneCrossing.Set(edge, crossing);
            }

            // determine basic node alignment
            foreach (var n in graph.Nodes)
            {
                LaneAlignment alignment = CalculateLaneAlignment(n);
                node2LaneAlignment.Set(n, alignment);
            }

            foreach (var n in graph.Nodes)
            {
                // sort the edges with the provided comparer
                n.SortInEdges(inEdgeOrder);
                n.SortOutEdges(outEdgeOrder);

                // calculate 'critical' in and out-edges whose nodes should be aligned in flow
                var bestInEdge  = n.InDegree > 0 ? GetBestFlowEdge(n.InEdges, ldp, graph) : null;
                var bestOutEdge = n.OutDegree > 0 ? GetBestFlowEdge(n.OutEdges, ldp, graph) : null;
                if (bestInEdge != null)
                {
                    criticalEdges.SetDouble(bestInEdge, criticalEdges.GetDouble(bestInEdge) + 0.5);
                }
                if (bestOutEdge != null)
                {
                    criticalEdges.SetDouble(bestOutEdge, criticalEdges.GetDouble(bestOutEdge) + 0.5);
                }
                if (n.Degree <= 4)
                {
                    // should usually be the case and we can distribute each edge to its own side

                    // remember which node side is already taken by an in- or out-edge
                    bool westTakenByInEdge  = false;
                    bool eastTakenByInEdge  = false;
                    bool westTakenByOutEdge = false;
                    bool eastTakenByOutEdge = false;

                    if (n.InDegree > 0 && n.OutDegree < 3)
                    {
                        // if there are at least three out-edges, we distribute those first, otherwise we start with the in-edges

                        var firstInEdge = n.FirstInEdge;
                        var lastInEdge  = n.LastInEdge;
                        if (GetLaneCrossing(firstInEdge) == LaneCrossing.ToEast &&
                            (n.InDegree > 1 || IsSameLayerEdge(firstInEdge, ldp)))
                        {
                            // the first in-edge comes from west and is either a same layer edge or there are other in-edges
                            ConstrainWest(firstInEdge, false, itemFactory);
                            westTakenByInEdge = true;
                        }
                        if (!westTakenByInEdge || n.OutDegree < 2)
                        {
                            // don't use west and east side for in-edges if there are at least 2 out-edges
                            if (GetLaneCrossing(lastInEdge) == LaneCrossing.ToWest &&
                                (n.InDegree > 1 || IsSameLayerEdge(lastInEdge, ldp)))
                            {
                                // the last in-edge comes from east and is either
                                // a same-layer edge or there are other in-edges
                                ConstrainEast(lastInEdge, false, itemFactory);
                                eastTakenByInEdge = true;
                            }
                        }
                    }

                    if (n.OutDegree > 0)
                    {
                        var firstOutEdge = n.FirstOutEdge;
                        var lastOutEdge  = n.LastOutEdge;

                        if (!westTakenByInEdge)
                        {
                            // the west side is still free
                            if (BpmnLayout.IsBoundaryInterrupting(firstOutEdge, graph) ||
                                (GetLaneCrossing(firstOutEdge) == LaneCrossing.ToWest) &&
                                (n.OutDegree > 1 || IsSameLayerEdge(firstOutEdge, ldp)))
                            {
                                // the first out-edge is either boundary interrupting or goes to west and
                                // is either a same layer edge or there are other out-edges
                                ConstrainWest(firstOutEdge, true, itemFactory);
                                westTakenByOutEdge = true;
                            }
                            else if (eastTakenByInEdge && n.OutDegree >= 2 && !IsSameLayerEdge(firstOutEdge.NextOutEdge, ldp))
                            {
                                // the east side is already taken but we have more then one out edge.
                                // if the second out edge is a same layer edge, constraining the firstOutEdge could lead to
                                // no in-flow edge
                                ConstrainWest(firstOutEdge, true, itemFactory);
                                westTakenByOutEdge = true;
                            }
                        }
                        if (!eastTakenByInEdge)
                        {
                            // the east side is still free
                            if (GetLaneCrossing(lastOutEdge) == LaneCrossing.ToEast &&
                                (n.OutDegree > 1 || IsSameLayerEdge(lastOutEdge, ldp)))
                            {
                                // the last out-edge goes to east and
                                // is either a same layer edge or there are other out-edges
                                ConstrainEast(lastOutEdge, true, itemFactory);
                                eastTakenByOutEdge = true;
                            }
                            else if (westTakenByInEdge && n.OutDegree >= 2 && !IsSameLayerEdge(lastOutEdge.PrevOutEdge, ldp))
                            {
                                // the west side is already taken but we have more then one out edge.
                                // if the second last out edge is a same layer edge, constraining the lastOutEdge could lead to
                                // no in-flow edge
                                ConstrainEast(lastOutEdge, true, itemFactory);
                                eastTakenByOutEdge = true;
                            }
                        }
                    }

                    // distribute remaining in-edges
                    if (n.InDegree == 2 &&
                        !(eastTakenByInEdge || westTakenByInEdge))
                    {
                        // two in-edges but none distributed, yet
                        if (bestInEdge == n.FirstInEdge && !eastTakenByOutEdge)
                        {
                            // first in-edge is in-flow edge and east side is still free
                            ConstrainEast(n.LastInEdge, false, itemFactory);
                            eastTakenByInEdge = true;
                        }
                        else if (bestInEdge == n.LastInEdge && !westTakenByOutEdge)
                        {
                            // last in-edge is in-flow edge and west side is still free
                            ConstrainWest(n.FirstInEdge, false, itemFactory);
                            westTakenByInEdge = true;
                        }
                    }
                    else if (n.InDegree == 3 &&
                             !(eastTakenByInEdge && westTakenByInEdge) &&
                             !(IsSameLayerEdge(n.FirstInEdge.NextInEdge, ldp)))
                    {
                        // three in-edges but not both sides taken, yet and the middle edge is no same layer edge
                        if (!eastTakenByOutEdge)
                        {
                            // if not already taken, constraint the last in-edge to east
                            ConstrainEast(n.LastInEdge, false, itemFactory);
                            eastTakenByInEdge = true;
                        }
                        if (!westTakenByOutEdge)
                        {
                            // if not already taken, constraint the first in-edge to west
                            ConstrainWest(n.FirstInEdge, false, itemFactory);
                            westTakenByInEdge = true;
                        }
                    }

                    // distribute remaining out-edges
                    if (n.OutDegree == 2 && !(eastTakenByOutEdge || westTakenByOutEdge))
                    {
                        // two out-edges but none distributed, yet
                        if (bestOutEdge == n.FirstOutEdge && !eastTakenByInEdge)
                        {
                            // first out-edge is in-flow edge and east side is still free
                            ConstrainEast(n.LastOutEdge, true, itemFactory);
                            eastTakenByOutEdge = true;
                        }
                        else if (bestOutEdge == n.LastOutEdge && !westTakenByInEdge)
                        {
                            // last out-edge is in-flow edge and west side is still free
                            ConstrainWest(n.FirstOutEdge, true, itemFactory);
                            westTakenByOutEdge = true;
                        }
                    }
                    else if (n.OutDegree == 3 &&
                             !(eastTakenByOutEdge && westTakenByOutEdge) &&
                             !(IsSameLayerEdge(n.FirstOutEdge.NextOutEdge, ldp)))
                    {
                        // three out-edges but not both sides taken, yet and the middle edge is no same layer edge
                        if (!eastTakenByInEdge)
                        {
                            // if not already taken, constraint the last out-edge to east
                            ConstrainEast(n.LastOutEdge, true, itemFactory);
                            eastTakenByOutEdge = true;
                        }
                        if (!westTakenByInEdge)
                        {
                            // if not already taken, constraint the first out-edge to west
                            ConstrainWest(n.FirstOutEdge, true, itemFactory);
                            westTakenByOutEdge = true;
                        }
                    }
                }
            }

            // register the data provider for critical edge paths. It is deregistered again by BpmnLayout itself
            graph.AddDataProvider(HierarchicLayout.CriticalEdgePriorityDpKey, criticalEdges);

            sameLayerData      = null;
            edge2LaneCrossing  = null;
            node2LaneAlignment = null;
        }
Beispiel #3
0
        /// <summary>
        /// Creates a small graph and applies a hierarchic group layout to it.
        /// The output of the calculated coordinates will be displayed in the
        /// console.
        /// </summary>
        public void Run()
        {
            DefaultLayoutGraph graph = new DefaultLayoutGraph();

            //construct graph. assign sizes to nodes
            Node v1 = graph.CreateNode();

            graph.SetSize(v1, 30, 30);
            Node v2 = graph.CreateNode();

            graph.SetSize(v2, 30, 30);
            Node v3 = graph.CreateNode();

            graph.SetSize(v3, 30, 30);
            Node v4 = graph.CreateNode();

            graph.SetSize(v4, 30, 30);

            Node groupNode = graph.CreateNode();

            graph.SetSize(groupNode, 100, 100);

            Edge e1 = graph.CreateEdge(v1, v2);
            Edge e2 = graph.CreateEdge(v4, groupNode);
            Edge e3 = graph.CreateEdge(v1, v3);
            Edge e4 = graph.CreateEdge(v1, v1);
            Edge e5 = graph.CreateEdge(v2, groupNode);
            Edge e6 = graph.CreateEdge(groupNode, v2);

            //optionally setup some edge groups
            IEdgeMap spg = graph.CreateEdgeMap();
            IEdgeMap tpg = graph.CreateEdgeMap();

            graph.AddDataProvider(PortConstraintKeys.SourceGroupIdDpKey, spg);
            graph.AddDataProvider(PortConstraintKeys.TargetGroupIdDpKey, tpg);

            spg.Set(e1, "SGroup1");
            spg.Set(e3, "SGroup1");
            tpg.Set(e1, "TGroup1");
            tpg.Set(e3, "TGroup1");

            //optionally setup the node grouping
            INodeMap nodeId       = graph.CreateNodeMap();
            INodeMap parentNodeId = graph.CreateNodeMap();
            INodeMap groupKey     = graph.CreateNodeMap();

            graph.AddDataProvider(GroupingKeys.NodeIdDpKey, nodeId);
            graph.AddDataProvider(GroupingKeys.ParentNodeIdDpKey, parentNodeId);
            graph.AddDataProvider(GroupingKeys.GroupDpKey, groupKey);

            //mark a node as a group node
            groupKey.SetBool(groupNode, true);

            // add ids for each node
            nodeId.Set(v1, "v1");
            nodeId.Set(v2, "v2");
            nodeId.Set(v3, "v3");
            nodeId.Set(v4, "v4");
            nodeId.Set(groupNode, "groupNode");

            // set the parent for each grouped node
            parentNodeId.Set(v2, "groupNode");
            parentNodeId.Set(v3, "groupNode");

            HierarchicLayout layout = new HierarchicLayout();

            layout.MinimumLayerDistance = 0;
            layout.EdgeLayoutDescriptor.MinimumDistance = 10;

            new BufferedLayout(layout).ApplyLayout(graph);

            Console.WriteLine("\n\nGRAPH LAID OUT USING HIERARCHICLAYOUT");
            Console.WriteLine("v1 center position = " + graph.GetCenter(v1));
            Console.WriteLine("v2 center position = " + graph.GetCenter(v2));
            Console.WriteLine("v3 center position = " + graph.GetCenter(v3));
            Console.WriteLine("v4 center position = " + graph.GetCenter(v4));
            Console.WriteLine("group center position = " + graph.GetCenter(groupNode));
            Console.WriteLine("group size = " + graph.GetSize(groupNode));
            Console.WriteLine("e1 path = " + graph.GetPath(e1));
            Console.WriteLine("e2 path = " + graph.GetPath(e2));
            Console.WriteLine("e3 path = " + graph.GetPath(e3));
            Console.WriteLine("e4 path = " + graph.GetPath(e4));
            Console.WriteLine("e5 path = " + graph.GetPath(e5));
            Console.WriteLine("e6 path = " + graph.GetPath(e4));

            //display the result in a simple viewer
            Application.Run(new Demo.yWorks.LayoutGraphViewer.GraphViewer(graph, "Hierarchical Group Layout"));
        }
        public static void Main()
        {
            DefaultLayoutGraph graph = new DefaultLayoutGraph();

            //construct graph. assign sizes to nodes
            Node v1 = graph.CreateNode();

            graph.SetSize(v1, 30, 30);
            Node v2 = graph.CreateNode();

            graph.SetSize(v2, 30, 30);
            Node v3 = graph.CreateNode();

            graph.SetSize(v3, 30, 30);
            Node v4 = graph.CreateNode();

            graph.SetSize(v4, 30, 30);

            // create some edges...
            Edge e1 = graph.CreateEdge(v1, v2);
            Edge e2 = graph.CreateEdge(v1, v3);
            Edge e3 = graph.CreateEdge(v2, v4);

            // create swim lane descriptors for two lanes
            var sl1 = new SwimlaneDescriptor(1);
            var sl2 = new SwimlaneDescriptor(2);

            // create a map to store the swim lane descriptors
            INodeMap slMap = graph.CreateNodeMap();

            // assign nodes to lanes
            slMap.Set(v1, sl1);
            slMap.Set(v2, sl2);
            slMap.Set(v3, sl2);
            slMap.Set(v4, sl1);

            // register the information
            graph.AddDataProvider(HierarchicLayout.SwimlaneDescriptorDpKey, slMap);

            // create the layout algorithm
            HierarchicLayout layout = new HierarchicLayout();

            // start the layout
            new BufferedLayout(layout).ApplyLayout(graph);


            Console.WriteLine("\n\nGRAPH LAID OUT HIERARCHICALLY IN SWIMLANES");
            Console.WriteLine("v1 center position = " + graph.GetCenter(v1));
            Console.WriteLine("v2 center position = " + graph.GetCenter(v2));
            Console.WriteLine("v3 center position = " + graph.GetCenter(v3));
            Console.WriteLine("v4 center position = " + graph.GetCenter(v4));
            Console.WriteLine("e1 path = " + graph.GetPath(e1));
            Console.WriteLine("e2 path = " + graph.GetPath(e2));
            Console.WriteLine("e3 path = " + graph.GetPath(e3));
            Console.WriteLine("SwimLane 1 index = " + sl1.ComputedLaneIndex);
            Console.WriteLine("SwimLane 1 position = " + sl1.ComputedLanePosition);
            Console.WriteLine("SwimLane 1 width = " + sl1.ComputedLaneWidth);
            Console.WriteLine("SwimLane 2 index = " + sl2.ComputedLaneIndex);
            Console.WriteLine("SwimLane 2 position = " + sl2.ComputedLanePosition);
            Console.WriteLine("SwimLane 2 width = " + sl2.ComputedLaneWidth);

            //clean up data maps
            graph.DisposeNodeMap(slMap);
            graph.RemoveDataProvider(HierarchicLayout.SwimlaneDescriptorDpKey);

            //display the graph in a simple viewer
            var viewer = new GraphViewer();

            viewer.AddLayoutGraph(graph, "Swimlane Demo");
            var application = new System.Windows.Application();

            application.Run(viewer);
        }
Beispiel #5
0
        public static void Main()
        {
            //instantiates an empty graph
            yWorks.Algorithms.Graph graph = new yWorks.Algorithms.Graph();

            //create a temporary node array for fast lookup
            Node[] tmpNodes = new Node[5];

            //create some nodes in the graph and store references in the array
            for (int i = 0; i < 5; i++)
            {
                tmpNodes[i] = graph.CreateNode();
            }

            //create some edges in the graph
            for (int i = 0; i < 5; i++)
            {
                for (int j = i + 1; j < 5; j++)
                {
                    //create an edge from node at index i to node at index j
                    graph.CreateEdge(tmpNodes[i], tmpNodes[j]);
                }
            }


            //output the nodes of the graph
            Console.WriteLine("The nodes of the graph");
            for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
            {
                Node node = nc.Node;
                Console.WriteLine(node);
                Console.WriteLine("in edges #" + node.InDegree);
                for (IEdgeCursor ec = node.GetInEdgeCursor(); ec.Ok; ec.Next())
                {
                    Console.WriteLine(ec.Edge);
                }
                Console.WriteLine("out edges #" + node.OutDegree);
                for (IEdgeCursor ec = node.GetOutEdgeCursor(); ec.Ok; ec.Next())
                {
                    Console.WriteLine(ec.Edge);
                }
            }


            //output the edges of the graph
            Console.WriteLine("\nThe edges of the graph");
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                Console.WriteLine(ec.Edge);
            }

            //reverse edges that have consecutive neighbors in graph
            //reversing means switching source and target node
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                if (Math.Abs(ec.Edge.Source.Index - ec.Edge.Target.Index) == 1)
                {
                    graph.ReverseEdge(ec.Edge);
                }
            }

            Console.WriteLine("\nthe edges of the graph after some edge reversal");
            for (IEdgeCursor ec = graph.GetEdgeCursor(); ec.Ok; ec.Next())
            {
                Console.WriteLine(ec.Edge);
            }

            ///////////////////////////////////////////////////////////////////////////
            // Node- and EdgeMap handling   ///////////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////

            //create a nodemap for the graph
            INodeMap nodeMap = graph.CreateNodeMap();

            foreach (var node in graph.Nodes)
            {
                //associate descriptive String to the node via a nodemap
                nodeMap.Set(node, "this is node " + node.Index);
            }

            //create an edgemap for the graph
            IEdgeMap edgeMap = graph.CreateEdgeMap();

            foreach (var edge in graph.Edges)
            {
                //associate descriptive String to the edge via an edgemap
                edgeMap.Set(edge, "this is edge [" +
                            nodeMap.Get(edge.Source) + "," +
                            nodeMap.Get(edge.Target) + "]");
            }

            //output the nodemap values of the nodes
            Console.WriteLine("\nThe node map values of the graph");
            foreach (var node in graph.Nodes)
            {
                Console.WriteLine(nodeMap.Get(node));
            }

            //output the edges of the graph
            Console.WriteLine("\nThe edge map values of the graph");
            foreach (var edge in graph.Edges)
            {
                Console.WriteLine(edgeMap.Get(edge));
            }

            //cleanup unneeded node and edge maps again (free resources)
            graph.DisposeNodeMap(nodeMap);
            graph.DisposeEdgeMap(edgeMap);

            ///////////////////////////////////////////////////////////////////////////
            // removing elements from the graph  //////////////////////////////////////
            ///////////////////////////////////////////////////////////////////////////

            for (INodeCursor nc = graph.GetNodeCursor(); nc.Ok; nc.Next())
            {
                //remove node that has a edge degree > 2
                if (nc.Node.Degree > 2)
                {
                    //removed the node and all of its adjacent edges from the graph
                    graph.RemoveNode(nc.Node);
                }
            }
            Console.WriteLine("\ngraph after some node removal");
            Console.WriteLine(graph);

            Console.WriteLine("\nPress key to end demo.");
            Console.ReadKey();
        }