Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11074


Ignore:
Timestamp:
07/02/14 16:27:51 (10 years ago)
Author:
gkronber
Message:

#2195 minor changes to the source code including variable rename, simplification of method to strore contents of a stream in a file and added code to delete temporary files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Optimizer/3.3/StartPage.cs

    r11067 r11074  
    4242    private const string SampleNameSuffix = ".hl";
    4343
    44     private readonly Dictionary<ListViewGroup, List<string>> GroupLookup = new Dictionary<ListViewGroup, List<string>>();
    45     private readonly ListViewGroup StandardProblemsGroup = new ListViewGroup(StandardProblemsGroupName);
    46     private readonly ListViewGroup DataAnalysisGroup = new ListViewGroup(DataAnalysisGroupName);
    47     private readonly ListViewGroup ScriptsGroup = new ListViewGroup(ScriptsGroupName);
    48     private readonly ListViewGroup UncategorizedGroup = new ListViewGroup(UncategorizedGroupName);
     44    private readonly Dictionary<ListViewGroup, List<string>> groupLookup = new Dictionary<ListViewGroup, List<string>>();
     45    private readonly ListViewGroup standardProblemsGroup = new ListViewGroup(StandardProblemsGroupName);
     46    private readonly ListViewGroup dataAnalysisGroup = new ListViewGroup(DataAnalysisGroupName);
     47    private readonly ListViewGroup scriptsGroup = new ListViewGroup(ScriptsGroupName);
     48    private readonly ListViewGroup uncategorizedGroup = new ListViewGroup(UncategorizedGroupName);
    4949
    5050    private IProgress progress;
     
    6868
    6969      samplesListView.Enabled = false;
    70       samplesListView.Groups.Add(StandardProblemsGroup);
    71       samplesListView.Groups.Add(DataAnalysisGroup);
    72       samplesListView.Groups.Add(ScriptsGroup);
    73       samplesListView.Groups.Add(UncategorizedGroup);
     70      samplesListView.Groups.Add(standardProblemsGroup);
     71      samplesListView.Groups.Add(dataAnalysisGroup);
     72      samplesListView.Groups.Add(scriptsGroup);
     73      samplesListView.Groups.Add(uncategorizedGroup);
    7474      FillGroupLookup();
    7575
     
    9393        var samples = assembly.GetManifestResourceNames().Where(x => x.EndsWith(SampleNameSuffix));
    9494        int count = samples.Count();
    95         string path = Path.GetTempFileName();
    96 
    97         foreach (var entry in GroupLookup) {
     95
     96        foreach (var entry in groupLookup) {
    9897          var group = entry.Key;
    9998          var sampleList = entry.Value;
    10099          foreach (var sampleName in sampleList) {
    101100            string resourceName = SampleNamePrefix + sampleName + SampleNameSuffix;
    102             LoadSample(resourceName, assembly, path, group, count);
     101            LoadSample(resourceName, assembly, group, count);
    103102          }
    104103        }
    105104
    106         var categorizedSamples = GroupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
     105        var categorizedSamples = groupLookup.Select(x => x.Value).SelectMany(x => x).Select(x => SampleNamePrefix + x + SampleNameSuffix);
    107106        var uncategorizedSamples = samples.Except(categorizedSamples);
    108107
    109108        foreach (var resourceName in uncategorizedSamples) {
    110           LoadSample(resourceName, assembly, path, UncategorizedGroup, count);
     109          LoadSample(resourceName, assembly, uncategorizedGroup, count);
    111110        }
    112111
     
    117116    }
    118117
    119     private void LoadSample(string name, Assembly assembly, string path, ListViewGroup group, int count) {
     118    private void LoadSample(string name, Assembly assembly, ListViewGroup group, int count) {
     119      string path = Path.GetTempFileName();
    120120      try {
    121121        using (var stream = assembly.GetManifestResourceStream(name)) {
    122           WriteStreamToTempFile(stream, path);
     122          WriteStreamToTempFile(stream, path); // create a file in a temporary folder (persistence cannot load these files directly from the stream)
    123123          var item = XmlParser.Deserialize<INamedItem>(path);
    124124          OnSampleLoaded(item, group, 1.0 / count);
    125125        }
    126       } catch (Exception) { }
     126      } catch (Exception) {
     127      } finally {
     128        if (File.Exists(path)) {
     129          File.Delete(path); // make sure we remove the temporary file
     130        }
     131      }
    127132    }
    128133
    129134    private void FillGroupLookup() {
    130       var standardProblems = new List<string>(
    131         new[] { "ES_Griewank", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
     135      var standardProblems = new List<string> { "ES_Griewank", "GA_TSP", "GA_VRP", "GE_ArtificialAnt",
    132136                "IslandGA_TSP", "LS_Knapsack", "PSO_Schwefel", "RAPGA_JSSP",
    133137                "SA_Rastrigin", "SGP_SantaFe","GP_Multiplexer", "SS_VRP", "TS_TSP", "TS_VRP", "VNS_TSP"
    134         });
    135       GroupLookup[StandardProblemsGroup] = standardProblems;
    136       var dataAnalysisProblems = new List<string>(new[] { "GE_SymbReg", "GPR", "SGP_SymbClass", "SGP_SymbReg" });
    137       GroupLookup[DataAnalysisGroup] = dataAnalysisProblems;
    138       var scripts = new List<string>(new[] { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script" });
    139       GroupLookup[ScriptsGroup] = scripts;
     138        };
     139      groupLookup[standardProblemsGroup] = standardProblems;
     140      var dataAnalysisProblems = new List<string> { "GE_SymbReg", "GPR", "SGP_SymbClass", "SGP_SymbReg" };
     141      groupLookup[dataAnalysisGroup] = dataAnalysisProblems;
     142      var scripts = new List<string> { "GA_QAP_Script", "GUI_Automation_Script", "OSGA_Rastrigin_Script" };
     143      groupLookup[scriptsGroup] = scripts;
    140144    }
    141145
     
    197201    private void WriteStreamToTempFile(Stream stream, string path) {
    198202      using (FileStream output = new FileStream(path, FileMode.Create, FileAccess.Write)) {
    199         int cnt = 0;
    200         byte[] buffer = new byte[32 * 1024];
    201         while ((cnt = stream.Read(buffer, 0, buffer.Length)) != 0)
    202           output.Write(buffer, 0, cnt);
     203        stream.CopyTo(output);
    203204      }
    204205    }
Note: See TracChangeset for help on using the changeset viewer.