Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ExperimentConverter.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.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Optimization;
8
9namespace HeuristicLab.JsonInterface {
10  public class ExperimentConverter : BaseConverter {
11    public override int Priority => 10;
12    public override Type ConvertableType => HEAL.Attic.Mapper.StaticCache.GetType(new Guid("A8A4536B-54C1-4A17-AB58-A6006F7F394B"));
13
14    public override bool CanConvertType(Type t) =>
15      ConvertableType.IsAssignableFrom(t);
16
17    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
18      dynamic experiment = (dynamic)value;
19      EmptyJsonItem experimentJI = new EmptyJsonItem() {
20        Name = experiment.Name,
21        Description = value.ItemDescription
22      };
23
24      OptimizerList optimizers = experiment.Optimizers;
25      foreach(var o in optimizers) {
26        var optimizerJI = root.Extract(o, root);
27        if (!(optimizerJI is UnsupportedJsonItem)) {
28          experimentJI.AddChildren(optimizerJI);
29        }
30      }
31
32      int worker = (int)experiment.NumberOfWorkers;
33      experimentJI.AddChildren(new IntJsonItem() {
34        Name = "NumberOfWorkers",
35        Description = "The number of workers for this experiment.",
36        Value = worker,
37        Minimum = 1,
38        Maximum = int.MaxValue
39      });
40
41      return experimentJI;
42    }
43
44    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
45      dynamic experiment = (dynamic)item;
46      OptimizerList optimizers = experiment.Optimizers;
47      if (data.Children != null) {
48        foreach (var i in data.Children) {
49          if (i.Path.EndsWith("NumberOfWorkers") && i is IntJsonItem worker) {
50            experiment.NumberOfWorkers = worker.Value;
51          } else {
52            var optimizer = optimizers.Find(o => i.Path.EndsWith(o.Name));
53            root.Inject(optimizer, i, root);
54          }
55        }
56      }
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.