Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/19/19 16:59:18 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • relocated BuildJsonItem from JCInstantiator into JsonItem
  • in JsonItem: removed JContainer usage in setter for Value (now in BuildJsonItem)
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface
Files:
3 edited

Legend:

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

    r17353 r17354  
    7575
    7676      obj.Property(nameof(JsonItem.Parameters))?.Remove();
    77       RefactorFreeParameters(obj);
    78       RefactorStaticParameters(obj);
     77      RefactorFreeParameters(obj, genData);
     78      RefactorStaticParameters(obj, genData);
    7979
    8080      obj.Property(nameof(JsonItem.Value))?.Remove();
     
    8787
    8888    // deletes unnecessary properties for free parameters.
    89     private static void RefactorFreeParameters(JToken token) {
     89    private static void RefactorFreeParameters(JToken token, GenData genData) {
    9090      IList<JObject> objToRemove = new List<JObject>();
    9191      TransformNodes(x => {
    92         var p = x.ToObject<JsonItem>();
     92        var p = JsonItem.BuildJsonItem(x, genData.TypeList);
    9393        x.Property(nameof(JsonItem.Type))?.Remove();
    9494        x.Property(nameof(JsonItem.Parameters))?.Remove();
     
    105105
    106106    // deletes unnecessary properties for static parameters.
    107     private static void RefactorStaticParameters(JToken token) {
     107    private static void RefactorStaticParameters(JToken token, GenData genData) {
    108108      IList<JObject> objToRemove = new List<JObject>();
    109109      TransformNodes(x => {
    110         var p = x.ToObject<JsonItem>();
     110        var p = JsonItem.BuildJsonItem(x, genData.TypeList);
    111111        x.Property(nameof(JsonItem.Range))?.Remove();
    112112        x.Property(nameof(JsonItem.Operators))?.Remove();
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JCInstantiator.cs

    r17353 r17354  
    8383    private static void CollectParameterizedItems(InstData instData) {
    8484      foreach (JObject item in instData.Template[Constants.Objects]) {
    85         JsonItem data = BuildJsonItem(item, instData);
     85        JsonItem data = JsonItem.BuildJsonItem(item, instData.TypeList);
    8686        instData.ParameterizedItems.Add(data.Path, data);
    8787      }
     
    120120      foreach (JObject obj in instData.Config) {
    121121        // build item from config object
    122         JsonItem item = BuildJsonItem(obj, instData);
     122        JsonItem item = JsonItem.BuildJsonItem(obj, instData.TypeList);
    123123        // override default value
    124124        if (instData.ConfigurableItems.TryGetValue(item.Path, out JsonItem param)) {
     
    145145      } else throw new TypeLoadException($"Cannot find AssemblyQualifiedName for {data.Name}.");
    146146    }
    147 
    148     #region BuildJsonItemMethods
    149     private static JsonItem BuildJsonItem(JObject obj, InstData instData) =>
    150       new JsonItem() {
    151         Name = obj[nameof(JsonItem.Name)]?.ToString(),
    152         Path = obj[nameof(JsonItem.Path)]?.ToString(),
    153         Value = obj[nameof(JsonItem.Value)]?.ToObject<object>(),
    154         Range = obj[nameof(JsonItem.Range)]?.ToObject<object[]>(),
    155         Type = GetType(obj[nameof(JsonItem.Path)]?.ToObject<string>(), instData),
    156         ActualName = obj[nameof(JsonItem.ActualName)]?.ToString(),
    157         Parameters = PopulateParameters(obj, instData),
    158         Operators = PopulateOperators(obj, instData)
    159       };
    160 
    161     private static string GetType(string path, InstData instData) {
    162       if(!string.IsNullOrEmpty(path))
    163         if (instData.TypeList.TryGetValue(path, out string value))
    164           return value;
    165       return null;
    166     }
    167 
    168     private static IList<JsonItem> PopulateParameters(JObject obj, InstData instData) {
    169       IList<JsonItem> list = new List<JsonItem>();
    170 
    171       // add staticParameters
    172       if (obj[Constants.StaticParameters] != null)
    173         foreach (JObject param in obj[Constants.StaticParameters])
    174           list.Add(BuildJsonItem(param, instData));
    175 
    176       // merge staticParameter with freeParameter
    177       if (obj[Constants.FreeParameters] != null) {
    178         foreach (JObject param in obj[Constants.FreeParameters]) {
    179           JsonItem tmp = BuildJsonItem(param, instData);
    180          
    181           // search staticParameter from list
    182           JsonItem comp = null;
    183           foreach (var p in list)
    184             if (p.Name == tmp.Name) comp = p;
    185           if (comp == null)
    186             throw new InvalidDataException($"Invalid {Constants.FreeParameters.Trim('s')}: '{tmp.Name}'!");
    187 
    188           JsonItem.Merge(comp, tmp);
    189         }
    190       }
    191       return list;
    192     }
    193 
    194     private static IList<JsonItem> PopulateOperators(JObject obj, InstData instData) {
    195       IList<JsonItem> list = new List<JsonItem>();
    196       JToken operators = obj[nameof(JsonItem.Operators)];
    197       if (operators != null)
    198         foreach (JObject sp in operators)
    199           list.Add(BuildJsonItem(sp, instData));
    200       return list;
    201     }
    202     #endregion
    203147    #endregion
    204148  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonItem.cs

    r17353 r17354  
    11using System;
    22using System.Collections.Generic;
     3using System.IO;
    34using System.Linq;
    45using System.Text;
     
    1920    private IEnumerable<object> range;
    2021    #endregion
    21    
    22     public string Name { 
    23       get => name; 
     22
     23    public string Name {
     24      get => name;
    2425      set {
    2526        name = value;
    2627        Path = Name;
    2728        UpdatePath();
    28       } 
     29      }
    2930    }
    3031    public string Type { get; set; }
     
    3334    public IList<JsonItem> Operators { get; set; }
    3435    public object Value {
    35       get => value; 
     36      get => value;
    3637      set {
    37         if (value is JContainer)
    38           this.value = ((JContainer)value).ToObject<object[]>();
    39         else
    40           this.value = value;
     38        this.value = value;
    4139        CheckConstraints();
    42       } 
     40      }
    4341    }
    44     public IEnumerable<object> Range { 
    45       get => range; 
     42    public IEnumerable<object> Range {
     43      get => range;
    4644      set {
    4745        range = value;
    4846        CheckConstraints();
    49       } 
     47      }
    5048    }
    5149    public string ActualName { get; set; }
     
    8482        UpdatePathHelper(Operators);
    8583
    86       if(Reference != null)
     84      if (Reference != null)
    8785        UpdatePathHelper(Reference);
    8886    }
     
    9088
    9189    #region Helper
    92     private void UpdatePathHelper(params JsonItem[] items) => 
     90    private void UpdatePathHelper(params JsonItem[] items) =>
    9391      UpdatePathHelper((IEnumerable<JsonItem>)items);
    9492
     
    105103    }
    106104
    107     private bool IsInRange() => IsInRangeList() || 
     105    private bool IsInRange() => IsInRangeList() ||
    108106      (Value.GetType().IsArray && ((object[])Value).All(x => IsInNumericRange(x))) ||
    109107      (!Value.GetType().IsArray && IsInNumericRange(Value));
     
    129127            (((T)max).CompareTo(value) == 1 || ((T)max).CompareTo(value) == 0);
    130128    }
    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    }
    132191    #endregion
    133192  }
Note: See TracChangeset for help on using the changeset viewer.