Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17712 was 17485, checked in by dpiringe, 5 years ago

#3026:

  • fixed a bug in ConstrainedValueParameterConverter -> added an extra check to get the correct child
  • fixed a bug in JsonItemValidator -> now the error messages get appended correctly
  • fixed a validation bug in JsonItemMultiValueControl -> now it should not validate a readonly textbox anymore
File size: 2.4 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 void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
15      StringJsonItem cdata = data as StringJsonItem;
16      IParameter parameter = item as IParameter;
17      foreach (var x in GetValidValues(parameter))
18        if(x.ToString() == cdata.Value)
19          parameter.ActualValue = x;
20
21      if (parameter.ActualValue != null && parameter.ActualValue is IParameterizedItem && cdata.Children != null) {
22        foreach(var child in cdata.Children) {
23          if(child.Name == cdata.Value || child.Path.EndsWith(cdata.Value) || child.Name == parameter.ActualValue.ItemName)
24            root.Inject(parameter.ActualValue, child, root);
25        }
26      }
27    }
28   
29    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
30      IParameter parameter = value as IParameter;
31
32      IJsonItem item = new StringJsonItem() {
33        Name = parameter.Name,
34        Description = value.ItemDescription,
35        Value = parameter.ActualValue?.ToString(),
36        ConcreteRestrictedItems = GetValidValues(parameter).Select(x => x.ToString())
37      };
38      item.AddChildren(GetParameterizedChilds(parameter, root));
39
40      return item;
41    }
42
43    #region Helper
44    private IItem[] GetValidValues(IParameter value) {
45      List<IItem> list = new List<IItem>();
46      var values = ((dynamic)value).ValidValues;
47      foreach (var x in values) list.Add((IItem)x);
48      return list.ToArray();
49    }
50
51    private IList<IJsonItem> GetParameterizedChilds(IParameter value, IJsonItemConverter root) {
52      List<IJsonItem> list = new List<IJsonItem>();
53      var values = ((dynamic)value).ValidValues;
54      foreach(var x in values) {
55        if (x is IParameterizedItem) { // only makes sense for IParameterizedItems to go deeper
56          IJsonItem tmp = root.Extract(x, root);
57          if(!(tmp is UnsupportedJsonItem))
58            list.Add(tmp);
59        }
60      }
61      return list.Count == 0 ? null : list;
62    }
63    #endregion
64  }
65}
Note: See TracBrowser for help on using the repository browser.