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