示例#1
0
        /// <summary>
        /// Returns a subset of a graph by extracting all nodes which names match one of the provided filters.
        /// These nodes are called direct matches.
        /// From each direct match the method walks along the graph and returns each
        /// node visited to the subset until the max
        /// </summary>
        /// <param name="origin">The graph we want to extract the subset from</param>
        /// <param name="filters">A list of filters which are used to find nodes by names</param>
        /// <param name="travelLimit">The distance the method will travel along from each direct match</param>
        /// <returns>The subset</returns>
        public Models.Graph Extract(Models.Graph origin, IReadOnlyCollection <string> filters, int travelLimit)
        {
            if (travelLimit < 1 || travelLimit > MAX_TRAVEL_LIMIT)
            {
                throw new ArgumentException($"The walking distance must be between 0 and {MAX_TRAVEL_LIMIT + 1}");
            }

            if (!origin.Final)
            {
                throw new InvalidOperationException("This method can only be performed on a graph that is set to immutable");
            }

            originalGraph = origin;
            subset        = new Models.Graph();

            _travelLimit = travelLimit;

            foreach (var node in originalGraph.Multitrees)
            {
                AddNodeIfMatch(node.Value, filters);
            }

            subset.SetImmutable();
            return(subset);
        }
示例#2
0
        public static void Main()
        {
            Console.WriteLine($"Given N nodes and E edges of a connected undirected graph. " +
                              $"The task is to do breadth first traversal of the graph.");

            Console.WriteLine($"");

            //Graph g = new Graph(4);

            //g.AddEdge(0, 1);
            //g.AddEdge(0, 2);
            //g.AddEdge(1, 2);
            //g.AddEdge(2, 0);
            //g.AddEdge(2, 3);
            //g.AddEdge(3, 3);

            //Another input for validation of logic
            Models.Graph g = new Models.Graph(7);
            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(0, 3);
            g.AddEdge(2, 4);
            g.AddEdge(2, 5);
            g.AddEdge(4, 6);
            g.AddEdge(5, 6);

            Console.WriteLine("Following is Breadth First Traversal");

            g.BFS();

            //Can also be done using stacks - lets check that out too.

            Console.ReadLine();
        }
        public static void Main()
        {
            Console.WriteLine($"Given a Directed Graph. Find any Topological Sorting of that Graph");
            int V = 6;

            //creating a graph
            Models.Graph g = new Models.Graph(V);
            g.AddEdge(5, 2);
            g.AddEdge(5, 0);
            g.AddEdge(4, 0);
            g.AddEdge(4, 1);
            g.AddEdge(2, 3);
            g.AddEdge(3, 1);

            Stack <int> stack = new Stack <int>();

            bool[] visited = new bool[V];

            for (int i = 0; i < V; i++)
            {
                if (visited[i] == false)
                {
                    g.TopologicalSortUtil(i, visited, stack);
                }
            }
            Console.ReadLine();
        }
示例#4
0
        internal MockProvider()
        {
            Graph_01 = new Models.Graph();
            Graph_02 = new Models.Graph();

            Table_01 = new Node("table_01", "table");
            Table_02 = new Node("table_02", "table");
            Table_03 = new Node("table_03", "table");
            Table_04 = new Node("table_01", "table");
            Table_05 = new Node("table_04", "table");
            Table_06 = new Node("table_05", "table");

            Database_01 = new Node("db_01", "database");
            Database_01.AddChild(Table_01);
            Database_01.AddChild(Table_02);

            Database_02 = new Node("db_02", "database");
            Database_02.AddChild(Table_03);

            Database_03 = new Node("db_01", "database");
            Database_03.AddChild(Table_04);
            Database_03.AddChild(Table_05);

            Database_04 = new Node("db_03", "database");
            Database_04.AddChild(Table_06);

            Server_01 = new Node("server_01", "sqlserver");
            Server_01.AddChild(Database_01);
            Server_01.AddChild(Database_02);

            Server_02 = new Node("server_01", "sqlserver");
            Server_02.AddChild(Database_03);
            Server_02.AddChild(Database_04);


            File_01 = new Node("file_01", "file");
            File_02 = new Node("file_01", "file");

            Report_01 = new Node("report_01", "report");
            Report_02 = new Node("report_02", "report");

            ReportService_01 = new Node("reportService_01", "ssrs");
            ReportService_01.AddChild(Report_01);
            ReportService_01.AddChild(Report_02);

            Graph_01.AddNode(Server_01);
            Graph_01.AddNode(File_01);

            Graph_02.AddNode(Server_02);
            Graph_02.AddNode(File_02);
            Graph_02.AddNode(ReportService_01);

            Graph_02.AddEdge(new Edge(Table_04, Report_01, "testEnvironment"));
            Graph_02.AddEdge(new Edge(Table_04, Report_02, "testEnvironment"));
            Graph_02.AddEdge(new Edge(Table_05, Report_02, "testEnvironment"));
            Graph_02.AddEdge(new Edge(File_02, Report_01, "testEnvironment"));
            Graph_02.AddEdge(new Edge(Table_04, Table_06, "testEnvironment"));
            Graph_02.AddEdge(new Edge(Table_06, Report_01, "testEnvironment"));
        }
        public SubsetExtractorTest()
        {
            var mockProvider = new MockProvider();

            graph = mockProvider.Graph_01;
            mockProvider.Graph_02.SetImmutable();
            graph.Merge(mockProvider.Graph_02);
            graph.SetImmutable();
        }
示例#6
0
        /// <summary>
        /// Creates benchmark graph
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PartialViewResult Graph(int id)
        {
            try
            {
                ///get property object from API
                Property          property  = ResponseReader.convertTo <Property>(emAPI.getProperty(id));
                BenchmarkProperty benchmark = ResponseReader.convertTo <BenchmarkProperty>(emAPI.getBenchmarkForProperty(id));

                ///previous year's performance
                DateTime end     = DateTime.Now;
                TimeSpan oneYear = new TimeSpan(365, 0, 0, 0);
                DateTime start   = end - oneYear;

                ///set up graph object
                Graph graph = new Models.Graph();
                graph.Legend         = "none";
                graph.vAxisFormat    = "#,###";
                graph.vAxisTitle     = "Annual kWh";
                graph.Title          = "";
                graph.Width          = 450;
                graph.Height         = 300;
                graph.chartAreaWidth = 300;

                ///create data table
                ArrayList data = new ArrayList
                {
                    new ArrayList {
                        "", "Amount"
                    },
                    new ArrayList {
                        property.Postcode, Math.Ceiling(property.AnnualkWh)
                    },
                    new ArrayList {
                        "Typical Practice", benchmark.BenchmarkkWhTypical
                    },
                    new ArrayList {
                        "Good Practice", benchmark.BenchmarkkWhGood
                    }
                };

                ///serialise string for inclusion into javascript via Graph View
                string graphData = JsonConvert.SerializeObject(data);
                graphData = graphData.Replace("\"", "'");

                graph.Data = graphData;

                return(PartialView("GraphFromArray", graph));
            }
            catch
            {
                return(PartialView("Error"));
            }
        }
        public void ShouldGetResultOfSingleGraph()
        {
            Models.Graph graph_01 = mockProvider.Graph_01;

            MergeProcessor processor = new MergeProcessor();

            processor.Queue(graph_01);

            Models.Graph result = processor.GetFinalResult().Result;

            Assert.Equal(2, result.Multitrees.Count);
        }
示例#8
0
        /// <summary>
        /// Checks if both references of an edge are contained in a given graph
        /// </summary>
        internal static bool IsValidEdge(Edge edge, Models.Graph graph)
        {
            if (!edge.Source.Valid || !edge.Target.Valid)
            {
                return(false);
            }

            bool sourceExists = false;
            bool targetExists = false;

            IsValidEdge(graph.Multitrees, ref sourceExists, ref targetExists, edge);

            return(sourceExists && targetExists);
        }
        public void ShouldMergeMultipleGraphs()
        {
            Models.Graph graph_01 = mockProvider.Graph_01;
            Models.Graph graph_02 = mockProvider.Graph_02;
            Models.Graph graph_03 = new Models.Graph();

            MergeProcessor processor = new MergeProcessor();

            processor.Queue(graph_01);
            processor.Queue(graph_02);
            processor.Queue(graph_03);

            Models.Graph result = processor.GetFinalResult().Result;
        }
        public void ShouldMergeLandscapesWithMergeProcessor()
        {
            Models.Graph graph_01 = mockProvider.Graph_01;
            Models.Graph graph_02 = mockProvider.Graph_02;

            MergeProcessor processor = new MergeProcessor();

            processor.Queue(graph_01);
            processor.Queue(graph_02);

            Models.Graph result = processor.GetFinalResult().Result;

            Assert.Equal(3, result.Multitrees.Count);
            Assert.Equal(6, TestHelper.CountEdges(result.AdjacencyList));
        }
示例#11
0
        /// <summary>
        /// Ensures that not a single id of one graph appears in another
        /// </summary>
        /// <param name="graph_01">The first graph</param>
        /// <param name="graph_02">The second graph</param>
        /// <returns>The state if the two graphs have completly seperate ids</returns>
        public static bool HaveNoDuplicateIds(Models.Graph graph_01, Models.Graph graph_02)
        {
            foreach (var node_01 in graph_01.Multitrees)
            {
                foreach (var node_02 in graph_02.Multitrees)
                {
                    bool haveNoDuplicateIds = HaveNoDuplicateIds(node_01.Value, node_02.Value);
                    if (!haveNoDuplicateIds)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#12
0
        public void ShouldAddUnuniqueNodes()
        {
            Models.Graph graph = new Models.Graph();

            var select_01 = new Node("SELECT", "CTE", false);

            graph.AddNode(select_01);

            var select_02 = new Node("SELECT", "CTE", false);

            graph.AddNode(select_02);

            graph.AddEdge(new Edge(select_01, select_02, "someLabel"));

            Assert.Equal(2, graph.Multitrees.Count);
        }
示例#13
0
文件: Graph.cs 项目: lie112/ApsimX
        public static Models.Graph CreateGraphFromResource(string resourceName)
        {
            string graphXmL = ReflectionUtilities.GetResourceAsString(resourceName);

            if (graphXmL != null)
            {
                List <Exception> errors = null;
                Models.Graph     graph  = Models.Core.ApsimFile.FileFormat.ReadFromString <Models.Graph>(graphXmL, e => throw e, false);
                if (errors != null && errors.Any())
                {
                    throw errors.First();
                }
                graph.ParentAllDescendants();
                return(graph);
            }
            return(null);
        }
示例#14
0
        public static Models.Graph CreateGraphFromResource(string resourceName)
        {
            string graphXmL = ApsimNG.Properties.Resources.ResourceManager.GetString(resourceName);

            if (graphXmL != null)
            {
                List <Exception> errors = null;
                Models.Graph     graph  = Models.Core.ApsimFile.FileFormat.ReadFromString <Models.Graph>(graphXmL, out errors);
                if (errors != null && errors.Any())
                {
                    throw errors.First();
                }
                Apsim.ParentAllChildren(graph);
                return(graph);
            }
            return(null);
        }
        public void ShouldExtractSubset()
        {
            var filter  = new SubsetExtractor();
            var filters = new List <string>()
            {
                "report_01",
                "report_02",
                "non_existing",
                "table_01"
            }.AsReadOnly();

            Models.Graph subset = filter.Extract(graph, filters, 1);

            bool haveNoDuplicateIds = TestHelper.HaveNoDuplicateIds(graph, subset);

            Assert.True(haveNoDuplicateIds);
            Assert.Equal(3, subset.Multitrees.Count);
        }
示例#16
0
        public void ShouldMergeGraphs()
        {
            Models.Graph graph_01 = mockProvider.Graph_01;
            Models.Graph graph_02 = mockProvider.Graph_02;

            graph_01.Merge(graph_02);
            graph_01.SetImmutable();

            graph_01.Multitrees.TryGetValue(mockProvider.ReportService_01.GetHashCode(), out Node reportService);
            graph_01.Multitrees.TryGetValue(mockProvider.Server_01.GetHashCode(), out Node server);
            server.ChildNodes.TryGetValue(mockProvider.Database_04.GetHashCode(), out Node db);

            Assert.Equal(3, graph_01.Multitrees.Count);
            Assert.Equal(1, reportService.DimensionFactor);
            Assert.Equal(1, db.IndegreeBoxed);
            Assert.Equal(0, db.OutdegreeBoxed);
            Assert.Equal(6, TestHelper.CountEdges(graph_01.AdjacencyList));
        }
示例#17
0
        internal static bool IsValidAdjacencyList(AdjacencyList adList, Models.Graph graph)
        {
            foreach (var group in adList.Groups)
            {
                if (!NodeExist(group.Key, graph.Multitrees))
                {
                    return(false);
                }

                foreach (var entry in group.Value.Edges)
                {
                    if (!NodeExist(entry.Key, graph.Multitrees))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
示例#18
0
        public static void Main()
        {
            Console.WriteLine($"Given N nodes and E edges of a connected undirected graph. " +
                              $"The task is to do depth first traversal of the graph. Suggested approach : use recursion");

            Console.WriteLine($"");

            Models.Graph g = new Models.Graph(4);

            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(1, 2);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(3, 3);

            Console.WriteLine("Following is Depth First Traversal");

            g.DFS();

            //Can also be done using stacks - lets check that out too.

            Console.ReadLine();
        }
示例#19
0
        public void ShouldAddEdgeAfterNodeTransform()
        {
            Models.Graph graph = new Models.Graph();

            var targetServer   = new Node("server", "Sql Server");
            var targetDatabase = new Node("database", "Database");
            var target         = new Node("table", "Table");

            targetDatabase.AddChild(target);
            targetServer.AddChild(targetDatabase);
            graph.AddNode(targetServer);

            var sourceServer   = new Node("server", "Sql Server");
            var sourceDatabase = new Node("database", "Database");
            var source         = new Node("table", "Table");

            sourceDatabase.AddChild(source);
            sourceServer.AddChild(sourceDatabase);

            graph.AddNode(sourceServer);
            graph.AddEdge(new Edge(source, target, "someLabel"));

            Assert.Single(graph.Multitrees);
        }
        /// <summary>
        /// Creates benchmark graph
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public PartialViewResult Graph(int id)
        {
            try
            {
                ///get property object from API
                Property property = ResponseReader.convertTo<Property>(emAPI.getProperty(id));
                BenchmarkProperty benchmark = ResponseReader.convertTo<BenchmarkProperty>(emAPI.getBenchmarkForProperty(id));

                ///previous year's performance
                DateTime end = DateTime.Now;
                TimeSpan oneYear = new TimeSpan(365, 0, 0, 0);
                DateTime start = end - oneYear;

                ///set up graph object
                Graph graph = new Models.Graph();
                graph.Legend = "none";
                graph.vAxisFormat = "#,###";
                graph.vAxisTitle = "Annual kWh";
                graph.Title = "";
                graph.Width = 450;
                graph.Height = 300;
                graph.chartAreaWidth = 300;

                ///create data table
                ArrayList data = new ArrayList
                {
                    new ArrayList { "", "Amount"},
                    new ArrayList { property.Postcode, Math.Ceiling(property.AnnualkWh) },
                    new ArrayList { "Typical Practice", benchmark.BenchmarkkWhTypical },
                    new ArrayList { "Good Practice", benchmark.BenchmarkkWhGood }
                };

                ///serialise string for inclusion into javascript via Graph View
                string graphData = JsonConvert.SerializeObject(data);
                graphData = graphData.Replace("\"", "'");

                graph.Data = graphData;

                return PartialView("GraphFromArray", graph);
            }
            catch
            {
                return PartialView("Error");
            }
        }
        public void ShouldReturnIfQueueIsEmpty()
        {
            MergeProcessor processor = new MergeProcessor();

            Models.Graph result = processor.GetFinalResult().Result;;
        }