[17263] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Threading.Tasks;
|
---|
| 6 | using HeuristicLab.Core;
|
---|
| 7 |
|
---|
[17266] | 8 | namespace HeuristicLab.Manufacture {
|
---|
[17281] | 9 | public class ConstrainedValueParameterConverter : ParameterBaseConverter {
|
---|
[17271] | 10 | public override void InjectData(IParameter parameter, Component data) {
|
---|
[17263] | 11 | foreach (var x in parameter.Cast<dynamic>().ValidValues)
|
---|
| 12 | if (x.GetType().Name == CastValue<string>(data.Default))
|
---|
| 13 | parameter.ActualValue = x;
|
---|
[17269] | 14 |
|
---|
| 15 | if (parameter.ActualValue is IParameterizedItem && data.Reference != null)
|
---|
[17281] | 16 | JsonItemConverter.Inject(parameter.ActualValue, data.Reference);
|
---|
[17263] | 17 | }
|
---|
[17266] | 18 |
|
---|
[17280] | 19 | public override Component ExtractData(IParameter value) =>
|
---|
| 20 | new Component() {
|
---|
[17266] | 21 | Name = value.Name,
|
---|
| 22 | Default = value.ActualValue?.GetType().Name,
|
---|
[17269] | 23 | Range = GetValidValues(value),
|
---|
[17280] | 24 | Parameters = GetParameterizedChilds(value)
|
---|
[17266] | 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;
|
---|
[17269] | 31 | foreach (var x in values) list.Add(x.GetType().Name);
|
---|
| 32 | return list.ToArray();
|
---|
[17266] | 33 | }
|
---|
[17280] | 34 | // id = kombi aus path + default
|
---|
[17271] | 35 | private IList<Component> GetParameterizedChilds(IParameter value) {
|
---|
| 36 | List<Component> list = new List<Component>();
|
---|
[17269] | 37 | var values = value.Cast<dynamic>().ValidValues;
|
---|
| 38 | foreach(var x in values) {
|
---|
[17280] | 39 | if (x is IParameterizedItem) {
|
---|
[17281] | 40 | Component tmp = JsonItemConverter.Extract(x);
|
---|
[17280] | 41 | tmp.PrependPath(value.Name);
|
---|
| 42 | list.Add(tmp);
|
---|
[17269] | 43 | }
|
---|
| 44 | }
|
---|
| 45 | return list.Count == 0 ? null : list;
|
---|
| 46 | }
|
---|
[17266] | 47 | #endregion
|
---|
[17263] | 48 | }
|
---|
| 49 | }
|
---|