[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
[3528] | 22 | using System.Drawing;
|
---|
[2830] | 23 | using HeuristicLab.Collections;
|
---|
[3376] | 24 | using HeuristicLab.Common;
|
---|
[1823] | 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 26 |
|
---|
| 27 | namespace HeuristicLab.Core {
|
---|
[776] | 28 | /// <summary>
|
---|
| 29 | /// Hierarchical container of variables (and of subscopes).
|
---|
| 30 | /// </summary>
|
---|
[2653] | 31 | [Item("Scope", "A scope which contains variables and sub-scopes.")]
|
---|
[3017] | 32 | [StorableClass]
|
---|
[2687] | 33 | public sealed class Scope : NamedItem, IScope {
|
---|
[13656] | 34 | public static new Image StaticItemImage
|
---|
| 35 | {
|
---|
| 36 | get { return new Bitmap(25, 25); }
|
---|
[3528] | 37 | }
|
---|
| 38 |
|
---|
[1727] | 39 | [Storable]
|
---|
[2687] | 40 | private IScope parent;
|
---|
[13656] | 41 | public IScope Parent
|
---|
| 42 | {
|
---|
[2687] | 43 | get { return parent; }
|
---|
[13656] | 44 | set
|
---|
| 45 | {
|
---|
[3280] | 46 | if (parent != value) {
|
---|
| 47 | parent = value;
|
---|
| 48 | }
|
---|
[2687] | 49 | }
|
---|
| 50 | }
|
---|
[2] | 51 |
|
---|
[2932] | 52 | [Storable]
|
---|
[2653] | 53 | private VariableCollection variables;
|
---|
[13656] | 54 | public VariableCollection Variables
|
---|
| 55 | {
|
---|
[2653] | 56 | get { return variables; }
|
---|
[1727] | 57 | }
|
---|
[1667] | 58 |
|
---|
[3317] | 59 | [Storable]
|
---|
[2653] | 60 | private ScopeList subScopes;
|
---|
[13656] | 61 | public ScopeList SubScopes
|
---|
| 62 | {
|
---|
[2653] | 63 | get { return subScopes; }
|
---|
[2] | 64 | }
|
---|
| 65 |
|
---|
[4722] | 66 | [StorableConstructor]
|
---|
| 67 | private Scope(bool deserializing) : base(deserializing) { }
|
---|
| 68 | private Scope(Scope original, Cloner cloner)
|
---|
| 69 | : base(original, cloner) {
|
---|
| 70 | if (original.variables.Count > 0) variables = cloner.Clone(original.variables);
|
---|
| 71 | else variables = new VariableCollection();
|
---|
| 72 | if (original.subScopes.Count > 0) {
|
---|
| 73 | subScopes = cloner.Clone(original.subScopes);
|
---|
| 74 | foreach (IScope child in SubScopes)
|
---|
| 75 | child.Parent = this;
|
---|
| 76 | } else subScopes = new ScopeList();
|
---|
| 77 | RegisterSubScopesEvents();
|
---|
| 78 | }
|
---|
[776] | 79 | /// <summary>
|
---|
| 80 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
| 81 | /// </summary>
|
---|
[2653] | 82 | public Scope()
|
---|
| 83 | : base("Anonymous") {
|
---|
| 84 | parent = null;
|
---|
[2932] | 85 | variables = new VariableCollection();
|
---|
[3317] | 86 | subScopes = new ScopeList();
|
---|
[4722] | 87 | RegisterSubScopesEvents();
|
---|
[2] | 88 | }
|
---|
[776] | 89 | /// <summary>
|
---|
| 90 | /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
|
---|
| 91 | /// </summary>
|
---|
| 92 | /// <param name="name">The name of the scope.</param>
|
---|
[2] | 93 | public Scope(string name)
|
---|
[2653] | 94 | : base(name) {
|
---|
| 95 | parent = null;
|
---|
[2932] | 96 | variables = new VariableCollection();
|
---|
[3317] | 97 | subScopes = new ScopeList();
|
---|
[4722] | 98 | RegisterSubScopesEvents();
|
---|
[2] | 99 | }
|
---|
[2931] | 100 | public Scope(string name, string description)
|
---|
| 101 | : base(name, description) {
|
---|
| 102 | parent = null;
|
---|
[2932] | 103 | variables = new VariableCollection();
|
---|
[3317] | 104 | subScopes = new ScopeList();
|
---|
[4722] | 105 | RegisterSubScopesEvents();
|
---|
[2931] | 106 | }
|
---|
[2] | 107 |
|
---|
[3317] | 108 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[4722] | 109 | private void AfterDeserialization() {
|
---|
[3317] | 110 | RegisterSubScopesEvents();
|
---|
| 111 | }
|
---|
| 112 |
|
---|
[776] | 113 | /// <inheritdoc/>
|
---|
[2] | 114 | public void Clear() {
|
---|
[2653] | 115 | variables.Clear();
|
---|
| 116 | subScopes.Clear();
|
---|
[2] | 117 | }
|
---|
| 118 |
|
---|
[776] | 119 | /// <inheritdoc/>
|
---|
[2653] | 120 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 121 | return new Scope(this, cloner);
|
---|
[2] | 122 | }
|
---|
| 123 |
|
---|
[2653] | 124 | #region SubScopes Events
|
---|
| 125 | private void RegisterSubScopesEvents() {
|
---|
| 126 | if (subScopes != null) {
|
---|
[2687] | 127 | subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
| 128 | subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
| 129 | subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
| 130 | subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
[2653] | 131 | }
|
---|
[2] | 132 | }
|
---|
[2687] | 133 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
| 134 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
| 135 | item.Value.Parent = this;
|
---|
[61] | 136 | }
|
---|
[2687] | 137 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
| 138 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
| 139 | item.Value.Parent = null;
|
---|
[61] | 140 | }
|
---|
[2687] | 141 | private void SubScopes_ItemsReplaced(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;
|
---|
[2] | 146 | }
|
---|
[2687] | 147 | private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
| 148 | foreach (IndexedItem<IScope> oldItem in e.OldItems)
|
---|
| 149 | oldItem.Value.Parent = null;
|
---|
| 150 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
| 151 | item.Value.Parent = this;
|
---|
[2] | 152 | }
|
---|
[2653] | 153 | #endregion
|
---|
[2] | 154 | }
|
---|
| 155 | }
|
---|