Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ResourceClassificationInstanceProvider.cs @ 7685

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

#1784:

  • changed namespace of files in Problem.Instances.Classification
  • added description to iris and mammography problem instances
File size: 3.5 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;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Reflection;
27using System.Text.RegularExpressions;
28using ICSharpCode.SharpZipLib.Zip;
29
30namespace HeuristicLab.Problems.Instances.Classification {
31  public abstract class ResourceClassificationInstanceProvider : ClassificationInstanceProvider {
32
33    protected abstract string FileExtension { get; }
34
35    public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
36      var solutionsArchiveName = GetResourceName(FileExtension + @"\.zip");
37      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
38        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
39          foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x))
40            yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry);
41        }
42      }
43    }
44
45    public override ClassificationData LoadData(IDataDescriptor id) {
46      var descriptor = (ResourceClassificationDataDescriptor)id;
47
48      ClassificationData claData = LoadData(GetTempFileForResource(descriptor.ResourceName));
49      claData.Name = descriptor.Name;
50      claData.Description = descriptor.Description;
51
52      return claData;
53    }
54
55    protected virtual string GetResourceName(string fileName) {
56      return Assembly.GetExecutingAssembly().GetManifestResourceNames()
57              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
58    }
59
60    protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
61      ZipEntry entry;
62      while ((entry = zipFile.GetNextEntry()) != null) {
63        yield return entry.Name;
64      }
65    }
66
67    private string GetTempFileForResource(string resourceName) {
68      var instanceArchiveName = GetResourceName(FileExtension + @"\.zip");
69      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
70        var entry = instancesZipFile.GetEntry(resourceName);
71        string path = Path.GetTempFileName();
72        using (var stream = instancesZipFile.GetInputStream(entry)) {
73          WriteStreamToTempFile(stream, path);
74        }
75        return path;
76      }
77    }
78
79    private void WriteStreamToTempFile(Stream stream, string path) {
80      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
81        int cnt = 0;
82        byte[] buffer = new byte[32 * 1024];
83        while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
84          output.Write(buffer, 0, cnt);
85      }
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.