namespace HeuristicLab.BenchmarkSuite { using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor { private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip"; private const string ResourcePath = @".*\.Data\."; private const char ExampleFieldDelimiter = ','; private const char ExampleFieldQualifier = '"'; private Example[] examples; protected Example[] CloneExamples() { if (examples == null) examples = ParseData().ToArray(); return examples.Select(e => (Example)e.Clone()).ToArray(); } public abstract ProblemData CreateProblemData(); protected abstract string FileName { get; } public abstract string Name { get; } public abstract string Description { get; } protected abstract int InputArgumentCount { get; } protected abstract int OutputArgumentCount { get; } protected abstract Example ParseExample(string[] input, string[] output); private IEnumerable ParseData() { using (var file = GetType().Assembly.GetManifestResourceStream(InstanceArchiveName)) using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) { var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName); using (var reader = new StreamReader(entry.Open())) { var lines = CsvParser.Parse(reader, ExampleFieldDelimiter, ExampleFieldQualifier); foreach (var line in lines) { var input = line.Take(InputArgumentCount).ToArray(); var output = line.Skip(InputArgumentCount).ToArray(); var example = ParseExample(input, output); yield return example; } } } } protected static string GetResourceName(string fileName) { return Assembly .GetExecutingAssembly() .GetManifestResourceNames() .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success); } } }