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