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