source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.Manufacture/Converters/ConstrainedValueParameterConverter.cs @ 17281

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

#3026: renamed Transformers -> ...Transformer to ...Converter, ITypeTransformer to IJsonItemConverter, Transformer to JsonItemConverter

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.Manufacture {
9  public class ConstrainedValueParameterConverter : ParameterBaseConverter {
10    public override void InjectData(IParameter parameter, Component 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 Component ExtractData(IParameter value) =>
20      new Component() {
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<Component> GetParameterizedChilds(IParameter value) {
36      List<Component> list = new List<Component>();
37      var values = value.Cast<dynamic>().ValidValues;
38      foreach(var x in values) {
39        if (x is IParameterizedItem) {
40          Component 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.