Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/tools/HeuristicLab.HiveDrain/HeuristicLab.HiveDrain/JobTaskOneFileDownloader.cs @ 15347

Last change on this file since 15347 was 15347, checked in by bburlacu, 5 years ago

#2829: Adapt the JobTaskOneFileDownloader to save an experiment instead of a run collection.

File size: 4.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
25using System.Threading;
26using HeuristicLab.Clients.Hive;
27using HeuristicLab.Clients.Hive.Jobs;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Optimization;
31
32namespace HeuristicLab.HiveDrain {
33  public class JobTaskOneFileDownloader {
34    public String RootLocation { get; set; }
35
36    public Job ParentJob { get; set; }
37
38    private ILog log;
39
40    private RunCollection results = new RunCollection();
41
42    private static ConcurrentTaskDownloader<ItemTask> downloader =
43        new ConcurrentTaskDownloader<ItemTask>(HeuristicLabHiveDrainApplication.MaxParallelDownloads, HeuristicLabHiveDrainApplication.MaxParallelDownloads);
44
45    private static int jobCount = 0;
46
47    private static bool endReached = false;
48
49    private ManualResetEvent allJobsFinished = new ManualResetEvent(false);
50
51    private Semaphore limitSemaphore = null;
52
53    static JobTaskOneFileDownloader() {
54      downloader.ExceptionOccured += downloader_ExceptionOccured;
55    }
56
57    static void downloader_ExceptionOccured(object sender, HeuristicLab.Common.EventArgs<Exception> e) {
58      HiveDrainMainWindow.Log.LogMessage(DateTime.Now.ToShortTimeString() + " ### Exception occured: " + e.Value);
59    }
60
61    public JobTaskOneFileDownloader(string path, Job parentJob, Semaphore sem, ILog log) {
62      RootLocation = path + ".hl";
63      ParentJob = parentJob;
64      limitSemaphore = sem;
65      this.log = log;
66    }
67
68    public void Start() {
69      results = new RunCollection();
70
71      var allTasks = HiveServiceLocator.Instance.CallHiveService(s => s.GetLightweightJobTasksWithoutStateLog(ParentJob.Id));
72      var totalJobCount = allTasks.Count();
73      var optimizers = new List<IOptimizer>();
74      var finishedCount = -1;
75      using (var downloader = new TaskDownloader(allTasks.Select(x => x.Id))) {
76        downloader.StartAsync();
77
78        while (!downloader.IsFinished) {
79          if (finishedCount != downloader.FinishedCount) {
80            finishedCount = downloader.FinishedCount;
81            log.LogMessage(string.Format("Downloading/deserializing tasks... ({0}/{1} finished)", finishedCount, totalJobCount));
82          }
83        }
84
85        IDictionary<Guid, HiveTask> allHiveTasks = downloader.Results;
86        log.LogMessage("Building hive job tree...");
87        var parentTasks = allHiveTasks.Values.Where(x => !x.Task.ParentTaskId.HasValue);
88
89        foreach (var parentTask in parentTasks) {
90          BuildHiveJobTree(parentTask, allTasks, allHiveTasks);
91
92          var optimizerTask = parentTask.ItemTask as OptimizerTask;
93
94          if (optimizerTask != null) {
95            optimizers.Add(optimizerTask.Item);
96          }
97        }
98      }
99      if (!optimizers.Any()) return;
100      IStorableContent storable;
101      if (optimizers.Count > 1) {
102        var experiment = new Experiment();
103        experiment.Optimizers.AddRange(optimizers);
104        storable = experiment;
105      } else {
106        var optimizer = optimizers.First();
107        storable = optimizer as IStorableContent;
108      }
109      if (storable != null) {
110        log.LogMessage(string.Format("Save job as {0}", RootLocation));
111        ContentManager.Save(storable, RootLocation, true);
112      } else {
113        log.LogMessage(string.Format("Could not save job, content is not storable."));
114      }
115    }
116
117    private static void BuildHiveJobTree(HiveTask parentHiveTask, IEnumerable<LightweightTask> allTasks, IDictionary<Guid, HiveTask> allHiveTasks) {
118      IEnumerable<LightweightTask> childTasks = from job in allTasks
119                                                where job.ParentTaskId.HasValue && job.ParentTaskId.Value == parentHiveTask.Task.Id
120                                                orderby job.DateCreated ascending
121                                                select job;
122      foreach (LightweightTask task in childTasks) {
123        HiveTask childHiveTask = allHiveTasks[task.Id];
124        BuildHiveJobTree(childHiveTask, allTasks, allHiveTasks);
125        parentHiveTask.AddChildHiveTask(childHiveTask);
126      }
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.