#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 HeuristicLab.Clients.Common;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Persistence.Default.Xml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace HeuristicLab.Clients.OKB.RunCreation {
[Item("RunCreationClient", "OKB run creation client.")]
public sealed class RunCreationClient : IContent {
private static RunCreationClient instance;
public static RunCreationClient Instance {
get {
if (instance == null) instance = new RunCreationClient();
return instance;
}
}
#region Properties
private List algorithms;
public IEnumerable Algorithms {
get { return algorithms; }
}
private List problems;
public IEnumerable Problems {
get { return problems; }
}
#endregion
private RunCreationClient() {
algorithms = new List();
problems = new List();
}
#region Refresh
public void Refresh() {
OnRefreshing();
algorithms = new List();
problems = new List();
try {
algorithms.AddRange(CallRunCreationService>(s => s.GetAlgorithms("HeuristicLab 3.3")));
problems.AddRange(CallRunCreationService>(s => s.GetProblems("HeuristicLab 3.3")));
} finally {
OnRefreshed();
}
}
public void RefreshAsync(Action exceptionCallback) {
var call = new Func(delegate() {
try {
Refresh();
} catch (Exception ex) {
return ex;
}
return null;
});
call.BeginInvoke(delegate(IAsyncResult result) {
Exception ex = call.EndInvoke(result);
if (ex != null) exceptionCallback(ex);
}, null);
}
#endregion
#region Algorithm Methods
public byte[] GetAlgorithmData(long algorithmId) {
return CallRunCreationService(s => s.GetAlgorithmData(algorithmId));
}
#endregion
#region Problem Methods
public byte[] GetProblemData(long problemId) {
return CallRunCreationService(s => s.GetProblemData(problemId));
}
#endregion
#region Solution Methods
public IEnumerable GetSolutions(long problemId) {
return CallRunCreationService(s => s.GetSolutions(problemId));
}
public Solution GetSolution(long solutionId) {
return CallRunCreationService(s => s.GetSolution(solutionId));
}
public byte[] GetSolutionData(long solutionId) {
return CallRunCreationService(s => s.GetSolutionData(solutionId));
}
public long AddSolution(Solution solution, byte[] data) {
return CallRunCreationService(s => s.AddSolution(solution, data));
}
public void DeleteSolution(Solution solution) {
CallRunCreationService(s => s.DeleteSolution(solution));
}
#endregion
#region Run Methods
public void AddRun(Run run) {
CallRunCreationService(s => s.AddRun(run));
}
#endregion
#region Characteristic Methods
public IEnumerable GetCharacteristicValues(long problemId) {
return CallRunCreationService(s => s.GetCharacteristicValues(problemId));
}
public void SetCharacteristicValue(long problemId, Value v) {
CallRunCreationService(s => s.SetCharacteristicValue(problemId, v));
}
public void SetCharacteristicValues(long problemId, IEnumerable values) {
CallRunCreationService(s => s.SetCharacteristicValues(problemId, values.ToList()));
}
#endregion
#region OKB-Item Conversion
public IItem ConvertToItem(Value value) {
if (value is BinaryValue) {
IItem item = null;
var binaryValue = (BinaryValue)value;
if (binaryValue.Value != null) {
using (var stream = new MemoryStream(binaryValue.Value)) {
try {
item = XmlParser.Deserialize(stream);
} catch (Exception) { }
stream.Close();
}
}
return item ?? new Data.StringValue(value.DataType.Name);
} else if (value is BoolValue) {
return new Data.BoolValue(((BoolValue)value).Value);
} else if (value is FloatValue) {
return new Data.DoubleValue(((FloatValue)value).Value);
} else if (value is PercentValue) {
return new Data.PercentValue(((PercentValue)value).Value);
} else if (value is DoubleValue) {
return new Data.DoubleValue(((DoubleValue)value).Value);
} else if (value is IntValue) {
return new Data.IntValue((int)((IntValue)value).Value);
} else if (value is LongValue) {
return new Data.IntValue((int)((LongValue)value).Value);
} else if (value is StringValue) {
return new Data.StringValue(((StringValue)value).Value);
} else if (value is TimeSpanValue) {
return new Data.TimeSpanValue(TimeSpan.FromSeconds((long)((TimeSpanValue)value).Value));
}
return null;
}
public Value ConvertToValue(IItem item, string name) {
Value result = null;
if (item is ValueTypeValue) {
var boolValue = (ValueTypeValue)item;
result = new BoolValue() { Value = boolValue.Value };
} else if (item is ValueTypeValue) {
var intValue = (ValueTypeValue)item;
result = new IntValue() { Value = intValue.Value };
} else if (item is ValueTypeValue) {
var longValue = (ValueTypeValue)item;
result = new LongValue() { Value = longValue.Value };
} else if (item is ValueTypeValue) {
var floatValue = (ValueTypeValue)item;
result = new FloatValue() { Value = floatValue.Value };
} else if (item is ValueTypeValue) {
var doubleValue = (ValueTypeValue)item;
if (item is Data.PercentValue) result = new PercentValue() { Value = doubleValue.Value };
else result = new DoubleValue() { Value = doubleValue.Value };
} else if (item is ValueTypeValue) {
var timeSpanValue = (ValueTypeValue)item;
result = new TimeSpanValue() { Value = (long)timeSpanValue.Value.TotalSeconds };
} else if (item is Data.StringValue) {
var stringValue = (Data.StringValue)item;
result = new StringValue() { Value = stringValue.Value };
}
if (result == null) {
var binaryValue = new BinaryValue {
DataType = new DataType() {
Name = item.GetType().Name,
TypeName = item.GetType().AssemblyQualifiedName
}
};
using (var memStream = new MemoryStream()) {
XmlGenerator.Serialize(item, memStream);
binaryValue.Value = memStream.ToArray();
}
result = binaryValue;
}
result.Name = name;
return result;
}
#endregion
#region Events
public event EventHandler Refreshing;
private void OnRefreshing() {
EventHandler handler = Refreshing;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Refreshed;
private void OnRefreshed() {
EventHandler handler = Refreshed;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region Helpers
private void CallRunCreationService(Action call) {
RunCreationServiceClient client = ClientFactory.CreateClient();
try {
call(client);
} finally {
try {
client.Close();
} catch (Exception) {
client.Abort();
}
}
}
private T CallRunCreationService(Func call) {
RunCreationServiceClient client = ClientFactory.CreateClient();
try {
return call(client);
} finally {
try {
client.Close();
} catch (Exception) {
client.Abort();
}
}
}
#endregion
}
}