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

Last change on this file since 18077 was 18077, checked in by dpiringe, 8 months ago

#3026

  • added the dockerhub readme file
  • fixed a bug which caused changed values (changed by events) to be overwritten with wrong values
File size: 4.1 KB
Line 
1using System;
2using HeuristicLab.Core;
3using HeuristicLab.Data;
4
5namespace HeuristicLab.JsonInterface {
6
7  public class IntArrayConverter : BaseConverter {
8    public override int Priority => 1;
9
10    public override bool CanConvertType(Type t) =>
11      typeof(IntArray).IsAssignableFrom(t);
12
13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
14      if(data.Active) {
15        IntArray arr = item as IntArray;
16        IntArrayJsonItem intArrayItem = data as IntArrayJsonItem;
17        bool resizeTmp = arr.Resizable;
18        arr.Resizable = true;
19        arr.Length = intArrayItem.Value.Length;
20        for (int i = 0; i < intArrayItem.Value.Length; ++i)
21          arr[i] = intArrayItem.Value[i];
22        arr.Resizable = resizeTmp;
23      }
24    }
25
26    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
27      new IntArrayJsonItem() {
28        Name = value.ItemName,
29        Description = value.ItemDescription,
30        Value = ((IntArray)value).CloneAsArray(),
31        Minimum = int.MinValue,
32        Maximum = int.MaxValue
33      };
34  }
35
36  public class DoubleArrayConverter : BaseConverter {
37    public override int Priority => 1;
38
39    public override bool CanConvertType(Type t) =>
40      typeof(DoubleArray).IsAssignableFrom(t);
41
42    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
43      if(data.Active) {
44        DoubleArray arr = item as DoubleArray;
45        DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
46        bool resizeTmp = arr.Resizable;
47        arr.Resizable = true;
48        arr.Length = doubleArrayItem.Value.Length;
49        for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
50          arr[i] = doubleArrayItem.Value[i];
51        arr.Resizable = resizeTmp;
52      }
53    }
54
55    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
56      new DoubleArrayJsonItem() {
57        Name = value.ItemName,
58        Description = value.ItemDescription,
59        Value = ((DoubleArray)value).CloneAsArray(),
60        Minimum = double.MinValue,
61        Maximum = double.MaxValue
62      };
63  }
64
65  public class PercentArrayConverter : BaseConverter {
66    public override int Priority => 2;
67
68    public override bool CanConvertType(Type t) =>
69      typeof(PercentArray).IsAssignableFrom(t);
70
71    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
72      if(data.Active) {
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
84    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
85      new DoubleArrayJsonItem() {
86        Name = value.ItemName,
87        Description = value.ItemDescription,
88        Value = ((PercentArray)value).CloneAsArray(),
89        Minimum = 0.0d,
90        Maximum = 1.0d
91      };
92  }
93
94  public class BoolArrayConverter : BaseConverter {
95    public override int Priority => 1;
96
97    public override bool CanConvertType(Type t) =>
98      typeof(BoolArray).IsAssignableFrom(t);
99
100    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
101      if(data.Active) {
102        BoolArray arr = item as BoolArray;
103        BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem;
104        bool resizeTmp = arr.Resizable;
105        arr.Resizable = true;
106        arr.Length = boolArrayItem.Value.Length;
107        for (int i = 0; i < boolArrayItem.Value.Length; ++i)
108          arr[i] = boolArrayItem.Value[i];
109        arr.Resizable = resizeTmp;
110      }
111    }
112
113    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
114      new BoolArrayJsonItem() {
115        Name = value.ItemName,
116        Description = value.ItemDescription,
117        Value = ((BoolArray)value).CloneAsArray()
118      };
119  }
120}
Note: See TracBrowser for help on using the repository browser.