#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using HeuristicLab.Problems.DataAnalysis;
using ICSharpCode.SharpZipLib.Zip;
namespace HeuristicLab.Problems.Instances.Classification {
public abstract class ResourceClassificationInstanceProvider : ClassificationInstanceProvider {
protected abstract string FileName { get; }
public override IEnumerable GetDataDescriptors() {
var solutionsArchiveName = GetResourceName(FileName + @"\.zip");
if (!String.IsNullOrEmpty(solutionsArchiveName)) {
using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x))
yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry);
}
}
}
public override ClassificationProblemData LoadData(IDataDescriptor id) {
var descriptor = (ResourceClassificationDataDescriptor)id;
ClassificationProblemData claData = LoadData(GetTempFileForResource(descriptor.ResourceName));
claData.Name = descriptor.Name;
claData.Description = descriptor.Description;
return claData;
}
protected virtual string GetResourceName(string fileName) {
return Assembly.GetExecutingAssembly().GetManifestResourceNames()
.Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
}
protected IEnumerable GetZipContents(ZipInputStream zipFile) {
ZipEntry entry;
while ((entry = zipFile.GetNextEntry()) != null) {
yield return entry.Name;
}
}
private string GetTempFileForResource(string resourceName) {
var instanceArchiveName = GetResourceName(FileName + @"\.zip");
using (var instancesZipFile = new ZipFile(GetType().Assembly.GetManifestResourceStream(instanceArchiveName))) {
var entry = instancesZipFile.GetEntry(resourceName);
string path = Path.GetTempFileName();
using (var stream = instancesZipFile.GetInputStream(entry)) {
WriteStreamToTempFile(stream, path);
}
return path;
}
}
private void WriteStreamToTempFile(Stream stream, string path) {
using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
int cnt = 0;
byte[] buffer = new byte[32 * 1024];
while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
output.Write(buffer, 0, cnt);
}
}
}
}