A graph of processes and resources from which a topological sort can be extracted
        public void BasicResourceResolution()
        {
            DependencyGraph g = new DependencyGraph();

            Resource res = new Resource(g, "resource");

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b = new OrderedProcess(g, "B");
            OrderedProcess c = new OrderedProcess(g, "C");

            a.Before(b);
            a.Before(c);

            b.Requires(res);
            c.Requires(res);

            IEnumerable<IEnumerable<OrderedProcess>> s = g.CalculateSort();

            Assert.AreEqual(3, s.Count());

            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.AreEqual(a, s.Skip(0).First().First());

            Assert.AreEqual(1, s.Skip(1).First().Count());
            Assert.IsTrue(s.Skip(1).First().First() == b || s.Skip(1).First().First() == c);

            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.IsTrue(s.Skip(2).First().First() == b || s.Skip(2).First().First() == c);

            Assert.AreNotEqual(s.Skip(1).First().First(), s.Skip(2).First().First());
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OrderedProcess"/> class.
        /// </summary>
        /// <param name="graph">The graph which this process is part of</param>
        /// <param name="name">The name of this process</param>
        public OrderedProcess(DependencyGraph graph, string name)
        {
            Graph = graph;
            Name = name;

            Graph.Add(this);
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Resource"/> class.
        /// </summary>
        /// <param name="graph">The graph which this ResourceClass is part of</param>
        /// <param name="name">The name of this resource</param>
        public Resource(DependencyGraph graph, string name)
        {
            Graph = graph;
            Name = name;

            Graph.Add(this);
        }
        public void BranchingResourceResolution()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b1 = new OrderedProcess(g, "B1");
            OrderedProcess b2 = new OrderedProcess(g, "B2");
            OrderedProcess c1 = new OrderedProcess(g, "C1");
            OrderedProcess c2 = new OrderedProcess(g, "C2");
            OrderedProcess c3 = new OrderedProcess(g, "C3");
            OrderedProcess c4 = new OrderedProcess(g, "C4");
            OrderedProcess d = new OrderedProcess(g, "D");

            a.Before(b1, b2).Before(c1, c2, c3, c4).Before(d);

            Resource resource = new Resource(g, "Resource");
            resource.UsedBy(c1, c3);

            IEnumerable<IEnumerable<OrderedProcess>> s = g.CalculateSort();

            //check that A comes first
            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.AreEqual(a, s.Skip(0).First().First());

            //check that D comes last
            Assert.AreEqual(1, s.Skip(4).First().Count());
            Assert.AreEqual(d, s.Skip(4).First().First());

            //check that no set contains both c1 and c3
            Assert.AreEqual(0, s.Where(set => set.Contains(c1) && set.Contains(c3)).Count());
        }
        public void BasicOrderBefore()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b = new OrderedProcess(g, "B");
            OrderedProcess c = new OrderedProcess(g, "C");

            a.Before(b).Before(c);

            IEnumerable<IEnumerable<OrderedProcess>> s = g.CalculateSort();

            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.AreEqual(a, s.Skip(0).First().First());

            Assert.AreEqual(1, s.Skip(1).First().Count());
            Assert.AreEqual(b, s.Skip(1).First().First());

            Assert.AreEqual(1, s.Skip(2).First().Count());
            Assert.AreEqual(c, s.Skip(2).First().First());
        }
        public void BasicBranching()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b1 = new OrderedProcess(g, "B1");
            OrderedProcess b2 = new OrderedProcess(g, "B2");
            OrderedProcess c = new OrderedProcess(g, "C");

            a.Before(b1, b2).Before(c);

            IEnumerable<IEnumerable<OrderedProcess>> s = g.CalculateSort();

            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.AreEqual(a, s.Skip(0).First().First());

            Assert.AreEqual(2, s.Skip(1).First().Count());
            Assert.IsTrue(s.Skip(1).First().Contains(b1));
            Assert.IsTrue(s.Skip(1).First().Contains(b2));

            Assert.AreEqual(1, s.Skip(2).First().Count());
            Assert.AreEqual(c, s.Skip(2).First().First());
        }
示例#7
0
        public virtual void TopologicalSort()
        {
            var graph = new TopologicalSorting.DependencyGraph();

            foreach (var t in this.Types)
            {
                var finder = new DependencyFinderVisitor(this, t);
                t.TypeDeclaration.AcceptVisitor(finder);

                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                if (tProcess == null)
                {
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                }

                if (finder.Dependencies.Count > 0)
                {
                    foreach (var dependency in finder.Dependencies)
                    {
                        if (tProcess.Predecessors.All(p => p.Name != dependency.Type.ReflectionName))
                        {
                            var dProcess = graph.Processes.FirstOrDefault(p => p.Name == dependency.Type.ReflectionName);
                            if (dProcess == null)
                            {
                                dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.ReflectionName);
                            }
                            //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                            if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                            {
                                tProcess.After(dProcess);
                            }
                        }
                    }
                }
            }

            if (graph.ProcessCount > 0)
            {
                ITypeInfo tInfo = null;

                try
                {
                    IEnumerable <IEnumerable <OrderedProcess> > sorted = graph.CalculateSort();

                    var list = new List <ITypeInfo>(this.Types.Count);
                    foreach (var processes in sorted)
                    {
                        foreach (var process in processes)
                        {
                            tInfo = this.Types.First(ti => ti.Type.ReflectionName == process.Name);

                            if (list.All(t => t.Type.ReflectionName != tInfo.Type.ReflectionName))
                            {
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Types.Clear();
                    this.Types.AddRange(list);
                }
                catch (System.Exception ex)
                {
                    this.LogWarning(string.Format("Topological sort failed {0} with error {1}", tInfo != null ? "at type " + tInfo.Type.ReflectionName : string.Empty, ex));
                }
            }
        }
示例#8
0
        public virtual void TopologicalSort()
        {
            this.Log.Trace("Topological sorting...");

            var graph = new TopologicalSorting.DependencyGraph();

            this.Log.Trace("\tTopological sorting first iteration...");

            var hitCounters = new long[7];

            foreach (var t in this.Types)
            {
                hitCounters[0]++;
                var parents = this.GetParents(t.Type);

                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                if (tProcess == null)
                {
                    hitCounters[1]++;
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    hitCounters[2]++;
                    var x = parents[i];

                    if (tProcess.Predecessors.All(p => p.Name != x.Type.ReflectionName))
                    {
                        hitCounters[3]++;

                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == x.Type.ReflectionName);
                        if (dProcess == null)
                        {
                            hitCounters[4]++;
                            dProcess = new TopologicalSorting.OrderedProcess(graph, x.Type.ReflectionName);
                        }
                        //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                        if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            hitCounters[4]++;
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            for (int i = 0; i < hitCounters.Length; i++)
            {
                this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
            }

            this.Log.Trace("\tTopological sorting first iteration done");

            /*this.Log.Trace("\tTopological sorting second iteration...");
             *
             * System.Array.Clear(hitCounters, 0, hitCounters.Length);
             *
             * foreach (var t in this.Types)
             * {
             *  hitCounters[0]++;
             *
             *  var finder = new DependencyFinderVisitor(this, t);
             *  t.TypeDeclaration.AcceptVisitor(finder);
             *
             *  var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
             *  if (tProcess == null)
             *  {
             *      hitCounters[1]++;
             *      tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
             *  }
             *
             *  if (finder.Dependencies.Count > 0)
             *  {
             *      hitCounters[2]++;
             *
             *      foreach (var dependency in finder.Dependencies)
             *      {
             *          hitCounters[3]++;
             *
             *          if (tProcess.Predecessors.All(p => p.Name != dependency.Type.ReflectionName))
             *          {
             *              hitCounters[4]++;
             *
             *              var dProcess = graph.Processes.FirstOrDefault(p => p.Name == dependency.Type.ReflectionName);
             *              if (dProcess == null)
             *              {
             *                  hitCounters[5]++;
             *                  dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.ReflectionName);
             *              }
             *              //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);
             *
             *              if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
             *              {
             *                  hitCounters[6]++;
             *                  tProcess.After(dProcess);
             *              }
             *          }
             *      }
             *  }
             * }
             *
             * for (int i = 0; i < hitCounters.Length; i++)
             * {
             *  this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
             * }
             *
             * this.Log.Trace("\t\tgraph.ProcessCount = " + graph.ProcessCount);
             *
             * this.Log.Trace("\tTopological sorting second iteration done");*/

            if (graph.ProcessCount > 0)
            {
                ITypeInfo tInfo = null;

                try
                {
                    this.Log.Trace("\tTopological sorting third iteration...");

                    System.Array.Clear(hitCounters, 0, hitCounters.Length);

                    this.Log.Trace("\t\tCalculate sorting...");
                    IEnumerable <IEnumerable <OrderedProcess> > sorted = graph.CalculateSort();
                    this.Log.Trace("\t\tCalculate sorting done");

                    var list = new List <ITypeInfo>(this.Types.Count);
                    foreach (var processes in sorted)
                    {
                        hitCounters[0]++;

                        foreach (var process in processes)
                        {
                            hitCounters[1]++;

                            tInfo = this.Types.First(ti => ti.Type.ReflectionName == process.Name);

                            if (list.All(t => t.Type.ReflectionName != tInfo.Type.ReflectionName))
                            {
                                hitCounters[2]++;
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Types.Clear();
                    this.Types.AddRange(list);

                    for (int i = 0; i < hitCounters.Length; i++)
                    {
                        this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
                    }

                    this.Log.Trace("\tTopological sorting third iteration done");
                }
                catch (System.Exception ex)
                {
                    this.LogWarning(string.Format("Topological sort failed {0} with error {1}", tInfo != null ? "at type " + tInfo.Type.ReflectionName : string.Empty, ex));
                }
            }

            this.Log.Trace("Topological sorting done");
        }
        public void Unorderable()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b = new OrderedProcess(g, "B");

            a.Before(b);
            b.Before(a);

            g.CalculateSort();
        }
        public virtual void TopologicalSort()
        {
            var graph = new TopologicalSorting.DependencyGraph();

            foreach (var t in this.Types)
            {
                var finder = new DependencyFinderVisitor(this, t);
                t.TypeDeclaration.AcceptVisitor(finder);

                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                if (tProcess == null)
                {
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                }

                if (finder.Dependencies.Count > 0)
                {
                    foreach (var dependency in finder.Dependencies)
                    {
                        if (tProcess.Predecessors.All(p => p.Name != dependency.Type.ReflectionName))
                        {
                            var dProcess = graph.Processes.FirstOrDefault(p => p.Name == dependency.Type.ReflectionName);
                            if (dProcess == null)
                            {
                                dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.ReflectionName);
                            }
                            //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                            if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                            {
                                tProcess.After(dProcess);
                            }
                        }
                    }
                }
            }

            foreach (var t in this.Types)
            {
                for (int i = this.Types.Count - 1; i > -1; i--)
                {
                    var x = this.Types[i];

                    var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                    if (tProcess == null)
                    {
                        tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                    }

                    if (this.IsInheritedFrom(t, x))
                    {
                        if (tProcess.Predecessors.All(p => p.Name != x.Type.ReflectionName))
                        {
                            var dProcess = graph.Processes.FirstOrDefault(p => p.Name == x.Type.ReflectionName);
                            if (dProcess == null)
                            {
                                dProcess = new TopologicalSorting.OrderedProcess(graph, x.Type.ReflectionName);
                            }
                            //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                            if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                            {
                                tProcess.After(dProcess);
                            }
                        }
                    }
                }
            }

            if (graph.ProcessCount > 0)
            {
                ITypeInfo tInfo = null;

                try
                {
                    IEnumerable<IEnumerable<OrderedProcess>> sorted = graph.CalculateSort();

                    var list = new List<ITypeInfo>(this.Types.Count);
                    foreach (var processes in sorted)
                    {
                        foreach (var process in processes)
                        {
                            tInfo = this.Types.First(ti => ti.Type.ReflectionName == process.Name);

                            if (list.All(t => t.Type.ReflectionName != tInfo.Type.ReflectionName))
                            {
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Types.Clear();
                    this.Types.AddRange(list);
                }
                catch (System.Exception ex)
                {
                    this.LogWarning(string.Format("Topological sort failed {0} with error {1}", tInfo != null ? "at type " + tInfo.Type.ReflectionName : string.Empty, ex));
                }
            }
        }
示例#11
0
        public void BranchingUnorderable()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b1 = new OrderedProcess(g, "B1");
            OrderedProcess b2 = new OrderedProcess(g, "B2");
            OrderedProcess c1 = new OrderedProcess(g, "C1");
            OrderedProcess c2 = new OrderedProcess(g, "C2");
            OrderedProcess c3 = new OrderedProcess(g, "C3");
            OrderedProcess c4 = new OrderedProcess(g, "C4");
            OrderedProcess d = new OrderedProcess(g, "D");

            a.Before(b1, b2).Before(c1, c2, c3, c4).Before(d).Before(b1);

            g.CalculateSort();
        }
示例#12
0
        protected virtual void SortReferences()
        {
            var graph = new TopologicalSorting.DependencyGraph();

            foreach (var t in this.References)
            {
                var parents  = this.GetParentAssemblies(t);
                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.FullName);

                if (tProcess == null)
                {
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.FullName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    var x = parents[i];

                    if (tProcess.Predecessors.All(p => p.Name != x.FullName))
                    {
                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == x.FullName);

                        if (dProcess == null)
                        {
                            dProcess = new TopologicalSorting.OrderedProcess(graph, x.FullName);
                        }

                        if (tProcess != dProcess && dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            if (graph.ProcessCount > 0)
            {
                AssemblyDefinition asmDef = null;

                try
                {
                    IEnumerable <IEnumerable <OrderedProcess> > sorted = graph.CalculateSort();

                    var list = new List <AssemblyDefinition>(this.References.Count());

                    this.Log.Trace("Sorting references...");

                    foreach (var processes in sorted)
                    {
                        foreach (var process in processes)
                        {
                            this.Log.Trace("\tHandling " + process.Name);

                            asmDef = this.References.First(r => r.FullName == process.Name);

                            if (list.All(r => r.FullName != asmDef.FullName))
                            {
                                list.Add(asmDef);
                            }
                        }
                    }

                    this.References = list;

                    this.Log.Trace("Sorting references done:");

                    for (int i = 0; i < list.Count; i++)
                    {
                        this.Log.Trace("\t" + list[i].Name);
                    }
                }
                catch (System.Exception ex)
                {
                    this.Log.Warn(string.Format("Topological sort failed {0} with error {1}", asmDef != null ? "at reference " + asmDef.FullName : string.Empty, ex));
                }
            }

            activeAssemblies = null;
        }
示例#13
0
        protected virtual void SortReferences()
        {
            var graph = new TopologicalSorting.DependencyGraph();

            foreach (var t in this.References)
            {
                var parents  = this.GetParentAssemblies(t);
                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.FullName);

                if (tProcess == null)
                {
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.FullName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    var x = parents[i];

                    if (tProcess.Predecessors.All(p => p.Name != x.FullName))
                    {
                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == x.FullName);

                        if (dProcess == null)
                        {
                            dProcess = new TopologicalSorting.OrderedProcess(graph, x.FullName);
                        }

                        if (tProcess != dProcess && dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            if (graph.ProcessCount > 0)
            {
                AssemblyDefinition asmDef = null;

                try
                {
                    var list = new List <AssemblyDefinition>(this.References.Count());

                    this.Log.Trace("Sorting references...");

                    this.Log.Trace("\t\tCalculate sorting references...");
                    //IEnumerable<IEnumerable<OrderedProcess>> sorted = graph.CalculateSort();
                    TopologicalSort sorted = graph.CalculateSort();
                    this.Log.Trace("\t\tCalculate sorting references done");

                    // The fix required for Mono 5.0.0.94
                    // It does not "understand" TopologicalSort's Enumerator in foreach
                    // foreach (var processes in sorted)
                    // The code is modified to get it "directly" and "typed"
                    var sortedISetEnumerable = sorted as IEnumerable <ISet <OrderedProcess> >;
                    this.Log.Trace("\t\tGot Enumerable<ISet<OrderedProcess>>");

                    var sortedISetEnumerator = sortedISetEnumerable.GetEnumerator();
                    this.Log.Trace("\t\tGot Enumerator<ISet<OrderedProcess>>");

                    //foreach (var processes in sorted)
                    while (sortedISetEnumerator.MoveNext())
                    {
                        var processes = sortedISetEnumerator.Current;

                        foreach (var process in processes)
                        {
                            this.Log.Trace("\tHandling " + process.Name);

                            asmDef = this.References.First(r => r.FullName == process.Name);

                            if (list.All(r => r.FullName != asmDef.FullName))
                            {
                                list.Add(asmDef);
                            }
                        }
                    }

                    this.References = list;

                    this.Log.Trace("Sorting references done:");

                    for (int i = 0; i < list.Count; i++)
                    {
                        this.Log.Trace("\t" + list[i].Name);
                    }
                }
                catch (System.Exception ex)
                {
                    this.Log.Warn(string.Format("Topological sort failed {0} with error {1}", asmDef != null ? "at reference " + asmDef.FullName : string.Empty, ex));
                }
            }

            activeAssemblies = null;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TopologicalSort"/> class.
 /// </summary>
 /// <param name="g">The graph to fill this sort with</param>
 public TopologicalSort(DependencyGraph g)
     : this()
 {
     g.CalculateSort(this);
 }
        public virtual void TopologicalSort()
        {
            this.Log.Trace("Topological sorting...");

            var graph = new TopologicalSorting.DependencyGraph();

            this.Log.Trace("\tTopological sorting first iteration...");

            var hitCounters = new long[7];

            foreach (var t in this.Types)
            {
                hitCounters[0]++;
                var parents = this.GetParents(t.Type);

                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                if (tProcess == null)
                {
                    hitCounters[1]++;
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    hitCounters[2]++;
                    var x = parents[i];

                    if (tProcess.Predecessors.All(p => p.Name != x.Type.ReflectionName))
                    {
                        hitCounters[3]++;

                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == x.Type.ReflectionName);
                        if (dProcess == null)
                        {
                            hitCounters[4]++;
                            dProcess = new TopologicalSorting.OrderedProcess(graph, x.Type.ReflectionName);
                        }
                        //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                        if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            hitCounters[4]++;
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            for (int i = 0; i < hitCounters.Length; i++)
            {
                this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
            }

            this.Log.Trace("\tTopological sorting first iteration done");

            /*this.Log.Trace("\tTopological sorting second iteration...");

            System.Array.Clear(hitCounters, 0, hitCounters.Length);

            foreach (var t in this.Types)
            {
                hitCounters[0]++;

                var finder = new DependencyFinderVisitor(this, t);
                t.TypeDeclaration.AcceptVisitor(finder);

                var tProcess = graph.Processes.FirstOrDefault(p => p.Name == t.Type.ReflectionName);
                if (tProcess == null)
                {
                    hitCounters[1]++;
                    tProcess = new TopologicalSorting.OrderedProcess(graph, t.Type.ReflectionName);
                }

                if (finder.Dependencies.Count > 0)
                {
                    hitCounters[2]++;

                    foreach (var dependency in finder.Dependencies)
                    {
                        hitCounters[3]++;

                        if (tProcess.Predecessors.All(p => p.Name != dependency.Type.ReflectionName))
                        {
                            hitCounters[4]++;

                            var dProcess = graph.Processes.FirstOrDefault(p => p.Name == dependency.Type.ReflectionName);
                            if (dProcess == null)
                            {
                                hitCounters[5]++;
                                dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.ReflectionName);
                            }
                            //var dProcess = new TopologicalSorting.OrderedProcess(graph, dependency.Type.FullName);

                            if (dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                            {
                                hitCounters[6]++;
                                tProcess.After(dProcess);
                            }
                        }
                    }
                }
            }

            for (int i = 0; i < hitCounters.Length; i++)
            {
                this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
            }

            this.Log.Trace("\t\tgraph.ProcessCount = " + graph.ProcessCount);

            this.Log.Trace("\tTopological sorting second iteration done");*/

            if (graph.ProcessCount > 0)
            {
                ITypeInfo tInfo = null;

                try
                {
                    this.Log.Trace("\tTopological sorting third iteration...");

                    System.Array.Clear(hitCounters, 0, hitCounters.Length);

                    this.Log.Trace("\t\tCalculate sorting...");
                    IEnumerable<IEnumerable<OrderedProcess>> sorted = graph.CalculateSort();
                    this.Log.Trace("\t\tCalculate sorting done");

                    var list = new List<ITypeInfo>(this.Types.Count);
                    foreach (var processes in sorted)
                    {
                        hitCounters[0]++;

                        foreach (var process in processes)
                        {
                            hitCounters[1]++;

                            tInfo = this.Types.First(ti => ti.Type.ReflectionName == process.Name);

                            if (list.All(t => t.Type.ReflectionName != tInfo.Type.ReflectionName))
                            {
                                hitCounters[2]++;
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Types.Clear();
                    this.Types.AddRange(list);

                    for (int i = 0; i < hitCounters.Length; i++)
                    {
                        this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
                    }

                    this.Log.Trace("\tTopological sorting third iteration done");
                }
                catch (System.Exception ex)
                {
                    this.LogWarning(string.Format("Topological sort failed {0} with error {1}", tInfo != null ? "at type " + tInfo.Type.ReflectionName : string.Empty, ex));
                }
            }

            this.Log.Trace("Topological sorting done");
        }
示例#16
0
        public void ComplexBranching()
        {
            DependencyGraph g = new DependencyGraph();

            OrderedProcess a = new OrderedProcess(g, "A");
            OrderedProcess b1 = new OrderedProcess(g, "B1");
            OrderedProcess b2 = new OrderedProcess(g, "B2");
            OrderedProcess c1 = new OrderedProcess(g, "C1");
            OrderedProcess c2 = new OrderedProcess(g, "C2");
            OrderedProcess c3 = new OrderedProcess(g, "C3");
            OrderedProcess c4 = new OrderedProcess(g, "C4");
            OrderedProcess d = new OrderedProcess(g, "D");

            a.Before(b1, b2).Before(c1, c2, c3, c4).Before(d);

            IEnumerable<IEnumerable<OrderedProcess>> s = g.CalculateSort();

            Assert.AreEqual(1, s.Skip(0).First().Count());
            Assert.AreEqual(a, s.Skip(0).First().First());

            Assert.AreEqual(2, s.Skip(1).First().Count());
            Assert.IsTrue(s.Skip(1).First().Contains(b1));
            Assert.IsTrue(s.Skip(1).First().Contains(b2));

            Assert.AreEqual(4, s.Skip(2).First().Count());
            Assert.IsTrue(s.Skip(2).First().Contains(c1));
            Assert.IsTrue(s.Skip(2).First().Contains(c2));
            Assert.IsTrue(s.Skip(2).First().Contains(c3));
            Assert.IsTrue(s.Skip(2).First().Contains(c4));

            Assert.AreEqual(1, s.Skip(3).First().Count());
            Assert.AreEqual(d, s.Skip(3).First().First());
        }
示例#17
0
        public virtual void TopologicalSort()
        {
            this.Log.Trace("Topological sorting...");

            var graph = new TopologicalSorting.DependencyGraph();

            this.Log.Trace("\tTopological sorting first iteration...");

            var hitCounters = new long[7];

            foreach (var t in this.Types)
            {
                hitCounters[0]++;
                var parents        = this.GetParents(t.Type);
                var reflectionName = GetReflectionName(t.Type);
                var tProcess       = graph.Processes.FirstOrDefault(p => p.Name == reflectionName);
                if (tProcess == null)
                {
                    hitCounters[1]++;
                    tProcess = new TopologicalSorting.OrderedProcess(graph, reflectionName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    hitCounters[2]++;
                    var x = parents[i];
                    reflectionName = GetReflectionName(x.Type);
                    if (tProcess.Predecessors.All(p => p.Name != reflectionName))
                    {
                        hitCounters[3]++;

                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == reflectionName);
                        if (dProcess == null)
                        {
                            hitCounters[4]++;
                            dProcess = new TopologicalSorting.OrderedProcess(graph, reflectionName);
                        }

                        if (tProcess != dProcess && dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            hitCounters[4]++;
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            for (int i = 0; i < hitCounters.Length; i++)
            {
                this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
            }

            this.Log.Trace("\tTopological sorting first iteration done");

            if (graph.ProcessCount > 0)
            {
                ITypeInfo tInfo = null;

                try
                {
                    this.Log.Trace("\tTopological sorting third iteration...");

                    System.Array.Clear(hitCounters, 0, hitCounters.Length);

                    this.Log.Trace("\t\tCalculate sorting...");
                    IEnumerable <IEnumerable <OrderedProcess> > sorted = graph.CalculateSort();
                    this.Log.Trace("\t\tCalculate sorting done");

                    var list = new List <ITypeInfo>(this.Types.Count);
                    foreach (var processes in sorted)
                    {
                        hitCounters[0]++;

                        foreach (var process in processes)
                        {
                            hitCounters[1]++;

                            tInfo = this.Types.First(ti => GetReflectionName(ti.Type) == process.Name);
                            var reflectionName = GetReflectionName(tInfo.Type);
                            if (list.All(t => GetReflectionName(t.Type) != reflectionName))
                            {
                                hitCounters[2]++;
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Types.Clear();
                    this.Types.AddRange(list);

                    for (int i = 0; i < hitCounters.Length; i++)
                    {
                        this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
                    }

                    this.Log.Trace("\tTopological sorting third iteration done");
                }
                catch (System.Exception ex)
                {
                    this.LogWarning(string.Format("Topological sort failed {0} with error {1}", tInfo != null ? "at type " + tInfo.Type.ReflectionName : string.Empty, ex));
                }
            }
            cacheParents = null;
            activeTypes  = null;
            this.Log.Trace("Topological sorting done");
        }
示例#18
0
        public virtual void TopologicalSort()
        {
            this.Log.Trace("Topological sorting...");

            var graph = new TopologicalSorting.DependencyGraph();

            this.Log.Trace("\tTopological sorting first iteration...");

            var hitCounters = new long[7];

            foreach (var t in this.Types)
            {
                hitCounters[0]++;
                var parents        = this.GetParents(t.Type);
                var reflectionName = GetReflectionName(t.Type);
                var tProcess       = graph.Processes.FirstOrDefault(p => p.Name == reflectionName);
                if (tProcess == null)
                {
                    hitCounters[1]++;
                    tProcess = new TopologicalSorting.OrderedProcess(graph, reflectionName);
                }

                for (int i = parents.Count - 1; i > -1; i--)
                {
                    hitCounters[2]++;
                    var x = parents[i];
                    reflectionName = GetReflectionName(x.Type);
                    if (tProcess.Predecessors.All(p => p.Name != reflectionName))
                    {
                        hitCounters[3]++;

                        var dProcess = graph.Processes.FirstOrDefault(p => p.Name == reflectionName);
                        if (dProcess == null)
                        {
                            hitCounters[4]++;
                            dProcess = new TopologicalSorting.OrderedProcess(graph, reflectionName);
                        }

                        if (tProcess != dProcess && dProcess.Predecessors.All(p => p.Name != tProcess.Name))
                        {
                            hitCounters[4]++;
                            tProcess.After(dProcess);
                        }
                    }
                }
            }

            for (int i = 0; i < hitCounters.Length; i++)
            {
                this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
            }

            this.Log.Trace("\tTopological sorting first iteration done");

            if (graph.ProcessCount > 0)
            {
                ITypeInfo      tInfo           = null;
                OrderedProcess handlingProcess = null;
                try
                {
                    this.Log.Trace("\tTopological sorting third iteration...");

                    System.Array.Clear(hitCounters, 0, hitCounters.Length);

                    this.Log.Trace("\t\tCalculate sorting...");
                    TopologicalSort sorted = graph.CalculateSort();
                    this.Log.Trace("\t\tCalculate sorting done");

                    this.Log.Trace("\t\tGetting Reflection names for " + this.Types.Count + " types...");

                    var list = new List <ITypeInfo>(this.Types.Count);
                    // The fix required for Mono 5.0.0.94
                    // It does not "understand" TopologicalSort's Enumerator in foreach
                    // foreach (var processes in sorted)
                    // The code is modified to get it "directly" and "typed"
                    var sortedISetEnumerable = sorted as IEnumerable <ISet <OrderedProcess> >;
                    this.Log.Trace("\t\tGot Enumerable<ISet<OrderedProcess>>");

                    var sortedISetEnumerator = sortedISetEnumerable.GetEnumerator();
                    this.Log.Trace("\t\tGot Enumerator<ISet<OrderedProcess>>");

                    while (sortedISetEnumerator.MoveNext())
                    {
                        var processes = sortedISetEnumerator.Current;

                        hitCounters[0]++;

                        foreach (var process in processes)
                        {
                            handlingProcess = process;
                            hitCounters[1]++;

                            tInfo = this.Types.First(ti => GetReflectionName(ti.Type) == process.Name);

                            var reflectionName = GetReflectionName(tInfo.Type);

                            if (list.All(t => GetReflectionName(t.Type) != reflectionName))
                            {
                                hitCounters[2]++;
                                list.Add(tInfo);
                            }
                        }
                    }

                    this.Log.Trace("\t\tGetting Reflection names done");

                    this.Types.Clear();
                    this.Types.AddRange(list);

                    for (int i = 0; i < hitCounters.Length; i++)
                    {
                        this.Log.Trace("\t\tHitCounter" + i + " = " + hitCounters[i]);
                    }

                    this.Log.Trace("\tTopological sorting third iteration done");
                }
                catch (System.Exception ex)
                {
                    this.LogWarning($"Topological sort failed {(tInfo != null || handlingProcess != null ? "at type " + (tInfo != null ? tInfo.Type.ReflectionName : handlingProcess.Name) : string.Empty)} with error {ex}");
                }
            }
            cacheParents = null;
            activeTypes  = null;
            this.Log.Trace("Topological sorting done");
        }