source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.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.7 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
15    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
16      IntMatrix mat = item as IntMatrix;
17      IntMatrixJsonItem d = data as IntMatrixJsonItem;
18      CopyMatrixData(mat, d.Value);
19    }
20
21    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
22      new IntMatrixJsonItem() {
23        Name = value.ItemName,
24        Description = value.ItemDescription,
25        Value = Transform((IntMatrix)value),
26        Minimum = int.MinValue,
27        Maximum = int.MaxValue
28      };
29  }
30
31  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
32    public override int Priority => 1;
33    public override Type ConvertableType => typeof(DoubleMatrix);
34
35    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
36      DoubleMatrix mat = item as DoubleMatrix;
37      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
38      CopyMatrixData(mat, d.Value);
39    }
40
41    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
42      new DoubleMatrixJsonItem() {
43        Name = value.ItemName,
44        Description = value.ItemDescription,
45        Value = Transform((DoubleMatrix)value),
46        Minimum = double.MinValue,
47        Maximum = double.MaxValue,
48        RowNames = ((DoubleMatrix)value).RowNames,
49        ColumnNames = ((DoubleMatrix)value).ColumnNames
50      };
51  }
52
53  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
54    public override int Priority => 2;
55    public override Type ConvertableType => typeof(PercentMatrix);
56
57    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
58      PercentMatrix mat = item as PercentMatrix;
59      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
60      CopyMatrixData(mat, d.Value);
61    }
62
63    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
64      new DoubleMatrixJsonItem() {
65        Name = value.ItemName,
66        Description = value.ItemDescription,
67        Value = Transform((PercentMatrix)value),
68        Minimum = 0.0d,
69        Maximum = 1.0d
70      };
71  }
72
73  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
74    public override int Priority => 1;
75    public override Type ConvertableType => typeof(BoolMatrix);
76
77    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
78      BoolMatrix mat = item as BoolMatrix;
79      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
80      CopyMatrixData(mat, d.Value);
81    }
82
83    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
84      new BoolMatrixJsonItem() {
85        Name = value.ItemName,
86        Description = value.ItemDescription,
87        Value = Transform((BoolMatrix)value)
88      };
89  }
90
91  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
92    where MatrixType : ValueTypeMatrix<T>
93    where T : struct
94  {
95    public override bool CanConvertType(Type t) =>
96      ConvertableType.IsAssignableFrom(t);
97
98    #region Helper
99    /// <summary>
100    /// Copies the data into the matrix object. Uses reflection to set the
101    /// row and column size of the matrix, because it is not possible to
102    /// create a new matrix at this point and there exists no public resize method.
103    /// </summary>
104    /// <param name="matrix"></param>
105    /// <param name="data"></param>
106    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
107      var cols = data.Length;
108      var rows = data.Length > 0 ? data[0].Length : 0;
109
110      var rowInfo = matrix.GetType().GetProperty("Rows");
111      rowInfo.SetValue(matrix, rows);
112      var colInfo = matrix.GetType().GetProperty("Columns");
113      colInfo.SetValue(matrix, cols);
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.