[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
[1823] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2653] | 27 | using HeuristicLab.Collections;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[776] | 30 | /// <summary>
|
---|
| 31 | /// Hierarchical container of variables (and of subscopes).
|
---|
| 32 | /// </summary>
|
---|
[2653] | 33 | [Item("Scope", "A scope which contains variables and sub-scopes.")]
|
---|
| 34 | [Creatable("Test")]
|
---|
[2687] | 35 | public sealed class Scope : NamedItem, IScope {
|
---|
[1727] | 36 | [Storable]
|
---|
[2687] | 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 | }
|
---|
[2] | 46 |
|
---|
[2653] | 47 | private VariableCollection variables;
|
---|
[1667] | 48 | [Storable]
|
---|
[2653] | 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);
|
---|
[1727] | 55 | }
|
---|
| 56 | }
|
---|
[1667] | 57 |
|
---|
[2653] | 58 | private ScopeList subScopes;
|
---|
[1667] | 59 | [Storable]
|
---|
[2653] | 60 | public ScopeList SubScopes {
|
---|
| 61 | get { return subScopes; }
|
---|
| 62 | private set {
|
---|
| 63 | DeregisterSubScopesEvents();
|
---|
| 64 | subScopes = value;
|
---|
| 65 | RegisterSubScopesEvents();
|
---|
| 66 | }
|
---|
[2] | 67 | }
|
---|
| 68 |
|
---|
[776] | 69 | /// <summary>
|
---|
| 70 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
| 71 | /// </summary>
|
---|
[2653] | 72 | public Scope()
|
---|
| 73 | : base("Anonymous") {
|
---|
| 74 | parent = null;
|
---|
| 75 | Variables = new VariableCollection();
|
---|
| 76 | SubScopes = new ScopeList();
|
---|
[2] | 77 | }
|
---|
[776] | 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>
|
---|
[2] | 82 | public Scope(string name)
|
---|
[2653] | 83 | : base(name) {
|
---|
| 84 | parent = null;
|
---|
| 85 | Variables = new VariableCollection();
|
---|
| 86 | SubScopes = new ScopeList();
|
---|
[2] | 87 | }
|
---|
| 88 |
|
---|
[776] | 89 | /// <inheritdoc/>
|
---|
[2] | 90 | public void Clear() {
|
---|
[2653] | 91 | variables.Clear();
|
---|
| 92 | subScopes.Clear();
|
---|
[2] | 93 | }
|
---|
| 94 |
|
---|
[776] | 95 | /// <inheritdoc/>
|
---|
[2653] | 96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[2687] | 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);
|
---|
[2653] | 102 | clone.Variables = (VariableCollection)cloner.Clone(variables);
|
---|
| 103 | clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
|
---|
[2] | 104 | return clone;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[2653] | 107 | #region SubScopes Events
|
---|
| 108 | private void RegisterSubScopesEvents() {
|
---|
| 109 | if (subScopes != null) {
|
---|
[2687] | 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);
|
---|
[2653] | 114 | subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
|
---|
| 115 | }
|
---|
[2] | 116 | }
|
---|
[2653] | 117 | private void DeregisterSubScopesEvents() {
|
---|
| 118 | if (subScopes != null) {
|
---|
[2687] | 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);
|
---|
[2653] | 123 | subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
|
---|
| 124 | }
|
---|
[2] | 125 | }
|
---|
[2687] | 126 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
| 127 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
| 128 | item.Value.Parent = this;
|
---|
[61] | 129 | }
|
---|
[2687] | 130 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
| 131 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
| 132 | item.Value.Parent = null;
|
---|
[61] | 133 | }
|
---|
[2687] | 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;
|
---|
[2] | 139 | }
|
---|
[2687] | 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;
|
---|
[2] | 145 | }
|
---|
[2653] | 146 | private void SubScopes_Changed(object sender, ChangedEventArgs e) {
|
---|
| 147 | OnChanged(e);
|
---|
[2] | 148 | }
|
---|
[2653] | 149 | #endregion
|
---|
| 150 |
|
---|
| 151 | #region Variables Events
|
---|
| 152 | private void Variables_Changed(object sender, ChangedEventArgs e) {
|
---|
| 153 | OnChanged(e);
|
---|
| 154 | }
|
---|
| 155 | #endregion
|
---|
[2] | 156 | }
|
---|
| 157 | }
|
---|