Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive/sources/HeuristicLab.Hive/HeuristicLab.Hive.ExperimentManager/3.3/HiveClient.cs @ 4760

Last change on this file since 4760 was 4760, checked in by cneumuel, 13 years ago

#1260

  • finished renaming of Hive.Experiment to Hive.ExperimentManager
  • renamed HiveClient to HiveExperimentManager
  • restructured menuitems in optimizer
File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Core;
26using HeuristicLab.Hive.Contracts;
27using HeuristicLab.Hive.Contracts.BusinessObjects;
28using HeuristicLab.Hive.Contracts.Interfaces;
29using HeuristicLab.Hive.Contracts.ResponseObjects;
30using HeuristicLab.Common;
31
32namespace HeuristicLab.Hive.ExperimentManager {
33  [Item("Hive Experiment Manager", "Connects to Hive and lists all submitted experiments by the current user.")]
34  public class HiveExperimentManager : Item, IProgressReporter {
35    private static object locker = new object();
36    private bool currentlyUpdating;
37
38    private ILog log;
39    public ILog Log {
40      get { return log; }
41    }
42
43    private HiveExperimentList hiveExperiments;
44    public HiveExperimentList HiveExperiments {
45      get { return hiveExperiments; }
46      set {
47        if (hiveExperiments != value) {
48          DeRegisterHiveExperimentsEvents();
49          hiveExperiments = value;
50          RegisterHiveExperimentsEvent();
51          OnHiveExperimentsChanged();
52        }
53      }
54    }
55
56    private bool isProgressing;
57    public bool IsProgressing {
58      get { return isProgressing; }
59      set {
60        if (isProgressing != value) {
61          isProgressing = value;
62          OnIsProgressingChanged();
63        }
64      }
65    }
66
67    private IProgress progress;
68    public IProgress Progress {
69      get { return progress; }
70    }
71
72    private void RegisterHiveExperimentsEvent() {
73      if (hiveExperiments != null) {
74        hiveExperiments.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
75      }
76    }
77
78    private void DeRegisterHiveExperimentsEvents() {
79      if (hiveExperiments != null) {
80        hiveExperiments.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<HiveExperiment>>(hiveExperiments_ItemsRemoved);
81      }
82    }
83
84    public HiveExperimentManager() {
85      this.log = new Log();
86    }
87    protected HiveExperimentManager(HiveExperimentManager original, Cloner cloner)
88      : base(original, cloner) {
89      this.log = cloner.Clone(original.Log);
90      this.HiveExperiments = cloner.Clone(original.HiveExperiments);
91    }
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new HiveExperimentManager(this, cloner);
94    }
95
96    public void UpdateExperimentList() {
97      this.progress = new Progress("Downloading HiveExperiments...");
98      try {
99        IsProgressing = true;
100        if (this.HiveExperiments == null) {
101          this.HiveExperiments = new HiveExperimentList();
102        }
103        using (Disposable<IClientFacade> service = ServiceLocator.Instance.ClientFacadePool.GetService()) {
104          currentlyUpdating = true;
105          ResponseObject<HiveExperimentDtoList> response = service.Obj.GetHiveExperiments();
106          progress.Status = "Populating HiveExperiment list...";
107          RefreshExperimentList(response.Obj);
108          currentlyUpdating = false;
109        }
110      }
111      catch (Exception) {
112        this.HiveExperiments = null;
113        throw;
114      }
115      finally {
116        IsProgressing = false;
117      }
118    }
119
120    private void RefreshExperimentList(HiveExperimentDtoList hiveExperiments) {
121      foreach (HiveExperimentDto hiveExperimentDto in hiveExperiments) {
122        HiveExperiment hiveExperiment = GetHiveExperiment(hiveExperimentDto.Id);
123        if (hiveExperiment == null) {
124          // not yet there, create new
125          this.HiveExperiments.Add(new HiveExperiment(hiveExperimentDto));
126        } else {
127          // update
128          hiveExperiment.UpdateFromDto(hiveExperimentDto);
129        }
130      }
131    }
132
133    private HiveExperiment GetHiveExperiment(Guid hiveExperimentId) {
134      return this.HiveExperiments.SingleOrDefault(he => he.HiveExperimentId.Equals(hiveExperimentId));
135    }
136
137    private void LogMessage(string message) {
138      // HeuristicLab.Log is not Thread-Safe, so lock on every call
139      lock (locker) {
140        log.LogMessage(message);
141      }
142    }
143
144    public event EventHandler HiveExperimentsChanged;
145    private void OnHiveExperimentsChanged() {
146      var handler = HiveExperimentsChanged;
147      if (handler != null) handler(this, EventArgs.Empty);
148    }
149
150    void hiveExperiments_ItemsRemoved(object sender, Collections.CollectionItemsChangedEventArgs<Collections.IndexedItem<HiveExperiment>> e) {
151      if (!currentlyUpdating) {
152        using (Disposable<IClientFacade> service = ServiceLocator.Instance.ClientFacadePool.GetService()) {
153          foreach (IndexedItem<HiveExperiment> item in e.Items) {
154            if (item.Value.HiveExperimentId != Guid.Empty) {
155              service.Obj.DeleteHiveExperiment(item.Value.HiveExperimentId);
156            }
157          }
158        }
159      }
160    }
161
162    public event EventHandler IsProgressingChanged;
163    private void OnIsProgressingChanged() {
164      var handler = IsProgressingChanged;
165      if (handler != null) handler(this, EventArgs.Empty);
166    }
167  }
168}
Note: See TracBrowser for help on using the repository browser.