Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/ItemParameter.cs @ 2664

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

Abandoned policy that the names of all abstract base classes have to end in "Base" (#95)

File size: 5.0 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Represents a parameter.
32  /// </summary>
33  [Item("Item Parameter", "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      if (Value != null) return Value;
87      ExecutionContext parent = context.Parent;
88      IParameter parameter = null;
89
90      while ((parent != null) && (parameter == null)) {
91        parent.Operator.Parameters.TryGetValue(ActualName, out parameter);
92        parent = parent.Parent;
93      }
94
95      if (parameter != null) return parameter.GetValue(context);
96      else return context.Scope.Lookup(ActualName, true).Value;
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      ItemParameter clone = (ItemParameter)base.Clone(cloner);
101      clone.actualName = actualName;
102      clone.Value = (IItem)cloner.Clone(value);
103      return clone;
104    }
105
106    public override string ToString() {
107      return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : ActualName, DataType.Name);
108    }
109
110    public event EventHandler ActualNameChanged;
111    private void OnActualNameChanged() {
112      if (ActualNameChanged != null)
113        ActualNameChanged(this, new EventArgs());
114      OnChanged();
115    }
116    public event EventHandler ValueChanged;
117    private void OnValueChanged() {
118      if (ValueChanged != null)
119        ValueChanged(this, new EventArgs());
120      OnChanged();
121    }
122
123    private void Value_Changed(object sender, ChangedEventArgs e) {
124      OnChanged(e);
125    }
126  }
127
128  [Item("Parameter<T>", "A generic parameter which represents an instance of type T.")]
129  [EmptyStorableClass]
130  public class ItemParameter<T> : ItemParameter where T : class, IItem {
131    public new T Value {
132      get { return (T)base.Value; }
133      set { base.Value = value; }
134    }
135
136    public ItemParameter()
137      : base("Anonymous", null, typeof(T)) {
138      Value = null;
139    }
140    public ItemParameter(string name, string description)
141      : base(name, description, typeof(T)) {
142      this.Value = null;
143    }
144    public ItemParameter(string name, string description, T value)
145      : base(name, description, typeof(T)) {
146      this.Value = value;
147    }
148
149    public new T GetValue(ExecutionContext context) {
150      return (T)base.GetValue(context);
151    }
152  }
153}
Note: See TracBrowser for help on using the repository browser.