Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/SubScopesLookupParameter.cs @ 2783

Last change on this file since 2783 was 2757, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • worked on parameters and operators
File size: 4.4 KB
RevLine 
[2740]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
[2756]26using HeuristicLab.Common;
[2740]27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Parameters {
31  /// <summary>
32  /// A generic parameter representing instances of type T which are collected from the sub-scopes of the current scope.
33  /// </summary>
[2757]34  [Item("SubScopesLookupParameter<T>", "A generic parameter representing instances of type T which are collected from or written to the sub-scopes of the current scope.")]
[2756]35  public class SubScopesLookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
[2740]36    [Storable]
37    private string actualName;
38    public string ActualName {
39      get { return actualName; }
40      set {
41        if (value == null) throw new ArgumentNullException();
42        if (!actualName.Equals(value)) {
43          actualName = value;
44          OnActualNameChanged();
45        }
46      }
47    }
48
[2757]49    public new ItemArray<T> ActualValue {
50      get { return (ItemArray<T>)GetActualValue(); }
51      set { SetActualValue(value); }
[2740]52    }
53
[2756]54    public SubScopesLookupParameter()
[2757]55      : base("Anonymous", typeof(ItemArray<T>)) {
[2740]56      actualName = Name;
57    }
[2756]58    public SubScopesLookupParameter(string name)
[2757]59      : base(name, typeof(ItemArray<T>)) {
[2756]60      actualName = Name;
61    }
62    public SubScopesLookupParameter(string name, string description)
[2757]63      : base(name, description, typeof(ItemArray<T>)) {
[2740]64      actualName = Name;
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
[2756]68      SubScopesLookupParameter<T> clone = (SubScopesLookupParameter<T>)base.Clone(cloner);
[2740]69      clone.actualName = actualName;
70      return clone;
71    }
72
73    public override string ToString() {
74      return string.Format("{0}: {1} ({2})", Name, ActualName, DataType.Name);
75    }
76
[2757]77    protected override IItem GetActualValue() {
[2756]78      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
[2740]79      IScope scope = ExecutionContext.Scope;
[2757]80      ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count);
[2740]81      IVariable var;
[2756]82      T value;
[2740]83
[2756]84      for (int i = 0; i < values.Length; i++) {
[2740]85        scope.SubScopes[i].Variables.TryGetValue(name, out var);
[2756]86        if (var != null) {
87          value = var.Value as T;
88          if (value == null)
89            throw new InvalidOperationException(
90              string.Format("Type mismatch. Variable \"{0}\" does not contain a \"{1}\".",
91                            name,
92                            typeof(T).GetPrettyName())
93            );
94          values[i] = value;
95        }
[2740]96      }
[2756]97      return values;
[2740]98    }
[2757]99    protected override void SetActualValue(IItem value) {
100      ItemArray<T> values = value as ItemArray<T>;
101      if (values == null)
102        throw new InvalidOperationException(
103          string.Format("Type mismatch. Value is not a \"{0}\".",
104                        typeof(ItemArray<T>).GetPrettyName())
105        );
106
[2756]107      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
[2740]108      IScope scope = ExecutionContext.Scope;
109      IVariable var;
110
111      for (int i = 0; i < values.Length; i++) {
112        scope.SubScopes[i].Variables.TryGetValue(name, out var);
113        if (var != null) var.Value = values[i];
114        else scope.SubScopes[i].Variables.Add(new Variable(name, values[i]));
115      }
116    }
117
118    public event EventHandler ActualNameChanged;
119    private void OnActualNameChanged() {
120      if (ActualNameChanged != null)
121        ActualNameChanged(this, new EventArgs());
122      OnChanged();
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.