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 MultiCheckedOperatorConverter : ParameterizedItemConverter {
|
---|
11 | public override int Priority => 3;
|
---|
12 | public override Type ConvertableType => typeof(ICheckedMultiOperator<>);
|
---|
13 |
|
---|
14 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
15 | base.Inject(item, data, root);
|
---|
16 |
|
---|
17 | dynamic val = item as dynamic;
|
---|
18 | foreach (var op in val.Operators)
|
---|
19 | val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data));
|
---|
20 | }
|
---|
21 |
|
---|
22 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
23 | IJsonItem item = base.Extract(value, root);
|
---|
24 | dynamic val = value as dynamic;
|
---|
25 | foreach (var op in val.Operators) {
|
---|
26 | item.AddChilds(new BoolJsonItem() {
|
---|
27 | Name = op.Name,
|
---|
28 | Value = val.Operators.ItemChecked(op),
|
---|
29 | Range = new bool[] { false, true }
|
---|
30 | });
|
---|
31 | }
|
---|
32 | return item;
|
---|
33 | }
|
---|
34 |
|
---|
35 |
|
---|
36 | #region Helper
|
---|
37 | private bool GetOperatorState(string name, IJsonItem data) {
|
---|
38 | foreach(var op in data.Children) {
|
---|
39 | if (op.Name == name && op.Value is bool) return (bool)op.Value;
|
---|
40 | }
|
---|
41 | return false;
|
---|
42 | }
|
---|
43 | #endregion
|
---|
44 | }
|
---|
45 | }
|
---|