Ejemplo n.º 1
0
        public QuickGraph.BidirectionalGraph <MapNode, UndirectedEdge <MapNode> > GetCurrentConvoyMapBidirectional()
        {
            var graph = GraphExtensions.ToBidirectionalGraph <MapNode, UndirectedEdge <MapNode> >(GetCurrentConvoyMap().Edges);

            graph.AddEdgeRange(graph.Edges.Select(e => new UndirectedEdge <MapNode>(e.Target, e.Source)));
            return(graph);
        }
Ejemplo n.º 2
0
        public override void SetRelation(PXGraph graph, Relation relation, object oldRecord, Type changingField, object newValue)
        {
            if (!CanProcess(relation))
            {
                return;
            }
            var oneToManyRelation = (OneToManyRelation)relation;

            Type mainDacType = oneToManyRelation.Right.TargetField.DeclaringType;
            Type parentField = oneToManyRelation.Right.TargetField;

            Type childForeignKeyField = oneToManyRelation.Right.ReferenceField;


            if (changingField == parentField)
            {
                object oldParentValue = graph.Caches[mainDacType].GetValue(oldRecord, parentField.Name);;

                if (!oldParentValue.Equals(newValue))
                {
                    foreach (object correctingEntity in GraphExtensions.SelectById(graph, childForeignKeyField, oldParentValue))
                    {
                        ChangeReference(graph, oneToManyRelation.Right, correctingEntity, newValue);
                    }
                }
            }

            if (changingField == childForeignKeyField)
            {
                ChangeReference(graph, oneToManyRelation.Right, oldRecord, newValue);
            }
        }
Ejemplo n.º 3
0
        protected override IVisualEdge CreateEdge(IVisual root, IVisual leaf)
        {
            var result = Registry.Pooled <IVisualFactory>().CreateEdge(root, leaf, "");

            result.Data = GraphExtensions.EdgeString <IVisual, IVisualEdge>(result);
            return(result);
        }
        public void RouteBetweenNodes_ValidInput_ExpectedResult(char start,
                                                                char end, bool expected)
        {
            var graph = CreateGraph();

            var actual = GraphExtensions.HasRouteBetweenNodes(graph, start, end);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public virtual void MakeEdgeStrings(IGraph <TItem, TEdge> graph)
        {
            var changer = Factory as IGraphModelPropertyChanger <TItem, TEdge>;

            if (changer != null)
            {
                foreach (var edge in graph.Edges())
                {
                    changer.SetProperty(edge, GraphExtensions.EdgeString <TItem, TEdge> (edge));
                }
            }
        }
Ejemplo n.º 6
0
        public void Initialize()
        {
            SchemaFacade.InitSchemata();
            descriptions.Clear();
            hiddens.Clear();
            describedMarkers.Clear();

            descriptions.Add(CommonSchema.DescriptionMarker.Id);
            hiddens.Add(CommonSchema.DescriptionMarker.Id);

            IThingGraph markerGraph = new ThingGraph();
            var         markers     = Markers();

            GraphExtensions.MergeInto(Schema.IdentityGraph, markerGraph);
            ThingGraph.DeepCopy(markers, markerGraph);
            markerGraph.DeepCopy(markers, ThingGraph);

            foreach (var marker in markerGraph)
            {
                var markerId = marker.Id;
                foreach (var link in markerGraph.Edges(marker))
                {
                    if (link.Marker == null)
                    {
                        continue;
                    }

                    var idLink = (ILink <Id>)link;

                    if (ViewMetaSchemaHideEnabled &&
                        idLink.Marker == ViewMetaSchema.Hide.Id &&
                        idLink.Leaf == markerId)
                    {
                        hiddens.Add(markerId);
                    }

                    if (idLink.Marker == MetaSchema.DescriptionMarker.Id)
                    {
                        if (idLink.Leaf == markerId)
                        {
                            hiddens.Add(markerId);
                            descriptions.Add(markerId);
                        }
                        else
                        {
                            describedMarkers[markerId] = idLink.Leaf;
                        }
                    }
                }
            }
        }
        public void Should_Report_Is_Correct_GetStatistics_Method()
        {
            //     Graph graph = new Graph("Master Graph", "One Graph to Rule Them All", new int[3, 3] { { 0, 1, 1 }, { 1, 0, 1 }, { 1, 1, 0 } });

            StringBuilder myStringBuilder = new StringBuilder();

            myStringBuilder.Append(String.Format("{0,6} {1,15}\n\n", "Name", "Master Graph"));
            myStringBuilder.Append(String.Format("{0,6} {1,15:N0}\n", "Description", "One Graph to Rule Them All"));
            myStringBuilder.Append(String.Format("{0,6} {1,15:N0}\n", "IsConnected", true));
            string newString = Convert.ToString(myStringBuilder);


            StringAssert.Contains(newString, GraphExtensions.GetStatistics(graph));
        }
Ejemplo n.º 8
0
        public IThingGraph Use(IThingGraph source, IThingGraph sink)
        {
            Action <IThing> message  = null;
            var             i        = 0;
            var             iStreams = 0;
            var             count    = source.Count;
            bool            streams  = false;

            if (this.Progress != null)
            {
                message = thing => {
                    i++;
                    if (thing != null)
                    {
                        var type = thing.GetType();
                        if (!streams && Reflector.Implements(type, typeof(IStreamThing)))
                        {
                            iStreams++;
                        }
                        var icount = streams ? count : count + iStreams;
                        this.Progress(string.Format("merging {2} of {3} ({4} Streams / {1} {0} )", thing.Id.ToString("X"),
                                                    streams ? "Streams" : type.Name, i, icount, iStreams), i, icount);
                    }
                }
            }
            ;

            GraphExtensions.MergeInto(source, sink, message, thing => {
                var existing = sink.GetById(thing.Id);
                if (existing == null)
                {
                    sink.Add(thing);
                }
                else
                {
                    sink.UniqueThing(thing);
                }
                var streamThing = thing as IStreamThing;
                if (streamThing != null)
                {
                    var data = source.ContentContainer.GetById(thing.Id);
                    sink.ContentContainer.Add(data);
                }
            });

            return(sink);
        }
Ejemplo n.º 9
0
        public void TestCycleDetector()
        {
            GraphExtensions <String> g = new GraphExtensions <string>();

            // We want to construct a triangle
            g.AddVertex("A");
            g.AddVertex("B");
            g.AddVertex("C");

            g.AddEdge("A", "B", 100);
            // At this point, we should definitely have no cycle!
            Assert.IsFalse(g.ExistsCycleFrom("A"));
            Assert.IsFalse(g.ExistsCycleFrom("B"));
            Assert.IsFalse(g.ExistsCycleFrom("C"));

            // Should still not have a cycle
            g.AddEdge("B", "C", 100);
            Assert.IsFalse(g.ExistsCycleFrom("A"));
            Assert.IsFalse(g.ExistsCycleFrom("B"));
            Assert.IsFalse(g.ExistsCycleFrom("C"));

            // Let's fully connect our trinagle
            g.AddEdge("C", "A", 100);
            Assert.IsTrue(g.ExistsCycleFrom("A"));
            Assert.IsTrue(g.ExistsCycleFrom("B"));
            Assert.IsTrue(g.ExistsCycleFrom("C"));

            // Let's add a branch from our triangle
            g.AddVertex("D");
            g.AddEdge("D", "A", 100);

            // These should still be true
            Assert.IsTrue(g.ExistsCycleFrom("A"));
            Assert.IsTrue(g.ExistsCycleFrom("B"));
            Assert.IsTrue(g.ExistsCycleFrom("C"));
            // this is a branch, so no cycle from D exists
            Assert.IsFalse(g.ExistsCycleFrom("D"));

            // Let's connect D to B, making a cycle.
            g.AddEdge("D", "B", 100);
            // There are now multiple possible cycles from every node to every other
            Assert.IsTrue(g.ExistsCycleFrom("A"));
            Assert.IsTrue(g.ExistsCycleFrom("B"));
            Assert.IsTrue(g.ExistsCycleFrom("C"));
            Assert.IsTrue(g.ExistsCycleFrom("D"));
        }
        protected override void InternalCompute()
        {
            var cancelManager = this.Services.CancelManager;

            TVertex root;

            if (!this.TryGetRootVertex(out root))
            {
                throw new InvalidOperationException("root vertex not set");
            }
            if (this.pairs == null)
            {
                throw new InvalidOperationException("pairs not set");
            }

            var gpair       = GraphExtensions.ToAdjacencyGraph(this.pairs);
            var disjointSet = new ForestDisjointSet <TVertex>();
            var vancestors  = new Dictionary <TVertex, TVertex>();
            var dfs         = new DepthFirstSearchAlgorithm <TVertex, TEdge>(this, this.VisitedGraph, new Dictionary <TVertex, GraphColor>(this.VisitedGraph.VertexCount));

            dfs.InitializeVertex += v => disjointSet.MakeSet(v);
            dfs.DiscoverVertex   += v => vancestors[v] = v;
            dfs.TreeEdge         += edge =>
            {
                disjointSet.Union(edge.Source, edge.Target);
                vancestors[disjointSet.FindSet(edge.Source)] = edge.Source;
            };
            dfs.FinishVertex += v =>
            {
                foreach (var e in gpair.OutEdges(v))
                {
                    if (dfs.VertexColors[e.Target] == GraphColor.Black)
                    {
                        this.ancestors[EdgeExtensions.ToVertexPair <TVertex, SEquatableEdge <TVertex> >(e)] = vancestors[disjointSet.FindSet(e.Target)];
                    }
                }
            };

            // go!
            dfs.Compute(root);
        }
Ejemplo n.º 11
0
        public void DelegateGraph()
        {
            // a simple adjacency graph representation
            int[][] graph = new int[5][];
            graph[0] = new int[] { 1 };
            graph[1] = new int[] { 2, 3 };
            graph[2] = new int[] { 3, 4 };
            graph[3] = new int[] { 4 };
            graph[4] = new int[] { };

            // interoping with quickgraph
            var g = GraphExtensions.ToDelegateVertexAndEdgeListGraph(
                Enumerable.Range(0, graph.Length),
                v => Array.ConvertAll(graph[v], w => new SEquatableEdge <int>(v, w))
                );

            // it's ready to use!
            foreach (var v in g.TopologicalSort())
            {
                Console.WriteLine(v);
            }
        }
Ejemplo n.º 12
0
        public async Task <IEnumerable> Execute(string queryString)
        {
            if (!IsOpen)
            {
                throw new Exception("Client must be opened prior to executing a query");
            }

            // Generate the query from the query string
            var query = GraphExtensions.CreateGremlinQuery(client, graph, queryString);

            // Get the semaphore
            await semaphore.WaitAsync();

            try
            {
                return(await query.ExecuteNextAsync <dynamic>());
            }
            finally
            {
                semaphore.Release();
            }
        }
Ejemplo n.º 13
0
        private void NodeItems(Node <IVisual> node, IVisual nodeVisual,
                               IDictionary <IVisual, IVisual> itemsDone)
        {
            var factory = Registry.Pooled <IVisualFactory>();

            foreach (var visual in node.Items)
            {
                IVisual childVisual = null;
                itemsDone.TryGetValue(visual, out childVisual);
                if (childVisual == null)
                {
                    string ws = visual.Data.ToString();
                    if (visual is IVisualEdge)
                    {
                        ws = GraphExtensions.EdgeString <IVisual, IVisualEdge>((IVisualEdge)visual);
                    }
                    string ds = ws + "\r\n" +
                                visual.Shape.BoundsRect.ToString();

                    if (!node.Envelope.Contains(visual.Shape.BoundsRect))
                    {
                        ds = "! " + ds;
                    }
                    childVisual          = factory.CreateItem(ds);
                    childVisual.Shape    = this.VisualsDisplay.ShapeFactory.Create <IRoundedRectangleShape>();
                    childVisual.Location = visual.Location;
                    childVisual.Size     = visual.Size;

                    this.VisualsDisplay.Data.Add(childVisual);
                    itemsDone.Add(visual, childVisual);
                }

                var edge = factory.CreateEdge(childVisual, nodeVisual, string.Empty);

                edge.Shape = this.VisualsDisplay.Layout.CreateShape(edge);
                this.VisualsDisplay.Data.Add(edge);
            }
        }
Ejemplo n.º 14
0
 public override string ToString()
 {
     return(GraphExtensions.EdgeString <IGraphEntity, IGraphEdge>(this));
 }
Ejemplo n.º 15
0
        public void TestDijkstraSimple()
        {
            GraphExtensions <String> g = new GraphExtensions <string>();

            // Add  towns
            g.AddVertex("Sydney");
            g.AddVertex("Melbourne");
            g.AddVertex("Adelaide");

            // Draw a straight line between them
            g.AddEdge("Sydney", "Melbourne", 100);
            g.AddEdge("Melbourne", "Adelaide", 100);

            /*
             *  We now have:
             *  Sydney <-> Melbourne <-> Adelaide
             */
            List <String> l = g.ShortestPathBetween("Sydney", "Adelaide");

            Assert.AreEqual("Sydney", l[0]);
            Assert.AreEqual("Melbourne", l[1]);
            Assert.AreEqual("Adelaide", l[2]);
            Assert.AreEqual(3, l.Count);

            // Adjacent vertices
            l = g.ShortestPathBetween("Sydney", "Melbourne");
            Assert.AreEqual("Sydney", l[0]);
            Assert.AreEqual("Melbourne", l[1]);
            Assert.AreEqual(2, l.Count);

            // From self to self
            l = g.ShortestPathBetween("Sydney", "Sydney");
            Assert.AreEqual(0, l.Count);

            // Add a new unconnected vertex
            g.AddVertex("Rome");
            // Not all roads lead to Rome!
            l = g.ShortestPathBetween("Sydney", "Rome");
            Assert.IsNull(l);

            // TODO remove
            g.AddVertex("Helsinki");
            g.AddEdge("Sydney", "Rome", 400);
            g.AddEdge("Sydney", "Helsinki", 500);

            // Make a heavier but direct connection
            g.AddEdge("Sydney", "Adelaide", 300);
            // The following from earlier should still hold
            Console.WriteLine("Here they come:");
            l = g.ShortestPathBetween("Sydney", "Adelaide");

            /*
             * foreach (string i in l) {
             *  Console.WriteLine(i);
             * }*/

            Assert.AreEqual("Sydney", l[0]);
            Assert.AreEqual("Melbourne", l[1]);
            Assert.AreEqual("Adelaide", l[2]);
            Assert.AreEqual(3, l.Count);
        }
        public void RouteBetweenNodes_NullGraph_False()
        {
            var actual = GraphExtensions.HasRouteBetweenNodes(null, 'a', 'a');

            Assert.AreEqual(false, actual);
        }
Ejemplo n.º 17
0
 public override string ToString()
 {
     return(GraphExtensions.EdgeString <T, IEdge <T> >(this));
 }