Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/07/12 14:00:45 (12 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.

File:
1 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    }
Note: See TracChangeset for help on using the changeset viewer.