using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.Serialization; using System.Globalization; using System.Xml; namespace HeuristicLab.Services.Optimization.ControllerService.Model { [DataContract] public enum ParameterType { [EnumMember] Decimal, [EnumMember] Integer, [EnumMember] Boolean, [EnumMember] Type, [EnumMember] DecimalMatrix, [EnumMember] DecimalVector, [EnumMember] Percent, [EnumMember] String } [DataContract] public abstract class Value { [DataMember] public string Name { get; set; } public abstract bool TrySetFromString(string input); } [DataContract] public class DecimalMatrix : Value { [DataMember] public double[][] Value { get; set; } [DataMember] public string[] RowNames { get; set; } public void CopyRow(int index) { var newData = new double[Value.Length + 1][]; var copyRow = new double[Value[index].Length]; for (var i = 0; i < copyRow.Length; i++) { copyRow[i] = Value[index][i]; } for (var i = 0; i < newData.Length; i++) { if (i < index) { newData[i] = Value[i]; } else if (i == index) { newData[i] = copyRow; } else if (i > index) { newData[i] = Value[i - 1]; } } Value = newData; } public void DeleteRow(int index) { if (Value.Length == 1) return; var newData = new double[Value.Length - 1][]; for (var i = 0; i < Value.Length; i++) { if (i < index) { newData[i] = Value[i]; } else if (i > index) { newData[i - 1] = Value[i]; } } Value = newData; } public override bool TrySetFromString(string input) { var splitted = input.Split(':'); int i, j; if (splitted.Length != 3) return false; if (!int.TryParse(splitted[0], out i)) return false; if (!int.TryParse(splitted[1], out j)) return false; double value; if (!double.TryParse(splitted[2], out value)) return false; // very, very buggy /*if (i >= Value.Length || j >= Value[0].Length ) { double[][] valueArr = new double[i >= Value.GetLength(0) ? i : Value.GetLength(0)][]; for (int k = 0; k < Value.Length; k++) { valueArr[k] = new double[j >= Value.GetLength(1) ? j : Value.GetLength(1)]; for (int l = 0; l < Value[k].Length; l++) { valueArr[k][l] = Value[k][l]; } } Value = valueArr; }*/ Value[i][j] = value; return true; } } [DataContract] public class DecimalVector : Value { [DataMember] public double[] Value { get; set; } public override bool TrySetFromString(string input) { var splitted = input.Split(':'); int index; if (splitted.Length != 2) return false; if (!int.TryParse(splitted[0], out index)) return false; double value; if (!double.TryParse(splitted[1], out value)) return false; if (index >= Value.Length) { double[] valueArr = new double[index + 1]; Array.Copy(Value, valueArr, Value.Length); Value = valueArr; } Value[index] = value; return true; } } [DataContract] public class TypeValue : Value { [DataMember] public string Value { get; set; } [DataMember] public string[] Options { get; set; } public override bool TrySetFromString(string input) { if (Options.Contains(input)) { Value = input; return true; } return false; } } [DataContract] public class DecimalValue : Value { [DataMember] public double Value { get; set; } public override bool TrySetFromString(string input) { double result; if (double.TryParse(input, out result)) { Value = result; return true; } return false; } } [DataContract] public class BoolValue : Value { [DataMember] public bool Value { get; set; } public override bool TrySetFromString(string input) { bool result; if (input.Contains(",")) input = input.Split(',')[0].Trim(); if (bool.TryParse(input, out result)) { Value = result; return true; } return false; } } [DataContract] public class StringValue : Value { [DataMember] public string Value { get; set; } public override bool TrySetFromString(string input) { Value = input; return true; } } [DataContract] [KnownType(typeof(BoolValue))] [KnownType(typeof(DecimalValue))] [KnownType(typeof(TypeValue))] [KnownType(typeof(DecimalVector))] [KnownType(typeof(DecimalMatrix))] [KnownType(typeof(StringValue))] public class Parameter { // some kind of value type [DataMember] public ParameterType Type { get; set; } [DataMember] public Value Value { get; set; } /* //[DataMember] public object[] Choices { get; set; }*/ } [DataContract] public class ProblemParameters { private IList items = new List(); [DataMember] public IList Items { get { return items; } set { items = value; } } public Parameter FindByName(string name) { foreach (var param in items) { if (param.Value.Name == name) { return param; } } return null; } } [DataContract] public class AlgorithmParameters { private IList items = new List(); [DataMember] public IList Items { get { return items; } set { items = value; } } public Parameter FindByName(string name) { foreach (var param in items) { if (param.Value.Name == name) { return param; } } return null; } } [DataContract] public class Problem { public Problem() { Parameters = new ProblemParameters(); } [DataMember] public ProblemParameters Parameters { get; set; } [DataMember] public string Class { get; set; } } [DataContract] public class Algorithm { public Algorithm() { Parameters = new AlgorithmParameters(); ChildAlgorithms = new List(); } [DataMember] public string Id { get; set; } [DataMember] public AlgorithmParameters Parameters { get; set; } [DataMember] public Problem Problem{ get; set; } [DataMember] public IList ChildAlgorithms { get; set; } [DataMember] public string Class { get; set; } [DataMember] public string Mapper { get; set; } [DataMember] public string Name { get; set; } [DataMember] public bool IsExperiment { get; set; } } [DataContract] public class OptimizationScenario { [DataMember] public string Id { get; set; } public OptimizationScenario() { Algorithm = new List(); } [DataMember] public IList Algorithm { get; set; } public Algorithm FirstAlgorithm { get { return Algorithm.FirstOrDefault(); } } } [DataContract] public class Experiment { [DataMember] public string Name { get; set; } [DataMember] public IList Algorithm { get; set; } [DataMember] public string Id { get; set; } public Experiment() { Algorithm = new List(); } [DataMember] public JobExecutionDetails JobDetails { get; set; } } [DataContract] public enum JobState { [EnumMember] Waiting, [EnumMember] Calculating, [EnumMember] Aborted, [EnumMember] Failed, [EnumMember] Finished } [DataContract] public class JobExecutionDetails { [DataMember] public int Repititions { get; set; } [DataMember] public string JobTitle { get; set; } [DataMember] public string Group { get; set; } } [DataContract] public class Property { [DataMember] public string Key { get; set; } [DataMember] public string Value { get; set; } } [DataContract] public class General { [DataMember] public string Name { get { return GetValue("Name"); } set { SetValue("Name", value); } } [DataMember] public string Id { get { return GetValue("Id"); } set { SetValue("Id", value); } } [DataMember] public string Priority { get { return GetValue("Priority"); } set { SetValue("Priority", value); } } [DataMember] public string LastChanged { get { return GetValue("LastChanged"); } set { SetValue("LastChanged", value); } } private IList properties = new List(); [DataMember] public IList Properties { get { if (properties == null) properties = new List(); return properties; } set { properties = value;} } private string GetValue(string key) { return (from p in Properties where p.Key == key select p.Value).FirstOrDefault(); } private void SetValue(string key, string value) { var prop = (from p in Properties where p.Key == key select p).FirstOrDefault(); if (prop == null) { prop = new Property(); Properties.Add(prop); } prop.Key = key; prop.Value = value; } } [DataContract] public class TaskState { [DataMember] public string State { get; set; } [DataMember] public string ExecutionTime { get; set; } [DataMember] public string DateCreated { get; set; } [DataMember] public string DateFinished { get; set; } } [DataContract] public class Task { private General general = new General(); [DataMember] public General General { get { return general; } set { general = value; } } private IList tasks = new List(); [DataMember] public IList Children { get { return tasks; } set { tasks = value; } } private TaskState state = new TaskState(); [DataMember] public TaskState State { get { return state; } set { state = value; } } } [DataContract] public class Job { [DataMember] public string Id { get; set; } [DataMember] public JobState State { get; set; } [DataMember] public string Name { get; set; } [DataMember] public string Resource { get; set; } [DataMember] public DateTime DateCreated { get; set; } private IList tasks = new List(); [DataMember] public IList Tasks { get { return tasks; } set { tasks = value; } } } [DataContract] public class VisualExtension { [DataMember] public string ScenarioId { get; set; } [DataMember] public string ScenarioJs { get; set; } } [DataContract] public class Run { [DataMember] public string Id { get; set; } [DataMember] public string Name { get; set; } [DataMember] public IList Results { get; set; } [DataMember] public IList InputParameters { get; set; } [DataMember] public string AlgorithmName { get; set; } } [DataContract] public class User { [DataMember] public string Username { get; set; } [DataMember] public string Password { get; set; } } }