Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • added initial VM (ArrayValueVM) and control for array values (JsonItemArrayValueControl)
  • new types of JsonItems for better type safety:
    • for arrays: DoubleArrayJsonItem, IntArrayJsonItem, BoolArrayJsonItem
    • for matrix: DoubleMatrixJsonItem, IntMatrixJsonItem, BoolMatrixJsonItem
  • refactored ValueTypeArrayConverter and ValueTypeMatrixConverter -> better type safety with new JsonItems
  • enhanced StringValueVM and implemented JsonItemValidValuesControl with MVVM architecture
  • the VM of JsonItemBaseControl is now protected (was private)
  • improved JsonItem<V,R> -> now handles JTokens correctly
File size: 3.8 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      };
25  }
26
27  public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
28    public override int Priority => 1;
29    public override Type ConvertableType => typeof(DoubleMatrix);
30
31    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
32      DoubleMatrix mat = item as DoubleMatrix;
33      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
34      CopyMatrixData(mat, d.Value);
35    }
36
37    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
38      new DoubleMatrixJsonItem() {
39        Name = "[OverridableParamName]",
40        Value = Transform((DoubleMatrix)value)
41      };
42  }
43
44  public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
45    public override int Priority => 2;
46    public override Type ConvertableType => typeof(PercentMatrix);
47
48    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
49      PercentMatrix mat = item as PercentMatrix;
50      DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
51      CopyMatrixData(mat, d.Value);
52    }
53
54    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
55      new DoubleMatrixJsonItem() {
56        Name = "[OverridableParamName]",
57        Value = Transform((PercentMatrix)value)
58      };
59  }
60
61  public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
62    public override int Priority => 1;
63    public override Type ConvertableType => typeof(BoolMatrix);
64
65    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
66      BoolMatrix mat = item as BoolMatrix;
67      BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
68      CopyMatrixData(mat, d.Value);
69    }
70
71    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
72      new BoolMatrixJsonItem() {
73        Name = "[OverridableParamName]",
74        Value = Transform((BoolMatrix)value)
75      };
76  }
77
78  public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
79    where MatrixType : ValueTypeMatrix<T>
80    where T : struct
81  {
82    #region Helper
83    protected void CopyMatrixData(MatrixType matrix, T[][] data) {
84      var rows = data.Length;
85      var cols = data.Length > 0 ? data[0].Length : 0;
86
87      var rowInfo = matrix.GetType().GetProperty("Rows");
88      rowInfo.SetValue(matrix, rows);
89      var colInfo = matrix.GetType().GetProperty("Columns");
90      colInfo.SetValue(matrix, cols);
91
92      for (int x = 0; x < rows; ++x) {
93        for (int y = 0; y < cols; ++y) {
94          matrix[x, y] = data[x][y];
95        }
96      }
97    }
98
99    protected T[][] Transform(MatrixType matrix) {
100      T[][] m = new T[matrix.Rows][];
101      for (int r = 0; r < matrix.Rows; ++r) {
102        m[r] = new T[matrix.Columns];
103        for (int c = 0; c < matrix.Columns; ++c) {
104          m[r][c] = matrix[r, c];
105        }
106      }
107      return m;
108    }
109    #endregion
110  }
111}
Note: See TracBrowser for help on using the repository browser.