Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17417


Ignore:
Timestamp:
02/03/20 15:13:35 (4 years ago)
Author:
dpiringe
Message:

#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
Location:
branches/3026_IntegrationIntoSymSpace
Files:
3 added
13 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/HeuristicLab.JsonInterface.OptimizerIntegration.csproj

    r17411 r17417  
    7878  </ItemGroup>
    7979  <ItemGroup>
    80     <Compile Include="Interfaces\IJsonItemValueParser.cs" />
    81     <Compile Include="Parser\JsonItemDoubleValueParser.cs" />
    82     <Compile Include="Parser\JsonItemIntValueParser.cs" />
    8380    <Compile Include="Properties\Resources.Designer.cs">
    8481      <AutoGen>True</AutoGen>
     
    9895      <DependentUpon>NumericRangeControl.cs</DependentUpon>
    9996    </Compile>
     97    <Compile Include="ViewModels\ArrayValueVM.cs" />
    10098    <Compile Include="ViewModels\JsonItemVMBase.cs" />
    10199    <Compile Include="ViewModels\RangeVM.cs" />
     
    109107    </Compile>
    110108    <Compile Include="FileManager.cs" />
     109    <Compile Include="Views\JsonItemArrayValueControl.cs">
     110      <SubType>UserControl</SubType>
     111    </Compile>
     112    <Compile Include="Views\JsonItemArrayValueControl.Designer.cs">
     113      <DependentUpon>JsonItemArrayValueControl.cs</DependentUpon>
     114    </Compile>
    111115    <Compile Include="Views\JsonItemBoolControl.cs">
    112116      <SubType>UserControl</SubType>
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Shared/JsonItemBaseControl.cs

    r17410 r17417  
    1111namespace HeuristicLab.JsonInterface.OptimizerIntegration {
    1212  public partial class JsonItemBaseControl : UserControl {
    13     private JsonItemVMBase VM { get; set; }
     13    protected JsonItemVMBase VM { get; set; }
    1414
    1515    private JsonItemBaseControl() {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/ViewModels/StringValueVM.cs

    r17412 r17417  
    1212
    1313    public string Value {
    14       get => Item.Value.ToString();
     14      get => Item.Value?.ToString();
    1515      set {
    1616        Item.Value = value;
     
    1818      }
    1919    }
     20
     21    public IEnumerable<string> Range {
     22      get => Item.Range.Cast<string>();
     23      set {
     24        Item.Range = value;
     25        //check if value is still in range
     26        if (!Range.Any(x => x == Value)) {
     27          Value = Range.FirstOrDefault();
     28          if (Range.Count() == 0)
     29            //if no elements exists -> deselect item
     30            base.Selected = false;
     31          OnPropertyChange(this, nameof(Value));
     32        }
     33       
     34        OnPropertyChange(this, nameof(Range));
     35      }
     36    }
    2037  }
    2138}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs

    r17412 r17417  
    4242
    4343    private IDictionary<Type, Type> JI2VM { get; set; }
    44 
    4544
    4645    private void InitCache() {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemValidValuesControl.Designer.cs

    r17410 r17417  
    4444      this.tableOptions.RowCount = 1;
    4545      this.tableOptions.RowStyles.Add(new System.Windows.Forms.RowStyle());
    46       this.tableOptions.Size = new System.Drawing.Size(478, 151);
     46      this.tableOptions.Size = new System.Drawing.Size(478, 137);
    4747      this.tableOptions.TabIndex = 12;
    4848      //
     
    5656      this.groupBoxRange.Location = new System.Drawing.Point(6, 108);
    5757      this.groupBoxRange.Name = "groupBoxRange";
    58       this.groupBoxRange.Size = new System.Drawing.Size(490, 176);
     58      this.groupBoxRange.Size = new System.Drawing.Size(490, 162);
    5959      this.groupBoxRange.TabIndex = 14;
    6060      this.groupBoxRange.TabStop = false;
     
    9191      this.ForeColor = System.Drawing.Color.Black;
    9292      this.Name = "JsonItemValidValuesControl";
    93       this.Size = new System.Drawing.Size(500, 290);
     93      this.Size = new System.Drawing.Size(500, 276);
    9494      this.Controls.SetChildIndex(this.groupBoxRange, 0);
    9595      this.Controls.SetChildIndex(this.comboBoxValues, 0);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemValidValuesControl.cs

    r17410 r17417  
    1414    public JsonItemValidValuesControl(StringValueVM vm) : base(vm) {
    1515      InitializeComponent();
    16       /*
    17       foreach (var i in VM.Item.Range) {
    18         AddOption((string)i);
    19         if(i == VM.Item.Value) {
    20           comboBoxValues.SelectedItem = (string)i;
    21         }
    22       }*/
     16      foreach (var i in VM.Item.Range)
     17        SetupOption((string)i);
     18
     19      comboBoxValues.DataBindings.Add("SelectedItem", VM, nameof(StringValueVM.Value));
    2320    }
    24     /*
    25     private void AddOption(string opt) {
     21   
     22    private void SetupOption(string opt) {
    2623      AddComboOption(opt);
    2724      TextBox tb = new TextBox();
     
    5047        items.Add((string)i);
    5148      }
    52       VM.Item.Range = items;
     49      ((StringValueVM)VM).Range = items;
     50      comboBoxValues.Enabled = true;
    5351      tableOptions.Refresh();
    5452    }
     
    6058        items.Add((string)i);
    6159      }
    62       VM.Item.Range = items;
     60      ((StringValueVM)VM).Range = items;
     61      if (((StringValueVM)VM).Range.Count() <= 0) {
     62        comboBoxValues.Enabled = false;
     63        comboBoxValues.SelectedIndex = -1;
     64      }
    6365      tableOptions.Refresh();
    6466    }
    65    
    66     private void comboBoxValues_SelectedValueChanged(object sender, EventArgs e) {
    67       VM.Item.Value = (string)comboBoxValues.SelectedItem;
    68     }
    69 
    70     private void JsonItemValidValuesControl_Load(object sender, EventArgs e) {
    71 
    72     }
    73     */
    7467  }
    7568}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/JsonItemValueControl.cs

    r17411 r17417  
    1313
    1414  public class JsonItemIntValueControl : JsonItemValueControl {
    15     private readonly IntValueVM vm;
    1615
    1716    #region Overriden Properties
    18     protected override object VM => vm;
    1917    protected override string ValuePropertyId => nameof(IntValueVM.Value);
    2018    protected override string MinRangePropertyId => nameof(IntValueVM.MinRange);
     
    2523
    2624    public JsonItemIntValueControl(IntValueVM vm) : base(vm) {
    27       this.vm = vm;
    2825      Init();
    2926    }
     
    3229
    3330  public class JsonItemDoubleValueControl : JsonItemValueControl {
    34     private readonly DoubleValueVM vm;
    3531
    3632    #region Overriden Properties
    37     protected override object VM => vm;
    3833    protected override string ValuePropertyId => nameof(DoubleValueVM.Value);
    3934    protected override string MinRangePropertyId => nameof(DoubleValueVM.MinRange);
     
    4439
    4540    public JsonItemDoubleValueControl(DoubleValueVM vm) : base(vm) {
    46       this.vm = vm;
    4741      Init();
    4842    }
     
    5751
    5852    #region Abstract Properties
    59     protected abstract object VM { get; }
    6053    protected abstract string ValuePropertyId { get; }
    6154    protected abstract string MinRangePropertyId { get; }
     
    7265
    7366    protected void Init() {
    74       TBValue.DataBindings.Add("Text", VM, ValuePropertyId);
     67      TBValue.DataBindings.Add("Text", base.VM, ValuePropertyId);
    7568      NumericRangeControl.TBMinRange.DataBindings.Add("Text", VM, MinRangePropertyId);
    7669      NumericRangeControl.TBMaxRange.DataBindings.Add("Text", VM, MaxRangePropertyId);
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs

    r17407 r17417  
    1010namespace HeuristicLab.JsonInterface {
    1111
    12   public class IntArrayConverter : ValueTypeArrayConverter<IntArray, int> {
     12  public class IntArrayConverter : BaseConverter {
    1313    public override int Priority => 1;
    1414    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      };
    1532  }
    1633
    17   public class DoubleArrayConverter : ValueTypeArrayConverter<DoubleArray, double> {
     34  public class DoubleArrayConverter : BaseConverter {
    1835    public override int Priority => 1;
    1936    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      };
    2054  }
    2155
    22   public class PercentArrayConverter : ValueTypeArrayConverter<PercentArray, double> {
     56  public class PercentArrayConverter : BaseConverter {
    2357    public override int Priority => 2;
    2458    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      };
    2576  }
    2677
    27   public class BoolArrayConverter : ValueTypeArrayConverter<BoolArray, bool> {
     78  public class BoolArrayConverter : BaseConverter {
    2879    public override int Priority => 1;
    2980    public override Type ConvertableType => typeof(BoolArray);
    30   }
    3181
    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    }
    3892
    3993    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
    40       new JsonItem() {
     94      new BoolArrayJsonItem() {
    4195        Name = "[OverridableParamName]",
    42         Value = ((ArrayType)value).CloneAsArray()
     96        Value = ((BoolArray)value).CloneAsArray()
    4397      };
    44 
    45     #region Helper
    46     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     #endregion
    5498  }
    5599}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs

    r17407 r17417  
    1212    public override int Priority => 1;
    1313    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      };
    1425  }
    1526
     
    1728    public override int Priority => 1;
    1829    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      };
    1942  }
    2043
     
    2245    public override int Priority => 2;
    2346    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      };
    2459  }
    2560
     
    2762    public override int Priority => 1;
    2863    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      };
    2976  }
    3077
     
    3380    where T : struct
    3481  {
    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;
    3786
    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);
    4291
    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];
    6195        }
    6296      }
     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;
    63108    }
    64109    #endregion
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/JsonTemplateInstantiator.cs

    r17410 r17417  
    6868    #region Helper
    6969
    70     private static object GetValueFromJObject(JObject obj) {
    71       object val = obj[nameof(IJsonItem.Value)]?.ToObject<object>();
    72       if (val is JContainer jContainer) // for resolving array values
    73         val = jContainer.ToObject<object[]>();
    74 
    75       return val;
    76     }
     70    private static object GetValueFromJObject(JObject obj) =>
     71      obj[nameof(IJsonItem.Value)]?.ToObject<object>();
    7772
    7873    private static void CollectParameterizedItems(InstData instData) {
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/GenericJsonItem.cs

    r17410 r17417  
    11using System;
     2using System.Collections;
    23using System.Collections.Generic;
    34using System.Linq;
    45using System.Text;
    56using System.Threading.Tasks;
     7using Newtonsoft.Json.Linq;
    68
    79namespace HeuristicLab.JsonInterface {
     
    1113  public class JsonItem<V,R> : JsonItem {
    1214    public new V Value {
    13       get => (V)Convert.ChangeType(base.Value, typeof(V));
     15      get {
     16        if(base.Value is IConvertible)
     17          return (V)Convert.ChangeType(base.Value, typeof(V));
     18
     19        if(base.Value is JToken token)
     20          return token.ToObject<V>();
     21
     22        return (V)base.Value;
     23      }
    1424      set => base.Value = value;
    1525    }
    1626
    1727    public new IEnumerable<R> Range {
    18       get => base.Range.Cast<R>();
     28      get => base.Range?.Cast<R>();
    1929      set => base.Range = value.Cast<object>();
    2030    }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Models/JsonItems.cs

    r17410 r17417  
    77namespace HeuristicLab.JsonInterface {
    88  public class IntJsonItem : JsonItem<int> {}
    9   public class IntArrayJsonItem: JsonItem<int[], int> {}
     9  public class IntArrayJsonItem: JsonItem<int[], int> { }
     10  public class IntMatrixJsonItem : JsonItem<int[][], int> { }
    1011
    1112  public class DoubleJsonItem: JsonItem<double> {}
    12   public class DoubleArrayJsonItem: JsonItem<double[], double> {}
     13  public class DoubleArrayJsonItem: JsonItem<double[], double> { }
     14  public class DoubleMatrixJsonItem : JsonItem<double[][], double> { }
    1315
    14   public class BoolJsonItem: JsonItem<bool> {}
     16  public class BoolJsonItem: JsonItem<bool> { }
     17  public class BoolArrayJsonItem : JsonItem<bool[], bool> { }
     18  public class BoolMatrixJsonItem : JsonItem<bool[][], bool> { }
    1519
    1620  public class StringJsonItem: JsonItem<string> {}
  • branches/3026_IntegrationIntoSymSpace/Heuristiclab.ConfigStarter/Program.cs

    r17410 r17417  
    4747      JCGenerator generator = new JCGenerator();
    4848
    49       //File.WriteAllText(@"C:\Workspace\Template.json", generator.GenerateTemplate(alg));
     49      File.WriteAllText(@"C:\Workspace\Template.json", generator.GenerateTemplate(alg));
    5050      JsonTemplateInstantiator.Instantiate(@"C:\Workspace\Template.json");
    5151      /*
Note: See TracChangeset for help on using the changeset viewer.