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 AlgorithmConverter : ParameterizedItemConverter {
|
---|
11 | public override int Priority => 30;
|
---|
12 |
|
---|
13 | public override Type ConvertableType => typeof(IAlgorithm);
|
---|
14 |
|
---|
15 | public override bool CanConvertType(Type t) =>
|
---|
16 | t.GetInterfaces().Any(x => x == ConvertableType);
|
---|
17 |
|
---|
18 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
19 | base.Inject(item, data, root);
|
---|
20 | IAlgorithm algorithm = item as IAlgorithm;
|
---|
21 | var collection = data.Children.Where(x => x.Name == algorithm.Problem.ItemName);
|
---|
22 | if(collection.Count() > 0) {
|
---|
23 | IJsonItem problemData = collection.First();
|
---|
24 | root.Inject(algorithm.Problem, problemData, root);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
|
---|
29 | IJsonItem item = base.Extract(value, root);
|
---|
30 | IAlgorithm algorithm = value as IAlgorithm;
|
---|
31 | foreach (var res in algorithm.Results) {
|
---|
32 | item.AddChildren(new ResultJsonItem() {
|
---|
33 | Name = res.Name,
|
---|
34 | Description = res.Description
|
---|
35 | });
|
---|
36 | }
|
---|
37 | item.AddChildren(root.Extract(algorithm.Problem, root));
|
---|
38 | return item;
|
---|
39 | }
|
---|
40 | }
|
---|
41 | }
|
---|