Free cookie consent management tool by TermsFeed Policy Generator

Changeset 7579 for trunk


Ignore:
Timestamp:
03/07/12 14:00:45 (13 years ago)
Author:
abeham
Message:

#1695
I solved this issue now. I found that CollectParameterValues was too monolithic in that you have to overwrite and re-implement the same method again if you wanted to change just a detail (in that case that operators are stored by their name). So I split CollectParameterValues into two separate logical parts:

  • CollectParameterValues is iterating over the parameters
  • GetCollectedValues decides what values are collected from the given parameter value

Algorithm and Problem now overwrite only GetCollectedValues, but reuse the implementation of the base class in that they only filter the values. When they see an IOperator they will instead convert it to its name. Using IEnumerable and yield I think that's a nice solution.

Location:
trunk/sources
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/3.3/ParameterizedNamedItem.cs

    r7259 r7579  
    2020#endregion
    2121
     22using System;
    2223using System.Collections.Generic;
    2324using System.Linq;
     
    8485    public virtual void CollectParameterValues(IDictionary<string, IItem> values) {
    8586      foreach (IValueParameter param in parameters.OfType<IValueParameter>()) {
    86         if (param.GetsCollected && param.Value != null) values.Add(param.Name, param.Value);
    87         if (param.Value is IParameterizedItem) {
    88           Dictionary<string, IItem> children = new Dictionary<string, IItem>();
    89           ((IParameterizedItem)param.Value).CollectParameterValues(children);
    90           foreach (string key in children.Keys)
    91             values.Add(param.Name + "." + key, children[key]);
     87        if (param.GetsCollected) {
     88          var children = GetCollectedValues(param.Value);
     89          foreach (var c in children) {
     90            if (String.IsNullOrEmpty(c.Key))
     91              values.Add(param.Name, c.Value);
     92            else values.Add(param.Name + "." + c.Key, c.Value);
     93          }
    9294        }
     95      }
     96    }
     97
     98    protected virtual IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IItem value) {
     99      if (value == null) yield break;
     100      yield return new KeyValuePair<string, IItem>(String.Empty, value);
     101      var parameterizedItem = value as IParameterizedItem;
     102      if (parameterizedItem != null) {
     103        var children = new Dictionary<string, IItem>();
     104        parameterizedItem.CollectParameterValues(children);
     105        foreach (var child in children) yield return child;
    93106      }
    94107    }
  • trunk/sources/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r7467 r7579  
    240240    }
    241241
     242    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IItem value) {
     243      var children = base.GetCollectedValues(value);
     244      foreach (var child in children) {
     245        if (child.Value is IOperator)
     246          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
     247        else yield return child;
     248      }
     249    }
     250
    242251    #region Events
    243252    public event EventHandler ExecutionStateChanged;
  • trunk/sources/HeuristicLab.Optimization/3.3/Problems/Problem.cs

    r7431 r7579  
    2626using HeuristicLab.Common;
    2727using HeuristicLab.Core;
     28using HeuristicLab.Data;
    2829using HeuristicLab.Parameters;
    2930using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    9798    #endregion
    9899
     100    protected override IEnumerable<KeyValuePair<string, IItem>> GetCollectedValues(IItem value) {
     101      var children = base.GetCollectedValues(value);
     102      foreach (var child in children) {
     103        if (child.Value is IOperator)
     104          yield return new KeyValuePair<string, IItem>(child.Key, new StringValue(((IOperator)child.Value).Name));
     105        else yield return child;
     106      }
     107    }
     108
    99109    #region events
    100110    private void Operators_Changed(object sender, EventArgs e) {
Note: See TracChangeset for help on using the changeset viewer.