source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs @ 17843

Last change on this file since 17843 was 17843, checked in by dpiringe, 8 weeks ago

#3026

  • removed property ConvertableType from all converters
  • removed the option to fixate or loosen the path of JsonItems (obsolete)
  • added a abstract formatter SymbolicRegressionSolutionFormatterBase as base formatter for ISymbolicRegressionSolution
  • unified the construction of exporter controls
  • code cleanup
File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using HeuristicLab.Core;
7using HeuristicLab.Data;
8using Newtonsoft.Json.Linq;
9
10namespace HeuristicLab.JsonInterface {
11
12  public class IntArrayConverter : BaseConverter {
13    public override int Priority => 1;
14
15    public override bool CanConvertType(Type t) =>
16      typeof(IntArray).IsAssignableFrom(t);
17
18    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
19      IntArray arr = item as IntArray;
20      IntArrayJsonItem intArrayItem = data as IntArrayJsonItem;
21      bool resizeTmp = arr.Resizable;
22      arr.Resizable = true;
23      arr.Length = intArrayItem.Value.Length;
24      for (int i = 0; i < intArrayItem.Value.Length; ++i)
25        arr[i] = intArrayItem.Value[i];
26      arr.Resizable = resizeTmp;
27    }
28
29    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
30      new IntArrayJsonItem() {
31        Name = value.ItemName,
32        Description = value.ItemDescription,
33        Value = ((IntArray)value).CloneAsArray(),
34        Minimum = int.MinValue,
35        Maximum = int.MaxValue
36      };
37  }
38
39  public class DoubleArrayConverter : BaseConverter {
40    public override int Priority => 1;
41
42    public override bool CanConvertType(Type t) =>
43      typeof(DoubleArray).IsAssignableFrom(t);
44
45    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
46      DoubleArray arr = item as DoubleArray;
47      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
48      bool resizeTmp = arr.Resizable;
49      arr.Resizable = true;
50      arr.Length = doubleArrayItem.Value.Length;
51      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
52        arr[i] = doubleArrayItem.Value[i];
53      arr.Resizable = resizeTmp;
54    }
55
56    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
57      new DoubleArrayJsonItem() {
58        Name = value.ItemName,
59        Description = value.ItemDescription,
60        Value = ((DoubleArray)value).CloneAsArray(),
61        Minimum = double.MinValue,
62        Maximum = double.MaxValue
63      };
64  }
65
66  public class PercentArrayConverter : BaseConverter {
67    public override int Priority => 2;
68
69    public override bool CanConvertType(Type t) =>
70      typeof(PercentArray).IsAssignableFrom(t);
71
72    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
73      PercentArray arr = item as PercentArray;
74      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
75      bool resizeTmp = arr.Resizable;
76      arr.Resizable = true;
77      arr.Length = doubleArrayItem.Value.Length;
78      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
79        arr[i] = doubleArrayItem.Value[i];
80      arr.Resizable = resizeTmp;
81    }
82
83    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
84      new DoubleArrayJsonItem() {
85        Name = value.ItemName,
86        Description = value.ItemDescription,
87        Value = ((PercentArray)value).CloneAsArray(),
88        Minimum = 0.0d,
89        Maximum = 1.0d
90      };
91  }
92
93  public class BoolArrayConverter : BaseConverter {
94    public override int Priority => 1;
95
96    public override bool CanConvertType(Type t) =>
97      typeof(BoolArray).IsAssignableFrom(t);
98
99    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
100      BoolArray arr = item as BoolArray;
101      BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem;
102      bool resizeTmp = arr.Resizable;
103      arr.Resizable = true;
104      arr.Length = boolArrayItem.Value.Length;
105      for(int i = 0; i < boolArrayItem.Value.Length; ++i)
106        arr[i] = boolArrayItem.Value[i];
107      arr.Resizable = resizeTmp;
108    }
109
110    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
111      new BoolArrayJsonItem() {
112        Name = value.ItemName,
113        Description = value.ItemDescription,
114        Value = ((BoolArray)value).CloneAsArray()
115      };
116  }
117}
Note: See TracBrowser for help on using the repository browser.