Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/AlgorithmConverter.cs @ 18057

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

#3026

  • code cleanup
File size: 1.6 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Core;
4using HeuristicLab.Optimization;
5
6namespace HeuristicLab.JsonInterface {
7  public class AlgorithmConverter : ParameterizedItemConverter {
8    public override int Priority => 30;
9
10    public override bool CanConvertType(Type t) =>
11      t.GetInterfaces().Any(x => x == typeof(IAlgorithm));
12
13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
14      base.Inject(item, data, root);
15      IAlgorithm algorithm = item as IAlgorithm;
16      var collection = data.Children.Where(x => x.Name == algorithm.Problem.ItemName);
17      if(collection.Count() > 0) {
18        IJsonItem problemData = collection.First();
19        root.Inject(algorithm.Problem, problemData, root);
20      }
21    }
22   
23    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
24      IJsonItem item = base.Extract(value, root);
25      IAlgorithm algorithm = value as IAlgorithm;
26      foreach (var res in algorithm.Results) {
27        var resultItem = root.Extract(res, root);
28        // fetch all result parameter items
29        var resultParameterItems =
30          item.Where(x => x
31          .GetType()
32          .GetInterfaces()
33          .Any(y => y == typeof(IResultJsonItem)));
34        // check for duplicates (to prevent double result items,
35        // which can occur with result parameters)
36        if(!resultParameterItems.Any(x => x.Name == resultItem.Name))
37          item.AddChildren(resultItem);
38      }
39      item.AddChildren(root.Extract(algorithm.Problem, root));
40      return item;
41    }
42  }
43}
Note: See TracBrowser for help on using the repository browser.