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 | [StorableClass]
|
---|
32 | public sealed class Scope : NamedItem, IScope {
|
---|
33 | [Storable]
|
---|
34 | private IScope parent;
|
---|
35 | public IScope Parent {
|
---|
36 | get { return parent; }
|
---|
37 | set {
|
---|
38 | if (parent != null) parent.SubScopes.Remove(this);
|
---|
39 | parent = value;
|
---|
40 | if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 | [Storable]
|
---|
45 | private VariableCollection variables;
|
---|
46 | public VariableCollection Variables {
|
---|
47 | get { return variables; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private ScopeList subScopes;
|
---|
51 | [Storable]
|
---|
52 | public ScopeList SubScopes {
|
---|
53 | get { return subScopes; }
|
---|
54 | private set {
|
---|
55 | DeregisterSubScopesEvents();
|
---|
56 | subScopes = value;
|
---|
57 | RegisterSubScopesEvents();
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
63 | /// </summary>
|
---|
64 | public Scope()
|
---|
65 | : base("Anonymous") {
|
---|
66 | parent = null;
|
---|
67 | variables = new VariableCollection();
|
---|
68 | SubScopes = new ScopeList();
|
---|
69 | }
|
---|
70 | /// <summary>
|
---|
71 | /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
|
---|
72 | /// </summary>
|
---|
73 | /// <param name="name">The name of the scope.</param>
|
---|
74 | public Scope(string name)
|
---|
75 | : base(name) {
|
---|
76 | parent = null;
|
---|
77 | variables = new VariableCollection();
|
---|
78 | SubScopes = new ScopeList();
|
---|
79 | }
|
---|
80 | public Scope(string name, string description)
|
---|
81 | : base(name, description) {
|
---|
82 | parent = null;
|
---|
83 | variables = new VariableCollection();
|
---|
84 | SubScopes = new ScopeList();
|
---|
85 | }
|
---|
86 |
|
---|
87 | /// <inheritdoc/>
|
---|
88 | public void Clear() {
|
---|
89 | variables.Clear();
|
---|
90 | subScopes.Clear();
|
---|
91 | }
|
---|
92 |
|
---|
93 | /// <inheritdoc/>
|
---|
94 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
95 | Scope clone = new Scope();
|
---|
96 | cloner.RegisterClonedObject(this, clone);
|
---|
97 | clone.Name = Name;
|
---|
98 | clone.Description = Description;
|
---|
99 | if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
|
---|
100 | if (subScopes.Count > 0) clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
|
---|
101 | return clone;
|
---|
102 | }
|
---|
103 |
|
---|
104 | #region SubScopes Events
|
---|
105 | private void RegisterSubScopesEvents() {
|
---|
106 | if (subScopes != null) {
|
---|
107 | subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
108 | subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
109 | subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
110 | subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | private void DeregisterSubScopesEvents() {
|
---|
114 | if (subScopes != null) {
|
---|
115 | subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
116 | subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
117 | subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
118 | subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
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 | #endregion
|
---|
142 | }
|
---|
143 | }
|
---|