1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using HeuristicLab.Common;
|
---|
7 | using HeuristicLab.Core;
|
---|
8 |
|
---|
9 | namespace 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 | }
|
---|