Ejemplo n.º 1
0
    private static int[] getTopologicalSortOrder(List <Field> fields)
    {
        TopologicalSorter        g        = new TopologicalSorter();
        Dictionary <string, int> _indexes = new Dictionary <string, int>();

        //add vertices
        for (int i = 0; i < fields.Count; i++)
        {
            g.AddVertex((char)i);
            _indexes[fields[i].Name.ToLower()] = i;
        }

        //add edges
        for (int i = 0; i < fields.Count; i++)
        {
            if (fields[i].DependsOn != null)
            {
                for (int j = 0; j < fields[i].DependsOn.Length; j++)
                {
                    g.AddEdge(i,
                              _indexes[fields[i].DependsOn[j].ToLower()]);
                }
            }
        }
        char[] array  = g.Sort();
        int[]  result = new int[fields.Count];
        for (int i = 0; i < fields.Count; i++)
        {
            result[i] = (int)array[i];
        }
        return(result);
    }
Ejemplo n.º 2
0
        private static int[] GetTopologicalSortOrder(IList <DependencyField> fields)
        {
            var g        = new TopologicalSorter(fields.Count());
            var _indexes = new Dictionary <string, int>();

            //add vertices
            for (int i = 0; i < fields.Count(); i++)
            {
                _indexes[fields[i].Alias.ToLower()] = g.AddVertex(i);
            }

            //add edges
            for (int i = 0; i < fields.Count; i++)
            {
                if (fields[i].DependsOn != null)
                {
                    for (int j = 0; j < fields[i].DependsOn.Length; j++)
                    {
                        g.AddEdge(i,
                                  _indexes[fields[i].DependsOn[j].ToLower()]);
                    }
                }
            }

            int[] result = g.Sort();
            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Sort a collection of service types so that each service may be started after all
        /// their dependencies are started.
        /// </summary>
        /// <param name="serviceTypes">
        /// The service types.
        /// </param>
        /// <returns>
        /// The sorted service types.
        /// </returns>
        public IEnumerable <Type> Sort(IEnumerable <Type> serviceTypes)
        {
            var dependencies = serviceTypes.Select(x => new Dependency(x, FindDependencies(x)))
                               .ToArray();

            var sorter  = new TopologicalSorter(dependencies.Length);
            var indexes = new Dictionary <Type, int>();

            for (var i = 0; i < dependencies.Length; i++)
            {
                indexes[dependencies[i].Dependant] = sorter.AddVertex(i);
            }

            for (var i = 0; i < dependencies.Length; i++)
            {
                if (dependencies[i].Dependencies == null || !dependencies[i].Dependencies.Any())
                {
                    continue;
                }

                foreach (var t in dependencies[i].Dependencies)
                {
                    sorter.AddEdge(i, indexes[t]);
                }
            }

            var sortedServices = sorter.Sort()
                                 .Reverse()
                                 .Select(x => dependencies[x].Dependant)
                                 .ToList();

            return(sortedServices);
        }
Ejemplo n.º 4
0
        public void TestMethod1()
        {
            // Arrange
            int[] idx    = { 0, 1, 2, 3, 4, 5 };
            var   sorter = new TopologicalSorter(idx.Length);

            sorter.AddVertex(0);
            sorter.AddVertex(1);
            sorter.AddVertex(2);
            sorter.AddVertex(3);
            sorter.AddVertex(4);
            sorter.AddVertex(5);
            sorter.AddEdge(5, 2);
            sorter.AddEdge(5, 0);
            sorter.AddEdge(4, 0);
            sorter.AddEdge(4, 1);
            sorter.AddEdge(2, 3);
            sorter.AddEdge(3, 1);

            // Act
            var result = sorter.Sort();

            // Assert
            result.Length.Should().Be(6);
            result.Should().ContainInOrder(new[] { 5, 4, 2, 3, 1, 0 });
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Examines the applications and application versions in a product and produces a sorted list for a sequence of
        /// actions (usually installation) that takes dependencies into account.
        /// </summary>
        /// <param name="product">The product to examine.</param>
        /// <returns>The sorted list of ids.</returns>
        private IEnumerable <Guid> GetSortedApplicationIds(IMarketplaceProduct product)
        {
            var set = new List <Guid?>();

            set.AddRange(product.IncludesApps.Select(a => a.ApplicationId));
            set.AddRange(product.IncludesAppVersions.Select(a => a.VersionId));
            set.AddRange(product.IncludesApps.SelectMany(i => i.RequiredApps).Select(a => a.ApplicationId));
            set.AddRange(product.IncludesApps.SelectMany(i => i.RequiredAppVersions).Select(a => a.VersionId));
            set.AddRange(product.IncludesAppVersions.SelectMany(i => i.RequiredApps).Select(a => a.ApplicationId));
            set.AddRange(product.IncludesAppVersions.SelectMany(i => i.RequiredAppVersions).Select(a => a.VersionId));

            var ids = set.Where(s => s != null && s.Value != Guid.Empty).Select(s => s.Value).Distinct().ToList();

            var sorter  = new TopologicalSorter(ids.Count);
            var indices = new Dictionary <Guid, int>();

            // add vertices
            for (var i = 0; i < ids.Count; i++)
            {
                indices[ids[i]] = sorter.AddVertex(i);
            }

            // add edges
            foreach (var app in product.IncludesApps)
            {
                if (app.ApplicationId == null || app.ApplicationId.Value == Guid.Empty)
                {
                    continue;
                }

                AddRequiredAppEdgesToSorter(app.RequiredApps, sorter, ids, app.ApplicationId.Value);
                AddRequiredAppVersionEdgesToSorter(app.RequiredAppVersions, sorter, ids, app.ApplicationId.Value);
            }

            foreach (var appVersion in product.IncludesAppVersions)
            {
                if (appVersion.VersionId == null || appVersion.VersionId.Value == Guid.Empty)
                {
                    continue;
                }

                AddRequiredAppEdgesToSorter(appVersion.RequiredApps, sorter, ids, appVersion.VersionId.Value);
                AddRequiredAppVersionEdgesToSorter(appVersion.RequiredAppVersions, sorter, ids, appVersion.VersionId.Value);
            }

            var sorted = sorter.Sort();
            var result = sorted.Select(t => ids[t]).ToList();

            return(result);
        }
        private static int[] GetTopologicalSortOrder(IList<DependencyField> fields)
        {
            var g = new TopologicalSorter(fields.Count());
            var _indexes = new Dictionary<string, int>();

            //add vertices
            for (int i = 0; i < fields.Count(); i++)
            {
                _indexes[fields[i].Alias.ToLower()] = g.AddVertex(i);
            }

            //add edges
            for (int i = 0; i < fields.Count; i++)
            {
                if (fields[i].DependsOn != null)
                {
                    for (int j = 0; j < fields[i].DependsOn.Length; j++)
                    {
                        g.AddEdge(i,
                            _indexes[fields[i].DependsOn[j].ToLower()]);
                    }
                }
            }

            int[] result = g.Sort();
            return result;

        }