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 | [Creatable("Test")]
|
---|
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 != null) parent.SubScopes.Remove(this);
|
---|
38 | parent = value;
|
---|
39 | if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | private VariableCollection variables;
|
---|
44 | [Storable]
|
---|
45 | public VariableCollection Variables {
|
---|
46 | get { return variables; }
|
---|
47 | private set {
|
---|
48 | if (variables != null) variables.Changed -= new ChangedEventHandler(Variables_Changed);
|
---|
49 | variables = value;
|
---|
50 | if (variables != null) variables.Changed += new ChangedEventHandler(Variables_Changed);
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | private ScopeList subScopes;
|
---|
55 | [Storable]
|
---|
56 | public ScopeList SubScopes {
|
---|
57 | get { return subScopes; }
|
---|
58 | private set {
|
---|
59 | DeregisterSubScopesEvents();
|
---|
60 | subScopes = value;
|
---|
61 | RegisterSubScopesEvents();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | /// <summary>
|
---|
66 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
67 | /// </summary>
|
---|
68 | public Scope()
|
---|
69 | : base("Anonymous") {
|
---|
70 | parent = null;
|
---|
71 | Variables = new VariableCollection();
|
---|
72 | SubScopes = new ScopeList();
|
---|
73 | }
|
---|
74 | /// <summary>
|
---|
75 | /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="name">The name of the scope.</param>
|
---|
78 | public Scope(string name)
|
---|
79 | : base(name) {
|
---|
80 | parent = null;
|
---|
81 | Variables = new VariableCollection();
|
---|
82 | SubScopes = new ScopeList();
|
---|
83 | }
|
---|
84 |
|
---|
85 | /// <inheritdoc/>
|
---|
86 | public void Clear() {
|
---|
87 | variables.Clear();
|
---|
88 | subScopes.Clear();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /// <inheritdoc/>
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
93 | Scope clone = new Scope();
|
---|
94 | cloner.RegisterClonedObject(this, clone);
|
---|
95 | clone.Name = Name;
|
---|
96 | clone.Description = Description;
|
---|
97 | if (variables.Count > 0) clone.Variables = (VariableCollection)cloner.Clone(variables);
|
---|
98 | if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
|
---|
99 | return clone;
|
---|
100 | }
|
---|
101 |
|
---|
102 | #region SubScopes Events
|
---|
103 | private void RegisterSubScopesEvents() {
|
---|
104 | if (subScopes != null) {
|
---|
105 | subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
106 | subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
107 | subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
108 | subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
109 | subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | private void DeregisterSubScopesEvents() {
|
---|
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 | subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
122 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
123 | item.Value.Parent = this;
|
---|
124 | }
|
---|
125 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
126 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
127 | item.Value.Parent = null;
|
---|
128 | }
|
---|
129 | private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
130 | foreach (IndexedItem<IScope> oldItem in e.OldItems)
|
---|
131 | oldItem.Value.Parent = null;
|
---|
132 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
133 | item.Value.Parent = this;
|
---|
134 | }
|
---|
135 | private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
136 | foreach (IndexedItem<IScope> oldItem in e.OldItems)
|
---|
137 | oldItem.Value.Parent = null;
|
---|
138 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
139 | item.Value.Parent = this;
|
---|
140 | }
|
---|
141 | private void SubScopes_Changed(object sender, ChangedEventArgs e) {
|
---|
142 | OnChanged(e);
|
---|
143 | }
|
---|
144 | #endregion
|
---|
145 |
|
---|
146 | #region Variables Events
|
---|
147 | private void Variables_Changed(object sender, ChangedEventArgs e) {
|
---|
148 | OnChanged(e);
|
---|
149 | }
|
---|
150 | #endregion
|
---|
151 | }
|
---|
152 | }
|
---|