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 |
|
---|
9 |
|
---|
10 | public abstract class BenchmarkSuiteDataDescriptor : IBenchmarkSuiteDataDescriptor {
|
---|
11 | private const string InstanceArchiveName = "HeuristicLab.BenchmarkSuite.Data.BenchmarkExamples.zip";
|
---|
12 | private const string ResourcePath = @".*\.Data\.";
|
---|
13 | private const string ExampleFieldDelimiter = ",";
|
---|
14 |
|
---|
15 | private Example[] examples;
|
---|
16 |
|
---|
17 | protected Example[] CloneExamples() {
|
---|
18 | if (examples == null) examples = ParseData().ToArray();
|
---|
19 | return examples.Select(e => (Example)e.Clone()).ToArray();
|
---|
20 | }
|
---|
21 |
|
---|
22 | public abstract ProblemData CreateProblemData();
|
---|
23 |
|
---|
24 | protected abstract string FileName { get; }
|
---|
25 | public abstract string Name { get; }
|
---|
26 | public abstract string Description { get; }
|
---|
27 | protected abstract int InputArgumentCount { get; }
|
---|
28 | protected abstract int OutputArgumentCount { get; }
|
---|
29 |
|
---|
30 | protected abstract Example ParseExample(string[] input, string[] output);
|
---|
31 |
|
---|
32 | private IEnumerable<Example> ParseData() {
|
---|
33 | using (var file = GetType().Assembly.GetManifestResourceStream(InstanceArchiveName))
|
---|
34 | using (var archive = new ZipArchive(file, ZipArchiveMode.Read)) {
|
---|
35 | var entry = archive.Entries.SingleOrDefault(x => x.Name == FileName);
|
---|
36 |
|
---|
37 | using (var reader = new StreamReader(entry.Open())) {
|
---|
38 | var lines = CsvParser.Parse(reader, ',', '"');
|
---|
39 |
|
---|
40 | foreach (var line in lines) {
|
---|
41 | var input = line.Take(InputArgumentCount).ToArray();
|
---|
42 | var output = line.Skip(InputArgumentCount).ToArray();
|
---|
43 |
|
---|
44 | var example = ParseExample(input, output);
|
---|
45 |
|
---|
46 | yield return example;
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | protected static string GetResourceName(string fileName) {
|
---|
53 | return Assembly
|
---|
54 | .GetExecutingAssembly()
|
---|
55 | .GetManifestResourceNames()
|
---|
56 | .SingleOrDefault(x => Regex.Match(x, ResourcePath + fileName).Success);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | } |
---|