1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Threading.Tasks;
|
---|
6 | using Newtonsoft.Json.Linq;
|
---|
7 | using HeuristicLab.Data;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.JsonInterface {
|
---|
11 | public class IntMatrixConverter : ValueTypeMatrixConverter<IntMatrix, int> {
|
---|
12 | public override int Priority => 1;
|
---|
13 | public override Type ConvertableType => typeof(IntMatrix);
|
---|
14 | }
|
---|
15 |
|
---|
16 | public class DoubleMatrixConverter : ValueTypeMatrixConverter<DoubleMatrix, double> {
|
---|
17 | public override int Priority => 1;
|
---|
18 | public override Type ConvertableType => typeof(DoubleMatrix);
|
---|
19 | }
|
---|
20 |
|
---|
21 | public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
|
---|
22 | public override int Priority => 2;
|
---|
23 | public override Type ConvertableType => typeof(PercentMatrix);
|
---|
24 | }
|
---|
25 |
|
---|
26 | public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
|
---|
27 | public override int Priority => 1;
|
---|
28 | public override Type ConvertableType => typeof(BoolMatrix);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
|
---|
32 | where MatrixType : ValueTypeMatrix<T>
|
---|
33 | where T : struct
|
---|
34 | {
|
---|
35 | public override void InjectData(IItem item, JsonItem data, IJsonItemConverter root) =>
|
---|
36 | CopyMatrixData(item as MatrixType, data.Value);
|
---|
37 |
|
---|
38 | public override void Populate(IItem value, JsonItem item, IJsonItemConverter root) {
|
---|
39 | item.Name = "[OverridableParamName]";
|
---|
40 | item.Value = ((MatrixType)value).CloneAsMatrix();
|
---|
41 | }
|
---|
42 |
|
---|
43 | #region Helper
|
---|
44 | private void CopyMatrixData(MatrixType matrix, object data) {
|
---|
45 | if (data is Array arr) {
|
---|
46 | var rows = arr.Length;
|
---|
47 | var cols = arr.Length > 0 && arr.GetValue(0) is JArray jarr ? jarr.Count : 0;
|
---|
48 |
|
---|
49 | var rowInfo = matrix.GetType().GetProperty("Rows");
|
---|
50 | rowInfo.SetValue(matrix, rows);
|
---|
51 | var colInfo = matrix.GetType().GetProperty("Columns");
|
---|
52 | colInfo.SetValue(matrix, cols);
|
---|
53 |
|
---|
54 | for (int x = 0; x < rows; ++x) {
|
---|
55 | jarr = (JArray)arr.GetValue(x);
|
---|
56 | for (int y = 0; y < cols; ++y) {
|
---|
57 | matrix[x, y] = jarr[y].ToObject<T>();
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 | #endregion
|
---|
63 | }
|
---|
64 | }
|
---|