Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17451 was 17451, checked in by dpiringe, 4 years ago

#3026:

  • renamed ResultItem to ResultJsonItem
  • implemented RegressionProblemDataConverter
  • added support for "named matrices" (named = rows and columns have names) -> INamedMatrixJsonItem incl. base classes and views
  • added a bool property Active in IJsonItem -> marks items, which should be written into the template
File size: 3.8 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    public override Type ConvertableType => typeof(IntArray);
15
16    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
17      IntArray arr = item as IntArray;
18      int[] d = CastValue<int[]>(data);
19      bool resizeTmp = arr.Resizable;
20      arr.Resizable = true;
21      arr.Length = d.Length;
22      for (int i = 0; i < d.Length; ++i)
23        arr[i] = d[i];
24      arr.Resizable = resizeTmp;
25    }
26
27    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
28      new IntArrayJsonItem() {
29        Name = "[OverridableParamName]",
30        Description = value.ItemDescription,
31        Value = ((IntArray)value).CloneAsArray(),
32        Range = new int[] { int.MinValue, int.MaxValue }
33      };
34  }
35
36  public class DoubleArrayConverter : BaseConverter {
37    public override int Priority => 1;
38    public override Type ConvertableType => typeof(DoubleArray);
39
40    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
41      DoubleArray arr = item as DoubleArray;
42      DoubleArrayJsonItem doubleItem = data as DoubleArrayJsonItem;
43      //double[] d = CastValue<double[]>(data);
44      bool resizeTmp = arr.Resizable;
45      arr.Resizable = true;
46      //arr.Length = d.Length;
47      arr.Length = doubleItem.Value.Length;
48      for (int i = 0; i < doubleItem.Value.Length; ++i)
49        arr[i] = doubleItem.Value[i];
50      arr.Resizable = resizeTmp;
51    }
52
53    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
54      new DoubleArrayJsonItem() {
55        Name = "[OverridableParamName]",
56        Description = value.ItemDescription,
57        Value = ((DoubleArray)value).CloneAsArray(),
58        Range = new double[] { double.MinValue, double.MaxValue }
59      };
60  }
61
62  public class PercentArrayConverter : BaseConverter {
63    public override int Priority => 2;
64    public override Type ConvertableType => typeof(PercentArray);
65
66    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
67      PercentArray arr = item as PercentArray;
68      double[] d = CastValue<double[]>(data);
69      bool resizeTmp = arr.Resizable;
70      arr.Resizable = true;
71      arr.Length = d.Length;
72      for (int i = 0; i < d.Length; ++i)
73        arr[i] = d[i];
74      arr.Resizable = resizeTmp;
75    }
76
77    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
78      new DoubleArrayJsonItem() {
79        Name = "[OverridableParamName]",
80        Description = value.ItemDescription,
81        Value = ((PercentArray)value).CloneAsArray(),
82        Range = new double[] { 0.0d, 1.0d }
83      };
84  }
85
86  public class BoolArrayConverter : BaseConverter {
87    public override int Priority => 1;
88    public override Type ConvertableType => typeof(BoolArray);
89
90    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
91      BoolArray arr = item as BoolArray;
92      bool[] d = CastValue<bool[]>(data);
93      bool resizeTmp = arr.Resizable;
94      arr.Resizable = true;
95      arr.Length = d.Length;
96      for(int i = 0; i < d.Length; ++i)
97        arr[i] = d[i];
98      arr.Resizable = resizeTmp;
99    }
100
101    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
102      new BoolArrayJsonItem() {
103        Name = "[OverridableParamName]",
104        Description = value.ItemDescription,
105        Value = ((BoolArray)value).CloneAsArray(),
106        Range = new bool[] { false, true }
107      };
108  }
109}
Note: See TracBrowser for help on using the repository browser.