source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs @ 17284

Last change on this file since 17284 was 17284, checked in by dpiringe, 3 years ago

#3026: renamed namespace and project from HeuristicLab.Manufacture to HeuristicLab.JsonInterface

File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7
8namespace HeuristicLab.JsonInterface {
9  public class ConstrainedValueParameterConverter : ParameterBaseConverter {
10    public override void InjectData(IParameter parameter, JsonItem data) {
11      foreach (var x in parameter.Cast<dynamic>().ValidValues)
12        if (x.GetType().Name == CastValue<string>(data.Default))
13          parameter.ActualValue = x;
14
15      if (parameter.ActualValue is IParameterizedItem && data.Reference != null)
16        JsonItemConverter.Inject(parameter.ActualValue, data.Reference);
17    }
18
19    public override JsonItem ExtractData(IParameter value) =>
20      new JsonItem() {
21        Name = value.Name,
22        Default = value.ActualValue?.GetType().Name,
23        Range = GetValidValues(value),
24        Parameters = GetParameterizedChilds(value)
25      };
26
27    #region Helper
28    private object[] GetValidValues(IParameter value) {
29      List<object> list = new List<object>();
30      var values = value.Cast<dynamic>().ValidValues;
31      foreach (var x in values) list.Add(x.GetType().Name);
32      return list.ToArray();
33    }
34    // id = kombi aus path + default
35    private IList<JsonItem> GetParameterizedChilds(IParameter value) {
36      List<JsonItem> list = new List<JsonItem>();
37      var values = value.Cast<dynamic>().ValidValues;
38      foreach(var x in values) {
39        if (x is IParameterizedItem) {
40          JsonItem tmp = JsonItemConverter.Extract(x);
41          tmp.PrependPath(value.Name);
42          list.Add(tmp);
43        }
44      }
45      return list.Count == 0 ? null : list;
46    }
47    #endregion
48  }
49}
Note: See TracBrowser for help on using the repository browser.