Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 2.1 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
13    public override bool CanConvertType(Type t) =>
[17843]14      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICheckedMultiOperator<>));
[17828]15
[17407]16    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
17      base.Inject(item, data, root);
[17280]18
[17407]19      dynamic val = item as dynamic;
[17439]20      foreach (var op in val.Operators) {
21        IJsonItem childItem = GetChildItem(op.Name, data);
22        if(childItem != null) {
[17473]23          if(childItem is BoolJsonItem boolJsonItem) {
24            val.Operators.SetItemCheckedState(op, boolJsonItem.Value);
[17439]25          }
26          root.Inject((IItem)op, childItem, root);
27        }
28      }
[17407]29    }
[17394]30
[17407]31    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
32      IJsonItem item = base.Extract(value, root);
[17394]33      dynamic val = value as dynamic;
[17269]34      foreach (var op in val.Operators) {
[17439]35        IJsonItem operatorItem = new BoolJsonItem() {
[17269]36          Name = op.Name,
[17433]37          Description = op.Description,
[17473]38          Value = val.Operators.ItemChecked(op)
[17439]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);
[17269]44      }
[17407]45      return item;
[17269]46    }
47
[17407]48    #region Helper
[17406]49    private bool GetOperatorState(string name, IJsonItem data) {
[17379]50      foreach(var op in data.Children) {
[17473]51        if (op.Name == name && op is BoolJsonItem b) return b.Value;
[17269]52      }
53      return false;
54    }
[17439]55
56    private IJsonItem GetChildItem(string name, IJsonItem parent) {
[17473]57      if (parent.Children == null) return null;
[17439]58      foreach(var c in parent.Children) {
59        if (c.Name == name) return c;
60      }
61      return null;
62    }
[17407]63    #endregion
[17269]64  }
65}
Note: See TracBrowser for help on using the repository browser.