Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16840 was 16840, checked in by fholzing, 5 years ago

#2864: Unified and replaced (broken) WebLink and used K&R Style

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      get { return "Martí, R., & Reinelt, G. (2011). The Linear Ordering Problem. Applied Mathematical Sciences. Springer Berlin Heidelberg."; }
13    }
14
15    public override Uri WebLink => new Uri("https://doi.org/10.1007/978-3-642-16729-4");
16
17    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
18      var instanceArchiveName = GetResourceName(Name + ".zip");
19      if (String.IsNullOrEmpty(instanceArchiveName)) yield break;
20
21      using (var instanceStream = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
22        foreach (var entry in instanceStream.Entries.Select(x => x.Name).OrderBy(x => x)) {
23          yield return new LOPDataDescriptor(Path.GetFileNameWithoutExtension(entry), GetDescription(), entry, null);
24        }
25      }
26    }
27
28    public override LOPData LoadData(IDataDescriptor id) {
29      var descriptor = (LOPDataDescriptor)id;
30      var instanceArchiveName = GetResourceName(Name + ".zip");
31      using (var instancesZipFile = new ZipArchive(GetType().Assembly.GetManifestResourceStream(instanceArchiveName), ZipArchiveMode.Read)) {
32        var entry = instancesZipFile.GetEntry(descriptor.InstanceIdentifier);
33
34        using (var stream = entry.Open()) {
35          var parser = new LOPParser();
36          parser.Parse(stream);
37          return Load(parser);
38        }
39      }
40    }
41
42    public override bool CanImportData {
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      get { return false; }
66    }
67
68    private string GetDescription() {
69      return "Embedded instance of plugin version " + Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyFileVersionAttribute), true).Cast<AssemblyFileVersionAttribute>().First().Version + ".";
70    }
71
72    protected virtual string GetResourceName(string fileName) {
73      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
74        .SingleOrDefault(x => Regex.Match(x, @".*\.Data\." + fileName).Success);
75    }
76  }
77}
Note: See TracBrowser for help on using the repository browser.