Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17404 was 17404, checked in by dpiringe, 4 years ago

#3026:

  • fixed a bug in BaseConverter -> the range for percent values should be returned correctly now
  • fixed a bug in ConstrainedValueParameterConverter, ParameterizedItemConverter, ValueParameterConverter -> unsupported json items get filtered now
  • JCGenerator is now a dynamic class and can return all JsonItems for an IOptimizer now (instead of string only) + it is now possible to generate an template string with an IEnumerable<JsonItem>
  • added first version of an export dialog for JsonInterface
    • it is organized with a main view (for the dialog) and some user controls (for a better visualization of an JsonItem -> to reduce wrong user inputs)
    • the user controls inherit a base control, which organizes some base values of an JsonItem
File size: 2.0 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 : ParameterBaseConverter {
11    public override int Priority => 3;
12    public override Type ConvertableType => typeof(IConstrainedValueParameter<>);
13
14    public override void InjectData(IParameter parameter, JsonItem data, IJsonItemConverter root) {
15      foreach(var x in GetValidValues(parameter))
16        if(x.ToString() == CastValue<string>(data.Value))
17          parameter.ActualValue = x;
18
19      if (parameter.ActualValue is IParameterizedItem && data.Children != null) {
20        foreach(var param in data.Children) {
21          if(param.Name == parameter.ActualValue.ItemName)
22            root.Inject(parameter.ActualValue, param, root);
23        }
24      }
25    }
26
27    public override void Populate(IParameter value, JsonItem item, IJsonItemConverter root) {
28      item.AddChilds(GetParameterizedChilds(value));
29      item.Name = value.Name;
30      item.Value = value.ActualValue?.ToString();
31      item.Range = GetValidValues(value).Select(x => x.ToString());
32    }     
33
34    #region Helper
35    private IItem[] GetValidValues(IParameter value) {
36      List<IItem> list = new List<IItem>();
37      var values = ((dynamic)value).ValidValues;
38      foreach (var x in values) list.Add((IItem)x);
39      return list.ToArray();
40    }
41
42    private IList<JsonItem> GetParameterizedChilds(IParameter value) {
43      List<JsonItem> list = new List<JsonItem>();
44      var values = ((dynamic)value).ValidValues;
45      foreach(var x in values) {
46        if (x is IParameterizedItem) {
47          JsonItem tmp = JsonItemConverter.Extract(x);
48          if(!(tmp is UnsupportedJsonItem))
49            list.Add(tmp);
50        }
51      }
52      return list.Count == 0 ? null : list;
53    }
54    #endregion
55  }
56}
Note: See TracBrowser for help on using the repository browser.