Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on parameters and operators
File size: 4.4 KB
Line 
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;
26using HeuristicLab.Common;
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>
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.")]
35  public class SubScopesLookupParameter<T> : Parameter, ILookupParameter<T> where T : class, IItem {
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
49    public new ItemArray<T> ActualValue {
50      get { return (ItemArray<T>)GetActualValue(); }
51      set { SetActualValue(value); }
52    }
53
54    public SubScopesLookupParameter()
55      : base("Anonymous", typeof(ItemArray<T>)) {
56      actualName = Name;
57    }
58    public SubScopesLookupParameter(string name)
59      : base(name, typeof(ItemArray<T>)) {
60      actualName = Name;
61    }
62    public SubScopesLookupParameter(string name, string description)
63      : base(name, description, typeof(ItemArray<T>)) {
64      actualName = Name;
65    }
66
67    public override IDeepCloneable Clone(Cloner cloner) {
68      SubScopesLookupParameter<T> clone = (SubScopesLookupParameter<T>)base.Clone(cloner);
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
77    protected override IItem GetActualValue() {
78      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
79      IScope scope = ExecutionContext.Scope;
80      ItemArray<T> values = new ItemArray<T>(scope.SubScopes.Count);
81      IVariable var;
82      T value;
83
84      for (int i = 0; i < values.Length; i++) {
85        scope.SubScopes[i].Variables.TryGetValue(name, out var);
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        }
96      }
97      return values;
98    }
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
107      string name = LookupParameter<T>.TranslateName(Name, ExecutionContext);
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.