Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17444 was 17433, checked in by dpiringe, 4 years ago

#3026:

  • added property Description in IJsonItem and updated all construction calls
  • updated UnsupportedJsonItem with unsupported property Description
  • updated JsonItemBaseControl and JsonItemVMBase for new property Description
File size: 4.2 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        Description = value.ItemDescription,
24        Value = Transform((IntMatrix)value),
25        Range = new int[] { int.MinValue, int.MaxValue }
26      };
27  }
28
29  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
30    public override int Priority => 1;
31    public override Type ConvertableType => typeof(DoubleMatrix);
32
33    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
34      DoubleMatrix mat = item as DoubleMatrix;
35      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
36      CopyMatrixData(mat, d.Value);
37    }
38
39    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
40      new DoubleMatrixJsonItem() {
41        Name = "[OverridableParamName]",
42        Description = value.ItemDescription,
43        Value = Transform((DoubleMatrix)value),
44        Range = new double[] { double.MinValue, double.MaxValue }
45      };
46  }
47
48  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
49    public override int Priority => 2;
50    public override Type ConvertableType => typeof(PercentMatrix);
51
52    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
53      PercentMatrix mat = item as PercentMatrix;
54      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
55      CopyMatrixData(mat, d.Value);
56    }
57
58    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
59      new DoubleMatrixJsonItem() {
60        Name = "[OverridableParamName]",
61        Description = value.ItemDescription,
62        Value = Transform((PercentMatrix)value),
63        Range = new double[] { 0.0d, 1.0d }
64      };
65  }
66
67  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
68    public override int Priority => 1;
69    public override Type ConvertableType => typeof(BoolMatrix);
70
71    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
72      BoolMatrix mat = item as BoolMatrix;
73      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
74      CopyMatrixData(mat, d.Value);
75    }
76
77    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
78      new BoolMatrixJsonItem() {
79        Name = "[OverridableParamName]",
80        Description = value.ItemDescription,
81        Value = Transform((BoolMatrix)value),
82        Range = new bool[] { false, true }
83      };
84  }
85
86  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
87    where MatrixType : ValueTypeMatrix<T>
88    where T : struct
89  {
90    #region Helper
91    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
92      var rows = data.Length;
93      var cols = data.Length > 0 ? data[0].Length : 0;
94
95      var rowInfo = matrix.GetType().GetProperty("Rows");
96      rowInfo.SetValue(matrix, rows);
97      var colInfo = matrix.GetType().GetProperty("Columns");
98      colInfo.SetValue(matrix, cols);
99
100      for (int x = 0; x < rows; ++x) {
101        for (int y = 0; y < cols; ++y) {
102          matrix[x, y] = data[x][y];
103        }
104      }
105    }
106
107    protected T[][] Transform(MatrixType matrix) {
108      T[][] m = new T[matrix.Rows][];
109      for (int r = 0; r < matrix.Rows; ++r) {
110        m[r] = new T[matrix.Columns];
111        for (int c = 0; c < matrix.Columns; ++c) {
112          m[r][c] = matrix[r, c];
113        }
114      }
115      return m;
116    }
117    #endregion
118  }
119}
Note: See TracBrowser for help on using the repository browser.