#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using HeuristicLab.Clients.Access;
using HeuristicLab.Core;
using HeuristicLab.MainForm;
using HeuristicLab.Optimization;
using View = HeuristicLab.MainForm.WindowsForms.View;
namespace HeuristicLab.Clients.OKB.RunCreation {
[View("OKBExperimentUpload View")]
public partial class OKBExperimentUploadView : View {
private const string AlgorithmTypeParameterName = "Algorithm MemberSelection";
private const string ProblemTypeParameterName = "Problem MemberSelection";
private const string AlgorithmNameParameterName = "Algorithm Name";
private const string ProblemNameParameterName = "Problem Name";
private List runs = new List();
private List problems = new List();
private List algorithms = new List();
Algorithm selectedAlgorithm = null;
Problem selectedProblem = null;
public OKBExperimentUploadView() {
InitializeComponent();
OKBAlgorithmColumn.ValueType = typeof(Algorithm);
OKBAlgorithmColumn.ValueMember = "Name";
OKBAlgorithmColumn.DisplayMember = "Name";
OKBProblemColumn.ValueType = typeof(Problem);
OKBProblemColumn.ValueMember = "Name";
OKBProblemColumn.DisplayMember = "Name";
RunCreationClient.Instance.Refreshing += RunCreationClient_Refreshing;
RunCreationClient.Instance.Refreshed += RunCreationClient_Refreshed;
}
private void DisposeSpecific() {
RunCreationClient.Instance.Refreshing -= RunCreationClient_Refreshing;
RunCreationClient.Instance.Refreshed -= RunCreationClient_Refreshed;
}
private bool refreshing;
protected override void SetEnabledStateOfControls() {
if (InvokeRequired) { Invoke((Action)SetEnabledStateOfControls); return; }
base.SetEnabledStateOfControls();
btnUpload.Enabled = runs.Count > 0 && !refreshing;
}
public void AddRuns(IItem item) {
if (InvokeRequired) { Invoke((Action)AddRuns, item); return; }
if (item is Experiment) {
runs.AddRange((item as Experiment).Runs);
DisplayRuns((item as Experiment).Runs);
} else if (item is RunCollection) {
runs.AddRange((item as RunCollection));
DisplayRuns((item as RunCollection));
} else if (item is IOptimizer) {
runs.AddRange((item as IOptimizer).Runs);
DisplayRuns((item as IOptimizer).Runs);
} else if (item is IRun) {
runs.Add(item as IRun);
RunCollection tmp = new RunCollection();
tmp.Add(item as IRun);
DisplayRuns(tmp);
}
SetEnabledStateOfControls();
}
private void DisplayError(Exception ex) {
PluginInfrastructure.ErrorHandling.ShowErrorDialog("An error occured while retrieving algorithm and problem information from the OKB.", ex);
}
private void DisplayRuns(RunCollection runs) {
if (RunCreationClient.Instance.Algorithms == null || RunCreationClient.Instance.Algorithms.Count() == 0) {
Action a = new Action(delegate {
RunCreationClient.Instance.Refresh();
CreateUI(runs);
});
Task.Factory.StartNew(a).ContinueWith((t) => { DisplayError(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
} else {
CreateUI(runs);
}
}
private void CreateUI(RunCollection runs) {
if (InvokeRequired) { Invoke((Action)CreateUI, runs); return; }
if (problems.Count == 0)
problems.AddRange(RunCreationClient.Instance.Problems);
if (algorithms.Count == 0)
algorithms.AddRange(RunCreationClient.Instance.Algorithms);
IItem algorithmType;
IItem problemType;
IItem algorithmName;
IItem problemName;
OKBAlgorithmColumn.DataSource = algorithms;
OKBProblemColumn.DataSource = problems;
foreach (IRun run in runs) {
int idx = dataGridView.Rows.Add(run.Name);
DataGridViewRow curRow = dataGridView.Rows[idx];
curRow.Tag = run;
HeuristicLab.Data.StringValue algStr = null, algTypeStr = null, prbStr = null, prbTypeStr = null;
if (run.Parameters.TryGetValue(AlgorithmNameParameterName, out algorithmName)) {
algStr = algorithmName as HeuristicLab.Data.StringValue;
if (algStr != null) {
curRow.Cells[AlgorithmNameColumn.Name].Value = algStr;
}
}
if (run.Parameters.TryGetValue(AlgorithmTypeParameterName, out algorithmType)) {
algTypeStr = algorithmType as HeuristicLab.Data.StringValue;
if (algTypeStr != null) {
curRow.Cells[AlgorithmTypeColumn.Name].Value = algTypeStr;
}
}
var uploadOk = false;
if (algStr != null && algTypeStr != null) {
var alg = algorithms.FirstOrDefault(x => x.DataType.Name == algTypeStr.Value && x.Name == algStr.Value);
if (alg != null) {
curRow.Cells[OKBAlgorithmColumn.Name].Value = alg.Name;
uploadOk = true;
}
}
if (run.Parameters.TryGetValue(ProblemNameParameterName, out problemName)) {
prbStr = problemName as HeuristicLab.Data.StringValue;
if (prbStr != null) {
curRow.Cells[ProblemNameColumn.Name].Value = prbStr;
}
}
if (run.Parameters.TryGetValue(ProblemTypeParameterName, out problemType)) {
prbTypeStr = problemType as HeuristicLab.Data.StringValue;
if (prbTypeStr != null) {
curRow.Cells[ProblemTypeColumn.Name].Value = prbTypeStr;
}
}
if (prbStr != null && prbTypeStr != null) {
var prb = problems.FirstOrDefault(x => x.DataType.Name == prbTypeStr.Value && x.Name == prbStr.Value);
if (prb != null) {
curRow.Cells[OKBProblemColumn.Name].Value = prb.Name;
} else uploadOk = false;
}
curRow.Cells[UploadColumn.Name].Value = uploadOk;
}
}
public void ClearRuns() {
if (InvokeRequired) { Invoke((Action)ClearRuns); return; }
dataGridView.Rows.Clear();
runs.Clear();
SetEnabledStateOfControls();
}
private void RunCreationClient_Refreshing(object sender, EventArgs e) {
if (InvokeRequired) { Invoke((Action