1 | namespace HeuristicLab.BenchmarkSuite {
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.IO.Compression;
|
---|
5 | using System.Linq;
|
---|
6 | using System.Reflection;
|
---|
7 | using System.Text.RegularExpressions;
|
---|
8 | using HeuristicLab.BenchmarkSuite.Problems;
|
---|
9 |
|
---|
10 | using Microsoft.VisualBasic.FileIO;
|
---|
11 |
|
---|
12 |
|
---|
13 | public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
|
---|
14 | private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
|
---|
15 | private const string ResourcePath = @".*\.Data\.";
|
---|
16 | private const string ExampleSeparator = ",";
|
---|
17 |
|
---|
18 | private Example[] examples;
|
---|
19 |
|
---|
20 | protected Example[] CloneExamples() {
|
---|
21 | if (examples == null) examples = ParseData().ToArray();
|
---|
22 | return examples.Select(e => (Example)e.Clone()).ToArray();
|
---|
23 | }
|
---|
24 |
|
---|
25 | public abstract ProblemData CreateProblemData();
|
---|
26 |
|
---|
27 | protected abstract string FileName { get; }
|
---|
28 | public abstract string Name { get; }
|
---|
29 | public abstract string Description { get; }
|
---|
30 | protected abstract int InputArgumentCount { get; }
|
---|
31 | protected abstract int OutputArgumentCount { get; }
|
---|
32 |
|
---|
33 | protected abstract Example ParseExample(string[] input, string[] output);
|
---|
34 |
|
---|
35 | private IEnumerable<Example> ParseData() {
|
---|
36 | using (var file = this.GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
|
---|
37 | using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
|
---|
38 | var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName);
|
---|
39 |
|
---|
40 | using (var parser = new TextFieldParser(entry.Open())) {
|
---|
41 | parser.TextFieldType = FieldType.Delimited;
|
---|
42 | parser.SetDelimiters(ExampleSeparator);
|
---|
43 | parser.TrimWhiteSpace = false;
|
---|
44 |
|
---|
45 | //Processing rows
|
---|
46 | while (!parser.EndOfData) {
|
---|
47 | var fields = parser.ReadFields();
|
---|
48 |
|
---|
49 | if (fields.Length != InputArgumentCount + OutputArgumentCount)
|
---|
50 | throw new InvalidDataException("Number of values do not fit");
|
---|
51 |
|
---|
52 | var input = fields.Take(InputArgumentCount).ToArray();
|
---|
53 | var output = fields.Skip(InputArgumentCount).ToArray();
|
---|
54 |
|
---|
55 | yield return ParseExample(input, output);
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected static string GetResourceName(string fileName) {
|
---|
62 | return Assembly
|
---|
63 | .GetExecutingAssembly()
|
---|
64 | .GetManifestResourceNames()
|
---|
65 | .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success);
|
---|
66 | }
|
---|
67 | }
|
---|
68 | } |
---|