Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2864_PermutationProblems/HeuristicLab.Problems.Instances.PermutationProblems/3.3/LOP/LOPInstanceProvider.cs @ 16724

Last change on this file since 16724 was 16013, checked in by fholzing, 6 years ago

#2864: Implemented a base class as the instance provider of LOPs

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.IO.Compression;
5using System.Linq;
6using System.Reflection;
7using System.Text.RegularExpressions;
8
9namespace HeuristicLab.Problems.Instances.PermutationProblems.LinearOrdering {
10  public abstract class LOPInstanceProvider : ProblemInstanceProvider<LOPData> {
11    public override string ReferencePublication
12    {
13      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"; }
14    }
15
16    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
17      var instanceArchiveName = GetResourceName(Name + ".zip");
18      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
19
20      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
21        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
22          yield return new LOPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
23        }
24      }
25    }
26
27    public override LOPData LoadData(IDataDescriptor id) {
28      var descriptor = (LOPDataDescriptor)id;
29      var instanceArchiveName = GetResourceName(Name + ".zip");
30      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
31        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
32
33        using (var stream = entry.Open()) {
34          var parser = new LOPParser();
35          parser.Parse(stream);
36          return Load(parser);
37        }
38      }
39    }
40
41    public override bool CanImportData
42    {
43      get { return true; }
44    }
45
46    public override LOPData ImportData(string path) {
47      var parser = new LOPParser();
48      parser.Parse(path);
49      return Load(parser);
50    }
51
52    private LOPData Load(LOPParser parser) {
53      var instance = new LOPData {
54        Name = parser.Name,
55        Description = parser.Description,
56        Dimension = parser.Dimension,
57        Matrix = parser.Matrix,
58        BestKnownPermutation = parser.BestKnownPermutation,
59        BestKnownQuality = parser.BestKnownQuality
60      };
61      return instance;
62    }
63
64    public override bool CanExportData
65    {
66      get { return false; }
67    }
68
69    private string GetDescription() {
70      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
71    }
72
73    protected virtual string GetResourceName(string fileName) {
74      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
75        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.