Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Parameters/3.3/ItemParameter.cs @ 2740

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

Operator architecture refactoring (#95)

  • worked on parameters and operators
File size: 5.5 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.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Parameters {
30  /// <summary>
31  /// A generic parameter which represents an instance of type T.
32  /// </summary>
33  [Item("ItemParameter<T>", "A generic parameter which represents an instance of type T.")]
34  public class ItemParameter<T> : Parameter where T : class, IItem {
35    [Storable]
36    private string actualName;
37    public string ActualName {
38      get { return actualName; }
39      set {
40        if (value == null) throw new ArgumentNullException();
41        if (!actualName.Equals(value)) {
42          actualName = value;
43          OnActualNameChanged();
44        }
45      }
46    }
47
48    private T localValue;
49    [Storable]
50    public T LocalValue {
51      get { return this.localValue; }
52      set {
53        if (value != this.localValue) {
54          if ((value != null) && (!DataType.IsInstanceOfType(value))) throw new ArgumentException("Static value does not match data type of parameter");
55          if (this.localValue != null) this.localValue.Changed -= new ChangedEventHandler(LocalValue_Changed);
56          this.localValue = value;
57          if (this.localValue != null) this.localValue.Changed += new ChangedEventHandler(LocalValue_Changed);
58          OnLocalValueChanged();
59        }
60      }
61    }
62
63    public T Value {
64      get { return GetValue(); }
65      set { SetValue(value); }
66    }
67
68    public ItemParameter()
69      : base("Anonymous", null, typeof(T)) {
70      actualName = Name;
71      LocalValue = null;
72    }
73    public ItemParameter(string name, string description)
74      : base(name, description, typeof(T)) {
75      actualName = Name;
76      LocalValue = null;
77    }
78    public ItemParameter(string name, string description, T localValue)
79      : base(name, description, typeof(T)) {
80      actualName = Name;
81      LocalValue = localValue;
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      ItemParameter<T> clone = (ItemParameter<T>)base.Clone(cloner);
86      clone.actualName = actualName;
87      clone.LocalValue = (T)cloner.Clone(localValue);
88      return clone;
89    }
90
91    public override string ToString() {
92      return string.Format("{0}: {1} ({2})", Name, LocalValue != null ? LocalValue.ToString() : ActualName, DataType.Name);
93    }
94
95    protected ItemParameter<T> GetParameter(out string name) {
96      ItemParameter<T> param = this;
97      ExecutionContext current = ExecutionContext;
98      name = param.Name;
99      while (param != null) {
100        if (param.LocalValue != null) return param;
101        name = param.ActualName;
102        current = current.Parent;
103        while ((current != null) && !current.Operator.Parameters.ContainsKey(name))
104          current = current.Parent;
105        if (current != null)
106          param = (ItemParameter<T>)current.Operator.Parameters[actualName];
107        else
108          param = null;
109      }
110      return null;
111    }
112    protected IVariable GetVariable(string name) {
113      IScope scope = ExecutionContext.Scope;
114      while ((scope != null) && !scope.Variables.ContainsKey(name))
115        scope = scope.Parent;
116      return scope != null ? scope.Variables[actualName] : null;
117    }
118    protected virtual T GetValue() {
119      string name;
120      // try to get local value from context stack
121      ItemParameter<T> param = GetParameter(out name);
122      if (param != null) return param.Value;
123      else {
124        // try to get variable from scope
125        IVariable var = GetVariable(name);
126        if (var != null) return (T)var.Value;
127      }
128      return null;
129    }
130    protected virtual void SetValue(T value) {
131      string name;
132      // try to get local value from context stack
133      ItemParameter<T> param = GetParameter(out name);
134      if (param != null) param.Value = value;
135      else {
136        // try to get variable from scope
137        IVariable var = GetVariable(name);
138        if (var != null) var.Value = value;
139        else ExecutionContext.Scope.Variables.Add(new Variable(name, value));
140      }
141    }
142
143    public event EventHandler ActualNameChanged;
144    private void OnActualNameChanged() {
145      if (ActualNameChanged != null)
146        ActualNameChanged(this, new EventArgs());
147      OnChanged();
148    }
149    public event EventHandler LocalValueChanged;
150    private void OnLocalValueChanged() {
151      if (LocalValueChanged != null)
152        LocalValueChanged(this, new EventArgs());
153      OnChanged();
154    }
155    private void LocalValue_Changed(object sender, ChangedEventArgs e) {
156      OnChanged(e);
157    }
158  }
159}
Note: See TracBrowser for help on using the repository browser.