- Timestamp:
- 02/03/20 15:13:35 (5 years ago)
- Location:
- branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs
r17407 r17417 10 10 namespace HeuristicLab.JsonInterface { 11 11 12 public class IntArrayConverter : ValueTypeArrayConverter<IntArray, int>{12 public class IntArrayConverter : BaseConverter { 13 13 public override int Priority => 1; 14 14 public override Type ConvertableType => typeof(IntArray); 15 16 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 17 IntArray arr = item as IntArray; 18 int[] d = CastValue<int[]>(data); 19 bool resizeTmp = arr.Resizable; 20 arr.Resizable = true; 21 arr.Length = d.Length; 22 for (int i = 0; i < d.Length; ++i) 23 arr[i] = d[i]; 24 arr.Resizable = resizeTmp; 25 } 26 27 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 28 new IntArrayJsonItem() { 29 Name = "[OverridableParamName]", 30 Value = ((IntArray)value).CloneAsArray() 31 }; 15 32 } 16 33 17 public class DoubleArrayConverter : ValueTypeArrayConverter<DoubleArray, double>{34 public class DoubleArrayConverter : BaseConverter { 18 35 public override int Priority => 1; 19 36 public override Type ConvertableType => typeof(DoubleArray); 37 38 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 39 DoubleArray arr = item as DoubleArray; 40 double[] d = CastValue<double[]>(data); 41 bool resizeTmp = arr.Resizable; 42 arr.Resizable = true; 43 arr.Length = d.Length; 44 for (int i = 0; i < d.Length; ++i) 45 arr[i] = d[i]; 46 arr.Resizable = resizeTmp; 47 } 48 49 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 50 new DoubleArrayJsonItem() { 51 Name = "[OverridableParamName]", 52 Value = ((DoubleArray)value).CloneAsArray() 53 }; 20 54 } 21 55 22 public class PercentArrayConverter : ValueTypeArrayConverter<PercentArray, double>{56 public class PercentArrayConverter : BaseConverter { 23 57 public override int Priority => 2; 24 58 public override Type ConvertableType => typeof(PercentArray); 59 60 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 61 PercentArray arr = item as PercentArray; 62 double[] d = CastValue<double[]>(data); 63 bool resizeTmp = arr.Resizable; 64 arr.Resizable = true; 65 arr.Length = d.Length; 66 for (int i = 0; i < d.Length; ++i) 67 arr[i] = d[i]; 68 arr.Resizable = resizeTmp; 69 } 70 71 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 72 new DoubleArrayJsonItem() { 73 Name = "[OverridableParamName]", 74 Value = ((PercentArray)value).CloneAsArray() 75 }; 25 76 } 26 77 27 public class BoolArrayConverter : ValueTypeArrayConverter<BoolArray, bool>{78 public class BoolArrayConverter : BaseConverter { 28 79 public override int Priority => 1; 29 80 public override Type ConvertableType => typeof(BoolArray); 30 }31 81 32 public abstract class ValueTypeArrayConverter<ArrayType, T> : BaseConverter 33 where ArrayType : ValueTypeArray<T> 34 where T : struct 35 { 36 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 37 CopyArrayData(((ArrayType)item), CastValue<T[]>(data.Value)); 82 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) { 83 BoolArray arr = item as BoolArray; 84 bool[] d = CastValue<bool[]>(data); 85 bool resizeTmp = arr.Resizable; 86 arr.Resizable = true; 87 arr.Length = d.Length; 88 for(int i = 0; i < d.Length; ++i) 89 arr[i] = d[i]; 90 arr.Resizable = resizeTmp; 91 } 38 92 39 93 public override IJsonItem Extract(IItem value, IJsonItemConverter root) => 40 new JsonItem() {94 new BoolArrayJsonItem() { 41 95 Name = "[OverridableParamName]", 42 Value = (( ArrayType)value).CloneAsArray()96 Value = ((BoolArray)value).CloneAsArray() 43 97 }; 44 45 #region Helper46 private void CopyArrayData(ArrayType array, T[] data) {47 var colInfo = array.GetType().GetProperty("Length");48 colInfo.SetValue(array, data.Length);49 for (int i = 0; i < data.Length; ++i) {50 array[i] = data[i];51 }52 }53 #endregion54 98 } 55 99 } -
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs
r17407 r17417 12 12 public override int Priority => 1; 13 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 }; 14 25 } 15 26 … … 17 28 public override int Priority => 1; 18 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 }; 19 42 } 20 43 … … 22 45 public override int Priority => 2; 23 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 }; 24 59 } 25 60 … … 27 62 public override int Priority => 1; 28 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 }; 29 76 } 30 77 … … 33 80 where T : struct 34 81 { 35 public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) => 36 CopyMatrixData(item as MatrixType, data.Value); 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; 37 86 38 public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>39 new JsonItem() {40 Name = "[OverridableParamName]",41 Value = ((MatrixType)value).CloneAsMatrix()87 var rowInfo = matrix.GetType().GetProperty("Rows"); 88 rowInfo.SetValue(matrix, rows); 89 var colInfo = matrix.GetType().GetProperty("Columns"); 90 colInfo.SetValue(matrix, cols); 42 91 43 }; 44 45 #region Helper 46 private void CopyMatrixData(MatrixType matrix, object data) { 47 if (data is Array arr) { 48 var rows = arr.Length; 49 var cols = arr.Length > 0 && arr.GetValue(0) is JArray jarr ? jarr.Count : 0; 50 51 var rowInfo = matrix.GetType().GetProperty("Rows"); 52 rowInfo.SetValue(matrix, rows); 53 var colInfo = matrix.GetType().GetProperty("Columns"); 54 colInfo.SetValue(matrix, cols); 55 56 for (int x = 0; x < rows; ++x) { 57 jarr = (JArray)arr.GetValue(x); 58 for (int y = 0; y < cols; ++y) { 59 matrix[x, y] = jarr[y].ToObject<T>(); 60 } 92 for (int x = 0; x < rows; ++x) { 93 for (int y = 0; y < cols; ++y) { 94 matrix[x, y] = data[x][y]; 61 95 } 62 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; 63 108 } 64 109 #endregion
Note: See TracChangeset
for help on using the changeset viewer.