- Timestamp:
- 02/19/13 08:46:01 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Controller/Parsers/AlgorithmConverter.cs
r9215 r9227 5 5 using HeuristicLab.Services.Optimization.ControllerService.Model; 6 6 using Newtonsoft.Json.Linq; 7 using Newtonsoft.Json; 7 8 8 9 namespace HeuristicLab.Services.Optimization.ControllerService.Parsers { … … 10 11 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) { 12 18 var algorithm = new Algorithm(); 13 var o = JObject.Parse(json); 14 foreach (JProperty param in o ["Algorithm Parameters"]) {19 20 foreach (JProperty param in obj["Algorithm Parameters"]) { 15 21 Parameter parameter = CreateParameter(param); 16 22 algorithm.Parameters.Items.Add(parameter); 17 23 } 18 24 19 var problemParams = o["Problem Parameters"]; 20 // TODO: Store problem parameters 25 var problemParams = obj["Problem Parameters"]; 21 26 if (problemParams != null) { 22 27 algorithm.Problem = new Problem(); … … 69 74 case JTokenType.String: 70 75 return new Parameter() { Type = ParameterType.Type, Value = new TypeValue() { Name = property.Name, Value = (string)property.Value } }; 76 case JTokenType.Array: 77 var arr = (JArray)token; 78 // its a matrix 79 if (arr[0].Type == JTokenType.Array) { 80 return CreateMatrixParameter(property.Name, arr); 81 } 82 return CreateVectorParameter(property.Name, arr); 71 83 default: 72 84 throw new Exception("Unhandled datatype: " + property.Type); 73 85 } 74 86 } 87 88 private static Parameter CreateMatrixParameter(string name, JArray arr) { 89 double[][] entries = new double[arr.Count][]; 90 for (int i = 0; i < entries.Length; i++) { 91 entries[i] = (from d in arr[i] select (double)d).ToArray<double>(); 92 } 93 return new Parameter { Type = ParameterType.DecimalMatrix, Value = new DecimalMatrix() { Name = name, Value = entries } }; 94 } 95 96 private static Parameter CreateVectorParameter(string name, JArray arr) { 97 double[] entries = (from d in arr select (double)d).ToArray<double>(); 98 return new Parameter { Type = ParameterType.DecimalVector, Value = new DecimalVector() { Name = name, Value = entries } }; 99 } 100 101 public static string ConvertJson(Algorithm algorithm) { 102 return JsonConvert.SerializeObject(algorithm); 103 } 104 105 106 private class StackEntry { 107 public Algorithm Parent { get; set; } 108 public JToken Child { get; set; } 109 } 110 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) { 120 var experiment = new Experiment(); 121 var jsonExperiment = JObject.Parse(json); 122 experiment.Name = (string)jsonExperiment["name"]; 123 var stack = new Stack<StackEntry>(); 124 var root = new Algorithm(); 125 126 if ((bool)jsonExperiment["run"]) { 127 experiment.JobDetails = new JobExecutionDetails() { 128 Group = (string)jsonExperiment["group"], 129 Repititions = (int)jsonExperiment["repititions"] 130 }; 131 } 132 133 foreach (var algo in jsonExperiment["algorithms"]["children"]) { 134 stack.Push(new StackEntry(){ Parent = root, Child = algo }); 135 } 136 137 while (stack.Count > 0) { 138 var entry = stack.Pop(); 139 var data = entry.Child["data"]; 140 var currentAlgo = data == null ? new Algorithm() : ParseAlgorithm(entry.Child["data"]); 141 currentAlgo.Name = (string)entry.Child["name"]; 142 entry.Parent.ChildAlgorithms.Add(currentAlgo); 143 // push children on stack (inverse order to preserve ordering) 144 var cnt = entry.Child["children"].Count(); 145 for (var i=0; i < cnt; i++) { 146 stack.Push(new StackEntry() { Parent = currentAlgo, Child = entry.Child["children"][cnt - 1 - i] }); 147 } 148 } 149 150 foreach (var algo in root.ChildAlgorithms) { 151 experiment.Algorithm.Add(algo); 152 } 153 154 return experiment; 155 } 75 156 } 76 157 }
Note: See TracChangeset
for help on using the changeset viewer.