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

Last change on this file since 2931 was 2931, checked in by swagner, 12 years ago

Operator architecture refactoring (#95)

  • worked on ItemAttribute and named items
  • corrected version information in Optimizer
File size: 6.1 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    private VariableCollection variables;
44    [Storable]
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);
51      }
52    }
53
54    private ScopeList subScopes;
55    [Storable]
56    public ScopeList SubScopes {
57      get { return subScopes; }
58      private set {
59        DeregisterSubScopesEvents();
60        subScopes = value;
61        RegisterSubScopesEvents();
62      }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
67    /// </summary>
68    public Scope()
69      : base("Anonymous") {
70      parent = null;
71      Variables = new VariableCollection();
72      SubScopes = new ScopeList();
73    }
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>
78    public Scope(string name)
79      : base(name) {
80      parent = null;
81      Variables = new VariableCollection();
82      SubScopes = new ScopeList();
83    }
84    public Scope(string name, string description)
85      : base(name, description) {
86      parent = null;
87      Variables = new VariableCollection();
88      SubScopes = new ScopeList();
89    }
90
91    /// <inheritdoc/>
92    public void Clear() {
93      variables.Clear();
94      subScopes.Clear();
95    }
96
97    /// <inheritdoc/>
98    public override IDeepCloneable Clone(Cloner cloner) {
99      Scope clone = new Scope();
100      cloner.RegisterClonedObject(this, clone);
101      clone.Name = Name;
102      clone.Description = Description;
103      if (variables.Count > 0) clone.Variables = (VariableCollection)cloner.Clone(variables);
104      if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
105      return clone;
106    }
107
108    #region SubScopes Events
109    private void RegisterSubScopesEvents() {
110      if (subScopes != null) {
111        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
112        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
113        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
114        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
115        subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
116      }
117    }
118    private void DeregisterSubScopesEvents() {
119      if (subScopes != null) {
120        subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
121        subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
122        subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
123        subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
124        subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
125      }
126    }
127    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
128      foreach (IndexedItem<IScope> item in e.Items)
129        item.Value.Parent = this;
130    }
131    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
132      foreach (IndexedItem<IScope> item in e.Items)
133        item.Value.Parent = null;
134    }
135    private void SubScopes_ItemsReplaced(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;
140    }
141    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
142      foreach (IndexedItem<IScope> oldItem in e.OldItems)
143        oldItem.Value.Parent = null;
144      foreach (IndexedItem<IScope> item in e.Items)
145        item.Value.Parent = this;
146    }
147    private void SubScopes_Changed(object sender, ChangedEventArgs e) {
148      OnChanged(e);
149    }
150    #endregion
151
152    #region Variables Events
153    private void Variables_Changed(object sender, ChangedEventArgs e) {
154      OnChanged(e);
155    }
156    #endregion
157  }
158}
Note: See TracBrowser for help on using the repository browser.