Free cookie consent management tool by TermsFeed Policy Generator

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

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

#3026:

  • removed classes:
    • CheckedItemListConverter: unnecessary
    • ItemCollectionConverter: unnecessary
    • PrimitiveConverter: not possible to implement because it needs to Extract/Inject from/into objects (but interfaces pretends IItem)
    • StorableConverter: unnecessary
    • ConfigurableConverter: unnecessary
  • removed graphviz code in Heuristiclab.ConfigStarter/Program.cs
  • updated Constants
  • some simple code refactors in BaseConverter
  • in JsonItem:
    • renamed Parameters -> Children
    • removed Properties: Operators, Type, Reference, IsConfigurable, IsParameterizedItem
    • removed unnecessary/old code
  • implemented a new way to get data from an object, which is a matrix, in ValueTypeMatrixConverter method: CopyMatrixData
    • converts the object into an array -> rows: from array.Length, cols: when the length is > 0 pick length of first array of index 0 (it is saved as an array of arrays)
  • created a binding flag const in ValueRangeConverter to prevent duplicates in code
File size: 1.4 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 ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
12    where MatrixType : ValueTypeMatrix<T>
13    where T : struct
14  {
15    public override void InjectData(IItem item, JsonItem data) =>
16      CopyMatrixData(item.Cast<MatrixType>(), data.Value);
17
18    public override JsonItem ExtractData(IItem value) =>
19      new JsonItem() {
20        Name = "[OverridableParamName]",
21        Value = value.Cast<MatrixType>().CloneAsMatrix()
22      };
23
24    #region Helper
25    private void CopyMatrixData(MatrixType matrix, object data) {
26      if (data is Array arr) {
27        var rows = arr.Length;
28        var cols = arr.Length > 0 && arr.GetValue(0) is JArray jarr ? jarr.Count : 0;
29
30        var rowInfo = matrix.GetType().GetProperty("Rows");
31        rowInfo.SetValue(matrix, rows);
32        var colInfo = matrix.GetType().GetProperty("Columns");
33        colInfo.SetValue(matrix, cols);
34
35        for (int x = 0; x < rows; ++x) {
36          jarr = (JArray)arr.GetValue(x);
37          for (int y = 0; y < cols; ++y) {
38            matrix[x, y] = jarr[y].ToObject<T>();
39          }
40        }
41      }
42    }
43    #endregion
44  }
45}
Note: See TracBrowser for help on using the repository browser.