Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • replaced Changed in IItem by ToStringChanged
File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Collections;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24
25namespace HeuristicLab.Core {
26  /// <summary>
27  /// Hierarchical container of variables (and of subscopes).
28  /// </summary>
29  [Item("Scope", "A scope which contains variables and sub-scopes.")]
30  [Creatable("Test")]
31  public sealed class Scope : NamedItem, IScope {
32    [Storable]
33    private IScope parent;
34    public IScope Parent {
35      get { return parent; }
36      set {
37        if (parent != null) parent.SubScopes.Remove(this);
38        parent = value;
39        if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
40      }
41    }
42
43    [Storable]
44    private VariableCollection variables;
45    public VariableCollection Variables {
46      get { return variables; }
47    }
48
49    private ScopeList subScopes;
50    [Storable]
51    public ScopeList SubScopes {
52      get { return subScopes; }
53      private set {
54        DeregisterSubScopesEvents();
55        subScopes = value;
56        RegisterSubScopesEvents();
57      }
58    }
59
60    /// <summary>
61    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
62    /// </summary>
63    public Scope()
64      : base("Anonymous") {
65      parent = null;
66      variables = new VariableCollection();
67      SubScopes = new ScopeList();
68    }
69    /// <summary>
70    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
71    /// </summary>
72    /// <param name="name">The name of the scope.</param>
73    public Scope(string name)
74      : base(name) {
75      parent = null;
76      variables = new VariableCollection();
77      SubScopes = new ScopeList();
78    }
79    public Scope(string name, string description)
80      : base(name, description) {
81      parent = null;
82      variables = new VariableCollection();
83      SubScopes = new ScopeList();
84    }
85
86    /// <inheritdoc/>
87    public void Clear() {
88      variables.Clear();
89      subScopes.Clear();
90    }
91
92    /// <inheritdoc/>
93    public override IDeepCloneable Clone(Cloner cloner) {
94      Scope clone = new Scope();
95      cloner.RegisterClonedObject(this, clone);
96      clone.Name = Name;
97      clone.Description = Description;
98      if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
99      if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
100      return clone;
101    }
102
103    #region SubScopes Events
104    private void RegisterSubScopesEvents() {
105      if (subScopes != null) {
106        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
107        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
108        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
109        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
110      }
111    }
112    private void DeregisterSubScopesEvents() {
113      if (subScopes != null) {
114        subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
115        subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
116        subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
117        subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
118      }
119    }
120    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
121      foreach (IndexedItem<IScope> item in e.Items)
122        item.Value.Parent = this;
123    }
124    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
125      foreach (IndexedItem<IScope> item in e.Items)
126        item.Value.Parent = null;
127    }
128    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
129      foreach (IndexedItem<IScope> oldItem in e.OldItems)
130        oldItem.Value.Parent = null;
131      foreach (IndexedItem<IScope> item in e.Items)
132        item.Value.Parent = this;
133    }
134    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
135      foreach (IndexedItem<IScope> oldItem in e.OldItems)
136        oldItem.Value.Parent = null;
137      foreach (IndexedItem<IScope> item in e.Items)
138        item.Value.Parent = this;
139    }
140    #endregion
141  }
142}
Note: See TracBrowser for help on using the repository browser.