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 | IJsonItem childItem = GetChildItem(op.Name, data);
|
---|
20 | if(childItem != null) {
|
---|
21 | if(childItem is BoolJsonItem boolJsonItem) {
|
---|
22 | val.Operators.SetItemCheckedState(op, boolJsonItem.Value);
|
---|
23 | }
|
---|
24 | root.Inject((IItem)op, childItem, root);
|
---|
25 | }
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
30 | IJsonItem item = base.Extract(value, root);
|
---|
31 | dynamic val = value as dynamic;
|
---|
32 | foreach (var op in val.Operators) {
|
---|
33 | IJsonItem operatorItem = new BoolJsonItem() {
|
---|
34 | Name = op.Name,
|
---|
35 | Description = op.Description,
|
---|
36 | Value = val.Operators.ItemChecked(op)
|
---|
37 | };
|
---|
38 | IJsonItem c = root.Extract((IItem)op, root);
|
---|
39 | if(c != null && c.Children != null)
|
---|
40 | operatorItem.AddChildren(c.Children);
|
---|
41 | item.AddChildren(operatorItem);
|
---|
42 | }
|
---|
43 | return item;
|
---|
44 | }
|
---|
45 |
|
---|
46 | #region Helper
|
---|
47 | private bool GetOperatorState(string name, IJsonItem data) {
|
---|
48 | foreach(var op in data.Children) {
|
---|
49 | if (op.Name == name && op is BoolJsonItem b) return b.Value;
|
---|
50 | }
|
---|
51 | return false;
|
---|
52 | }
|
---|
53 |
|
---|
54 | private IJsonItem GetChildItem(string name, IJsonItem parent) {
|
---|
55 | if (parent.Children == null) return null;
|
---|
56 | foreach(var c in parent.Children) {
|
---|
57 | if (c.Name == name) return c;
|
---|
58 | }
|
---|
59 | return null;
|
---|
60 | }
|
---|
61 | #endregion
|
---|
62 | }
|
---|
63 | }
|
---|