Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/27/20 12:18:06 (5 years ago)
Author:
dpiringe
Message:

#3026:

  • simplified converter inheritance:
    • BaseConverter now only has Inject and Extract from IJsonItemConverter as abstract methods
    • removed ParameterBaseConverter
    • concrete converters have to initialize their JsonItem now -> enables better handling with different types of JsonItem
Location:
branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters
Files:
1 deleted
15 edited

Legend:

Unmodified
Added
Removed
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/AlgorithmConverter.cs

    r17406 r17407  
    1313    public override Type ConvertableType => typeof(IAlgorithm);
    1414
    15     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) {
    16       base.InjectData(item, data, root);
     15    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
     16      base.Inject(item, data, root);
    1717      IAlgorithm algorithm = item as IAlgorithm;
    1818      IJsonItem problemData = data.Children.Where(x => x.Name == algorithm.Problem.Name).First();
     
    2020
    2121    }
    22 
    23     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    24       base.Populate(value, item, root);
     22   
     23    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     24      IJsonItem item = base.Extract(value, root);
    2525      IAlgorithm algorithm = value as IAlgorithm;
    26       foreach(var res in algorithm.Results) {
    27         item.AddChilds(new ResultItem() { 
    28           Name = res.Name, 
    29           Value = true, 
    30           Range = new object[] { true, false } 
     26      foreach (var res in algorithm.Results) {
     27        item.AddChilds(new ResultItem() {
     28          Name = res.Name,
     29          Value = true,
     30          Range = new object[] { true, false }
    3131        });
    3232      }
    3333      item.AddChilds(root.Extract(algorithm.Problem, root));
     34      return item;
    3435    }
    3536  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/BaseConverter.cs

    r17406 r17407  
    1414    public abstract Type ConvertableType { get; }
    1515
    16     public void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    17 
    18       InjectData(item, data, root);
    19     }
    20 
    21     public IJsonItem Extract(IItem value, IJsonItemConverter root) {
    22       IJsonItem data = new JsonItem() { Name = value.ItemName };
    23       Populate(value, data, root);
    24       return data;
    25     }
    26    
    27     public abstract void InjectData(IItem item, IJsonItem data, IJsonItemConverter root);
    28     public abstract void Populate(IItem value, IJsonItem item, IJsonItemConverter root); // TODO: populate?
     16    public abstract void Inject(IItem item, IJsonItem data, IJsonItemConverter root);
     17    public abstract IJsonItem Extract(IItem value, IJsonItemConverter root);
    2918
    3019    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ConstrainedValueParameterConverter.cs

    r17406 r17407  
    88
    99namespace HeuristicLab.JsonInterface {
    10   public class ConstrainedValueParameterConverter : ParameterBaseConverter {
     10  public class ConstrainedValueParameterConverter : BaseConverter {
    1111    public override int Priority => 3;
    1212    public override Type ConvertableType => typeof(IConstrainedValueParameter<>);
    1313
    14     public override void InjectData(IParameter parameter, IJsonItem data, IJsonItemConverter root) {
    15       foreach(var x in GetValidValues(parameter))
     14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
     15      IParameter parameter = item as IParameter;
     16      foreach (var x in GetValidValues(parameter))
    1617        if(x.ToString() == CastValue<string>(data.Value))
    1718          parameter.ActualValue = x;
     
    2425      }
    2526    }
     27   
     28    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     29      IParameter parameter = value as IParameter;
    2630
    27     public override void Populate(IParameter value, IJsonItem item, IJsonItemConverter root) {
    28       item.AddChilds(GetParameterizedChilds(value));
    29       item.Name = value.Name;
    30       item.Value = value.ActualValue?.ToString();
    31       item.Range = GetValidValues(value).Select(x => x.ToString());
    32     }     
     31      IJsonItem item = new JsonItem() {
     32        Name = parameter.Name,
     33        Value = parameter.ActualValue?.ToString(),
     34        Range = GetValidValues(parameter).Select(x => x.ToString())
     35      };
     36      item.AddChilds(GetParameterizedChilds(parameter));
     37
     38      return item;
     39    }
    3340
    3441    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/EnumTypeConverter.cs

    r17406 r17407  
    1212    public override Type ConvertableType => typeof(EnumValue<>);
    1313
    14     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) =>
     14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
    1515      ((dynamic)item).Value = Enum.Parse(
    1616        item.GetType().GenericTypeArguments.First(),
    1717        CastValue<string>(data.Value));
    18 
    19     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
     18   
     19    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
    2020      object val = ((dynamic)value).Value;
    2121      Type enumType = val.GetType();
    22       item.Value = Enum.GetName(enumType, val);
    23       item.Range = Enum.GetNames(enumType);
     22      return new JsonItem() {
     23        Name = value.ItemName,
     24        Value = Enum.GetName(enumType, val),
     25        Range = Enum.GetNames(enumType)
     26    };
    2427    }
    2528  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/LookupParameterConverter.cs

    r17406 r17407  
    77
    88namespace HeuristicLab.JsonInterface {
    9   public class LookupParameterConverter : ParameterBaseConverter {
     9  public class LookupParameterConverter : BaseConverter {
    1010    public override int Priority => 3;
    1111    public override Type ConvertableType => typeof(ILookupParameter);
     12   
     13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
     14      ((ILookupParameter)item).ActualName = data.ActualName as string;
    1215
    13     public override void Populate(IParameter value, IJsonItem item, IJsonItemConverter root) {
    14       item.Name = value.Name;
    15       item.ActualName = ((ILookupParameter)value).ActualName;
    16     }
     16    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     17      IParameter parameter = value as IParameter;
    1718
    18     public override void InjectData(IParameter parameter, IJsonItem data, IJsonItemConverter root) =>
    19       ((ILookupParameter)parameter).ActualName = data.ActualName as string;
     19      IJsonItem item = new JsonItem() {
     20        Name = parameter.Name,
     21        ActualName = ((ILookupParameter)parameter).ActualName
     22      };
     23      return item;
     24    }
    2025  }
    2126}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/MultiCheckedOperatorConverter.cs

    r17406 r17407  
    1111    public override int Priority => 3;
    1212    public override Type ConvertableType => typeof(ICheckedMultiOperator<>);
     13   
     14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
     15      base.Inject(item, data, root);
    1316
    14     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    15       base.Populate(value, item, root);
     17      dynamic val = item as dynamic;
     18      foreach (var op in val.Operators)
     19        val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data));
     20    }
    1621
     22    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     23      IJsonItem item = base.Extract(value, root);
    1724      dynamic val = value as dynamic;
    1825      foreach (var op in val.Operators) {
     
    2330        });
    2431      }
     32      return item;
    2533    }
    2634
    27     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) {
    28       base.InjectData(item, data, root);
    2935
    30       dynamic val = item as dynamic;
    31       foreach (var op in val.Operators)
    32         val.Operators.SetItemCheckedState(op, GetOperatorState(op.Name, data));
    33     }
    34 
     36    #region Helper
    3537    private bool GetOperatorState(string name, IJsonItem data) {
    3638      foreach(var op in data.Children) {
     
    3941      return false;
    4042    }
     43    #endregion
    4144  }
    4245}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ParameterizedItemConverter.cs

    r17406 r17407  
    1111    public override Type ConvertableType => typeof(IParameterizedItem);
    1212
    13     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) {
     13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    1414      IParameterizedItem pItem = item as IParameterizedItem;
    1515
     
    2121    }
    2222
    23     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
     23    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     24      IJsonItem item = new JsonItem() { Name = value.ItemName };
     25
    2426      var parameterizedItem = value as IParameterizedItem;
    25 
    2627      foreach (var param in parameterizedItem.Parameters) {
    2728        if (!param.Hidden) {
    2829          IJsonItem tmp = root.Extract(param, root);
    29           if(!(tmp is UnsupportedJsonItem))
     30          if (!(tmp is UnsupportedJsonItem))
    3031            item.AddChilds(tmp);
    3132        }
    32          
    3333      }
     34
     35      return item;
    3436    }
    3537  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/RegressionProblemDataConverter.cs

    r17406 r17407  
    1414    public override Type ConvertableType => HEAL.Attic.Mapper.StaticCache.GetType(new Guid("EE612297-B1AF-42D2-BF21-AF9A2D42791C"));
    1515
    16     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {     
     16    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
     17      // TODO: inject data
     18      throw new NotImplementedException();
     19    }
     20
     21    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     22      IJsonItem item = new JsonItem() { Name = value.ItemName };
     23
    1724      dynamic val = (dynamic)value;
    1825      object dataset = (object)val.Dataset;
     
    3845        Range = variables.Select(x => x.Value)
    3946      });
    40     }
    41 
    42     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) {
    43       // TODO: inject data
    44       throw new NotImplementedException();
     47      return item;
    4548    }
    4649  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/StringValueConverter.cs

    r17406 r17407  
    1212    public override Type ConvertableType => typeof(StringValue);
    1313
    14     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) =>
     14    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
    1515      ((StringValue)item).Value = CastValue<string>(data.Value);
    1616
    17     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) =>
    18       item.Value = ((StringValue)value).Value;
     17    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
     18      new JsonItem() {
     19        Name = value.ItemName,
     20        Value = ((StringValue)value).Value
     21      };
    1922  }
    2023}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueLookupParameterConverter.cs

    r17406 r17407  
    77
    88namespace HeuristicLab.JsonInterface {
    9   public class ValueLookupParameterConverter : ParameterBaseConverter {
     9  public class ValueLookupParameterConverter : BaseConverter {
    1010    public override int Priority => 4;
    1111    public override Type ConvertableType => typeof(IValueLookupParameter);
    1212
    13     public override void Populate(IParameter value, IJsonItem item, IJsonItemConverter root) {
     13    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
     14      IValueLookupParameter param = item as IValueLookupParameter;
     15      param.ActualName = CastValue<string>(data.ActualName);
     16      if (param.Value != null)
     17        root.Inject(param.Value, data, root);
     18    }
     19
     20    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
    1421      IValueLookupParameter param = value as IValueLookupParameter;
    15      
    16       item.Name = value.Name;
    17       item.ActualName = param.ActualName;
     22
     23      IJsonItem item = new JsonItem() {
     24        Name = param.Name,
     25        ActualName = param.ActualName
     26      };
    1827
    1928      object actualValue = null;
    2029      IEnumerable<object> actualRange = null;
    21       if(param.Value != null) {
     30      if (param.Value != null) {
    2231        IJsonItem tmp = root.Extract(param.Value, root);
    2332        tmp.Parent = item;
     
    2938      item.Value = actualValue;
    3039      item.Range = actualRange;
    31     }
    32 
    33     public override void InjectData(IParameter parameter, IJsonItem data, IJsonItemConverter root) {
    34       IValueLookupParameter param = parameter as IValueLookupParameter;
    35       param.ActualName = CastValue<string>(data.ActualName);
    36       if (param.Value != null)
    37         root.Inject(param.Value, data, root);
     40      return item;
    3841    }
    3942  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueParameterConverter.cs

    r17406 r17407  
    77
    88namespace HeuristicLab.JsonInterface {
    9   public class ValueParameterConverter : ParameterBaseConverter {
     9  public class ValueParameterConverter : BaseConverter {
    1010    public override int Priority => 2;
    1111    public override Type ConvertableType => typeof(IValueParameter);
    1212
    13     public override void InjectData(IParameter parameter, IJsonItem data, IJsonItemConverter root) {
     13    public override void Inject(IItem value, IJsonItem data, IJsonItemConverter root) {
     14      IParameter parameter = value as IParameter;
     15
    1416      if (parameter.ActualValue == null && data.Value != null)
    1517        parameter.ActualValue = Instantiate(parameter.DataType);
     
    1719    }
    1820
    19     public override void Populate(IParameter value, IJsonItem item, IJsonItemConverter root) {
    20       item.Name = value.Name;
    21       if (value.ActualValue != null) {
    22         IJsonItem tmp = root.Extract(value.ActualValue, root);
    23         if(!(tmp is UnsupportedJsonItem)) {
     21    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     22      IParameter parameter = value as IParameter;
     23
     24      IJsonItem item = new JsonItem() {
     25        Name = parameter.Name
     26      };
     27
     28      if (parameter.ActualValue != null) {
     29        IJsonItem tmp = root.Extract(parameter.ActualValue, root);
     30        if (!(tmp is UnsupportedJsonItem)) {
    2431          if (tmp.Name == "[OverridableParamName]") {
    25             tmp.Name = value.Name;
     32            tmp.Name = parameter.Name;
    2633            JsonItem.Merge(item as JsonItem, tmp as JsonItem);
    2734          } else
     
    2936        }
    3037      }
     38      return item;
    3139    }
    3240  }
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueRangeConverter.cs

    r17406 r17407  
    2727    private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Instance;
    2828
    29     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    30       var field = value.GetType().GetField("values", Flags);
    31       Tuple<T,T> tuple = (Tuple<T,T>)field.GetValue(value);
    32       item.Name = "[OverridableParamName]";
    33       item.Value = new object[] { tuple.Item1.Value, tuple.Item2.Value };
    34       item.Range = new object[] { GetMinValue(tuple.Item1.Value.GetType()), GetMaxValue(tuple.Item2.Value.GetType()) };
    35     }
    36 
    37     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) {
     29    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) {
    3830      object[] arr = (object[])data.Value;
    3931      Tuple<T,T> tuple = new Tuple<T,T>(Instantiate<T>(arr[0]), Instantiate<T>(arr[1]));
     
    4133      field.SetValue(tuple, item);
    4234    }
     35
     36    public override IJsonItem Extract(IItem value, IJsonItemConverter root) {
     37      var field = value.GetType().GetField("values", Flags);
     38      Tuple<T, T> tuple = (Tuple<T, T>)field.GetValue(value);
     39      return new JsonItem() {
     40        Name = "[OverridableParamName]",
     41        Value = new object[] { tuple.Item1.Value, tuple.Item2.Value },
     42        Range = new object[] { GetMinValue(typeof(TType)), GetMaxValue(typeof(TType)) }
     43      };
     44    }
     45     
    4346  }
    4447}
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeArrayConverter.cs

    r17406 r17407  
    3434    where T : struct
    3535  {
    36     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) =>
     36    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
    3737      CopyArrayData(((ArrayType)item), CastValue<T[]>(data.Value));
    3838
    39     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    40       item.Name = "[OverridableParamName]";
    41       item.Value = ((ArrayType)value).CloneAsArray();
    42     }
     39    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
     40      new JsonItem() {
     41        Name = "[OverridableParamName]",
     42        Value = ((ArrayType)value).CloneAsArray()
     43      };
    4344
    4445    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeMatrixConverter.cs

    r17406 r17407  
    3333    where T : struct
    3434  {
    35     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) =>
     35    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
    3636      CopyMatrixData(item as MatrixType, data.Value);
    3737
    38     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    39       item.Name = "[OverridableParamName]";
    40       item.Value = ((MatrixType)value).CloneAsMatrix();
    41     }
     38    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
     39      new JsonItem() {
     40        Name = "[OverridableParamName]",
     41        Value = ((MatrixType)value).CloneAsMatrix()
     42
     43      };
    4244
    4345    #region Helper
  • branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface/Converters/ValueTypeValueConverter.cs

    r17406 r17407  
    3838    where T : struct {
    3939
    40     public override void InjectData(IItem item, IJsonItem data, IJsonItemConverter root) =>
     40    public override void Inject(IItem item, IJsonItem data, IJsonItemConverter root) =>
    4141      ((ValueType)item).Value = CastValue<T>(data.Value);
    4242
    43     public override void Populate(IItem value, IJsonItem item, IJsonItemConverter root) {
    44       item.Name = "[OverridableParamName]";
    45       item.Value = ((ValueType)value).Value;
    46       item.Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T)) };
    47     }
     43    public override IJsonItem Extract(IItem value, IJsonItemConverter root) =>
     44      new JsonItem() {
     45        Name = "[OverridableParamName]",
     46        Value = ((ValueType)value).Value,
     47        Range = new object[] { GetMinValue(typeof(T)), GetMaxValue(typeof(T)) }
     48      };
    4849  }
    4950}
Note: See TracChangeset for help on using the changeset viewer.