Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1724 was 1667, checked in by epitzer, 15 years ago

Convert persistence of Core plugin to new persistence framework. The target framework has been upgraded from 2.0 to 3.5 and events during persistence are not generated anymore. (#603)

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.Decomposers.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    private IScope parent;
35
36    [Storable]
37    private string myName;
38    /// <summary>
39    /// Gets the name of the current scope.
40    /// </summary>
41    public string Name {
42      get { return myName; }
43    }
44
45    [Storable]
46    private IDictionary<string, IVariable> myVariables;
47    /// <inheritdoc/>
48    public ICollection<IVariable> Variables {
49      get { return myVariables.Values; }
50    }
51
52    [Storable]
53    private IDictionary<string, string> myAliases;
54    /// <inheritdoc/>
55    public IEnumerable<KeyValuePair<string, string>> Aliases {
56      get { return myAliases; }
57    }
58
59    [Storable]
60    private List<IScope> mySubScopes;
61    /// <summary>
62    /// Gets all subscopes of the current instance.
63    /// <note type="caution"> The subscopes are returned as read-only.</note>
64    /// </summary>
65    public IList<IScope> SubScopes {
66      get { return mySubScopes.AsReadOnly(); }
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
71    /// </summary>
72    public Scope() {
73      myName = "Anonymous";
74      myVariables = new Dictionary<string, IVariable>();
75      myAliases = new Dictionary<string, string>();
76      mySubScopes = new List<IScope>();
77    }
78    /// <summary>
79    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
80    /// </summary>
81    /// <param name="name">The name of the scope.</param>
82    public Scope(string name)
83      : this() {
84      if (name != null) myName = name;
85    }
86
87    /// <inheritdoc/>
88    public void SetParent(IScope scope) {
89      parent = scope;
90    }
91
92    /// <summary>
93    /// Creates a new instance of <see cref="ScopeView"/> to represent the current instance visually.
94    /// </summary>
95    /// <returns>The created view as <see cref="ScopeView"/>.</returns>
96    public override IView CreateView() {
97      return new ScopeView(this);
98    }
99
100    /// <inheritdoc/>
101    public IVariable GetVariable(string name) {
102      IVariable variable;
103      if (myVariables.TryGetValue(name, out variable))
104        return variable;
105      else
106        return null;
107    }
108    /// <inheritdoc/>
109    public void AddVariable(IVariable variable) {
110      myVariables.Add(variable.Name, variable);
111      variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
112      variable.NameChanged += new EventHandler(Variable_NameChanged);
113      OnVariableAdded(variable);
114    }
115
116    /// <inheritdoc/>
117    public void RemoveVariable(string name) {
118      IVariable variable;
119      if (myVariables.TryGetValue(name, out variable)) {
120        variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
121        variable.NameChanged -= new EventHandler(Variable_NameChanged);
122        myVariables.Remove(name);
123        OnVariableRemoved(variable);
124      }
125    }
126    private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
127      e.Cancel = myVariables.ContainsKey(e.Name);
128    }
129    private void Variable_NameChanged(object sender, EventArgs e) {
130      IVariable variable = (IVariable)sender;
131      string oldName = null;
132      foreach (KeyValuePair<string, IVariable> element in myVariables) {
133        if (element.Value == variable)
134          oldName = element.Key;
135      }
136      myVariables.Remove(oldName);
137      myVariables.Add(variable.Name, variable);
138    }
139    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool)"/>
140    public T GetVariableValue<T>(string name, bool recursiveLookup) where T : class, IItem {
141      return GetVariableValue<T>(name, recursiveLookup, true);
142    }
143    /// <inheritdoc cref="IScope.GetVariableValue&lt;T&gt;(string, bool, bool)"/>
144    public T GetVariableValue<T>(string name, bool recursiveLookup, bool throwOnError) where T : class, IItem {
145      return (T)GetVariableValue(name, recursiveLookup, throwOnError);
146    }
147    /// <inheritdoc cref="IScope.GetVariableValue(string, bool)"/>
148    public IItem GetVariableValue(string name, bool recursiveLookup) {
149      return GetVariableValue(name, recursiveLookup, true);
150    }
151    /// <inheritdoc cref="IScope.GetVariableValue(string, bool, bool)"/>
152    public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
153      IVariable variable;
154      name = TranslateName(name);
155      if (myVariables.TryGetValue(name, out variable)) {
156        return variable.Value;
157      } else {
158        if (recursiveLookup && (parent != null))
159          return parent.GetVariableValue(name, recursiveLookup, throwOnError);
160        else {
161          if (throwOnError)
162            throw new ArgumentException("Variable " + name + " not found");
163          else
164            return null;
165        }
166      }
167    }
168    /// <inheritdoc/>
169    public string TranslateName(string name) {
170      while (myAliases.ContainsKey(name))
171        name = myAliases[name];
172      if (parent != null)
173        name = parent.TranslateName(name);
174      return name;
175    }
176    /// <inheritdoc/>
177    public void AddAlias(string alias, string name) {
178      RemoveAlias(alias);
179      if (alias != name) {
180        myAliases.Add(alias, name);
181        OnAliasAdded(alias);
182      }
183    }
184    /// <inheritdoc/>
185    public void RemoveAlias(string alias) {
186      if (myAliases.ContainsKey(alias)) {
187        myAliases.Remove(alias);
188        OnAliasRemoved(alias);
189      }
190    }
191
192    /// <inheritdoc/>
193    public void AddSubScope(IScope scope) {
194      scope.SetParent(this);
195      mySubScopes.Add(scope);
196      OnSubScopeAdded(scope, mySubScopes.Count - 1);
197    }
198    /// <inheritdoc/>
199    public void RemoveSubScope(IScope scope) {
200      int index = mySubScopes.IndexOf(scope);
201      if (mySubScopes.Remove(scope)) {
202        scope.SetParent(null);
203        OnSubScopeRemoved(scope, index);
204      }
205    }
206    /// <inheritdoc/>
207    public void ReorderSubScopes(int[] sequence) {
208      IScope[] scopes = mySubScopes.ToArray();
209      mySubScopes.Clear();
210      for (int i = 0; i < scopes.Length; i++)
211        mySubScopes.Add(scopes[sequence[i]]);
212      OnSubScopesReordered();
213    }
214    /// <inheritdoc/>
215    public IScope GetScope(Guid guid) {
216      if (Guid == guid) return this;
217      else {
218        for (int i = 0; i < mySubScopes.Count; i++) {
219          IScope s = mySubScopes[i].GetScope(guid);
220          if (s != null) return s;
221        }
222      }
223      return null;
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<Guid, 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.