[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 {
|
---|
[17407] | 10 | public class ConstrainedValueParameterConverter : BaseConverter {
|
---|
[17394] | 11 | public override int Priority => 3;
|
---|
| 12 |
|
---|
[17828] | 13 | public override bool CanConvertType(Type t) =>
|
---|
| 14 | t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IConstrainedValueParameter<>));
|
---|
| 15 |
|
---|
[17407] | 16 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
[17410] | 17 | StringJsonItem cdata = data as StringJsonItem;
|
---|
[17407] | 18 | IParameter parameter = item as IParameter;
|
---|
| 19 | foreach (var x in GetValidValues(parameter))
|
---|
[17477] | 20 | if(x.ToString() == cdata.Value)
|
---|
[17263] | 21 | parameter.ActualValue = x;
|
---|
[17269] | 22 |
|
---|
[17477] | 23 | if (parameter.ActualValue != null && parameter.ActualValue is IParameterizedItem && cdata.Children != null) {
|
---|
| 24 | foreach(var child in cdata.Children) {
|
---|
[17485] | 25 | if(child.Name == cdata.Value || child.Path.EndsWith(cdata.Value) || child.Name == parameter.ActualValue.ItemName)
|
---|
[17477] | 26 | root.Inject(parameter.ActualValue, child, root);
|
---|
[17379] | 27 | }
|
---|
| 28 | }
|
---|
[17263] | 29 | }
|
---|
[17407] | 30 |
|
---|
| 31 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
| 32 | IParameter parameter = value as IParameter;
|
---|
[17266] | 33 |
|
---|
[17410] | 34 | IJsonItem item = new StringJsonItem() {
|
---|
[17407] | 35 | Name = parameter.Name,
|
---|
[17433] | 36 | Description = value.ItemDescription,
|
---|
[17407] | 37 | Value = parameter.ActualValue?.ToString(),
|
---|
[17473] | 38 | ConcreteRestrictedItems = GetValidValues(parameter).Select(x => x.ToString())
|
---|
[17407] | 39 | };
|
---|
[17477] | 40 | item.AddChildren(GetParameterizedChilds(parameter, root));
|
---|
[17266] | 41 |
|
---|
[17407] | 42 | return item;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[17266] | 45 | #region Helper
|
---|
[17353] | 46 | private IItem[] GetValidValues(IParameter value) {
|
---|
| 47 | List<IItem> list = new List<IItem>();
|
---|
[17394] | 48 | var values = ((dynamic)value).ValidValues;
|
---|
[17353] | 49 | foreach (var x in values) list.Add((IItem)x);
|
---|
[17269] | 50 | return list.ToArray();
|
---|
[17266] | 51 | }
|
---|
[17339] | 52 |
|
---|
[17477] | 53 | private IList<IJsonItem> GetParameterizedChilds(IParameter value, IJsonItemConverter root) {
|
---|
[17406] | 54 | List<IJsonItem> list = new List<IJsonItem>();
|
---|
[17394] | 55 | var values = ((dynamic)value).ValidValues;
|
---|
[17269] | 56 | foreach(var x in values) {
|
---|
[17477] | 57 | if (x is IParameterizedItem) { // only makes sense for IParameterizedItems to go deeper
|
---|
| 58 | IJsonItem tmp = root.Extract(x, root);
|
---|
[17404] | 59 | if(!(tmp is UnsupportedJsonItem))
|
---|
| 60 | list.Add(tmp);
|
---|
[17269] | 61 | }
|
---|
| 62 | }
|
---|
| 63 | return list.Count == 0 ? null : list;
|
---|
| 64 | }
|
---|
[17266] | 65 | #endregion
|
---|
[17263] | 66 | }
|
---|
| 67 | }
|
---|