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

Last change on this file since 17420 was 17420, checked in by dpiringe, 9 months ago

#3026:

  • refactored ranged based VMs -> created new 'base' class for ranged based VMs RangedValueBaseVM
  • renamed AddChilds to AddChildren
  • implemented ArrayValueVM and JsonItemArrayValueControl
  • added ranges for array and matrix values
File size: 3.5 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        Value = ((IntArray)value).CloneAsArray(),
31        Range = new int[] { int.MinValue, int.MaxValue }
32      };
33  }
34
35  public class DoubleArrayConverter : BaseConverter {
36    public override int Priority => 1;
37    public override Type ConvertableType => typeof(DoubleArray);
38
39    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
40      DoubleArray arr = item as DoubleArray;
41      double[] d = CastValue<double[]>(data);
42      bool resizeTmp = arr.Resizable;
43      arr.Resizable = true;
44      arr.Length = d.Length;
45      for (int i = 0; i < d.Length; ++i)
46        arr[i] = d[i];
47      arr.Resizable = resizeTmp;
48    }
49
50    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
51      new DoubleArrayJsonItem() {
52        Name = "[OverridableParamName]",
53        Value = ((DoubleArray)value).CloneAsArray(),
54        Range = new double[] { double.MinValue, double.MaxValue }
55      };
56  }
57
58  public class PercentArrayConverter : BaseConverter {
59    public override int Priority => 2;
60    public override Type ConvertableType => typeof(PercentArray);
61
62    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
63      PercentArray arr = item as PercentArray;
64      double[] d = CastValue<double[]>(data);
65      bool resizeTmp = arr.Resizable;
66      arr.Resizable = true;
67      arr.Length = d.Length;
68      for (int i = 0; i < d.Length; ++i)
69        arr[i] = d[i];
70      arr.Resizable = resizeTmp;
71    }
72
73    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
74      new DoubleArrayJsonItem() {
75        Name = "[OverridableParamName]",
76        Value = ((PercentArray)value).CloneAsArray(),
77        Range = new double[] { 0.0d, 1.0d }
78      };
79  }
80
81  public class BoolArrayConverter : BaseConverter {
82    public override int Priority => 1;
83    public override Type ConvertableType => typeof(BoolArray);
84
85    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
86      BoolArray arr = item as BoolArray;
87      bool[] d = CastValue<bool[]>(data);
88      bool resizeTmp = arr.Resizable;
89      arr.Resizable = true;
90      arr.Length = d.Length;
91      for(int i = 0; i < d.Length; ++i)
92        arr[i] = d[i];
93      arr.Resizable = resizeTmp;
94    }
95
96    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
97      new BoolArrayJsonItem() {
98        Name = "[OverridableParamName]",
99        Value = ((BoolArray)value).CloneAsArray(),
100        Range = new bool[] { false, true }
101      };
102  }
103}
Note: See TracBrowser for help on using the repository browser.