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

Last change on this file since 17828 was 17828, checked in by dpiringe, 2 months ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 2.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8
9namespace HeuristicLab.JsonInterface {
10  public class ConstrainedValueParameterConverter : BaseConverter {
11    public override int Priority => 3;
12    public override Type ConvertableType => typeof(IConstrainedValueParameter<>);
13
14    public override bool CanConvertType(Type t) =>
15      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConstrainedValueParameter<>));
16
17    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
18      StringJsonItem cdata = data as StringJsonItem;
19      IParameter parameter = item as IParameter;
20      foreach (var x in GetValidValues(parameter))
21        if(x.ToString() == cdata.Value)
22          parameter.ActualValue = x;
23
24      if (parameter.ActualValue != null && parameter.ActualValue is IParameterizedItem && cdata.Children != null) {
25        foreach(var child in cdata.Children) {
26          if(child.Name == cdata.Value || child.Path.EndsWith(cdata.Value) || child.Name == parameter.ActualValue.ItemName)
27            root.Inject(parameter.ActualValue, child, root);
28        }
29      }
30    }
31   
32    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
33      IParameter parameter = value as IParameter;
34
35      IJsonItem item = new StringJsonItem() {
36        Name = parameter.Name,
37        Description = value.ItemDescription,
38        Value = parameter.ActualValue?.ToString(),
39        ConcreteRestrictedItems = GetValidValues(parameter).Select(x => x.ToString())
40      };
41      item.AddChildren(GetParameterizedChilds(parameter, root));
42
43      return item;
44    }
45
46    #region Helper
47    private IItem[] GetValidValues(IParameter value) {
48      List<IItem> list = new List<IItem>();
49      var values = ((dynamic)value).ValidValues;
50      foreach (var x in values) list.Add((IItem)x);
51      return list.ToArray();
52    }
53
54    private IList<IJsonItem> GetParameterizedChilds(IParameter value, IJsonItemConverter root) {
55      List<IJsonItem> list = new List<IJsonItem>();
56      var values = ((dynamic)value).ValidValues;
57      foreach(var x in values) {
58        if (x is IParameterizedItem) { // only makes sense for IParameterizedItems to go deeper
59          IJsonItem tmp = root.Extract(x, root);
60          if(!(tmp is UnsupportedJsonItem))
61            list.Add(tmp);
62        }
63      }
64      return list.Count == 0 ? null : list;
65    }
66    #endregion
67  }
68}
Note: See TracBrowser for help on using the repository browser.