#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")] 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); } } private VariableCollection variables; [Storable] public VariableCollection Variables { get { return variables; } private set { if (variables != null) variables.Changed -= new ChangedEventHandler(Variables_Changed); variables = value; if (variables != null) variables.Changed += new ChangedEventHandler(Variables_Changed); } } 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); subScopes.Changed += new ChangedEventHandler(SubScopes_Changed); } } 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); subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed); } } 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; } private void SubScopes_Changed(object sender, ChangedEventArgs e) { OnChanged(e); } #endregion #region Variables Events private void Variables_Changed(object sender, ChangedEventArgs e) { OnChanged(e); } #endregion } }