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
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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
[2830]22using HeuristicLab.Collections;
[1823]23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]24
25namespace HeuristicLab.Core {
[776]26  /// <summary>
27  /// Hierarchical container of variables (and of subscopes).
28  /// </summary>
[2653]29  [Item("Scope", "A scope which contains variables and sub-scopes.")]
30  [Creatable("Test")]
[2687]31  public sealed class Scope : NamedItem, IScope {
[1727]32    [Storable]
[2687]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    }
[2]42
[2932]43    [Storable]
[2653]44    private VariableCollection variables;
45    public VariableCollection Variables {
46      get { return variables; }
[1727]47    }
[1667]48
[2653]49    private ScopeList subScopes;
[1667]50    [Storable]
[2653]51    public ScopeList SubScopes {
52      get { return subScopes; }
53      private set {
54        DeregisterSubScopesEvents();
55        subScopes = value;
56        RegisterSubScopesEvents();
57      }
[2]58    }
59
[776]60    /// <summary>
61    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
62    /// </summary>
[2653]63    public Scope()
64      : base("Anonymous") {
65      parent = null;
[2932]66      variables = new VariableCollection();
[2653]67      SubScopes = new ScopeList();
[2]68    }
[776]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>
[2]73    public Scope(string name)
[2653]74      : base(name) {
75      parent = null;
[2932]76      variables = new VariableCollection();
[2653]77      SubScopes = new ScopeList();
[2]78    }
[2931]79    public Scope(string name, string description)
80      : base(name, description) {
81      parent = null;
[2932]82      variables = new VariableCollection();
[2931]83      SubScopes = new ScopeList();
84    }
[2]85
[776]86    /// <inheritdoc/>
[2]87    public void Clear() {
[2653]88      variables.Clear();
89      subScopes.Clear();
[2]90    }
91
[776]92    /// <inheritdoc/>
[2653]93    public override IDeepCloneable Clone(Cloner cloner) {
[2687]94      Scope clone = new Scope();
95      cloner.RegisterClonedObject(this, clone);
[2790]96      clone.Name = Name;
97      clone.Description = Description;
[2932]98      if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
[2830]99      if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
[2]100      return clone;
101    }
102
[2653]103    #region SubScopes Events
104    private void RegisterSubScopesEvents() {
105      if (subScopes != null) {
[2687]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);
[2653]110      }
[2]111    }
[2653]112    private void DeregisterSubScopesEvents() {
113      if (subScopes != null) {
[2687]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);
[2653]118      }
[2]119    }
[2687]120    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
121      foreach (IndexedItem<IScope> item in e.Items)
122        item.Value.Parent = this;
[61]123    }
[2687]124    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
125      foreach (IndexedItem<IScope> item in e.Items)
126        item.Value.Parent = null;
[61]127    }
[2687]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;
[2]133    }
[2687]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;
[2]139    }
[2653]140    #endregion
[2]141  }
142}
Note: See TracBrowser for help on using the repository browser.