Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • moved parameters and parameter views into new plugins
File size: 5.3 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  /// Represents a parameter.
32  /// </summary>
33  [Item("ItemParameter", "A parameter which represents an IItem.")]
34  [Creatable("Test")]
35  public class ItemParameter : Parameter {
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    private IItem value;
50    [Storable]
51    public IItem Value {
52      get { return this.value; }
53      set {
54        if (value != this.value) {
55          if ((value != null) && (!DataType.IsInstanceOfType(value))) throw new ArgumentException("Static value does not match data type of parameter");
56          if (this.value != null) this.value.Changed -= new ChangedEventHandler(Value_Changed);
57          this.value = value;
58          if (this.value != null) this.value.Changed += new ChangedEventHandler(Value_Changed);
59          OnValueChanged();
60        }
61      }
62    }
63
64    public ItemParameter()
65      : base("Anonymous", null, typeof(IItem)) {
66      actualName = Name;
67      Value = null;
68    }
69    protected ItemParameter(string name, string description, Type dataType)
70      : base(name, description, dataType) {
71      this.actualName = Name;
72      this.Value = null;
73    }
74    public ItemParameter(string name, string description)
75      : base(name, description, typeof(IItem)) {
76      this.actualName = Name;
77      this.Value = null;
78    }
79    public ItemParameter(string name, string description, IItem value)
80      : base(name, description, typeof(IItem)) {
81      this.actualName = Name;
82      this.Value = value;
83    }
84
85    public override IItem GetValue(ExecutionContext context) {
86      ItemParameter param = this;
87      ExecutionContext current = context;
88      string actualName = null;
89      while (param != null) {
90        if (param.Value != null) return param.Value;
91        actualName = param.ActualName;
92        current = current.Parent;
93        while ((current != null) && !current.Operator.Parameters.ContainsKey(actualName))
94          current = current.Parent;
95        if (current != null)
96          param = (ItemParameter)current.Operator.Parameters[actualName];
97        else
98          param = null;
99      }
100
101      IScope scope = context.Scope;
102      while ((scope != null) && !scope.Variables.ContainsKey(actualName))
103        scope = scope.Parent;
104      return scope != null ? scope.Variables[actualName].Value : null;
105    }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      ItemParameter clone = (ItemParameter)base.Clone(cloner);
109      clone.actualName = actualName;
110      clone.Value = (IItem)cloner.Clone(value);
111      return clone;
112    }
113
114    public override string ToString() {
115      return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : ActualName, DataType.Name);
116    }
117
118    public event EventHandler ActualNameChanged;
119    private void OnActualNameChanged() {
120      if (ActualNameChanged != null)
121        ActualNameChanged(this, new EventArgs());
122      OnChanged();
123    }
124    public event EventHandler ValueChanged;
125    private void OnValueChanged() {
126      if (ValueChanged != null)
127        ValueChanged(this, new EventArgs());
128      OnChanged();
129    }
130
131    private void Value_Changed(object sender, ChangedEventArgs e) {
132      OnChanged(e);
133    }
134  }
135
136  [Item("ItemParameter<T>", "A generic parameter which represents an instance of type T.")]
137  [EmptyStorableClass]
138  public class ItemParameter<T> : ItemParameter where T : class, IItem {
139    public new T Value {
140      get { return (T)base.Value; }
141      set { base.Value = value; }
142    }
143
144    public ItemParameter()
145      : base("Anonymous", null, typeof(T)) {
146      Value = null;
147    }
148    public ItemParameter(string name, string description)
149      : base(name, description, typeof(T)) {
150      this.Value = null;
151    }
152    public ItemParameter(string name, string description, T value)
153      : base(name, description, typeof(T)) {
154      this.Value = value;
155    }
156
157    public new T GetValue(ExecutionContext context) {
158      return (T)base.GetValue(context);
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.