Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/17/19 17:16:03 (4 years ago)
Author:
dpiringe
Message:

#3026:

  • removed classes:
    • CheckedItemListConverter: unnecessary
    • ItemCollectionConverter: unnecessary
    • PrimitiveConverter: not possible to implement because it needs to Extract/Inject from/into objects (but interfaces pretends IItem)
    • StorableConverter: unnecessary
    • ConfigurableConverter: unnecessary
  • removed graphviz code in Heuristiclab.ConfigStarter/Program.cs
  • updated Constants
  • some simple code refactors in BaseConverter
  • in JsonItem:
    • renamed Parameters -> Children
    • removed Properties: Operators, Type, Reference, IsConfigurable, IsParameterizedItem
    • removed unnecessary/old code
  • implemented a new way to get data from an object, which is a matrix, in ValueTypeMatrixConverter method: CopyMatrixData
    • converts the object into an array -> rows: from array.Length, cols: when the length is > 0 pick length of first array of index 0 (it is saved as an array of arrays)
  • created a binding flag const in ValueRangeConverter to prevent duplicates in code
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs

    r17374 r17379  
    2828          string oldName = Name;
    2929          name = value;
     30          // replace name in path if path != null
    3031          if (Path != null) {
    3132            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;
    3634            Path = string.Join(".", parts);
    3735          } else
     
    4240      }
    4341    }
    44     public string Type { get; set; }
    4542    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; }
    4844    public object Value {
    4945      get => value;
     
    6258    public string ActualName { get; set; }
    6359
    64     #region JsonIgnore Properties
    65     [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     #endregion
    74 
    7560    #region Public Static Methods
    7661    public static void Merge(JsonItem target, JsonItem from) {
    7762      target.Name = from.Name ?? target.Name;
    78       target.Type = from.Type ?? target.Type;
    7963      target.Range = from.Range ?? target.Range;
    8064      target.Path = from.Path ?? target.Path;
    8165      target.Value = from.Value ?? target.Value;
    82       target.Reference = from.Reference ?? target.Reference;
    8366      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      }
    8673    }
    8774    #endregion
     
    8976    #region Public Methods
    9077    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);
    9481      item.Path = $"{Path}.{item.Name}";
    9582      item.UpdatePath();
     
    9784
    9885    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);
    10788    }
    10889    #endregion
    10990
    11091    #region Helper
    111     private void UpdatePathHelper(params JsonItem[] items) =>
    112       UpdatePathHelper((IEnumerable<JsonItem>)items);
    113 
    11492    private void UpdatePathHelper(IEnumerable<JsonItem> items) {
    11593      foreach (var item in items) {
     
    136114        b2 = IsInNumericRange(Value);
    137115      }
    138 
    139116      return b1 || b2;
    140117    }
     
    166143        (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
    167144    }
    168 
    169145    #endregion
    170146
    171147    #region BuildJsonItemMethods
    172     public static JsonItem BuildJsonItem(JObject obj, IDictionary<string, string> typeList) {
     148    public static JsonItem BuildJsonItem(JObject obj) {
    173149      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[]>();
    182152       
    183 
    184153      return new JsonItem() {
    185154        Name = obj[nameof(Name)]?.ToString(),
     
    187156        Value = val,
    188157        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()
    193159      };
    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 staticParameters
    207       if (obj[Constants.StaticParameters] != null)
    208         foreach (JObject param in obj[Constants.StaticParameters])
    209           list.Add(BuildJsonItem(param, typeList));
    210 
    211       // merge staticParameter with freeParameter
    212       if (obj[Constants.FreeParameters] != null) {
    213         foreach (JObject param in obj[Constants.FreeParameters]) {
    214           JsonItem tmp = BuildJsonItem(param, typeList);
    215 
    216           // search staticParameter from list
    217           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;
    236160    }
    237161    #endregion
Note: See TracChangeset for help on using the changeset viewer.