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.Common;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Core {
|
---|
27 | /// <summary>
|
---|
28 | /// Hierarchical container of variables (and of subscopes).
|
---|
29 | /// </summary>
|
---|
30 | [Item("Scope", "A scope which contains variables and sub-scopes.")]
|
---|
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 != value) {
|
---|
39 | IScope oldParent = parent;
|
---|
40 | parent = null;
|
---|
41 | if (oldParent != null) oldParent.SubScopes.Remove(this);
|
---|
42 | parent = value;
|
---|
43 | if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | [Storable]
|
---|
49 | private VariableCollection variables;
|
---|
50 | public VariableCollection Variables {
|
---|
51 | get { return variables; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | [Storable]
|
---|
55 | private ScopeList subScopes;
|
---|
56 | public ScopeList SubScopes {
|
---|
57 | get { return subScopes; }
|
---|
58 | }
|
---|
59 |
|
---|
60 | /// <summary>
|
---|
61 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
62 | /// </summary>
|
---|
63 | public Scope()
|
---|
64 | : base("Anonymous") {
|
---|
65 | parent = null;
|
---|
66 | variables = new VariableCollection();
|
---|
67 | subScopes = new ScopeList();
|
---|
68 | Initialize();
|
---|
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 | Initialize();
|
---|
80 | }
|
---|
81 | public Scope(string name, string description)
|
---|
82 | : base(name, description) {
|
---|
83 | parent = null;
|
---|
84 | variables = new VariableCollection();
|
---|
85 | subScopes = new ScopeList();
|
---|
86 | Initialize();
|
---|
87 | }
|
---|
88 | [StorableConstructor]
|
---|
89 | private Scope(bool deserializing) : base(deserializing) { }
|
---|
90 |
|
---|
91 | [StorableHook(HookType.AfterDeserialization)]
|
---|
92 | private void Initialize() {
|
---|
93 | RegisterSubScopesEvents();
|
---|
94 | }
|
---|
95 |
|
---|
96 | /// <inheritdoc/>
|
---|
97 | public void Clear() {
|
---|
98 | variables.Clear();
|
---|
99 | subScopes.Clear();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /// <inheritdoc/>
|
---|
103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
104 | Scope clone = new Scope();
|
---|
105 | cloner.RegisterClonedObject(this, clone);
|
---|
106 | clone.name = name;
|
---|
107 | clone.description = description;
|
---|
108 | if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
|
---|
109 | if (subScopes.Count > 0) {
|
---|
110 | clone.subScopes = (ScopeList)cloner.Clone(subScopes);
|
---|
111 | foreach (IScope child in clone.SubScopes)
|
---|
112 | child.Parent = clone;
|
---|
113 | clone.Initialize();
|
---|
114 | }
|
---|
115 | return clone;
|
---|
116 | }
|
---|
117 |
|
---|
118 | #region SubScopes Events
|
---|
119 | private void RegisterSubScopesEvents() {
|
---|
120 | if (subScopes != null) {
|
---|
121 | subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
122 | subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
123 | subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
124 | subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
125 | }
|
---|
126 | }
|
---|
127 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
128 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
129 | item.Value.Parent = this;
|
---|
130 | }
|
---|
131 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
132 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
133 | item.Value.Parent = null;
|
---|
134 | }
|
---|
135 | private void SubScopes_ItemsReplaced(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_CollectionReset(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;
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 | }
|
---|
149 | }
|
---|