source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.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: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using Newtonsoft.Json.Linq;
7using HeuristicLab.Data;
8using HeuristicLab.Core;
9
10namespace HeuristicLab.JsonInterface {
11  public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
12    public override int Priority => 1;
13    public override Type ConvertableType => typeof(IntMatrix);
14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
15      IntMatrix mat = item as IntMatrix;
16      IntMatrixJsonItem d = data as IntMatrixJsonItem;
17      CopyMatrixData(mat, d.Value);
18    }
19
20    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
21      new IntMatrixJsonItem() {
22        Name = "[OverridableParamName]",
23        Value = Transform((IntMatrix)value),
24        Range = new int[] { int.MinValue, int.MaxValue }
25      };
26  }
27
28  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
29    public override int Priority => 1;
30    public override Type ConvertableType => typeof(DoubleMatrix);
31
32    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
33      DoubleMatrix mat = item as DoubleMatrix;
34      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
35      CopyMatrixData(mat, d.Value);
36    }
37
38    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
39      new DoubleMatrixJsonItem() {
40        Name = "[OverridableParamName]",
41        Value = Transform((DoubleMatrix)value),
42        Range = new double[] { double.MinValue, double.MaxValue }
43      };
44  }
45
46  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
47    public override int Priority => 2;
48    public override Type ConvertableType => typeof(PercentMatrix);
49
50    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
51      PercentMatrix mat = item as PercentMatrix;
52      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
53      CopyMatrixData(mat, d.Value);
54    }
55
56    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
57      new DoubleMatrixJsonItem() {
58        Name = "[OverridableParamName]",
59        Value = Transform((PercentMatrix)value),
60        Range = new double[] { 0.0d, 1.0d }
61      };
62  }
63
64  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
65    public override int Priority => 1;
66    public override Type ConvertableType => typeof(BoolMatrix);
67
68    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
69      BoolMatrix mat = item as BoolMatrix;
70      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
71      CopyMatrixData(mat, d.Value);
72    }
73
74    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
75      new BoolMatrixJsonItem() {
76        Name = "[OverridableParamName]",
77        Value = Transform((BoolMatrix)value),
78        Range = new bool[] { false, true }
79      };
80  }
81
82  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
83    where MatrixType : ValueTypeMatrix<T>
84    where T : struct
85  {
86    #region Helper
87    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
88      var rows = data.Length;
89      var cols = data.Length > 0 ? data[0].Length : 0;
90
91      var rowInfo = matrix.GetType().GetProperty("Rows");
92      rowInfo.SetValue(matrix, rows);
93      var colInfo = matrix.GetType().GetProperty("Columns");
94      colInfo.SetValue(matrix, cols);
95
96      for (int x = 0; x < rows; ++x) {
97        for (int y = 0; y < cols; ++y) {
98          matrix[x, y] = data[x][y];
99        }
100      }
101    }
102
103    protected T[][] Transform(MatrixType matrix) {
104      T[][] m = new T[matrix.Rows][];
105      for (int r = 0; r < matrix.Rows; ++r) {
106        m[r] = new T[matrix.Columns];
107        for (int c = 0; c < matrix.Columns; ++c) {
108          m[r][c] = matrix[r, c];
109        }
110      }
111      return m;
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.