- Timestamp:
- 12/17/19 17:16:03 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs
r17374 r17379 28 28 string oldName = Name; 29 29 name = value; 30 // replace name in path if path != null 30 31 if (Path != null) { 31 32 var parts = Path.Split('.'); 32 for (int i = 0; i < parts.Length; ++i) 33 if (parts[i] == oldName) 34 parts[i] = Name; 35 33 parts[Array.IndexOf(parts, oldName)] = name; 36 34 Path = string.Join(".", parts); 37 35 } else … … 42 40 } 43 41 } 44 public string Type { get; set; }45 42 public string Path { get; set; } 46 public IList<JsonItem> Parameters { get; set; } // -> für flachen aufbau -> childs? 47 public IList<JsonItem> Operators { get; set; } 43 public IList<JsonItem> Children { get; set; } 48 44 public object Value { 49 45 get => value; … … 62 58 public string ActualName { get; set; } 63 59 64 #region JsonIgnore Properties65 [JsonIgnore]66 public JsonItem Reference { get; set; }67 68 [JsonIgnore]69 public bool IsConfigurable => (Value != null && Range != null);70 71 [JsonIgnore]72 public bool IsParameterizedItem => Parameters != null;73 #endregion74 75 60 #region Public Static Methods 76 61 public static void Merge(JsonItem target, JsonItem from) { 77 62 target.Name = from.Name ?? target.Name; 78 target.Type = from.Type ?? target.Type;79 63 target.Range = from.Range ?? target.Range; 80 64 target.Path = from.Path ?? target.Path; 81 65 target.Value = from.Value ?? target.Value; 82 target.Reference = from.Reference ?? target.Reference;83 66 target.ActualName = from.ActualName ?? target.ActualName; 84 target.Parameters = from.Parameters ?? target.Parameters; 85 target.Operators = from.Operators ?? target.Operators; 67 if(target.Children != null) { 68 if (from.Children != null) 69 ((List<JsonItem>)from.Children).AddRange(target.Children); 70 } else { 71 target.Children = from.Children; 72 } 86 73 } 87 74 #endregion … … 89 76 #region Public Methods 90 77 public void AddParameter(JsonItem item) { 91 if ( Parameters== null)92 Parameters= new List<JsonItem>();93 Parameters.Add(item);78 if (Children == null) 79 Children = new List<JsonItem>(); 80 Children.Add(item); 94 81 item.Path = $"{Path}.{item.Name}"; 95 82 item.UpdatePath(); … … 97 84 98 85 public void UpdatePath() { 99 if (Parameters != null) 100 UpdatePathHelper(Parameters); 101 102 if (Operators != null) 103 UpdatePathHelper(Operators); 104 105 if (Reference != null) 106 UpdatePathHelper(Reference); 86 if (Children != null) 87 UpdatePathHelper(Children); 107 88 } 108 89 #endregion 109 90 110 91 #region Helper 111 private void UpdatePathHelper(params JsonItem[] items) =>112 UpdatePathHelper((IEnumerable<JsonItem>)items);113 114 92 private void UpdatePathHelper(IEnumerable<JsonItem> items) { 115 93 foreach (var item in items) { … … 136 114 b2 = IsInNumericRange(Value); 137 115 } 138 139 116 return b1 || b2; 140 117 } … … 166 143 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0); 167 144 } 168 169 145 #endregion 170 146 171 147 #region BuildJsonItemMethods 172 public static JsonItem BuildJsonItem(JObject obj , IDictionary<string, string> typeList) {148 public static JsonItem BuildJsonItem(JObject obj) { 173 149 object val = obj[nameof(Value)]?.ToObject<object>(); 174 if (val is JContainer) { 175 //try { 176 val = ((JContainer)val).ToObject<object[]>(); 177 /*} catch (Exception) { } 178 try { 179 val = ((JContainer)val).ToObject<object[,]>(); 180 } catch (Exception) { }*/ 181 } 150 if (val is JContainer jContainer) // for resolving array values 151 val = jContainer.ToObject<object[]>(); 182 152 183 184 153 return new JsonItem() { 185 154 Name = obj[nameof(Name)]?.ToString(), … … 187 156 Value = val, 188 157 Range = obj[nameof(Range)]?.ToObject<object[]>(), 189 Type = GetType(obj[nameof(Path)]?.ToObject<string>(), typeList), 190 ActualName = obj[nameof(ActualName)]?.ToString(), 191 Parameters = PopulateParameters(obj, typeList), 192 Operators = PopulateOperators(obj, typeList) 158 ActualName = obj[nameof(ActualName)]?.ToString() 193 159 }; 194 }195 196 private static string GetType(string path, IDictionary<string, string> typeList) {197 if (!string.IsNullOrEmpty(path))198 if (typeList.TryGetValue(path, out string value))199 return value;200 return null;201 }202 203 private static IList<JsonItem> PopulateParameters(JObject obj, IDictionary<string, string> typeList) {204 IList<JsonItem> list = new List<JsonItem>();205 206 // add staticParameters207 if (obj[Constants.StaticParameters] != null)208 foreach (JObject param in obj[Constants.StaticParameters])209 list.Add(BuildJsonItem(param, typeList));210 211 // merge staticParameter with freeParameter212 if (obj[Constants.FreeParameters] != null) {213 foreach (JObject param in obj[Constants.FreeParameters]) {214 JsonItem tmp = BuildJsonItem(param, typeList);215 216 // search staticParameter from list217 JsonItem comp = null;218 foreach (var p in list)219 if (p.Name == tmp.Name) comp = p;220 if (comp == null)221 throw new InvalidDataException($"Invalid {Constants.FreeParameters.Trim('s')}: '{tmp.Name}'!");222 223 JsonItem.Merge(comp, tmp);224 }225 }226 return list;227 }228 229 private static IList<JsonItem> PopulateOperators(JObject obj, IDictionary<string, string> typeList) {230 IList<JsonItem> list = new List<JsonItem>();231 JToken operators = obj[nameof(JsonItem.Operators)];232 if (operators != null)233 foreach (JObject sp in operators)234 list.Add(BuildJsonItem(sp, typeList));235 return list;236 160 } 237 161 #endregion
Note: See TracChangeset
for help on using the changeset viewer.