namespace HeuristicLab.BenchmarkSuite { using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; using System.Linq; using System.Reflection; using System.Text.RegularExpressions; using HeuristicLab.BenchmarkSuite.Problems; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using Microsoft.VisualBasic.FileIO; [StorableClass] public abstract class BenchmarkSuiteDataDescriptor : NamedItem, IBenchmarkSuiteDataDescriptor { private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip"; private const string ResourcePath = @".*\.Data\."; private const string ExampleSeparator = ","; private const char ArrayValueSeparator = ' '; private static readonly char[] ArraySymbolTrim = { '[', ']' }; protected BenchmarkSuiteDataDescriptor() { } [StorableConstructor] protected BenchmarkSuiteDataDescriptor(bool deserializing) : base(deserializing) { } [Storable] private Example[] examples; public Example[] Examples { get { return this.examples ?? (this.examples = this.ParseData().ToArray()); } } public new abstract string Name { get; } protected abstract string FileName { get; } public new abstract string Description { get; } public abstract int OriginalTrainingCount { get; } public abstract int OriginalTestCount { get; } public abstract int BestResult { get; } public abstract int WorstResult { get; } public abstract int InputArgumentCount { get; } public abstract int OutputArgumentCount { get; } public int TotalArgumentCount { get { return InputArgumentCount + OutputArgumentCount; } } public abstract Example ParseExample(string[] input, string[] output); private IEnumerable ParseData() { using (var file = this.GetType().Assembly.GetManifestResourceStream(InstanceArchiveName)) using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) { var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName); using (var parser = new TextFieldParser(entry.Open())) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(ExampleSeparator); parser.TrimWhiteSpace = false; //Processing rows while (!parser.EndOfData) { var fields = parser.ReadFields(); if (fields.Length != TotalArgumentCount) throw new InvalidDataException("Number of values do not fit"); var input = fields.Take(InputArgumentCount).ToArray(); var output = fields.Skip(InputArgumentCount).ToArray(); yield return ParseExample(input, output); } } } } protected static string GetResourceName(string fileName) { return Assembly .GetExecutingAssembly() .GetManifestResourceNames() .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success); } protected static double[] ConvertDoubles(string input) { return ConvertMultiple(input, double.Parse); } protected static double ConvertDouble(string input) { return double.Parse(input); } protected static long[] ConvertIntegers(string input) { return ConvertMultiple(input, long.Parse); } protected static long ConvertInteger(string input) { return string.IsNullOrEmpty(input) ? default(long) : long.Parse(input); } protected static T[] ConvertMultiple(string input, Func converter) { return input .Trim(ArraySymbolTrim) .Split(ArrayValueSeparator) .Where(s => !string.IsNullOrWhiteSpace(s)) .Select(converter) .ToArray(); } protected static IEnumerable SplitByNewLine(string input) { return input.Split(new[] { Environment.NewLine, "\n", "\r\n" }, StringSplitOptions.None); } private static string[] trueFormats = new[] { "true", "t", "1" }; private static string[] falseFormats = new[] { "false", "f", "0" }; protected static bool ConvertBool(string input) { var str = input.ToLower(); if (trueFormats.Contains(str)) return true; if (falseFormats.Contains(str)) return false; throw new InvalidDataException(string.Format("Unable to parse {0} as boolean", str)); } public override IDeepCloneable Clone(Cloner cloner) { return this; } } }