[7682] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.IO;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Reflection;
|
---|
| 27 | using System.Text.RegularExpressions;
|
---|
[7759] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[7682] | 29 | using ICSharpCode.SharpZipLib.Zip;
|
---|
| 30 |
|
---|
[7685] | 31 | namespace HeuristicLab.Problems.Instances.Classification {
|
---|
[7682] | 32 | public abstract class ResourceClassificationInstanceProvider : ClassificationInstanceProvider {
|
---|
| 33 |
|
---|
[7758] | 34 | protected abstract string FileName { get; }
|
---|
[7682] | 35 |
|
---|
| 36 | public override IEnumerable<IDataDescriptor> GetDataDescriptors() {
|
---|
[7758] | 37 | var solutionsArchiveName = GetResourceName(FileName + @"\.zip");
|
---|
[7682] | 38 | if (!String.IsNullOrEmpty(solutionsArchiveName)) {
|
---|
| 39 | using (var solutionsZipFile = new ZipInputStream(GetType().Assembly.GetManifestResourceStream(solutionsArchiveName))) {
|
---|
| 40 | foreach (var entry in GetZipContents(solutionsZipFile).OrderBy(x => x))
|
---|
| 41 | yield return new ResourceClassificationDataDescriptor(Path.GetFileNameWithoutExtension(entry), Description, entry);
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
[7759] | 46 | public override ClassificationProblemData LoadData(IDataDescriptor id) {
|
---|
[7682] | 47 | var descriptor = (ResourceClassificationDataDescriptor)id;
|
---|
| 48 |
|
---|
[7759] | 49 | ClassificationProblemData claData = LoadData(GetTempFileForResource(descriptor.ResourceName));
|
---|
[7682] | 50 | claData.Name = descriptor.Name;
|
---|
| 51 | claData.Description = descriptor.Description;
|
---|
| 52 |
|
---|
| 53 | return claData;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | protected virtual string GetResourceName(string fileName) {
|
---|
| 57 | return Assembly.GetExecutingAssembly().GetManifestResourceNames()
|
---|
| 58 | .Where(x => Regex.Match(x, @".*\.Data\." + fileName).Success).SingleOrDefault();
|
---|
| 59 | }
|
---|
| 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) {
|
---|
[7758] | 69 | var instanceArchiveName = GetResourceName(FileName + @"\.zip");
|
---|
[7682] | 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 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|