Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Regression/3.4/ResourceRegressionInstanceProvider.cs @ 7666

Last change on this file since 7666 was 7666, checked in by sforsten, 12 years ago

#1784:

  • deleted obsolete project Problems.Instances.Regression.Views
  • added TrentMcConaghy and Various problem instances (zip file from TrentMcConaghy is rather big)
File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.IO;
23using System.Linq;
24using System.Reflection;
25using System.Text.RegularExpressions;
26using ICSharpCode.SharpZipLib.Zip;
27
28namespace HeuristicLab.Problems.Instances.Regression {
29  public abstract class ResourceRegressionInstanceProvider : RegressionInstanceProvider {
30
31    protected abstract string FileExtension { get; }
32
33    public override RegressionData LoadData(IDataDescriptor id) {
34      var descriptor = (ResourceRegressionDataDescriptor)id;
35
36      RegressionData regData = LoadData(GetTempFileForResource(descriptor.ResourceName));
37      regData.Name = descriptor.Name;
38      regData.Description = descriptor.Description;
39
40      return regData;
41    }
42
43    protected virtual string GetResourceName(string fileName) {
44      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
45              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
46    }
47
48    private string GetTempFileForResource(string resourceName) {
49      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
50      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
51        var entry = instancesZipFile.GetEntry(resourceName);
52        string path = Path.GetTempFileName();
53        using (var stream = instancesZipFile.GetInputStream(entry)) {
54          WriteStreamToTempFile(stream, path);
55        }
56        return path;
57      }
58    }
59
60    private void WriteStreamToTempFile(Stream stream, string path) {
61      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
62        int cnt = 0;
63        byte[] buffer = new byte[32 * 1024];
64        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
65          output.Write(buffer, 0, cnt);
66      }
67    }
68  }
69}
Note: See TracBrowser for help on using the repository browser.