Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Scope.cs @ 2151

Last change on this file since 2151 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 12.3 KB
RevLine 
[2]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;
[1823]26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]27
28namespace HeuristicLab.Core {
[776]29  /// <summary>
30  /// Hierarchical container of variables (and of subscopes).
31  /// </summary>
[2]32  public class Scope : ItemBase, IScope {
[1667]33
[1727]34    [Storable]
[2]35    private IScope parent;
36
[1667]37    [Storable]
38    private string myName;
[776]39    /// <summary>
40    /// Gets the name of the current scope.
41    /// </summary>
[2]42    public string Name {
43      get { return myName; }
44    }
[1727]45   
[2]46    private IDictionary<string, IVariable> myVariables;
[1727]47    /// <inheritdoc/>   
[2]48    public ICollection<IVariable> Variables {
49      get { return myVariables.Values; }
50    }
[1667]51
[1727]52    [Storable(Name="Variables")]
53    private List<IVariable> VariablePersistence {
54      get { return new List<IVariable>(myVariables.Values); }
55      set {
[1732]56        myVariables.Clear();
[1727]57        foreach (IVariable var in value) {
58          AddVariable(var);
59        }
60      }
61    }
62   
[1667]63    [Storable]
[61]64    private IDictionary<string, string> myAliases;
[776]65    /// <inheritdoc/>
[63]66    public IEnumerable<KeyValuePair<string, string>> Aliases {
67      get { return myAliases; }
[61]68    }
[1667]69
70    [Storable]
[2]71    private List<IScope> mySubScopes;
[776]72    /// <summary>
73    /// Gets all subscopes of the current instance.
74    /// <note type="caution"> The subscopes are returned as read-only.</note>
75    /// </summary>
[2]76    public IList<IScope> SubScopes {
77      get { return mySubScopes.AsReadOnly(); }
78    }
79
[776]80    /// <summary>
81    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
82    /// </summary>
[2]83    public Scope() {
84      myName = "Anonymous";
85      myVariables = new Dictionary<string, IVariable>();
[61]86      myAliases = new Dictionary<string, string>();
[2]87      mySubScopes = new List<IScope>();
88    }
[776]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>
[2]93    public Scope(string name)
94      : this() {
95      if (name != null) myName = name;
96    }
97
[776]98    /// <inheritdoc/>
[2]99    public void SetParent(IScope scope) {
100      parent = scope;
101    }
102
[776]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>
[2]107    public override IView CreateView() {
108      return new ScopeView(this);
109    }
110
[776]111    /// <inheritdoc/>
[2]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    }
[776]119    /// <inheritdoc/>
[2]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
[776]127    /// <inheritdoc/>
[2]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    }
[776]150    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool)"/>
[2]151    public T GetVariableValue<T>(string name, bool recursiveLookup) where T : class, IItem {
152      return GetVariableValue<T>(name, recursiveLookup, true);
153    }
[776]154    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool, bool)"/>
[2]155    public T GetVariableValue<T>(string name, bool recursiveLookup, bool throwOnError) where T : class, IItem {
156      return (T)GetVariableValue(name, recursiveLookup, throwOnError);
157    }
[776]158    /// <inheritdoc cref="IScope.GetVariableValue(string, bool)"/>
[2]159    public IItem GetVariableValue(string name, bool recursiveLookup) {
160      return GetVariableValue(name, recursiveLookup, true);
161    }
[776]162    /// <inheritdoc cref="IScope.GetVariableValue(string, bool, bool)"/>
[2]163    public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
164      IVariable variable;
[61]165      name = TranslateName(name);
[2]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    }
[776]179    /// <inheritdoc/>
[61]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    }
[776]187    /// <inheritdoc/>
[61]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    }
[776]195    /// <inheritdoc/>
[61]196    public void RemoveAlias(string alias) {
197      if (myAliases.ContainsKey(alias)) {
198        myAliases.Remove(alias);
199        OnAliasRemoved(alias);
200      }
201    }
202
[776]203    /// <inheritdoc/>
[2]204    public void AddSubScope(IScope scope) {
205      scope.SetParent(this);
206      mySubScopes.Add(scope);
207      OnSubScopeAdded(scope, mySubScopes.Count - 1);
208    }
[776]209    /// <inheritdoc/>
[2]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    }
[776]217    /// <inheritdoc/>
[2]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    }
[776]225    /// <inheritdoc/>
[2]226    public IScope GetScope(Guid guid) {
227      if (Guid == guid) return this;
228      else {
229        for (int i = 0; i < mySubScopes.Count; i++) {
[261]230          IScope s = mySubScopes[i].GetScope(guid);
[2]231          if (s != null) return s;
232        }
233      }
234      return null;
235    }
[776]236    /// <inheritdoc/>
[2]237    public IScope GetScope(string name) {
238      if (Name == name) return this;
239      else {
240        for (int i = 0; i < mySubScopes.Count; i++) {
[261]241          IScope s = mySubScopes[i].GetScope(name);
[2]242          if (s != null) return s;
243        }
244      }
245      return null;
246    }
247
[776]248    /// <inheritdoc/>
[2]249    public void Clear() {
250      string[] variableNames = new string[Variables.Count];
251      int i = 0;
252      foreach (IVariable variable in Variables) {
253        variableNames[i] = variable.Name;
254        i++;
255      }
256      for (int j = 0; j < variableNames.Length; j++)
257        RemoveVariable(variableNames[j]);
258
[1667]259      KeyValuePair<string, string>[] aliases = new KeyValuePair<string, string>[myAliases.Count];
[63]260      myAliases.CopyTo(aliases, 0);
[61]261      for (int j = 0; j < aliases.Length; j++)
[63]262        RemoveAlias(aliases[j].Key);
[61]263
[2]264      while (SubScopes.Count > 0)
265        RemoveSubScope(SubScopes[0]);
266    }
267
[776]268    /// <inheritdoc/>
[2]269    public override object Clone(IDictionary<Guid, object> clonedObjects) {
270      Scope clone = (Scope)base.Clone(clonedObjects);
271      clone.myName = Name;
272
273      foreach (IVariable variable in myVariables.Values)
274        clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
[61]275      foreach (KeyValuePair<string, string> alias in myAliases)
276        clone.AddAlias(alias.Key, alias.Value);
[2]277      for (int i = 0; i < SubScopes.Count; i++)
278        clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
279
280      return clone;
281    }
282
[776]283    /// <inheritdoc />
[2]284    public event EventHandler<VariableEventArgs> VariableAdded;
[776]285    /// <summary>
286    /// Fires a new <c>VariableAdded</c> event.
287    /// </summary>
288    /// <param name="variable">The variable that has been added.</param>
[2]289    protected virtual void OnVariableAdded(IVariable variable) {
290      if (VariableAdded != null)
291        VariableAdded(this, new VariableEventArgs(variable));
292    }
[776]293    /// <inheritdoc />
[2]294    public event EventHandler<VariableEventArgs> VariableRemoved;
[776]295    /// <summary>
296    /// Fires a new <c>VariableRemoved</c>.
297    /// </summary>
298    /// <param name="variable">The variable that has been deleted.</param>
[2]299    protected virtual void OnVariableRemoved(IVariable variable) {
300      if (VariableRemoved != null)
301        VariableRemoved(this, new VariableEventArgs(variable));
302    }
[776]303    /// <inheritdoc />
[61]304    public event EventHandler<AliasEventArgs> AliasAdded;
[776]305    /// <summary>
306    /// Fires a new <c>AliasAdded</c> event.
307    /// </summary>
308    /// <param name="alias">The alias that has been added.</param>
[61]309    protected virtual void OnAliasAdded(string alias) {
310      if (AliasAdded != null)
311        AliasAdded(this, new AliasEventArgs(alias));
312    }
[776]313    /// <inheritdoc/>
[61]314    public event EventHandler<AliasEventArgs> AliasRemoved;
[776]315    /// <summary>
316    /// Fires a new <c>AliasRemoved</c> event.
317    /// </summary>
318    /// <param name="alias">The alias that has been deleted.</param>
[61]319    protected virtual void OnAliasRemoved(string alias) {
320      if (AliasRemoved != null)
321        AliasRemoved(this, new AliasEventArgs(alias));
322    }
[776]323    /// <inheritdoc/>
[2]324    public event EventHandler<ScopeIndexEventArgs> SubScopeAdded;
[776]325    /// <summary>
326    /// Fires a new <c>SubScopeAdded</c> event.
327    /// </summary>
328    /// <param name="scope">The sub scope that has been added.</param>
329    /// <param name="index">The index where the scope has been added.</param>
[2]330    protected virtual void OnSubScopeAdded(IScope scope, int index) {
331      if (SubScopeAdded != null)
332        SubScopeAdded(this, new ScopeIndexEventArgs(scope, index));
333    }
[776]334    /// <inheritdoc/>
[2]335    public event EventHandler<ScopeIndexEventArgs> SubScopeRemoved;
[776]336    /// <summary>
337    /// Fires a new <c>SubScopeRemoved</c> event.
338    /// </summary>
339    /// <param name="scope">The sub scope that has been deleted.</param>
340    /// <param name="index">The position of the sub scope.</param>
[2]341    protected virtual void OnSubScopeRemoved(IScope scope, int index) {
342      if (SubScopeRemoved != null)
343        SubScopeRemoved(this, new ScopeIndexEventArgs(scope, index));
344    }
[776]345    /// <inheritdoc />
[2]346    public event EventHandler SubScopesReordered;
[776]347    /// <summary>
348    /// Fires a new <c>SubScopesReordered</c> event
349    /// </summary>
[2]350    protected virtual void OnSubScopesReordered() {
351      if (SubScopesReordered != null)
352        SubScopesReordered(this, new EventArgs());
353    }
354  }
355}
Note: See TracBrowser for help on using the repository browser.