Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs @ 17473

Last change on this file since 17473 was 17473, checked in by dpiringe, 4 years ago

#3026:

  • refactored inheritance structure of json items, now the default JsonItem is an abstract class without properties Value and Range -> splitted up into new interfaces
  • updated view models for new json item structure
  • updated SingleLineArrayJsonWriter
File size: 2.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8
9namespace 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
47    #region Helper
48    private bool GetOperatorState(string name, IJsonItem data) {
49      foreach(var op in data.Children) {
50        if (op.Name == name && op is BoolJsonItem b) return b.Value;
51      }
52      return false;
53    }
54
55    private IJsonItem GetChildItem(string name, IJsonItem parent) {
56      if (parent.Children == null) return null;
57      foreach(var c in parent.Children) {
58        if (c.Name == name) return c;
59      }
60      return null;
61    }
62    #endregion
63  }
64}
Note: See TracBrowser for help on using the repository browser.