Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.OKB.Cockpit.Admin/AlgorithmEditor.xaml.cs @ 7005

Last change on this file since 7005 was 4311, checked in by swagner, 14 years ago

Integrated OKB clients for HL 3.3 (#1166)

File size: 7.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.MainForm.WPF;
6using System.Windows.Controls;
7using System.Windows;
8using HeuristicLab.BackgroundProcessing;
9using System.Diagnostics;
10using System.IO;
11using HeuristicLab.Persistence.Default.Xml;
12using HeuristicLab.Persistence.Core;
13using System.Collections.ObjectModel;
14using HeuristicLab.MainForm;
15using System.Data;
16using HeuristicLab.OKB.Client;
17
18namespace HeuristicLab.OKB.Cockpit.Admin {
19
20  public partial class AlgorithmEditor : UserControl, IView, IOKBCockpitItem {
21
22    public static DependencyProperty AlgorithmProperty = DependencyProperty.Register(
23      "Algorithm", typeof(OKBAdmin.Algorithm), typeof(AlgorithmEditor));
24
25    public OKBAdmin.Algorithm Algorithm {
26      get { return (OKBAdmin.Algorithm)GetValue(AlgorithmProperty); }
27      set { SetValue(AlgorithmProperty, value); }
28    }
29
30    public AlgorithmEditor() {
31      InitializeComponent();
32      Algorithm = null;
33    }
34
35    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) {
36      base.OnPropertyChanged(e);
37      if (e.Property == AlgorithmProperty)
38        UpdateAlgorithm();
39    }
40
41    private void UpdateAlgorithm() {
42      if (Algorithm == null) {
43        Clear();
44        return;
45      }
46      OnLoad(this, new RoutedEventArgs());
47    }
48
49    private void Clear() {
50      IdBox.Text = null;
51      NameBox.Text = null;
52      DescriptionBox.Text = null;
53    }
54
55    private bool isLoading = false;
56    protected void OnLoad(object sender, RoutedEventArgs args) {
57      if (isLoading || Algorithm == null) return;
58      isLoading = true;
59      OKBAdmin.Algorithm algorithm = Algorithm;
60      OperatorGraphEditorHost editor = OperatorGraphEditor;
61      var loader = new ObservableBackgroundWorker("downloading algorithm") {
62        WorkerReportsProgress = true,
63        WorkerSupportsCancellation = true,
64      };
65      loader.DoWork += (s, a) => {
66        OKBAdmin.AdminServiceClient client = ClientFactory.Create<OKBAdmin.AdminServiceClient, OKBAdmin.IAdminService>();
67        algorithm = client.GetCompleteAlgorithm(algorithm.Id);
68        client.Close();
69        loader.ReportProgress(10);
70        byte[] data = new DataClientHelper(loader, a) {
71          ReportProgress = p => loader.ReportProgress(10 + 80 * p / 100)
72        }.Load(OKBData.EntityType.Algorithm, algorithm.Id);
73        if (data != null) {
74          editor.LoadData(data, algorithm.Platform, OperatorGraphEditorHost.EditorType.Algorithm);
75          loader.ReportProgress(100);
76        }
77      };
78      loader.RunWorkerCompleted += (s, a) => {
79        if (algorithm == null) return;
80        Algorithm = algorithm;
81        IdBox.Text = Algorithm.Id.ToString();
82        NameBox.Text = Algorithm.Name;
83        DescriptionBox.Text = Algorithm.Description;
84        isLoading = false;
85      };
86      loader.RunWorkerAsync();
87    }
88
89    protected void OnSave(object sender, RoutedEventArgs args) {
90      if (Algorithm == null) return;
91      Algorithm.Name = NameBox.Text;
92      Algorithm.Description = DescriptionBox.Text;
93      OKBAdmin.Algorithm algorithm = Algorithm;
94      OperatorGraphEditorHost editor = OperatorGraphEditor;
95      var saver = new ObservableBackgroundWorker("uploading algorithm") {
96        WorkerReportsProgress = true,
97        WorkerSupportsCancellation = true,
98      };
99      saver.DoWork += (s, a) => {
100        OKBAdmin.AdminServiceClient client = ClientFactory.Create<OKBAdmin.AdminServiceClient, OKBAdmin.IAdminService>();
101        client.UpdateCompleteAlgorithm(algorithm);
102        client.Close();
103        saver.ReportProgress(10);
104        byte[] data = editor.GetAlgorithmData();
105        if (data != null) {
106          saver.ReportProgress(20);
107          new DataClientHelper(saver, a) {
108            ReportProgress = p => saver.ReportProgress(20 + 80 * p / 100)
109          }.Save(OKBData.EntityType.Algorithm, algorithm.Id, data);
110        } else {
111          saver.ReportProgress(100);
112        }
113      };
114      saver.RunWorkerAsync();
115    }
116
117    private IEnumerable<T> GetObjects<T>(DataTable table, IEnumerable<string> propertyNames) where T : class {
118      foreach (DataRow row in table.Rows) {
119        if (row["Id"] != DBNull.Value) {
120          T o = Activator.CreateInstance<T>();
121          foreach (string name in propertyNames)
122            typeof(T).GetProperty(name).SetValue(o, row[name] == DBNull.Value ? null : (object)row[name], null);
123          yield return o;
124        }
125      }
126    }
127
128    private bool ContentHasParameter(OKBAdmin.Parameter p) {
129      return Algorithm.Algorithm_Parameters.Any(ap => ap.ParameterId == p.Id);
130    }
131
132    private bool ContentHasResult(OKBAdmin.Result result) {
133      return Algorithm.Algorithm_Results.Any(ar => ar.ResultId == result.Id);
134    }
135
136    protected void OnView(object sender, EventArgs args) {
137      if (sender == ParametersButton && Algorithm != null)
138        EntityEditorSupport.ShowSelector<OKBAdmin.Parameter>(ContentHasParameter, UpdateParameters);
139      if (sender == ResultsButton && Algorithm != null)
140        EntityEditorSupport.ShowSelector<OKBAdmin.Result>(ContentHasResult, UpdateResults);
141    }
142
143    private void UpdateParameters(IEnumerable<OKBAdmin.Parameter> parameters) {
144      Algorithm.Algorithm_Parameters.Clear();
145      foreach (OKBAdmin.Parameter p in parameters) {
146        Algorithm.Algorithm_Parameters.Add(new OKBAdmin.Algorithm_Parameter() {
147          AlgorithmId = Algorithm.Id,
148          ParameterId = p.Id,
149        });
150      }
151    }
152
153    private void UpdateResults(IEnumerable<OKBAdmin.Result> results) {
154      Algorithm.Algorithm_Results.Clear();
155      foreach (OKBAdmin.Result r in results) {
156        Algorithm.Algorithm_Results.Add(new OKBAdmin.Algorithm_Result() {
157          AlgorithmId = Algorithm.Id,
158          ResultId = r.Id,
159        });
160      }
161    }
162
163    private void OnDownload(object sender, EventArgs args) {
164      PersistenceHacker.Download(OKBData.EntityType.Algorithm, Algorithm.Id);
165    }
166
167    private void OnUpload(object sender, EventArgs args) {
168      PersistenceHacker.Upload(OKBData.EntityType.Algorithm, Algorithm.Id);
169    }
170
171    #region IView Members
172
173    public string Caption {
174      get { return "Algorithm Editor"; }
175      set { throw new NotSupportedException(); }
176    }   
177
178    public event EventHandler CaptionChanged;
179
180    public event EventHandler Changed;
181
182    protected void OnChanged() {
183      EventHandler handler = CaptionChanged;
184      if (handler != null)
185        handler(this, EventArgs.Empty);
186    }
187
188    public virtual void Close() {
189      MainFormManager.GetMainForm<WPFMainFormBase>().CloseView(this);
190      IsShown = false;
191    }
192
193    public void Hide() {
194      MainFormManager.GetMainForm<WPFMainFormBase>().HideView(this);
195      IsShown = false;
196    }
197
198    public bool IsShown { get; protected set; }
199
200    private bool readOnly = false;
201    public bool ReadOnly {
202      get {
203        return readOnly;
204      }
205      set {
206        if (value == readOnly) return;
207        readOnly = value;
208        OnReadOnlyChanged();
209      }
210    }
211    public event EventHandler ReadOnlyChanged;
212    protected void OnReadOnlyChanged() {
213      EventHandler handler = ReadOnlyChanged;
214      if (handler != null)
215        handler(this, EventArgs.Empty);
216    }
217
218    public void Show() {
219      MainFormManager.GetMainForm<WPFMainFormBase>().ShowView(this);
220      IsShown = true;
221    }
222
223    #endregion   
224  }
225}
Note: See TracBrowser for help on using the repository browser.