Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17834 was 17828, checked in by dpiringe, 3 years ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 2.2 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
13    public override Type ConvertableType => typeof(ICheckedMultiOperator<>);
14
15    public override bool CanConvertType(Type t) =>
16      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == ConvertableType);
17
18    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
19      base.Inject(item, data, root);
20
21      dynamic val = item as dynamic;
22      foreach (var op in val.Operators) {
23        IJsonItem childItem = GetChildItem(op.Name, data);
24        if(childItem != null) {
25          if(childItem is BoolJsonItem boolJsonItem) {
26            val.Operators.SetItemCheckedState(op, boolJsonItem.Value);
27          }
28          root.Inject((IItem)op, childItem, root);
29        }
30      }
31    }
32
33    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
34      IJsonItem item = base.Extract(value, root);
35      dynamic val = value as dynamic;
36      foreach (var op in val.Operators) {
37        IJsonItem operatorItem = new BoolJsonItem() {
38          Name = op.Name,
39          Description = op.Description,
40          Value = val.Operators.ItemChecked(op)
41        };
42        IJsonItem c = root.Extract((IItem)op, root);
43        if(c != null && c.Children != null)
44          operatorItem.AddChildren(c.Children);
45        item.AddChildren(operatorItem);
46      }
47      return item;
48    }
49
50    #region Helper
51    private bool GetOperatorState(string name, IJsonItem data) {
52      foreach(var op in data.Children) {
53        if (op.Name == name && op is BoolJsonItem b) return b.Value;
54      }
55      return false;
56    }
57
58    private IJsonItem GetChildItem(string name, IJsonItem parent) {
59      if (parent.Children == null) return null;
60      foreach(var c in parent.Children) {
61        if (c.Name == name) return c;
62      }
63      return null;
64    }
65    #endregion
66  }
67}
Note: See TracBrowser for help on using the repository browser.