#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Collections;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Core {
///
/// Hierarchical container of variables (and of subscopes).
///
[Item("Scope", "A scope which contains variables and sub-scopes.")]
[Creatable("Test")]
[StorableClass]
public sealed class Scope : NamedItem, IScope {
[Storable]
private IScope parent;
public IScope Parent {
get { return parent; }
set {
if (parent != null) parent.SubScopes.Remove(this);
parent = value;
if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
}
}
[Storable]
private VariableCollection variables;
public VariableCollection Variables {
get { return variables; }
}
private ScopeList subScopes;
[Storable]
public ScopeList SubScopes {
get { return subScopes; }
private set {
DeregisterSubScopesEvents();
subScopes = value;
RegisterSubScopesEvents();
}
}
///
/// Initializes a new instance of having "Anonymous" as default name.
///
public Scope()
: base("Anonymous") {
parent = null;
variables = new VariableCollection();
SubScopes = new ScopeList();
}
///
/// Initializes a new instance of with the given .
///
/// The name of the scope.
public Scope(string name)
: base(name) {
parent = null;
variables = new VariableCollection();
SubScopes = new ScopeList();
}
public Scope(string name, string description)
: base(name, description) {
parent = null;
variables = new VariableCollection();
SubScopes = new ScopeList();
}
///
public void Clear() {
variables.Clear();
subScopes.Clear();
}
///
public override IDeepCloneable Clone(Cloner cloner) {
Scope clone = new Scope();
cloner.RegisterClonedObject(this, clone);
clone.Name = Name;
clone.Description = Description;
if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
return clone;
}
#region SubScopes Events
private void RegisterSubScopesEvents() {
if (subScopes != null) {
subScopes.ItemsAdded += new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded);
subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved);
subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced);
subScopes.CollectionReset += new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset);
}
}
private void DeregisterSubScopesEvents() {
if (subScopes != null) {
subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded);
subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved);
subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced);
subScopes.CollectionReset -= new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset);
}
}
private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
item.Value.Parent = null;
}
private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem oldItem in e.OldItems)
oldItem.Value.Parent = null;
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem oldItem in e.OldItems)
oldItem.Value.Parent = null;
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
#endregion
}
}