using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Data; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.Xml; using System.IO; using HeuristicLab.OKB.AlgorithmHost.OKBRunner; namespace HeuristicLab.OKB.AlgorithmHost { public static class OKBExtensions { public static void AddParameterValue(this Algorithm a, string name, object value) { if (value == null) return; if (value is StringValue) { value = ((StringValue)value).Value; } else if (value is IntValue) { value = ((IntValue)value).Value; } else if (value is DoubleValue) { value = ((DoubleValue)value).Value; } else if (value is BoolValue) { value = ((BoolValue)value).Value; } Type t = value.GetType(); Parameter p = new Parameter() { DataType = new DataType() { ClrName = t.FullName }, Name = name }; if (t == typeof(int)) { p.IntParameterValues = new List() { new IntParameterValue() { Value = (int)value } }; } else if (t == typeof(double)) { p.FloatParameterValues = new List() { new FloatParameterValue() { Value = (double)value } }; } else if (t == typeof(string)) { p.CharParameterValues = new List() { new CharParameterValue() { Value = (string)value } }; } else if (t == typeof(bool)) { p.IntParameterValues = new List() { new IntParameterValue() { Value = ((bool)value) ? 1 : 0 } }; } else if (value is Item) { byte[] data; using (var stream = new MemoryStream()) { XmlGenerator.Serialize(value, stream); data = stream.ToArray(); } p.DataType.ClrName = "IOperator"; p.OperatorParameterValues = new List() { new OperatorParameterValue() { DataType = new DataType() { ClrName = t.FullName }, Value = new Binary() { Bytes = data }, }, }; if (value is IParameterizedItem) { foreach (var param in ((IParameterizedItem)value).Parameters) { try { a.AddParameterValue(string.Format("{0}:{1}.{2}", name, t.Name, param.Name), param.ActualValue); } catch (Exception x) { // swallow exceptions } } } } else { throw new ArgumentException("invalid parameter data type" + t.ToString()); } if (a.Algorithm_Parameters.Any(ap => ap.Parameter.Name == name)) { throw new ArgumentException("duplicate parameter " + name); } a.Algorithm_Parameters.Add(new Algorithm_Parameter() { Algorithm = a, Parameter = p, }); } public static void AddResultValue(this Algorithm a, string name, object value) { if (value == null) return; if (value is StringValue) { value = ((StringValue)value).Value; } else if (value is IntValue) { value = ((IntValue)value).Value; } else if (value is DoubleValue) { value = ((DoubleValue)value).Value; } else if (value is BoolValue) { value = ((BoolValue)value).Value; } Type t = value.GetType(); Result r = new Result() { DataType = new DataType() { ClrName = t.FullName }, Name = name }; if (t == typeof(int)) { r.IntResultValues = new List() { new IntResultValue() { Value = (int)value } }; } else if (t == typeof(double)) { r.FloatResultValues = new List() { new FloatResultValue() { Value = (double)value } }; } else if (t == typeof(string)) { r.CharResultValues = new List() { new CharResultValue() { Value = (string)value } }; } else if (value is Item) { byte[] data; using (var stream = new MemoryStream()) { XmlGenerator.Serialize(value, stream); data = stream.ToArray(); } r.BlobResultValues = new List() { new BlobResultValue() { Value = new Binary() { Bytes = data }, } }; } else { throw new ArgumentException("unsupported result data type" + t.ToString()); } a.Algorithm_Results.Add(new Algorithm_Result() { Algorithm = a, Result = r, }); } } }