Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2754 was 2687, checked in by swagner, 15 years ago

Operator architecture refactoring (#95)

  • implemented ideas which came up during today's presentation of HeuristicLab.Core and related plugins
File size: 6.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.CompositeSerializers.Storable;
27using HeuristicLab.Collections;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Hierarchical container of variables (and of subscopes).
32  /// </summary>
33  [Item("Scope", "A scope which contains variables and sub-scopes.")]
34  [Creatable("Test")]
35  public sealed class Scope : NamedItem, IScope {
36    [Storable]
37    private IScope parent;
38    public IScope Parent {
39      get { return parent; }
40      set {
41        if (parent != null) parent.SubScopes.Remove(this);
42        parent = value;
43        if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
44      }
45    }
46
47    private VariableCollection variables;
48    [Storable]
49    public VariableCollection Variables {
50      get { return variables; }
51      private set {
52        if (variables != null) variables.Changed -= new ChangedEventHandler(Variables_Changed);
53        variables = value;
54        if (variables != null) variables.Changed += new ChangedEventHandler(Variables_Changed);
55      }
56    }
57
58    private ScopeList subScopes;
59    [Storable]
60    public ScopeList SubScopes {
61      get { return subScopes; }
62      private set {
63        DeregisterSubScopesEvents();
64        subScopes = value;
65        RegisterSubScopesEvents();
66      }
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
71    /// </summary>
72    public Scope()
73      : base("Anonymous") {
74      parent = null;
75      Variables = new VariableCollection();
76      SubScopes = new ScopeList();
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      : base(name) {
84      parent = null;
85      Variables = new VariableCollection();
86      SubScopes = new ScopeList();
87    }
88
89    /// <inheritdoc/>
90    public void Clear() {
91      variables.Clear();
92      subScopes.Clear();
93    }
94
95    /// <inheritdoc/>
96    public override IDeepCloneable Clone(Cloner cloner) {
97      Scope clone = new Scope();
98      cloner.RegisterClonedObject(this, clone);
99      clone.name = name;
100      clone.description = description;
101      clone.parent = (IScope)cloner.Clone(parent);
102      clone.Variables = (VariableCollection)cloner.Clone(variables);
103      clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
104      return clone;
105    }
106
107    #region SubScopes Events
108    private void RegisterSubScopesEvents() {
109      if (subScopes != null) {
110        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
111        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
112        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
113        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
114        subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
115      }
116    }
117    private void DeregisterSubScopesEvents() {
118      if (subScopes != null) {
119        subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
120        subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
121        subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
122        subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
123        subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
124      }
125    }
126    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
127      foreach (IndexedItem<IScope> item in e.Items)
128        item.Value.Parent = this;
129    }
130    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
131      foreach (IndexedItem<IScope> item in e.Items)
132        item.Value.Parent = null;
133    }
134    private void SubScopes_ItemsReplaced(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    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
141      foreach (IndexedItem<IScope> oldItem in e.OldItems)
142        oldItem.Value.Parent = null;
143      foreach (IndexedItem<IScope> item in e.Items)
144        item.Value.Parent = this;
145    }
146    private void SubScopes_Changed(object sender, ChangedEventArgs e) {
147      OnChanged(e);
148    }
149    #endregion
150
151    #region Variables Events
152    private void Variables_Changed(object sender, ChangedEventArgs e) {
153      OnChanged(e);
154    }
155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.