Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on operators and SGA
  • improved performance
File size: 5.9 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
[2653]43    private VariableCollection variables;
[1667]44    [Storable]
[2653]45    public VariableCollection Variables {
46      get { return variables; }
47      private set {
48        if (variables != null) variables.Changed -= new ChangedEventHandler(Variables_Changed);
49        variables = value;
50        if (variables != null) variables.Changed += new ChangedEventHandler(Variables_Changed);
[1727]51      }
52    }
[1667]53
[2653]54    private ScopeList subScopes;
[1667]55    [Storable]
[2653]56    public ScopeList SubScopes {
57      get { return subScopes; }
58      private set {
59        DeregisterSubScopesEvents();
60        subScopes = value;
61        RegisterSubScopesEvents();
62      }
[2]63    }
64
[776]65    /// <summary>
66    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
67    /// </summary>
[2653]68    public Scope()
69      : base("Anonymous") {
70      parent = null;
71      Variables = new VariableCollection();
72      SubScopes = new ScopeList();
[2]73    }
[776]74    /// <summary>
75    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
76    /// </summary>
77    /// <param name="name">The name of the scope.</param>
[2]78    public Scope(string name)
[2653]79      : base(name) {
80      parent = null;
81      Variables = new VariableCollection();
82      SubScopes = new ScopeList();
[2]83    }
84
[776]85    /// <inheritdoc/>
[2]86    public void Clear() {
[2653]87      variables.Clear();
88      subScopes.Clear();
[2]89    }
90
[776]91    /// <inheritdoc/>
[2653]92    public override IDeepCloneable Clone(Cloner cloner) {
[2687]93      Scope clone = new Scope();
94      cloner.RegisterClonedObject(this, clone);
[2790]95      clone.Name = Name;
96      clone.Description = Description;
[2830]97      if (variables.Count > 0) clone.Variables = (VariableCollection)cloner.Clone(variables);
98      if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
[2]99      return clone;
100    }
101
[2653]102    #region SubScopes Events
103    private void RegisterSubScopesEvents() {
104      if (subScopes != null) {
[2687]105        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
106        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
107        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
108        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
[2653]109        subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
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        subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
119      }
[2]120    }
[2687]121    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
122      foreach (IndexedItem<IScope> item in e.Items)
123        item.Value.Parent = this;
[61]124    }
[2687]125    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
126      foreach (IndexedItem<IScope> item in e.Items)
127        item.Value.Parent = null;
[61]128    }
[2687]129    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
130      foreach (IndexedItem<IScope> oldItem in e.OldItems)
131        oldItem.Value.Parent = null;
132      foreach (IndexedItem<IScope> item in e.Items)
133        item.Value.Parent = this;
[2]134    }
[2687]135    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
136      foreach (IndexedItem<IScope> oldItem in e.OldItems)
137        oldItem.Value.Parent = null;
138      foreach (IndexedItem<IScope> item in e.Items)
139        item.Value.Parent = this;
[2]140    }
[2653]141    private void SubScopes_Changed(object sender, ChangedEventArgs e) {
142      OnChanged(e);
[2]143    }
[2653]144    #endregion
145
146    #region Variables Events
147    private void Variables_Changed(object sender, ChangedEventArgs e) {
148      OnChanged(e);
149    }
150    #endregion
[2]151  }
152}
Note: See TracBrowser for help on using the repository browser.