Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17828 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
RevLine 
[17269]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
[17353]6using HeuristicLab.Common;
[17269]7using HeuristicLab.Core;
8
[17284]9namespace HeuristicLab.JsonInterface {
[17281]10  public class MultiCheckedOperatorConverter : ParameterizedItemConverter {
[17394]11    public override int Priority => 3;
[17828]12
[17394]13    public override Type ConvertableType => typeof(ICheckedMultiOperator<>);
[17828]14
15    public override bool CanConvertType(Type t) =>
16      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == ConvertableType);
17
[17407]18    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
19      base.Inject(item, data, root);
[17280]20
[17407]21      dynamic val = item as dynamic;
[17439]22      foreach (var op in val.Operators) {
23        IJsonItem childItem = GetChildItem(op.Name, data);
24        if(childItem != null) {
[17473]25          if(childItem is BoolJsonItem boolJsonItem) {
26            val.Operators.SetItemCheckedState(op, boolJsonItem.Value);
[17439]27          }
28          root.Inject((IItem)op, childItem, root);
29        }
30      }
[17407]31    }
[17394]32
[17407]33    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
34      IJsonItem item = base.Extract(value, root);
[17394]35      dynamic val = value as dynamic;
[17269]36      foreach (var op in val.Operators) {
[17439]37        IJsonItem operatorItem = new BoolJsonItem() {
[17269]38          Name = op.Name,
[17433]39          Description = op.Description,
[17473]40          Value = val.Operators.ItemChecked(op)
[17439]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);
[17269]46      }
[17407]47      return item;
[17269]48    }
49
[17407]50    #region Helper
[17406]51    private bool GetOperatorState(string name, IJsonItem data) {
[17379]52      foreach(var op in data.Children) {
[17473]53        if (op.Name == name && op is BoolJsonItem b) return b.Value;
[17269]54      }
55      return false;
56    }
[17439]57
58    private IJsonItem GetChildItem(string name, IJsonItem parent) {
[17473]59      if (parent.Children == null) return null;
[17439]60      foreach(var c in parent.Children) {
61        if (c.Name == name) return c;
62      }
63      return null;
64    }
[17407]65    #endregion
[17269]66  }
67}
Note: See TracBrowser for help on using the repository browser.