- Timestamp:
- 11/19/19 16:59:18 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs
r17353 r17354 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.IO; 3 4 using System.Linq; 4 5 using System.Text; … … 19 20 private IEnumerable<object> range; 20 21 #endregion 21 22 public string Name { 23 get => name; 22 23 public string Name { 24 get => name; 24 25 set { 25 26 name = value; 26 27 Path = Name; 27 28 UpdatePath(); 28 } 29 } 29 30 } 30 31 public string Type { get; set; } … … 33 34 public IList<JsonItem> Operators { get; set; } 34 35 public object Value { 35 get => value; 36 get => value; 36 37 set { 37 if (value is JContainer) 38 this.value = ((JContainer)value).ToObject<object[]>(); 39 else 40 this.value = value; 38 this.value = value; 41 39 CheckConstraints(); 42 } 40 } 43 41 } 44 public IEnumerable<object> Range { 45 get => range; 42 public IEnumerable<object> Range { 43 get => range; 46 44 set { 47 45 range = value; 48 46 CheckConstraints(); 49 } 47 } 50 48 } 51 49 public string ActualName { get; set; } … … 84 82 UpdatePathHelper(Operators); 85 83 86 if (Reference != null)84 if (Reference != null) 87 85 UpdatePathHelper(Reference); 88 86 } … … 90 88 91 89 #region Helper 92 private void UpdatePathHelper(params JsonItem[] items) => 90 private void UpdatePathHelper(params JsonItem[] items) => 93 91 UpdatePathHelper((IEnumerable<JsonItem>)items); 94 92 … … 105 103 } 106 104 107 private bool IsInRange() => IsInRangeList() || 105 private bool IsInRange() => IsInRangeList() || 108 106 (Value.GetType().IsArray && ((object[])Value).All(x => IsInNumericRange(x))) || 109 107 (!Value.GetType().IsArray && IsInNumericRange(Value)); … … 129 127 (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0); 130 128 } 131 129 130 #endregion 131 132 #region BuildJsonItemMethods 133 public static JsonItem BuildJsonItem(JObject obj, IDictionary<string, string> typeList) { 134 object val = obj[nameof(Value)]?.ToObject<object>(); 135 if (val is JContainer) 136 val = ((JContainer)val).ToObject<object[]>(); 137 138 return new JsonItem() { 139 Name = obj[nameof(Name)]?.ToString(), 140 Path = obj[nameof(Path)]?.ToString(), 141 Value = val, 142 Range = obj[nameof(Range)]?.ToObject<object[]>(), 143 Type = GetType(obj[nameof(Path)]?.ToObject<string>(), typeList), 144 ActualName = obj[nameof(ActualName)]?.ToString(), 145 Parameters = PopulateParameters(obj, typeList), 146 Operators = PopulateOperators(obj, typeList) 147 }; 148 } 149 150 private static string GetType(string path, IDictionary<string, string> typeList) { 151 if (!string.IsNullOrEmpty(path)) 152 if (typeList.TryGetValue(path, out string value)) 153 return value; 154 return null; 155 } 156 157 private static IList<JsonItem> PopulateParameters(JObject obj, IDictionary<string, string> typeList) { 158 IList<JsonItem> list = new List<JsonItem>(); 159 160 // add staticParameters 161 if (obj[Constants.StaticParameters] != null) 162 foreach (JObject param in obj[Constants.StaticParameters]) 163 list.Add(BuildJsonItem(param, typeList)); 164 165 // merge staticParameter with freeParameter 166 if (obj[Constants.FreeParameters] != null) { 167 foreach (JObject param in obj[Constants.FreeParameters]) { 168 JsonItem tmp = BuildJsonItem(param, typeList); 169 170 // search staticParameter from list 171 JsonItem comp = null; 172 foreach (var p in list) 173 if (p.Name == tmp.Name) comp = p; 174 if (comp == null) 175 throw new InvalidDataException($"Invalid {Constants.FreeParameters.Trim('s')}: '{tmp.Name}'!"); 176 177 JsonItem.Merge(comp, tmp); 178 } 179 } 180 return list; 181 } 182 183 private static IList<JsonItem> PopulateOperators(JObject obj, IDictionary<string, string> typeList) { 184 IList<JsonItem> list = new List<JsonItem>(); 185 JToken operators = obj[nameof(JsonItem.Operators)]; 186 if (operators != null) 187 foreach (JObject sp in operators) 188 list.Add(BuildJsonItem(sp, typeList)); 189 return list; 190 } 132 191 #endregion 133 192 }
Note: See TracChangeset
for help on using the changeset viewer.