Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17712 was 17484, checked in by dpiringe, 5 years ago

#3026:

  • fixed a bug with JsonItemMultiValueControl -> the size of the matrix should now be saved correctly
  • fixed a bug with RegressionProblemDataConverter -> should now set the row/col sizes correctly
  • simplified the code for saving matrix data in JsonItemMultiValueControl, MatrixValueVM and ArrayValueVM
  • removed unnecessary casts
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    #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}
Note: See TracBrowser for help on using the repository browser.