source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs @ 18043

Last change on this file since 18043 was 18043, checked in by dpiringe, 11 months ago

#3026

  • code cleanup
File size: 4.6 KB
Line 
1using System;
2using HeuristicLab.Data;
3using HeuristicLab.Core;
4using System.Reflection;
5
6namespace HeuristicLab.JsonInterface {
7  public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
8    public override int Priority => 1;
9
10    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
11      IntMatrix mat = item as IntMatrix;
12      IntMatrixJsonItem d = data as IntMatrixJsonItem;
13      CopyMatrixData(mat, d.Value);
14    }
15
16    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
17      new IntMatrixJsonItem() {
18        Name = value.ItemName,
19        Description = value.ItemDescription,
20        Value = Transform((IntMatrix)value),
21        Minimum = int.MinValue,
22        Maximum = int.MaxValue
23      };
24  }
25
26  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
27    public override int Priority => 1;
28
29    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
30      DoubleMatrix mat = item as DoubleMatrix;
31      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
32      CopyMatrixData(mat, d.Value);
33    }
34
35    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
36      new DoubleMatrixJsonItem() {
37        Name = value.ItemName,
38        Description = value.ItemDescription,
39        Value = Transform((DoubleMatrix)value),
40        Minimum = double.MinValue,
41        Maximum = double.MaxValue,
42        RowNames = ((DoubleMatrix)value).RowNames,
43        ColumnNames = ((DoubleMatrix)value).ColumnNames
44      };
45  }
46
47  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
48    public override int Priority => 2;
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 = value.ItemName,
59        Description = value.ItemDescription,
60        Value = Transform((PercentMatrix)value),
61        Minimum = 0.0d,
62        Maximum = 1.0d
63      };
64  }
65
66  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
67    public override int Priority => 1;
68
69    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
70      BoolMatrix mat = item as BoolMatrix;
71      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
72      CopyMatrixData(mat, d.Value);
73    }
74
75    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
76      new BoolMatrixJsonItem() {
77        Name = value.ItemName,
78        Description = value.ItemDescription,
79        Value = Transform((BoolMatrix)value)
80      };
81  }
82
83  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
84    where MatrixType : ValueTypeMatrix<T>
85    where T : struct
86  {
87    public override bool CanConvertType(Type t) =>
88      typeof(MatrixType).IsAssignableFrom(t);
89
90    #region Helper
91    /// <summary>
92    /// Copies the data into the matrix object. Uses reflection to set the
93    /// row and column size of the matrix, because it is not possible to
94    /// create a new matrix at this point and there exists no public resize method.
95    /// </summary>
96    /// <param name="matrix"></param>
97    /// <param name="data"></param>
98    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
99      var cols = data.Length;
100      var rows = data.Length > 0 ? data[0].Length : 0;
101
102
103      // matrix
104      var t = matrix.GetType();
105      var matrixInfo = matrix.GetType().GetField("matrix", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
106      matrixInfo.SetValue(matrix, new T[rows, cols]);
107      /*
108      var rowInfo = matrix.GetType().GetProperty("Rows");
109      rowInfo.SetValue(matrix, rows); // TODO
110      var colInfo = matrix.GetType().GetProperty("Columns");
111      colInfo.SetValue(matrix, cols);
112      */
113
114
115      for (int x = 0; x < rows; ++x) {
116        for (int y = 0; y < cols; ++y) {
117          matrix[x, y] = data[y][x];
118        }
119      }
120    }
121
122    protected T[][] Transform(MatrixType matrix) {
123      T[][] m = new T[matrix.Columns][];
124      for (int column = 0; column < matrix.Columns; ++column) {
125        m[column] = new T[matrix.Rows];
126        for (int row = 0; row < matrix.Rows; ++row) {
127          m[column][row] = matrix[row, column];
128        }
129      }
130      return m;
131    }
132    #endregion
133  }
134}
Note: See TracBrowser for help on using the repository browser.