1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HeuristicLab.Optimization;
|
---|
5 |
|
---|
6 | namespace 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 | }
|
---|