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

Last change on this file since 17828 was 17828, checked in by dpiringe, 3 months ago

#3026

  • removed the option to set the value for JsonItems via exporter
    • reworked some base controls
    • added new controls for JsonItem specific properties (e.g. ArrayResizable)
    • deleted a lot of obsolet controls
  • removed the Enable checkbox in the detail view of JsonItems
  • exporter now clones the IOptimizer object
  • added a check + message for unsupported exports
  • list of JsonItems now includes unsupported JsonItems (disabled and marked with 'unsupported')
  • refactored the converter type check
    • now every converter has to specify its supported type(s)
File size: 4.3 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 bool CanConvertType(Type t) =>
17      ConvertableType.IsAssignableFrom(t);
18
19    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
20      IntArray arr = item as IntArray;
21      IntArrayJsonItem intArrayItem = data as IntArrayJsonItem;
22      bool resizeTmp = arr.Resizable;
23      arr.Resizable = true;
24      arr.Length = intArrayItem.Value.Length;
25      for (int i = 0; i < intArrayItem.Value.Length; ++i)
26        arr[i] = intArrayItem.Value[i];
27      arr.Resizable = resizeTmp;
28    }
29
30    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
31      new IntArrayJsonItem() {
32        Name = value.ItemName,
33        Description = value.ItemDescription,
34        Value = ((IntArray)value).CloneAsArray(),
35        Minimum = int.MinValue,
36        Maximum = int.MaxValue
37      };
38  }
39
40  public class DoubleArrayConverter : BaseConverter {
41    public override int Priority => 1;
42    public override Type ConvertableType => typeof(DoubleArray);
43
44    public override bool CanConvertType(Type t) =>
45      ConvertableType.IsAssignableFrom(t);
46
47    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
48      DoubleArray arr = item as DoubleArray;
49      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
50      bool resizeTmp = arr.Resizable;
51      arr.Resizable = true;
52      arr.Length = doubleArrayItem.Value.Length;
53      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
54        arr[i] = doubleArrayItem.Value[i];
55      arr.Resizable = resizeTmp;
56    }
57
58    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
59      new DoubleArrayJsonItem() {
60        Name = value.ItemName,
61        Description = value.ItemDescription,
62        Value = ((DoubleArray)value).CloneAsArray(),
63        Minimum = double.MinValue,
64        Maximum = double.MaxValue
65      };
66  }
67
68  public class PercentArrayConverter : BaseConverter {
69    public override int Priority => 2;
70    public override Type ConvertableType => typeof(PercentArray);
71
72    public override bool CanConvertType(Type t) =>
73      ConvertableType.IsAssignableFrom(t);
74
75    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
76      PercentArray arr = item as PercentArray;
77      DoubleArrayJsonItem doubleArrayItem = data as DoubleArrayJsonItem;
78      bool resizeTmp = arr.Resizable;
79      arr.Resizable = true;
80      arr.Length = doubleArrayItem.Value.Length;
81      for (int i = 0; i < doubleArrayItem.Value.Length; ++i)
82        arr[i] = doubleArrayItem.Value[i];
83      arr.Resizable = resizeTmp;
84    }
85
86    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
87      new DoubleArrayJsonItem() {
88        Name = value.ItemName,
89        Description = value.ItemDescription,
90        Value = ((PercentArray)value).CloneAsArray(),
91        Minimum = 0.0d,
92        Maximum = 1.0d
93      };
94  }
95
96  public class BoolArrayConverter : BaseConverter {
97    public override int Priority => 1;
98    public override Type ConvertableType => typeof(BoolArray);
99
100    public override bool CanConvertType(Type t) =>
101      ConvertableType.IsAssignableFrom(t);
102
103    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
104      BoolArray arr = item as BoolArray;
105      BoolArrayJsonItem boolArrayItem = data as BoolArrayJsonItem;
106      bool resizeTmp = arr.Resizable;
107      arr.Resizable = true;
108      arr.Length = boolArrayItem.Value.Length;
109      for(int i = 0; i < boolArrayItem.Value.Length; ++i)
110        arr[i] = boolArrayItem.Value[i];
111      arr.Resizable = resizeTmp;
112    }
113
114    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
115      new BoolArrayJsonItem() {
116        Name = value.ItemName,
117        Description = value.ItemDescription,
118        Value = ((BoolArray)value).CloneAsArray()
119      };
120  }
121}
Note: See TracBrowser for help on using the repository browser.