1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Core {
|
---|
29 | /// <summary>
|
---|
30 | /// Hierarchical container of variables (and of subscopes).
|
---|
31 | /// </summary>
|
---|
32 | public class Scope : ItemBase, IScope {
|
---|
33 |
|
---|
34 | [Storable]
|
---|
35 | private IScope parent;
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | private string myName;
|
---|
39 | /// <summary>
|
---|
40 | /// Gets the name of the current scope.
|
---|
41 | /// </summary>
|
---|
42 | public string Name {
|
---|
43 | get { return myName; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private IDictionary<string, IVariable> myVariables;
|
---|
47 | /// <inheritdoc/>
|
---|
48 | public ICollection<IVariable> Variables {
|
---|
49 | get { return myVariables.Values; }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable(Name="Variables")]
|
---|
53 | private List<IVariable> VariablePersistence {
|
---|
54 | get { return new List<IVariable>(myVariables.Values); }
|
---|
55 | set {
|
---|
56 | myVariables.Clear();
|
---|
57 | foreach (IVariable var in value) {
|
---|
58 | AddVariable(var);
|
---|
59 | }
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | [Storable]
|
---|
64 | private List<IScope> mySubScopes;
|
---|
65 | /// <summary>
|
---|
66 | /// Gets all subscopes of the current instance.
|
---|
67 | /// <note type="caution"> The subscopes are returned as read-only.</note>
|
---|
68 | /// </summary>
|
---|
69 | public IList<IScope> SubScopes {
|
---|
70 | get { return mySubScopes.AsReadOnly(); }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
|
---|
75 | /// </summary>
|
---|
76 | public Scope() {
|
---|
77 | myName = "Anonymous";
|
---|
78 | myVariables = new Dictionary<string, IVariable>();
|
---|
79 | mySubScopes = new List<IScope>();
|
---|
80 | }
|
---|
81 | /// <summary>
|
---|
82 | /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
|
---|
83 | /// </summary>
|
---|
84 | /// <param name="name">The name of the scope.</param>
|
---|
85 | public Scope(string name)
|
---|
86 | : this() {
|
---|
87 | if (name != null) myName = name;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /// <inheritdoc/>
|
---|
91 | public void SetParent(IScope scope) {
|
---|
92 | parent = scope;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// Creates a new instance of <see cref="ScopeView"/> to represent the current instance visually.
|
---|
97 | /// </summary>
|
---|
98 | /// <returns>The created view as <see cref="ScopeView"/>.</returns>
|
---|
99 | public override IView CreateView() {
|
---|
100 | return new ScopeView(this);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <inheritdoc/>
|
---|
104 | public IVariable GetVariable(string name) {
|
---|
105 | IVariable variable;
|
---|
106 | if (myVariables.TryGetValue(name, out variable))
|
---|
107 | return variable;
|
---|
108 | else
|
---|
109 | return null;
|
---|
110 | }
|
---|
111 | /// <inheritdoc/>
|
---|
112 | public void AddVariable(IVariable variable) {
|
---|
113 | myVariables.Add(variable.Name, variable);
|
---|
114 | variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
115 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
116 | OnVariableAdded(variable);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /// <inheritdoc/>
|
---|
120 | public void RemoveVariable(string name) {
|
---|
121 | IVariable variable;
|
---|
122 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
123 | variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
124 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
125 | myVariables.Remove(name);
|
---|
126 | OnVariableRemoved(variable);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
|
---|
130 | e.Cancel = myVariables.ContainsKey(e.Name);
|
---|
131 | }
|
---|
132 | private void Variable_NameChanged(object sender, EventArgs e) {
|
---|
133 | IVariable variable = (IVariable)sender;
|
---|
134 | string oldName = null;
|
---|
135 | foreach (KeyValuePair<string, IVariable> element in myVariables) {
|
---|
136 | if (element.Value == variable)
|
---|
137 | oldName = element.Key;
|
---|
138 | }
|
---|
139 | myVariables.Remove(oldName);
|
---|
140 | myVariables.Add(variable.Name, variable);
|
---|
141 | }
|
---|
142 | /// <inheritdoc cref="IScope.GetVariableValue<T>(string, bool)"/>
|
---|
143 | public T GetVariableValue<T>(string name, bool recursiveLookup) where T : class, IItem {
|
---|
144 | return GetVariableValue<T>(name, recursiveLookup, true);
|
---|
145 | }
|
---|
146 | /// <inheritdoc cref="IScope.GetVariableValue<T>(string, bool, bool)"/>
|
---|
147 | public T GetVariableValue<T>(string name, bool recursiveLookup, bool throwOnError) where T : class, IItem {
|
---|
148 | return (T)GetVariableValue(name, recursiveLookup, throwOnError);
|
---|
149 | }
|
---|
150 | /// <inheritdoc cref="IScope.GetVariableValue(string, bool)"/>
|
---|
151 | public IItem GetVariableValue(string name, bool recursiveLookup) {
|
---|
152 | return GetVariableValue(name, recursiveLookup, true);
|
---|
153 | }
|
---|
154 | /// <inheritdoc cref="IScope.GetVariableValue(string, bool, bool)"/>
|
---|
155 | public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
|
---|
156 | IVariable variable;
|
---|
157 | name = TranslateName(name);
|
---|
158 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
159 | return variable.Value;
|
---|
160 | } else {
|
---|
161 | if (recursiveLookup && (parent != null))
|
---|
162 | return parent.GetVariableValue(name, recursiveLookup, throwOnError);
|
---|
163 | else {
|
---|
164 | if (throwOnError)
|
---|
165 | throw new ArgumentException("Variable " + name + " not found");
|
---|
166 | else
|
---|
167 | return null;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | /// <inheritdoc/>
|
---|
173 | public void AddSubScope(IScope scope) {
|
---|
174 | scope.SetParent(this);
|
---|
175 | mySubScopes.Add(scope);
|
---|
176 | OnSubScopeAdded(scope, mySubScopes.Count - 1);
|
---|
177 | }
|
---|
178 | /// <inheritdoc/>
|
---|
179 | public void RemoveSubScope(IScope scope) {
|
---|
180 | int index = mySubScopes.IndexOf(scope);
|
---|
181 | if (mySubScopes.Remove(scope)) {
|
---|
182 | scope.SetParent(null);
|
---|
183 | OnSubScopeRemoved(scope, index);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | /// <inheritdoc/>
|
---|
187 | public void ReorderSubScopes(int[] sequence) {
|
---|
188 | IScope[] scopes = mySubScopes.ToArray();
|
---|
189 | mySubScopes.Clear();
|
---|
190 | for (int i = 0; i < scopes.Length; i++)
|
---|
191 | mySubScopes.Add(scopes[sequence[i]]);
|
---|
192 | OnSubScopesReordered();
|
---|
193 | }
|
---|
194 | /// <inheritdoc/>
|
---|
195 | public IScope GetScope(string name) {
|
---|
196 | if (Name == name) return this;
|
---|
197 | else {
|
---|
198 | for (int i = 0; i < mySubScopes.Count; i++) {
|
---|
199 | IScope s = mySubScopes[i].GetScope(name);
|
---|
200 | if (s != null) return s;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | return null;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /// <inheritdoc/>
|
---|
207 | public void Clear() {
|
---|
208 | string[] variableNames = new string[Variables.Count];
|
---|
209 | int i = 0;
|
---|
210 | foreach (IVariable variable in Variables) {
|
---|
211 | variableNames[i] = variable.Name;
|
---|
212 | i++;
|
---|
213 | }
|
---|
214 | for (int j = 0; j < variableNames.Length; j++)
|
---|
215 | RemoveVariable(variableNames[j]);
|
---|
216 |
|
---|
217 | KeyValuePair<string, string>[] aliases = new KeyValuePair<string, string>[myAliases.Count];
|
---|
218 | myAliases.CopyTo(aliases, 0);
|
---|
219 | for (int j = 0; j < aliases.Length; j++)
|
---|
220 | RemoveAlias(aliases[j].Key);
|
---|
221 |
|
---|
222 | while (SubScopes.Count > 0)
|
---|
223 | RemoveSubScope(SubScopes[0]);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /// <inheritdoc/>
|
---|
227 | public override ICloneable Clone(ICloner cloner) {
|
---|
228 | Scope clone = (Scope)base.Clone(cloner);
|
---|
229 | clone.myName = Name;
|
---|
230 |
|
---|
231 | foreach (IVariable variable in myVariables.Values)
|
---|
232 | clone.AddVariable((IVariable)cloner.Clone(variable));
|
---|
233 | for (int i = 0; i < SubScopes.Count; i++)
|
---|
234 | clone.AddSubScope((IScope)cloner.Clone(SubScopes[i]));
|
---|
235 |
|
---|
236 | return clone;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /// <inheritdoc />
|
---|
240 | public event EventHandler<EventArgs<IVariable>> VariableAdded;
|
---|
241 | /// <summary>
|
---|
242 | /// Fires a new <c>VariableAdded</c> event.
|
---|
243 | /// </summary>
|
---|
244 | /// <param name="variable">The variable that has been added.</param>
|
---|
245 | protected virtual void OnVariableAdded(IVariable variable) {
|
---|
246 | if (VariableAdded != null)
|
---|
247 | VariableAdded(this, new EventArgs<IVariable>(variable));
|
---|
248 | }
|
---|
249 | /// <inheritdoc />
|
---|
250 | public event EventHandler<EventArgs<IVariable>> VariableRemoved;
|
---|
251 | /// <summary>
|
---|
252 | /// Fires a new <c>VariableRemoved</c>.
|
---|
253 | /// </summary>
|
---|
254 | /// <param name="variable">The variable that has been deleted.</param>
|
---|
255 | protected virtual void OnVariableRemoved(IVariable variable) {
|
---|
256 | if (VariableRemoved != null)
|
---|
257 | VariableRemoved(this, new EventArgs<IVariable>(variable));
|
---|
258 | }
|
---|
259 | /// <inheritdoc/>
|
---|
260 | public event EventHandler<EventArgs<IScope, int>> SubScopeAdded;
|
---|
261 | /// <summary>
|
---|
262 | /// Fires a new <c>SubScopeAdded</c> event.
|
---|
263 | /// </summary>
|
---|
264 | /// <param name="scope">The sub scope that has been added.</param>
|
---|
265 | /// <param name="index">The index where the scope has been added.</param>
|
---|
266 | protected virtual void OnSubScopeAdded(IScope scope, int index) {
|
---|
267 | if (SubScopeAdded != null)
|
---|
268 | SubScopeAdded(this, new EventArgs<IScope,int>(scope, index));
|
---|
269 | }
|
---|
270 | /// <inheritdoc/>
|
---|
271 | public event EventHandler<EventArgs<IScope, int>> SubScopeRemoved;
|
---|
272 | /// <summary>
|
---|
273 | /// Fires a new <c>SubScopeRemoved</c> event.
|
---|
274 | /// </summary>
|
---|
275 | /// <param name="scope">The sub scope that has been deleted.</param>
|
---|
276 | /// <param name="index">The position of the sub scope.</param>
|
---|
277 | protected virtual void OnSubScopeRemoved(IScope scope, int index) {
|
---|
278 | if (SubScopeRemoved != null)
|
---|
279 | SubScopeRemoved(this, new EventArgs<IScope,int>(scope, index));
|
---|
280 | }
|
---|
281 | /// <inheritdoc />
|
---|
282 | public event EventHandler SubScopesReordered;
|
---|
283 | /// <summary>
|
---|
284 | /// Fires a new <c>SubScopesReordered</c> event
|
---|
285 | /// </summary>
|
---|
286 | protected virtual void OnSubScopesReordered() {
|
---|
287 | if (SubScopesReordered != null)
|
---|
288 | SubScopesReordered(this, new EventArgs());
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|