Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/14/12 15:21:28 (12 years ago)
Author:
sforsten
Message:

#1784: changes have been applied, according to the review comments of mkommend

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ProblemInstancesRegressionAndClassification/HeuristicLab.Problems.Instances.Classification/3.4/ResourceClassificationInstanceProvider.cs

    r7759 r7805  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Globalization;
    2425using System.IO;
    2526using System.Linq;
     
    3839      if (!String.IsNullOrEmpty(solutionsArchiveName)) {
    3940        using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
    40           foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x))
     41          IList<string> entries = new List<string>();
     42          ZipEntry curEntry;
     43          while ((curEntry = solutionsZipFile.GetNextEntry()) != null) {
     44            entries.Add(curEntry.Name);
     45          }
     46          foreach (var entry in entries.OrderBy(x => x)) {
    4147            yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry);
     48          }
    4249        }
    4350      }
    4451    }
    4552
    46     public override ClassificationProblemData LoadData(IDataDescriptor id) {
     53    public override IClassificationProblemData LoadData(IDataDescriptor id) {
    4754      var descriptor = (ResourceClassificationDataDescriptor)id;
    4855
    49       ClassificationProblemData claData = LoadData(GetTempFileForResource(descriptor.ResourceName));
    50       claData.Name = descriptor.Name;
    51       claData.Description = descriptor.Description;
     56      var instanceArchiveName = GetResourceName(FileName + @"\.zip");
     57      using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
     58        var entry = instancesZipFile.GetEntry(descriptor.ResourceName);
     59        NumberFormatInfo numberFormat;
     60        DateTimeFormatInfo dateFormat;
     61        char separator;
     62        using (Stream stream = instancesZipFile.GetInputStream(entry)) {
     63          TableFileParser.DetermineFileFormat(stream, out numberFormat, out dateFormat, out separator);
     64        }
    5265
    53       return claData;
     66        IClassificationProblemData claData;
     67        using (Stream stream = instancesZipFile.GetInputStream(entry)) {
     68          claData = LoadData(stream, numberFormat, dateFormat, separator);
     69        }
     70        claData.Name = descriptor.Name;
     71        claData.Description = descriptor.Description;
     72        return claData;
     73      }
    5474    }
    5575
     
    5878              .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
    5979    }
    60 
    61     protected IEnumerable<string> GetZipContents(ZipInputStream zipFile) {
    62       ZipEntry entry;
    63       while ((entry = zipFile.GetNextEntry()) != null) {
    64         yield return entry.Name;
    65       }
    66     }
    67 
    68     private string GetTempFileForResource(string resourceName) {
    69       var instanceArchiveName = GetResourceName(FileName + @"\.zip");
    70       using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
    71         var entry = instancesZipFile.GetEntry(resourceName);
    72         string path = Path.GetTempFileName();
    73         using (var stream = instancesZipFile.GetInputStream(entry)) {
    74           WriteStreamToTempFile(stream, path);
    75         }
    76         return path;
    77       }
    78     }
    79 
    80     private void WriteStreamToTempFile(Stream stream, string path) {
    81       using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
    82         int cnt = 0;
    83         byte[] buffer = new byte[32 * 1024];
    84         while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
    85           output.Write(buffer, 0, cnt);
    86       }
    87     }
    8880  }
    8981}
Note: See TracChangeset for help on using the changeset viewer.