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