Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/3.3/Scope.cs @ 2033

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

Refactoring of the operator architecture (#95)

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