using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; namespace HeuristicLab.Problems.Instances.PermutationProblems.LinearOrdering { public abstract class LOPInstanceProvider : ProblemInstanceProvider { public override string ReferencePublication { get { return "Martí, R., & Reinelt, G. (2011). The Linear Ordering Problem. Applied Mathematical Sciences. Springer Berlin Heidelberg. https://doi.org/10.1007/978-3-642-16729-4"; } } public override IEnumerable GetDataDescriptors() { var instanceArchiveName = GetResourceName(Name + ".zip"); if (String.IsNullOrEmpty(instanceArchiveName)) yield break; using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) { foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) { yield return new LOPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null); } } } public override LOPData LoadData(IDataDescriptor id) { var descriptor = (LOPDataDescriptor)id; var instanceArchiveName = GetResourceName(Name + ".zip"); using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) { var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier); using (var stream = entry.Open()) { var parser = new LOPParser(); parser.Parse(stream); return Load(parser); } } } public override bool CanImportData { get { return true; } } public override LOPData ImportData(string path) { var parser = new LOPParser(); parser.Parse(path); return Load(parser); } private LOPData Load(LOPParser parser) { var instance = new LOPData { Name = parser.Name, Description = parser.Description, Dimension = parser.Dimension, Matrix = parser.Matrix, BestKnownPermutation = parser.BestKnownPermutation, BestKnownQuality = parser.BestKnownQuality }; return instance; } public override bool CanExportData { get { return false; } } private string GetDescription() { return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast().First().Version + "."; } protected virtual string GetResourceName(string fileName) { return Assembly.GetExecutingAssembly().GetManifestResourceNames() .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success); } } }