[17263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
[17353] | 6 | using HeuristicLab.Common;
|
---|
[17263] | 7 | using HeuristicLab.Core;
|
---|
| 8 |
|
---|
[17284] | 9 | namespace HeuristicLab.JsonInterface {
|
---|
[17281] | 10 | public class ConstrainedValueParameterConverter : ParameterBaseConverter {
|
---|
[17394] | 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) {
|
---|
[17353] | 15 | foreach(var x in GetValidValues(parameter))
|
---|
| 16 | if(x.ToString() == CastValue<string>(data.Value))
|
---|
[17263] | 17 | parameter.ActualValue = x;
|
---|
[17269] | 18 |
|
---|
[17379] | 19 | if (parameter.ActualValue is IParameterizedItem && data.Children != null) {
|
---|
| 20 | foreach(var param in data.Children) {
|
---|
| 21 | if(param.Name == parameter.ActualValue.ItemName)
|
---|
[17394] | 22 | root.Inject(parameter.ActualValue, param, root);
|
---|
[17379] | 23 | }
|
---|
| 24 | }
|
---|
[17263] | 25 | }
|
---|
[17266] | 26 |
|
---|
[17394] | 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 | }
|
---|
[17266] | 33 |
|
---|
| 34 | #region Helper
|
---|
[17353] | 35 | private IItem[] GetValidValues(IParameter value) {
|
---|
| 36 | List<IItem> list = new List<IItem>();
|
---|
[17394] | 37 | var values = ((dynamic)value).ValidValues;
|
---|
[17353] | 38 | foreach (var x in values) list.Add((IItem)x);
|
---|
[17269] | 39 | return list.ToArray();
|
---|
[17266] | 40 | }
|
---|
[17339] | 41 |
|
---|
[17283] | 42 | private IList<JsonItem> GetParameterizedChilds(IParameter value) {
|
---|
| 43 | List<JsonItem> list = new List<JsonItem>();
|
---|
[17394] | 44 | var values = ((dynamic)value).ValidValues;
|
---|
[17269] | 45 | foreach(var x in values) {
|
---|
[17280] | 46 | if (x is IParameterizedItem) {
|
---|
[17283] | 47 | JsonItem tmp = JsonItemConverter.Extract(x);
|
---|
[17280] | 48 | list.Add(tmp);
|
---|
[17269] | 49 | }
|
---|
| 50 | }
|
---|
| 51 | return list.Count == 0 ? null : list;
|
---|
| 52 | }
|
---|
[17266] | 53 | #endregion
|
---|
[17263] | 54 | }
|
---|
| 55 | }
|
---|