Beispiel #1
0
        static void Main(string[] args)
        {
            //Create six node that each do nothing for at least 1 second
            var nodeA = new Vertex(() => Thread.Sleep(1000));
            var nodeB = new Vertex(() => Thread.Sleep(1000));
            var nodeC = new Vertex(() => Thread.Sleep(1000));
            var nodeD = new Vertex(() => Thread.Sleep(1000));
            var nodeE = new Vertex(() => Thread.Sleep(1000));
            var nodeF = new Vertex(() => Thread.Sleep(1000));

            //Create a DAG from the nodes
            nodeC.AddDependencies(nodeA, nodeB);
            nodeD.AddDependencies(nodeC);
            nodeE.AddDependencies(nodeC);
            nodeF.AddDependencies(nodeB);
            var graph = new DirectedAcyclicGraph(nodeA, nodeB, nodeC, nodeD, nodeE, nodeF);

            //Create a graph executive
            var graphExecutive = new GraphExecutive(graph, new ConcurrencyThrottle(8));

            graphExecutive.ExecuteAndWait();

            //Output result
            Console.WriteLine("Graph Execution Complete");
            Console.WriteLine(graphExecutive.VerticesFailed.Any() ? "\tn nodes failed" : "\tall nodes successful");

#if DEBUG
            Console.ReadLine();
#endif
        }
Beispiel #2
0
 protected DirectedAcyclicGraph<int> GrowTree()
 {
     var tree = new DirectedAcyclicGraph<int>(5);
     var branch = new DirectedAcyclicGraph<int>(3);
     branch.AppendChild(new DirectedAcyclicGraph<int>(1));
     branch.AppendChild(new DirectedAcyclicGraph<int>(2));
     tree.AppendChild(branch);
     tree.AppendChild(new DirectedAcyclicGraph<int>(7));
     return tree;
 }
Beispiel #3
0
        private static void PrintProjectTopologicalOrdering(DirectedAcyclicGraph <ProjectNode_BeforeThis> graph, string header = "project topological ordering:")
        {
            var topologicalSortResult = graph.TopologicalSort(out var topologicalOrdering) ? "Success" : "Failed";

            Console.WriteLine($"{header}: {topologicalSortResult}");

            foreach (var project in topologicalOrdering)
            {
                Console.WriteLine($"  #{project.ProjectInfo.Id}");
            }

            Console.WriteLine();
        }
Beispiel #4
0
        private static void PrintReachableProjects(DirectedAcyclicGraph <ProjectNode_BeforeThis> graph, string header = "reachable projects:")
        {
            var reachableCalculationResult = graph.GetReachableNodes(out var reachables) ? "Success" : "Failed";

            Console.WriteLine($"{header}: {reachableCalculationResult}");

            foreach (var pair in reachables)
            {
                var reachableNodes = string.Join(", ", pair.Value.Select(node => $"#{node.ProjectInfo.Id}"));
                Console.WriteLine($"  #{pair.Key.ProjectInfo.Id}: {reachableNodes}");
            }

            Console.WriteLine();
        }
Beispiel #5
0
        private static void PrintProjectNodes(Component.Build build)
        {
            PrintBuild(build, printErrors: true, printWarnings: true);

            var projectGraph = new DirectedAcyclicGraph <ProjectNode_BeforeThis>(build.ProjectsById.Values.Select(project => project.Node_BeforeThis));

            PrintProjectGraph(projectGraph);
            PrintProjectTopologicalOrdering(projectGraph);
            PrintReachableProjects(projectGraph);
            PrintReversedProjectGraph(projectGraph);

            // PrintAllLogs(build.AllMessages, "all messages:");
            PrintAllLogs(build.AllWarnings, "all warnings:");
            PrintAllLogs(build.AllErrors, "all errors:");
        }
Beispiel #6
0
        private static void PrintProjectGraph(DirectedAcyclicGraph <ProjectNode_BeforeThis> graph, string header = "project graph:")
        {
            Console.WriteLine(header);

            Console.WriteLine("  graph TD");

            foreach (var node in graph.Nodes)
            {
                foreach (var adjacentNode in node.AdjacentNodes)
                {
                    Console.WriteLine($"  {node.ProjectInfo.Id} --> {adjacentNode.ProjectInfo.Id}");
                }
            }

            Console.WriteLine();
        }
        public void SimpleGraphAggregates()
        {
            var node0 = new Vertex(() => Thread.Sleep(1));
            var node1 = new Vertex(() => Thread.Sleep(1));
            var node2 = new Vertex(() => Thread.Sleep(1));

            node2.AddDependencies(node1);
            node1.AddDependencies(node0);

            var graph = new DirectedAcyclicGraph(node0, node1, node2);

            Assert.AreEqual(3, graph.AllVertices.Count());
            Assert.AreEqual(1, graph.RootVertices.Count());
            Assert.AreEqual(1, graph.TerminalVertices.Count());

            Assert.AreEqual(node0, graph.RootVertices.First());
            Assert.AreEqual(node2, graph.TerminalVertices.First());
        }
Beispiel #8
0
        public DirectedAcyclicGraph <belief> SaveBeliefNetworkToCharacter(string json, character c)
        {
            var dg           = new DirectedGraph(json);
            var dag_subgraph = new DirectedAcyclicGraph <belief>(dg);

            foreach (var n in dag_subgraph.ToList())
            {
                n.Value.variable  = n.Key;
                n.Value.character = c;
                n.Value.partition = this.Partition;
            }

            foreach (var n in dag_subgraph.Roots)
            {
                this.CreateOrRetrieve(this.Beliefs, x => x.variable == n.variable && x.character.id == n.character.id && x.partition == n.partition, n);
            }
            return(dag_subgraph);
        }
Beispiel #9
0
        public void Populate(params Assembly[] dataFixtureAssemblies)
        {
            var fixtureContainer = new FixtureContainer();

            foreach (var dataFixtureType in dataFixtureAssemblies.SelectMany(dataFixtureAssembly => dataFixtureAssembly.GetTypes().Where(t => typeof(DataFixture).IsAssignableFrom(t))))
            {
                fixtureContainer.AddDataFixture((DataFixture)Activator.CreateInstance(dataFixtureType));
            }

            var fixtureVertices = fixtureContainer.All.ToDictionary(fixture => fixture.GetType(), fixture => new Vertex(() =>
            {
                using (var session = new Session(_sessionFactory.OpenSession()))
                    using (var transaction = session.BeginTransaction())
                    {
                        foreach (var entity in fixture.GetEntities(fixtureContainer))
                        {
                            session.Save(entity);
                        }

                        transaction.Commit();
                    }
            }));

            foreach (var dataFixtureType in fixtureVertices.Keys)
            {
                var dataFixture = fixtureContainer.Get(dataFixtureType);
                var vertex      = fixtureVertices[dataFixtureType];

                vertex.AddDependencies(dataFixture.Dependencies.Select(d => fixtureVertices[d]).ToArray());
            }

            var graph          = new DirectedAcyclicGraph(fixtureVertices.Values);
            var graphExecutive = new ConcurrentGraphExecutive(graph);

            graphExecutive.ExecuteAndWait();

            if (graphExecutive.VerticesFailed.Any())
            {
                throw new AggregateException(graphExecutive.VerticesFailed.Select(e => e.Value));
            }
        }
Beispiel #10
0
 private void Assert_AreDAGs(DirectedGraphContext[] directed_graphs_in_json, bool invert = false)
 {
     foreach (var expected in directed_graphs_in_json)
     {
         var dag = new DirectedAcyclicGraph <Node>(expected.Json);
         expected.Roots.Assert_NoDifferences(dag.Template.Roots);
         expected.Sinks.Assert_NoDifferences(dag.Template.Sinks);
         Assert.IsTrue(dag.Template.IsDirectedAcyclicGraph ^ invert);
         if (!string.IsNullOrEmpty(expected.NodeRelations))
         {
             var nodes = new NodeDictionary(expected.NodeRelations);
             foreach (var n in nodes.ToArray())
             {
                 dag[n.Key]._children.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["children"]);
                 dag[n.Key]._parents.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["parents"]);
                 dag.Remove(n.Key);
             }
             Assert.IsTrue(dag.Count == 0);
         }
     }
 }
Beispiel #11
0
        private IChainStep <T> ComputeChainSequence()
        {
            DirectedAcyclicGraph <IChainStep <T> > graph = new DirectedAcyclicGraph <IChainStep <T> >();

            foreach (var step in _steps.Values)
            {
                graph.AddNode(new GraphNode <IChainStep <T> >(step.Name, step));
            }

            foreach (var step in _steps.Values)
            {
                GraphNode <IChainStep <T> > node = graph.GetNode(step.Name);

                foreach (var dependency in step.Dependencies)
                {
                    if (dependency.MustExist)
                    {
                        Asserter.Assert(
                            _steps.ContainsKey(dependency.Name),
                            string.Format(
                                "Cannot execute chain '{0}' because step '{1}' has a mandatory dependency on step '{2}' and '{2}' cannot be found in the {0} chain.",
                                _name, step.Name, dependency.Name));
                    }

                    var dependentNode = graph.GetNode(dependency.Name);
                    node.AddDependent(dependentNode);
                }
            }

            var ordered = graph.ComputeDependencyOrderedList().Select(node => node.Item).ToArray();

            for (int i = ordered.Length - 2; i > 0; i--)
            {
                ordered[i].Successor = ordered[i + 1];
            }

            return(ordered[0]);
        }
        public void Populate(params Assembly[] dataFixtureAssemblies)
        {
            var fixtureContainer = new FixtureContainer();

            foreach (var dataFixtureType in dataFixtureAssemblies.SelectMany(dataFixtureAssembly => dataFixtureAssembly.GetTypes().Where(t => typeof (DataFixture).IsAssignableFrom(t))))
                fixtureContainer.AddDataFixture((DataFixture) Activator.CreateInstance(dataFixtureType));

            var fixtureVertices = fixtureContainer.All.ToDictionary(fixture => fixture.GetType(), fixture => new Vertex(() =>
            {
                using (var session = new Session(_sessionFactory.OpenSession()))
                using (var transaction = session.BeginTransaction())
                {
                    foreach (var entity in fixture.GetEntities(fixtureContainer))
                        session.Save(entity);

                    transaction.Commit();
                }
            }));

            foreach (var dataFixtureType in fixtureVertices.Keys)
            {
                var dataFixture = fixtureContainer.Get(dataFixtureType);
                var vertex = fixtureVertices[dataFixtureType];

                vertex.AddDependencies(dataFixture.Dependencies.Select(d => fixtureVertices[d]).ToArray());
            }

            var graph = new DirectedAcyclicGraph(fixtureVertices.Values);
            var graphExecutive = new ConcurrentGraphExecutive(graph);

            graphExecutive.ExecuteAndWait();

            if (graphExecutive.VerticesFailed.Any())
                throw new AggregateException(graphExecutive.VerticesFailed.Select(e=>e.Value));
        }
Beispiel #13
0
 private static void PrintReversedProjectGraph(DirectedAcyclicGraph <ProjectNode_BeforeThis> graph, string header = "reversed project graph:")
 {
     PrintProjectGraph(graph.Reverse(), header);
 }
Beispiel #14
0
 public Passcode()
 {
     graph = new DirectedAcyclicGraph <int>();
 }