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 | 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 | DoubleNamedMatrixJsonItem d = data as DoubleNamedMatrixJsonItem;
|
---|
36 | CopyMatrixData(mat, d.Value);
|
---|
37 | }
|
---|
38 |
|
---|
39 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
|
---|
40 | new DoubleNamedMatrixJsonItem() {
|
---|
41 | Name = "[OverridableParamName]",
|
---|
42 | Description = value.ItemDescription,
|
---|
43 | Value = Transform((DoubleMatrix)value),
|
---|
44 | Range = new double[] { double.MinValue, double.MaxValue },
|
---|
45 | RowNames = ((DoubleMatrix)value).RowNames,
|
---|
46 | ColumnNames = ((DoubleMatrix)value).ColumnNames
|
---|
47 | };
|
---|
48 | }
|
---|
49 |
|
---|
50 | public class PercentMatrixConverter : ValueTypeMatrixConverter<PercentMatrix, double> {
|
---|
51 | public override int Priority => 2;
|
---|
52 | public override Type ConvertableType => typeof(PercentMatrix);
|
---|
53 |
|
---|
54 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
55 | PercentMatrix mat = item as PercentMatrix;
|
---|
56 | DoubleMatrixJsonItem d = data as DoubleMatrixJsonItem;
|
---|
57 | CopyMatrixData(mat, d.Value);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
|
---|
61 | new DoubleMatrixJsonItem() {
|
---|
62 | Name = "[OverridableParamName]",
|
---|
63 | Description = value.ItemDescription,
|
---|
64 | Value = Transform((PercentMatrix)value),
|
---|
65 | Range = new double[] { 0.0d, 1.0d }
|
---|
66 | };
|
---|
67 | }
|
---|
68 |
|
---|
69 | public class BoolMatrixConverter : ValueTypeMatrixConverter<BoolMatrix, bool> {
|
---|
70 | public override int Priority => 1;
|
---|
71 | public override Type ConvertableType => typeof(BoolMatrix);
|
---|
72 |
|
---|
73 | public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
|
---|
74 | BoolMatrix mat = item as BoolMatrix;
|
---|
75 | BoolMatrixJsonItem d = data as BoolMatrixJsonItem;
|
---|
76 | CopyMatrixData(mat, d.Value);
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
|
---|
80 | new BoolMatrixJsonItem() {
|
---|
81 | Name = "[OverridableParamName]",
|
---|
82 | Description = value.ItemDescription,
|
---|
83 | Value = Transform((BoolMatrix)value),
|
---|
84 | Range = new bool[] { false, true }
|
---|
85 | };
|
---|
86 | }
|
---|
87 |
|
---|
88 | public abstract class ValueTypeMatrixConverter<MatrixType, T> : BaseConverter
|
---|
89 | where MatrixType : ValueTypeMatrix<T>
|
---|
90 | where T : struct
|
---|
91 | {
|
---|
92 | #region Helper
|
---|
93 | protected void CopyMatrixData(MatrixType matrix, T[][] data) {
|
---|
94 | var cols = data.Length;
|
---|
95 | var rows = data.Length > 0 ? data[0].Length : 0;
|
---|
96 |
|
---|
97 | var rowInfo = matrix.GetType().GetProperty("Rows");
|
---|
98 | rowInfo.SetValue(matrix, rows);
|
---|
99 | var colInfo = matrix.GetType().GetProperty("Columns");
|
---|
100 | colInfo.SetValue(matrix, cols);
|
---|
101 |
|
---|
102 | for (int x = 0; x < rows; ++x) {
|
---|
103 | for (int y = 0; y < cols; ++y) {
|
---|
104 | matrix[x, y] = data[y][x];
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected T[][] Transform(MatrixType matrix) {
|
---|
110 | T[][] m = new T[matrix.Columns][];
|
---|
111 | for (int column = 0; column < matrix.Columns; ++column) {
|
---|
112 | m[column] = new T[matrix.Rows];
|
---|
113 | for (int row = 0; row < matrix.Rows; ++row) {
|
---|
114 | m[column][row] = matrix[row, column];
|
---|
115 | }
|
---|
116 | }
|
---|
117 | return m;
|
---|
118 | }
|
---|
119 | #endregion
|
---|
120 | }
|
---|
121 | }
|
---|