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

Last change on this file since 17843 was 17843, checked in by dpiringe, 7 weeks 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
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 bool CanConvertType(Type t) =>
14      t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICheckedMultiOperator<>));
15
16    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
17      base.Inject(item, data, root);
18
19      dynamic val = item as dynamic;
20      foreach (var op in val.Operators) {
21        IJsonItem childItem = GetChildItem(op.Name, data);
22        if(childItem != null) {
23          if(childItem is BoolJsonItem boolJsonItem) {
24            val.Operators.SetItemCheckedState(op, boolJsonItem.Value);
25          }
26          root.Inject((IItem)op, childItem, root);
27        }
28      }
29    }
30
31    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
32      IJsonItem item = base.Extract(value, root);
33      dynamic val = value as dynamic;
34      foreach (var op in val.Operators) {
35        IJsonItem operatorItem = new BoolJsonItem() {
36          Name = op.Name,
37          Description = op.Description,
38          Value = val.Operators.ItemChecked(op)
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);
44      }
45      return item;
46    }
47
48    #region Helper
49    private bool GetOperatorState(string name, IJsonItem data) {
50      foreach(var op in data.Children) {
51        if (op.Name == name && op is BoolJsonItem b) return b.Value;
52      }
53      return false;
54    }
55
56    private IJsonItem GetChildItem(string name, IJsonItem parent) {
57      if (parent.Children == null) return null;
58      foreach(var c in parent.Children) {
59        if (c.Name == name) return c;
60      }
61      return null;
62    }
63    #endregion
64  }
65}
Note: See TracBrowser for help on using the repository browser.