- Timestamp:
- 03/11/13 14:40:04 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Controller/Parsers/AlgorithmConverter.cs
r9227 r9305 9 9 namespace HeuristicLab.Services.Optimization.ControllerService.Parsers { 10 10 public static class AlgorithmConverter { 11 12 public static Algorithm Convert(string json) { 13 var o = JObject.Parse(json); 14 return ParseAlgorithm(o); 15 } 16 17 private static Algorithm ParseAlgorithm(JToken obj) { 18 var algorithm = new Algorithm(); 19 20 foreach (JProperty param in obj["Algorithm Parameters"]) { 21 Parameter parameter = CreateParameter(param); 22 algorithm.Parameters.Items.Add(parameter); 23 } 24 25 var problemParams = obj["Problem Parameters"]; 26 if (problemParams != null) { 27 algorithm.Problem = new Problem(); 28 foreach (JProperty param in problemParams) { 29 Parameter parameter = CreateParameter(param); 30 algorithm.Problem.Parameters.Items.Add(parameter); 31 } 32 } 33 return algorithm; 34 } 35 36 public static Algorithm ConvertAppend(string json, Algorithm algorithm) { 37 var o = JObject.Parse(json); 38 foreach (JProperty param in o["Algorithm Parameters"]) { 39 Parameter parameter = CreateParameter(param); 40 var oldParameter = (from p in algorithm.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault(); 41 if (oldParameter != null) { 42 oldParameter.Value = parameter.Value; 43 } 44 else { 45 algorithm.Parameters.Items.Add(parameter); 46 } 47 } 48 49 var problemParams = o["Problem Parameters"]; 50 if (problemParams != null) { 51 algorithm.Problem = new Problem(); 52 foreach (JProperty param in problemParams) { 53 Parameter parameter = CreateParameter(param); 54 var oldParameter = (from p in algorithm.Problem.Parameters.Items where p.Value.Name == parameter.Value.Name select p).FirstOrDefault(); 55 if (oldParameter != null) { 56 oldParameter.Value = parameter.Value; 57 } 58 else { 59 algorithm.Problem.Parameters.Items.Add(parameter); 60 } 61 } 62 } 63 return algorithm; 64 } 65 66 private static Parameter CreateParameter(JProperty property) { 67 JToken token = property.Value; 68 switch (token.Type) { 11 #region private methods 12 private static Parameter CreateParameter(JObject property) { 13 var name = (string)property["Name"]; 14 var value = property["Value"]; 15 switch (value.Type) { 69 16 case JTokenType.Integer: 70 17 case JTokenType.Float: 71 return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = property.Name, Value = (double)property.Value } };18 return new Parameter() { Type = ParameterType.Decimal, Value = new DecimalValue() { Name = name, Value = (double)value } }; 72 19 case JTokenType.Boolean: 73 return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = property.Name, Value = (bool)property.Value } };20 return new Parameter() { Type = ParameterType.Boolean, Value = new BoolValue() { Name = name, Value = (bool)value } }; 74 21 case JTokenType.String: 75 return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = property.Name, Value = (string)property.Value } };22 return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = name, Value = (string)value, Options = (from e in property["Options"] select (string)e).ToArray() } }; 76 23 case JTokenType.Array: 77 var arr = (JArray) token;24 var arr = (JArray)value; 78 25 // its a matrix 79 26 if (arr[0].Type == JTokenType.Array) { 80 return CreateMatrixParameter( property.Name, arr);27 return CreateMatrixParameter(name, arr); 81 28 } 82 return CreateVectorParameter( property.Name, arr);29 return CreateVectorParameter(name, arr); 83 30 default: 84 31 throw new Exception("Unhandled datatype: " + property.Type); … … 89 36 double[][] entries = new double[arr.Count][]; 90 37 for (int i = 0; i < entries.Length; i++) { 91 entries[i] = (from d in arr[i] select (double)d).ToArray<double>(); 38 entries[i] = (from d in arr[i] select (double)d).ToArray<double>(); 92 39 } 93 40 return new Parameter { Type = ParameterType.DecimalMatrix, Value = new DecimalMatrix() { Name = name, Value = entries } }; … … 99 46 } 100 47 101 public static string ConvertJson(Algorithm algorithm) {102 return JsonConvert.SerializeObject(algorithm);103 }104 105 106 48 private class StackEntry { 107 49 public Algorithm Parent { get; set; } … … 109 51 } 110 52 111 public static Experiment ConvertExperimentSimple(string json) { 112 return JsonConvert.DeserializeObject<Experiment>(json, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto }); 113 } 114 115 public static string ConvertJson(Experiment experiment) { 116 return JsonConvert.SerializeObject(experiment, Formatting.None, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); 117 } 118 119 public static Experiment ConvertExperiment(string json) { 53 private static JArray ConvertParametersToJson(IList<Parameter> parameters) { 54 var array = new JArray(); 55 foreach (var param in parameters) { 56 array.Add(JObject.FromObject(param.Value)); 57 } 58 return array; 59 } 60 61 private struct Entry { 62 public JObject TargetAlgorithm { get; set; } 63 64 public Algorithm SourceAlgorithm { get; set; } 65 } 66 67 private static Algorithm ParseAlgorithm(JToken jsonAlgorithm) { 68 var algorithm = new Algorithm(); 69 70 foreach (JObject param in jsonAlgorithm["AlgorithmParameters"]) { 71 Parameter parameter = CreateParameter(param); 72 algorithm.Parameters.Items.Add(parameter); 73 } 74 75 var problemParams = jsonAlgorithm["ProblemParameters"]; 76 if (problemParams != null) { 77 algorithm.Problem = new Problem(); 78 foreach (JObject param in problemParams) { 79 Parameter parameter = CreateParameter(param); 80 algorithm.Problem.Parameters.Items.Add(parameter); 81 } 82 } 83 return algorithm; 84 } 85 #endregion 86 87 88 #region public methods 89 90 91 public static JObject ConvertExperimentToJson(Experiment experiment) { 92 var exp = new JObject(); 93 exp["title"] = experiment.Name; 94 exp["children"] = new JArray(); 95 exp["nodeId"] = experiment.Id; 96 exp["isExperiment"] = true; 97 98 var stack = new Stack<Entry>(); 99 foreach (var algo in experiment.Algorithm) 100 stack.Push(new Entry() { TargetAlgorithm = exp, SourceAlgorithm = algo }); 101 102 while (stack.Count > 0) { 103 var entry = stack.Pop(); 104 var algorithm = ConvertAlgorithmToJson(entry.SourceAlgorithm); 105 (entry.TargetAlgorithm["children"] as JArray).Add(algorithm); 106 107 if (entry.SourceAlgorithm.ChildAlgorithms.Count > 0) { 108 // push children 109 foreach (var child in entry.SourceAlgorithm.ChildAlgorithms) { 110 stack.Push(new Entry() { TargetAlgorithm = algorithm, SourceAlgorithm = child }); 111 } 112 } 113 114 } 115 return exp; 116 } 117 118 public static JArray ConvertExperimentsToJson(IEnumerable<Experiment> experiments) { 119 var jarray = new JArray(); 120 foreach (var exp in experiments) 121 jarray.Add(ConvertExperimentToJson(exp)); 122 return jarray; 123 } 124 125 public static JArray ConvertAlgorithmsToJson(IEnumerable<Algorithm> algorithms) { 126 var jarray = new JArray(); 127 foreach (var algo in algorithms) 128 jarray.Add(ConvertAlgorithmToJson(algo)); 129 return jarray; 130 } 131 132 133 public static JArray ConvertScenariosToJson(IEnumerable<OptimizationScenario> scenarios) { 134 var jarray = new JArray(); 135 foreach (var scen in scenarios) 136 jarray.Add(ConvertScenarioToJson(scen)); 137 return jarray; 138 } 139 140 public static JObject ConvertScenarioToJson(OptimizationScenario scenario) { 141 var exp = new JObject(); 142 exp["title"] = scenario.Id; 143 exp["children"] = new JArray(); 144 exp["nodeId"] = scenario.Id; 145 exp["isExperiment"] = false; 146 147 var stack = new Stack<Entry>(); 148 var baseAlgorithm = ConvertAlgorithmToJson(scenario.FirstAlgorithm); 149 exp["data"] = baseAlgorithm["data"]; 150 if (scenario.FirstAlgorithm.ChildAlgorithms.Count > 0) { 151 foreach (var child in scenario.FirstAlgorithm.ChildAlgorithms) 152 stack.Push(new Entry() { TargetAlgorithm = exp, SourceAlgorithm = child}); 153 } 154 155 while (stack.Count > 0) { 156 var entry = stack.Pop(); 157 var algorithm = ConvertAlgorithmToJson(entry.SourceAlgorithm); 158 (entry.TargetAlgorithm["children"] as JArray).Add(algorithm); 159 160 if (entry.SourceAlgorithm.ChildAlgorithms.Count > 0) { 161 // push children 162 foreach (var child in entry.SourceAlgorithm.ChildAlgorithms) { 163 stack.Push(new Entry() { TargetAlgorithm = algorithm, SourceAlgorithm = child }); 164 } 165 } 166 167 } 168 return exp; 169 } 170 171 public static JObject ConvertAlgorithmToJson(Algorithm algorithm) { 172 var jalgo = new JObject(); 173 jalgo["title"] = algorithm.Name; 174 jalgo["nodeId"] = algorithm.Id; 175 var jalgoData = new JObject(); 176 jalgo["data"] = jalgoData; 177 jalgo["isExperiment"] = algorithm.IsExperiment; 178 jalgoData["AlgorithmParameters"] = ConvertParametersToJson(algorithm.Parameters.Items); 179 if (algorithm.Problem != null && algorithm.Problem.Parameters != null) 180 jalgoData["ProblemParameters"] = ConvertParametersToJson(algorithm.Problem.Parameters.Items); 181 182 jalgo["children"] = new JArray(); 183 return jalgo; 184 } 185 186 public static Experiment ConvertJsonToExperiment(string json) { 120 187 var experiment = new Experiment(); 121 188 var jsonExperiment = JObject.Parse(json); 122 experiment.Name = (string)jsonExperiment["name"]; 189 experiment.Name = (string)jsonExperiment["title"]; 190 experiment.Id = (string)jsonExperiment["nodeId"]; 123 191 var stack = new Stack<StackEntry>(); 124 192 var root = new Algorithm(); 125 193 126 if ( (bool)jsonExperiment["run"]) {194 if (jsonExperiment["run"] != null && (bool)jsonExperiment["run"]) { 127 195 experiment.JobDetails = new JobExecutionDetails() { 128 129 196 Group = (string)jsonExperiment["group"], 197 Repititions = (int)jsonExperiment["repititions"] 130 198 }; 131 199 } 132 200 133 foreach (var algo in jsonExperiment["algorithms"]["children"]) { 134 stack.Push(new StackEntry(){ Parent = root, Child = algo }); 135 } 201 // ignore experiment node, skip to its children 202 if (jsonExperiment["experiment"] != null) 203 foreach (var algo in jsonExperiment["experiment"]["children"]) { 204 stack.Push(new StackEntry() { Parent = root, Child = algo }); 205 } 206 207 if (jsonExperiment["children"] != null) 208 foreach (var algo in jsonExperiment["children"]) { 209 stack.Push(new StackEntry() { Parent = root, Child = algo }); 210 } 136 211 137 212 while (stack.Count > 0) { 138 213 var entry = stack.Pop(); 139 214 var data = entry.Child["data"]; 140 var currentAlgo = data == null ? new Algorithm() : ParseAlgorithm(entry.Child["data"]); 141 currentAlgo.Name = (string)entry.Child["name"]; 215 var currentAlgo = data == null || !data.HasValues ? new Algorithm() : ParseAlgorithm(entry.Child["data"]); 216 currentAlgo.Name = (string)entry.Child["title"]; 217 currentAlgo.Id = (string)entry.Child["nodeId"]; 218 currentAlgo.IsExperiment = (bool)entry.Child["isExperiment"]; 142 219 entry.Parent.ChildAlgorithms.Add(currentAlgo); 143 220 // push children on stack (inverse order to preserve ordering) 144 221 var cnt = entry.Child["children"].Count(); 145 for (var i =0; i < cnt; i++) {222 for (var i = 0; i < cnt; i++) { 146 223 stack.Push(new StackEntry() { Parent = currentAlgo, Child = entry.Child["children"][cnt - 1 - i] }); 147 224 } … … 154 231 return experiment; 155 232 } 233 #endregion 156 234 } 157 235 }
Note: See TracChangeset
for help on using the changeset viewer.