public void Clear()
        {
            int verticesRemoved = 0;
            int edgesRemoved    = 0;

            var graph = new BidirectionalGraph <int, Edge <int> >();

            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.VertexRemoved += v =>
            {
                Assert.IsNotNull(v);
                // ReSharper disable once AccessToModifiedClosure
                ++verticesRemoved;
            };
            // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Local
            graph.EdgeRemoved += e =>
            {
                Assert.IsNotNull(e);
                // ReSharper disable once AccessToModifiedClosure
                ++edgesRemoved;
            };

            AssertEmptyGraph(graph);

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounters(0, 0);

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));
            graph.AddVerticesAndEdge(new Edge <int>(2, 3));
            graph.AddVerticesAndEdge(new Edge <int>(3, 1));

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounters(3, 3);

            graph.AddVerticesAndEdge(new Edge <int>(1, 2));
            graph.AddVerticesAndEdge(new Edge <int>(3, 2));
            graph.AddVerticesAndEdge(new Edge <int>(3, 1));
            graph.AddVerticesAndEdge(new Edge <int>(3, 3));

            graph.Clear();
            AssertEmptyGraph(graph);
            CheckCounters(3, 4);

            #region Local function

            void CheckCounters(int expectedVerticesRemoved, int expectedEdgesRemoved)
            {
                Assert.AreEqual(expectedVerticesRemoved, verticesRemoved);
                Assert.AreEqual(expectedEdgesRemoved, edgesRemoved);
                verticesRemoved = 0;
                edgesRemoved    = 0;
            }

            #endregion
        }
        public void Clear()
        {
            lock (SyncRoot)
                Graph.Clear();

            Cleared?.Invoke(this, EventArgs.Empty);
        }
Beispiel #3
0
        /// <inheritdoc/>
        public async Task Populate()
        {
            _graph.Clear();

            await PopulateStreams();
            await PopulateEvents();
        }
        /// <summary>
        /// Reloads the proxy data from the dataset.
        /// </summary>
        /// <returns>A task that will finish when the reload is complete.</returns>
        public Task ReloadFromDataset()
        {
            m_Diagnostics.Log(
                LevelToLog.Trace,
                HostConstants.LogPrefix,
                Resources.ProxyCompositionLayer_LogMessage_ReloadingFromDataset);

            var task = m_Commands.CurrentState();

            return(task.ContinueWith(
                       t =>
            {
                lock (m_Lock)
                {
                    m_Groups.Clear();
                    m_GroupConnections.Clear();

                    var state = t.Result;
                    foreach (var group in state.Groups)
                    {
                        m_Groups.Add(group.Item1, group.Item2);
                        m_GroupConnections.AddVertex(group.Item1);
                    }

                    foreach (var connection in state.Connections)
                    {
                        m_GroupConnections.AddEdge(new CompositionLayerGroupEdge(connection.Item1, connection.Item2, connection.Item3));
                    }
                }
            }));
        }
            /// <summary>
            /// Applies the changes in the current change to the given history object.
            /// </summary>
            /// <param name="historyObject">The object to which the changes should be applied.</param>
            /// <exception cref="ArgumentNullException">
            ///     Thrown if <paramref name="historyObject"/> is <see langword="null" />.
            /// </exception>
            public void ApplyTo(BidirectionalGraph <TVertex, TEdge> historyObject)
            {
                {
                    Lokad.Enforce.Argument(() => historyObject);
                }

                historyObject.Clear();
            }
Beispiel #6
0
        private void CreateGraph()
        {
            _graph.Clear();
            SampleVertex obj1 = new SampleVertex("One");

            _graph.AddVertex(obj1);
            SampleVertex obj2 = new SampleVertex("Two");

            _graph.AddVertex(obj2);
            SampleVertex obj3 = new SampleVertex("Three");

            _graph.AddVertex(obj3);
            _graph.AddEdge(new Edge <object>(obj1, obj2));
            _graph.AddEdge(new Edge <object>(obj1, obj3));
        }
Beispiel #7
0
        private void CreateGraph()
        {
            _graph.Clear();

            // Add all "normal" points to the graph.
            AddAllNormalPoints(_graph, 0);

            // Get all the portal locations.
            var portals = GetPortals().GroupBy(p => p.Name);

            foreach (var portalPair in portals.Where(g => g.Count() == 2))
            {
                var pair = portalPair.ToArray();

                var portal      = portalPair.First();
                var otherPortal = portalPair.Skip(1).First();
                var edge        = new Edge <Point>(portal.MapLocation, otherPortal.MapLocation);
                _graph.AddEdge(edge);
                _graph.AddEdge(edge.Reverse);
            }

            _startingLocation = portals.First(p => p.Key == "AA").First().MapLocation;
            _goal             = portals.First(p => p.Key == "ZZ").First().MapLocation;
        }
Beispiel #8
0
        public void Evaluate(int SpreadMax)
        {
            if (FBuild[0] || (pd.InputChanged && init))
            {
                graph.Clear();
                VSize.Clear();
                VThickness.Clear();
                VType.Clear();
                VPos.Clear();
                VCache.Clear();

                for (int i = 0; i < FVertices.SliceCount; i++)
                {
                    graph.AddVertex(FVertices[i]);
                    VSize.Add(FVertices[i], new Size(FVSize[i], FVSize[i]));
                    VThickness.Add(FVertices[i], new Thickness(1.0));
                    VType.Add(FVertices[i], FVType[i]);
                    VPos.Add(FVertices[i], new Point(FVPos[i].x, FVPos[i].y));
                    VCache.Add(FVertices[i]);
                }
                for (int i = 0; i < Math.Max(FEdgeFrom.SliceCount, FEdgeTo.SliceCount); i++)
                {
                    graph.AddEdge(new Edge <string>(FEdgeFrom[i], FEdgeTo[i]));
                }

                var cparams = new FreeFRLayoutParameters();
                foreach (var prop in typeof(FreeFRLayoutParameters).GetProperties())
                {
                    var input = pd.InputPins[prop.Name].Spread[0];
                    if (!(input.ToString().StartsWith("0") && input.ToString().EndsWith("0")))
                    {
                        prop.SetValue(cparams, pd.InputPins[prop.Name].Spread[0]);
                    }
                }
                layout = new FRLayoutAlgorithm <string, IEdge <string>, BidirectionalGraph <string, IEdge <string> > >(graph, VPos, cparams);
                foreach (var prop in typeof(FreeFRLayoutParameters).GetProperties())
                {
                    var input = pd.InputPins[prop.Name].Spread[0];
                    if (!(input.ToString().StartsWith("0") && input.ToString().EndsWith("0")))
                    {
                        prop.SetValue(layout.Parameters, pd.InputPins[prop.Name].Spread[0]);
                    }
                }
                layout.Finished += (sender, args) =>
                {
                    FPosOut.SliceCount = VCache.Count;
                    for (int i = 0; i < VCache.Count; i++)
                    {
                        var point = layout.VertexPositions[VCache[i]];
                        FPosOut[i] = new Vector2D(point.X, point.Y);
                    }
                };
                if (FAsync[0])
                {
                    Task.Run(() =>
                    {
                        layout.Compute();
                    });
                }
                else
                {
                    layout.Compute();
                }
                init = true;
            }
            if (layout != null)
            {
                FState[0] = layout.State.ToString();
            }
        }
Beispiel #9
0
        public static void ValidatePathsUsingDijkstra(Topology.IGP.Topology igp_topology, Topology.MPLS.HierarchicalLabelSwitchedPath.HierarchicalLabelSwitchedPath hlsp)
        {
            Console.WriteLine("\n====Validating Paths====\n");

            List <yggdrasil2.Topology.Node.Node>     nodes_copy = igp_topology.Nodes.DeepClone();
            List <yggdrasil2.Topology.IGP.Link.Link> links_copy = igp_topology.Links.DeepClone();

            BidirectionalGraph <string, TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link> > graph =
                new BidirectionalGraph <string, TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link> >();

            Dictionary <TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>, double> cost =
                new Dictionary <TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>, double>();


            var start = nodes_copy.Where(n => n.IPv4RouterIdentifier == hlsp.IPv4TunnelSenderAddress).SingleOrDefault();
            var end   = nodes_copy.Where(n => n.IPv4RouterIdentifier == hlsp.IPv4TunnelEndpointAddress).SingleOrDefault();

            if (start != null && end != null)
            {
                foreach (var lsp in hlsp.Children)
                {
                    graph.Clear();

                    foreach (var node in nodes_copy)
                    {
                        graph.AddVertex(node.Id);
                    }

                    if (!start.IsPseudonode)  // it will never be a pseudonode, get rid of this
                    {
                        var nodeLinks = links_copy.Where(l => l.SourceNode == start.Id).ToList();

                        foreach (var l in nodeLinks)
                        {
                            if (!graph.ContainsEdge(l.SourceNode, l.TargetNode))
                            {
                                if (l.OperationalStatus)
                                {
                                    var forwardEdge = new TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>(l.SourceNode, l.TargetNode, l);
                                    graph.AddEdge(forwardEdge);
                                    cost.Add(forwardEdge, 1);
                                }
                            }
                        }
                    }

                    foreach (var hop in lsp.ComputedExplicitRouteObject)
                    {
                        var link = links_copy.Where(l => l.IPv4InterfaceAddress == hop).SingleOrDefault();

                        if (link != null)
                        {
                            if (!graph.ContainsEdge(link.SourceNode, link.TargetNode))
                            {
                                if (link.OperationalStatus)
                                {
                                    var backwardEdge = new TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>(link.TargetNode, link.SourceNode, link);
                                    graph.AddEdge(backwardEdge);
                                    cost.Add(backwardEdge, 1);
                                }

                                //var srcNode = nodes_copy.Where(n => n.IgpRouterIdentifier == link.SourceNode).SingleOrDefault();
                                var dstNode = nodes_copy.Where(n => n.IgpRouterIdentifier == link.TargetNode).SingleOrDefault();

                                //if (srcNode != null)
                                //{

                                //    if (srcNode.IsPseudonode)
                                //    {
                                //        var nodeLinks = links_copy.Where(l => l.TargetNode == srcNode.Id).ToList();

                                //        foreach (var l in nodeLinks)
                                //        {

                                //            if (!graph.ContainsEdge(l.SourceNode, l.TargetNode))
                                //            {
                                //                if (l.OperationalStatus)
                                //                {
                                //                    var forwardEdge = new TaggedEdge<string, yggdrasil2.Topology.IGP.Link.Link>(l.SourceNode, l.TargetNode, l);
                                //                    graph.AddEdge(forwardEdge);
                                //                    cost.Add(forwardEdge, 1);
                                //                }
                                //            }
                                //        }
                                //    }

                                //}

                                if (dstNode != null)
                                {
                                    if (dstNode.IsPseudonode)
                                    {
                                        var nodeLinks = links_copy.Where(l => l.TargetNode == dstNode.Id).ToList();

                                        foreach (var l in nodeLinks)
                                        {
                                            if (!graph.ContainsEdge(l.SourceNode, l.TargetNode))
                                            {
                                                if (l.OperationalStatus)
                                                {
                                                    var forwardEdge = new TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>(l.SourceNode, l.TargetNode, l);
                                                    graph.AddEdge(forwardEdge);
                                                    cost.Add(forwardEdge, 1);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    DijkstraShortestPathAlgorithm <string, TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link> > dijkstra =
                        new DijkstraShortestPathAlgorithm <string, TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link> >(graph,
                                                                                                                            AlgorithmExtensions.GetIndexer <TaggedEdge <string, yggdrasil2.Topology.IGP.Link.Link>, double>(cost));

                    dijkstra.Compute(start.Id);

                    if (dijkstra.Distances.ContainsKey(end.Id) && dijkstra.Distances[end.Id] != double.MaxValue)
                    {
                        lsp.Feasible = true;
                        Console.WriteLine("Path {0} is \u001b[32mFEASIBLE\u001b[0m.\n\t{1} is REACHABLE from {2} in {3} hops (includes pseudonodes).",
                                          lsp.SymbolicPathName, lsp.IPv4TunnelEndpointAddress, lsp.IPv4TunnelSenderAddress, dijkstra.Distances[end.Id]);
                    }
                    else
                    {
                        lsp.Feasible = false;
                        Console.WriteLine("Path {0} is \u001b[31mNOT FEASIBLE\u001b[0m.\n\t{1} is UREACHABLE from {2}.",
                                          lsp.SymbolicPathName, lsp.IPv4TunnelEndpointAddress, lsp.IPv4TunnelSenderAddress);
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Generates the change graph.
        /// </summary>
        /// <returns>A graph representation of the interaction between resources during the update.</returns>
        public BidirectionalGraph <IChangeVertex, TaggedEdge <IChangeVertex, string> > GenerateChangeGraph()
        {
            var parameters       = new List <ParameterVertex>();
            var direct           = new DirectModificationVertex();
            var resourceVertices = this.Changes.Select(ResourceVertex.Create).ToList();
            var edges            = new List <TaggedEdge <IChangeVertex, string> >();
            var graph            = new BidirectionalGraph <IChangeVertex, TaggedEdge <IChangeVertex, string> >();

            graph.Clear();

            // Create edge list
            foreach (var resource in resourceVertices)
            {
                foreach (var detail in resource.Change.ResourceChange.Details)
                {
                    var edgeName = detail.Target.Name ?? detail.Target.Attribute.Value;

                    if (detail.ChangeSource == ChangeSource.ParameterReference)
                    {
                        // Value of stack parameter has changed
                        var param = parameters.FirstOrDefault(p => p.Name == detail.CausingEntity);

                        if (param == null)
                        {
                            param = new ParameterVertex(detail.CausingEntity);
                            parameters.Add(param);
                        }

                        edges.Add(new TaggedEdge <IChangeVertex, string>(param, resource, edgeName));
                    }

                    if (detail.ChangeSource == ChangeSource.DirectModification &&
                        detail.Evaluation == EvaluationType.Static)
                    {
                        // User directly modified a property
                        edges.Add(new TaggedEdge <IChangeVertex, string>(direct, resource, edgeName));
                    }

                    if (detail.ChangeSource == ChangeSource.ResourceReference)
                    {
                        // Change via Ref to another resource
                        var causingEntity = resourceVertices.First(r => r.Name == detail.CausingEntity);
                        edges.Add(new TaggedEdge <IChangeVertex, string>(causingEntity, resource, edgeName));
                    }

                    if (detail.ChangeSource == ChangeSource.ResourceAttribute)
                    {
                        // Change via GetAtt from another resource
                        var causingEntity = resourceVertices.First(
                            r => r.Name == detail.CausingEntity.Split('.').First());
                        edges.Add(new TaggedEdge <IChangeVertex, string>(causingEntity, resource, edgeName));
                    }
                }
            }

            if (edges.Any(e => e.Source.GetType() == typeof(DirectModificationVertex)))
            {
                graph.AddVertex(direct);
            }

            graph.AddVertexRange(parameters);
            graph.AddVertexRange(resourceVertices);
            graph.AddEdgeRange(edges);

            return(graph);
        }