Пример #1
0
        /// <summary>
        /// Erstellt einen neuen Vergleichsalgorithmus auf Basis einer Dateibeschreibung.
        /// </summary>
        /// <param name="fileContents">Der tatsächliche Inhalt der Datei.</param>
        /// <param name="nameComparer">Der Algorithmus zum Vegleich von Gerätenamen.</param>
        /// <returns>Die gewünschte Beschreibung.</returns>
        internal static IComparer <SchedulePlan> Create(byte[] fileContents, IEqualityComparer <string> nameComparer)
        {
            // Validate
            if (fileContents == null)
            {
                throw new ArgumentNullException("fileContents");
            }

            // Result
            var resourceComparer = default(CompoundComparer <ResourcePlan>);
            var planComparer     = new CompoundComparer <SchedulePlan>();

            // Create reader
            using (var stream = new MemoryStream(fileContents, false))
                using (var reader = new StreamReader(stream, true))
                    for (string line; (line = reader.ReadLine()) != null;)
                    {
                        // Check for comment and empty line
                        var dataLength = line.IndexOf('#');
                        var data       = line.Substring(0, (dataLength < 0) ? line.Length : dataLength).Trim();
                        if (string.IsNullOrEmpty(data))
                        {
                            continue;
                        }

                        // Check operation
                        var parts = data.Split(':');
                        if (parts.Length != 2)
                        {
                            throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_BadScheduleFile, data));
                        }

                        // Check mode
                        if (resourceComparer == null)
                        {
                            // New rule to use
                            var rule = default(IComparer <SchedulePlan>);

                            // Check for supported operations
                            switch (parts[0])
                            {
                            case "StartTime": planComparer.Comparers.Add(SchedulePlan.CompareByOverlappingStart(parts[1], nameComparer)); continue;

                            case "ParallelSource": rule = SchedulePlan.CompareByParallelSourceTime; break;

                            case "ResourceCount": rule = SchedulePlan.CompareByResourceCount; break;

                            case "TotalCut": rule = SchedulePlan.CompareByTotalCut; break;

                            case "ByPriority":
                            {
                                // Create inner
                                resourceComparer = new CompoundComparer <ResourcePlan>();

                                // Check mode
                                switch (parts[1])
                                {
                                case "Ascending": rule = resourceComparer.ByPriority(false); break;

                                case "Descending": rule = resourceComparer.ByPriority(true); break;

                                default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1]));
                                }

                                // Done
                                break;
                            }
                            }

                            // Validate
                            if (rule == null)
                            {
                                throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownProperty, parts[0]));
                            }

                            // Process adaption
                            if (resourceComparer == null)
                            {
                                switch (parts[1])
                                {
                                case "Min": rule = Invert(rule); break;

                                case "Max": break;

                                default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1]));
                                }
                            }

                            // Remember
                            planComparer.Comparers.Add(rule);
                        }
                        else
                        {
                            // New rule to use
                            var rule = default(IComparer <ResourcePlan>);

                            // Check for supported operations
                            switch (parts[0])
                            {
                            case "RecordingCount": rule = ResourcePlan.CompareByRecordingCount; break;

                            case "SourceCount": rule = ResourcePlan.CompareBySourceCount; break;

                            case "ByPriority":
                            {
                                // Check mode
                                switch (parts[1])
                                {
                                case "End": resourceComparer = null; break;

                                default: throw new InvalidDataException(Properties.SchedulerResources.Exception_ResourcesNotTerminated);
                                }

                                // Done
                                break;
                            }
                            }

                            // Validate
                            if (rule != null)
                            {
                                // May invert
                                switch (parts[1])
                                {
                                case "Min": rule = Invert(rule); break;

                                case "Max": break;

                                default: throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownOrder, parts[0], parts[1]));
                                }

                                // Remember
                                resourceComparer.Comparers.Add(rule);
                            }
                            else if (resourceComparer != null)
                            {
                                throw new InvalidDataException(string.Format(Properties.SchedulerResources.Exception_UnknownProperty, parts[0]));
                            }
                        }
                    }

            // Validate
            if (resourceComparer != null)
            {
                throw new InvalidDataException(Properties.SchedulerResources.Exception_ResourcesNotTerminated);
            }

            // Report
            return(planComparer);
        }