Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.3/Scope.cs @ 2565

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

Implemented generic EventArgs (#796)

File size: 12.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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Hierarchical container of variables (and of subscopes).
32  /// </summary>
33  public class Scope : ItemBase, IScope {
34
35    [Storable]
36    private IScope parent;
37
38    [Storable]
39    private string myName;
40    /// <summary>
41    /// Gets the name of the current scope.
42    /// </summary>
43    public string Name {
44      get { return myName; }
45    }
46   
47    private IDictionary<string, IVariable> myVariables;
48    /// <inheritdoc/>   
49    public ICollection<IVariable> Variables {
50      get { return myVariables.Values; }
51    }
52
53    [Storable(Name="Variables")]
54    private List<IVariable> VariablePersistence {
55      get { return new List<IVariable>(myVariables.Values); }
56      set {
57        myVariables.Clear();
58        foreach (IVariable var in value) {
59          AddVariable(var);
60        }
61      }
62    }
63   
64    [Storable]
65    private IDictionary<string, string> myAliases;
66    /// <inheritdoc/>
67    public IEnumerable<KeyValuePair<string, string>> Aliases {
68      get { return myAliases; }
69    }
70
71    [Storable]
72    private List<IScope> mySubScopes;
73    /// <summary>
74    /// Gets all subscopes of the current instance.
75    /// <note type="caution"> The subscopes are returned as read-only.</note>
76    /// </summary>
77    public IList<IScope> SubScopes {
78      get { return mySubScopes.AsReadOnly(); }
79    }
80
81    /// <summary>
82    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
83    /// </summary>
84    public Scope() {
85      myName = "Anonymous";
86      myVariables = new Dictionary<string, IVariable>();
87      myAliases = new Dictionary<string, string>();
88      mySubScopes = new List<IScope>();
89    }
90    /// <summary>
91    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
92    /// </summary>
93    /// <param name="name">The name of the scope.</param>
94    public Scope(string name)
95      : this() {
96      if (name != null) myName = name;
97    }
98
99    /// <inheritdoc/>
100    public void SetParent(IScope scope) {
101      parent = scope;
102    }
103
104    /// <summary>
105    /// Creates a new instance of <see cref="ScopeView"/> to represent the current instance visually.
106    /// </summary>
107    /// <returns>The created view as <see cref="ScopeView"/>.</returns>
108    public override IView CreateView() {
109      return new ScopeView(this);
110    }
111
112    /// <inheritdoc/>
113    public IVariable GetVariable(string name) {
114      IVariable variable;
115      if (myVariables.TryGetValue(name, out variable))
116        return variable;
117      else
118        return null;
119    }
120    /// <inheritdoc/>
121    public void AddVariable(IVariable variable) {
122      myVariables.Add(variable.Name, variable);
123      variable.NameChanging += new EventHandler<CancelEventArgs<string>>(Variable_NameChanging);
124      variable.NameChanged += new EventHandler(Variable_NameChanged);
125      OnVariableAdded(variable);
126    }
127
128    /// <inheritdoc/>
129    public void RemoveVariable(string name) {
130      IVariable variable;
131      if (myVariables.TryGetValue(name, out variable)) {
132        variable.NameChanging -= new EventHandler<CancelEventArgs<string>>(Variable_NameChanging);
133        variable.NameChanged -= new EventHandler(Variable_NameChanged);
134        myVariables.Remove(name);
135        OnVariableRemoved(variable);
136      }
137    }
138    private void Variable_NameChanging(object sender, CancelEventArgs<string> e) {
139      e.Cancel = myVariables.ContainsKey(e.Value);
140    }
141    private void Variable_NameChanged(object sender, EventArgs e) {
142      IVariable variable = (IVariable)sender;
143      string oldName = null;
144      foreach (KeyValuePair<string, IVariable> element in myVariables) {
145        if (element.Value == variable)
146          oldName = element.Key;
147      }
148      myVariables.Remove(oldName);
149      myVariables.Add(variable.Name, variable);
150    }
151    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool)"/>
152    public T GetVariableValue<T>(string name, bool recursiveLookup) where T : class, IItem {
153      return GetVariableValue<T>(name, recursiveLookup, true);
154    }
155    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool, bool)"/>
156    public T GetVariableValue<T>(string name, bool recursiveLookup, bool throwOnError) where T : class, IItem {
157      return (T)GetVariableValue(name, recursiveLookup, throwOnError);
158    }
159    /// <inheritdoc cref="IScope.GetVariableValue(string, bool)"/>
160    public IItem GetVariableValue(string name, bool recursiveLookup) {
161      return GetVariableValue(name, recursiveLookup, true);
162    }
163    /// <inheritdoc cref="IScope.GetVariableValue(string, bool, bool)"/>
164    public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
165      IVariable variable;
166      name = TranslateName(name);
167      if (myVariables.TryGetValue(name, out variable)) {
168        return variable.Value;
169      } else {
170        if (recursiveLookup && (parent != null))
171          return parent.GetVariableValue(name, recursiveLookup, throwOnError);
172        else {
173          if (throwOnError)
174            throw new ArgumentException("Variable " + name + " not found");
175          else
176            return null;
177        }
178      }
179    }
180    /// <inheritdoc/>
181    public string TranslateName(string name) {
182      while (myAliases.ContainsKey(name))
183        name = myAliases[name];
184      if (parent != null)
185        name = parent.TranslateName(name);
186      return name;
187    }
188    /// <inheritdoc/>
189    public void AddAlias(string alias, string name) {
190      RemoveAlias(alias);
191      if (alias != name) {
192        myAliases.Add(alias, name);
193        OnAliasAdded(alias);
194      }
195    }
196    /// <inheritdoc/>
197    public void RemoveAlias(string alias) {
198      if (myAliases.ContainsKey(alias)) {
199        myAliases.Remove(alias);
200        OnAliasRemoved(alias);
201      }
202    }
203
204    /// <inheritdoc/>
205    public void AddSubScope(IScope scope) {
206      scope.SetParent(this);
207      mySubScopes.Add(scope);
208      OnSubScopeAdded(scope, mySubScopes.Count - 1);
209    }
210    /// <inheritdoc/>
211    public void RemoveSubScope(IScope scope) {
212      int index = mySubScopes.IndexOf(scope);
213      if (mySubScopes.Remove(scope)) {
214        scope.SetParent(null);
215        OnSubScopeRemoved(scope, index);
216      }
217    }
218    /// <inheritdoc/>
219    public void ReorderSubScopes(int[] sequence) {
220      IScope[] scopes = mySubScopes.ToArray();
221      mySubScopes.Clear();
222      for (int i = 0; i < scopes.Length; i++)
223        mySubScopes.Add(scopes[sequence[i]]);
224      OnSubScopesReordered();
225    }
226    /// <inheritdoc/>
227    public IScope GetScope(Guid guid) {
228      if (Guid == guid) return this;
229      else {
230        for (int i = 0; i < mySubScopes.Count; i++) {
231          IScope s = mySubScopes[i].GetScope(guid);
232          if (s != null) return s;
233        }
234      }
235      return null;
236    }
237    /// <inheritdoc/>
238    public IScope GetScope(string name) {
239      if (Name == name) return this;
240      else {
241        for (int i = 0; i < mySubScopes.Count; i++) {
242          IScope s = mySubScopes[i].GetScope(name);
243          if (s != null) return s;
244        }
245      }
246      return null;
247    }
248
249    /// <inheritdoc/>
250    public void Clear() {
251      string[] variableNames = new string[Variables.Count];
252      int i = 0;
253      foreach (IVariable variable in Variables) {
254        variableNames[i] = variable.Name;
255        i++;
256      }
257      for (int j = 0; j < variableNames.Length; j++)
258        RemoveVariable(variableNames[j]);
259
260      KeyValuePair<string, string>[] aliases = new KeyValuePair<string, string>[myAliases.Count];
261      myAliases.CopyTo(aliases, 0);
262      for (int j = 0; j < aliases.Length; j++)
263        RemoveAlias(aliases[j].Key);
264
265      while (SubScopes.Count > 0)
266        RemoveSubScope(SubScopes[0]);
267    }
268
269    /// <inheritdoc/>
270    public override object Clone(IDictionary<Guid, object> clonedObjects) {
271      Scope clone = (Scope)base.Clone(clonedObjects);
272      clone.myName = Name;
273
274      foreach (IVariable variable in myVariables.Values)
275        clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
276      foreach (KeyValuePair<string, string> alias in myAliases)
277        clone.AddAlias(alias.Key, alias.Value);
278      for (int i = 0; i < SubScopes.Count; i++)
279        clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
280
281      return clone;
282    }
283
284    /// <inheritdoc />
285    public event EventHandler<EventArgs<IVariable>> VariableAdded;
286    /// <summary>
287    /// Fires a new <c>VariableAdded</c> event.
288    /// </summary>
289    /// <param name="variable">The variable that has been added.</param>
290    protected virtual void OnVariableAdded(IVariable variable) {
291      if (VariableAdded != null)
292        VariableAdded(this, new EventArgs<IVariable>(variable));
293    }
294    /// <inheritdoc />
295    public event EventHandler<EventArgs<IVariable>> VariableRemoved;
296    /// <summary>
297    /// Fires a new <c>VariableRemoved</c>.
298    /// </summary>
299    /// <param name="variable">The variable that has been deleted.</param>
300    protected virtual void OnVariableRemoved(IVariable variable) {
301      if (VariableRemoved != null)
302        VariableRemoved(this, new EventArgs<IVariable>(variable));
303    }
304    /// <inheritdoc />
305    public event EventHandler<EventArgs<string>> AliasAdded;
306    /// <summary>
307    /// Fires a new <c>AliasAdded</c> event.
308    /// </summary>
309    /// <param name="alias">The alias that has been added.</param>
310    protected virtual void OnAliasAdded(string alias) {
311      if (AliasAdded != null)
312        AliasAdded(this, new EventArgs<string>(alias));
313    }
314    /// <inheritdoc/>
315    public event EventHandler<EventArgs<string>> AliasRemoved;
316    /// <summary>
317    /// Fires a new <c>AliasRemoved</c> event.
318    /// </summary>
319    /// <param name="alias">The alias that has been deleted.</param>
320    protected virtual void OnAliasRemoved(string alias) {
321      if (AliasRemoved != null)
322        AliasRemoved(this, new EventArgs<string>(alias));
323    }
324    /// <inheritdoc/>
325    public event EventHandler<EventArgs<IScope, int>> SubScopeAdded;
326    /// <summary>
327    /// Fires a new <c>SubScopeAdded</c> event.
328    /// </summary>
329    /// <param name="scope">The sub scope that has been added.</param>
330    /// <param name="index">The index where the scope has been added.</param>
331    protected virtual void OnSubScopeAdded(IScope scope, int index) {
332      if (SubScopeAdded != null)
333        SubScopeAdded(this, new EventArgs<IScope, int>(scope, index));
334    }
335    /// <inheritdoc/>
336    public event EventHandler<EventArgs<IScope, int>> SubScopeRemoved;
337    /// <summary>
338    /// Fires a new <c>SubScopeRemoved</c> event.
339    /// </summary>
340    /// <param name="scope">The sub scope that has been deleted.</param>
341    /// <param name="index">The position of the sub scope.</param>
342    protected virtual void OnSubScopeRemoved(IScope scope, int index) {
343      if (SubScopeRemoved != null)
344        SubScopeRemoved(this, new EventArgs<IScope, int>(scope, index));
345    }
346    /// <inheritdoc />
347    public event EventHandler SubScopesReordered;
348    /// <summary>
349    /// Fires a new <c>SubScopesReordered</c> event
350    /// </summary>
351    protected virtual void OnSubScopesReordered() {
352      if (SubScopesReordered != null)
353        SubScopesReordered(this, new EventArgs());
354    }
355  }
356}
Note: See TracBrowser for help on using the repository browser.