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 |
|
---|
27 | namespace HeuristicLab.Core {
|
---|
28 | public class Scope : ItemBase, IScope {
|
---|
29 | private IScope parent;
|
---|
30 |
|
---|
31 | private string myName;
|
---|
32 | public string Name {
|
---|
33 | get { return myName; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private IDictionary<string, IVariable> myVariables;
|
---|
37 | public ICollection<IVariable> Variables {
|
---|
38 | get { return myVariables.Values; }
|
---|
39 | }
|
---|
40 | private List<IScope> mySubScopes;
|
---|
41 | public IList<IScope> SubScopes {
|
---|
42 | get { return mySubScopes.AsReadOnly(); }
|
---|
43 | }
|
---|
44 |
|
---|
45 | public Scope() {
|
---|
46 | myName = "Anonymous";
|
---|
47 | myVariables = new Dictionary<string, IVariable>();
|
---|
48 | mySubScopes = new List<IScope>();
|
---|
49 | }
|
---|
50 | public Scope(string name)
|
---|
51 | : this() {
|
---|
52 | if (name != null) myName = name;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public void SetParent(IScope scope) {
|
---|
56 | parent = scope;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IView CreateView() {
|
---|
60 | return new ScopeView(this);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IVariable GetVariable(string name) {
|
---|
64 | IVariable variable;
|
---|
65 | if (myVariables.TryGetValue(name, out variable))
|
---|
66 | return variable;
|
---|
67 | else
|
---|
68 | return null;
|
---|
69 | }
|
---|
70 | public void AddVariable(IVariable variable) {
|
---|
71 | myVariables.Add(variable.Name, variable);
|
---|
72 | variable.NameChanging += new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
73 | variable.NameChanged += new EventHandler(Variable_NameChanged);
|
---|
74 | OnVariableAdded(variable);
|
---|
75 | }
|
---|
76 |
|
---|
77 | public void RemoveVariable(string name) {
|
---|
78 | IVariable variable;
|
---|
79 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
80 | variable.NameChanging -= new EventHandler<NameChangingEventArgs>(Variable_NameChanging);
|
---|
81 | variable.NameChanged -= new EventHandler(Variable_NameChanged);
|
---|
82 | myVariables.Remove(name);
|
---|
83 | OnVariableRemoved(variable);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | private void Variable_NameChanging(object sender, NameChangingEventArgs e) {
|
---|
87 | e.Cancel = myVariables.ContainsKey(e.Name);
|
---|
88 | }
|
---|
89 | private void Variable_NameChanged(object sender, EventArgs e) {
|
---|
90 | IVariable variable = (IVariable)sender;
|
---|
91 | string oldName = null;
|
---|
92 | foreach (KeyValuePair<string, IVariable> element in myVariables) {
|
---|
93 | if (element.Value == variable)
|
---|
94 | oldName = element.Key;
|
---|
95 | }
|
---|
96 | myVariables.Remove(oldName);
|
---|
97 | myVariables.Add(variable.Name, variable);
|
---|
98 | }
|
---|
99 | public T GetVariableValue<T>(string name, bool recursiveLookup) where T : class, IItem {
|
---|
100 | return GetVariableValue<T>(name, recursiveLookup, true);
|
---|
101 | }
|
---|
102 | public T GetVariableValue<T>(string name, bool recursiveLookup, bool throwOnError) where T : class, IItem {
|
---|
103 | return (T)GetVariableValue(name, recursiveLookup, throwOnError);
|
---|
104 | }
|
---|
105 | public IItem GetVariableValue(string name, bool recursiveLookup) {
|
---|
106 | return GetVariableValue(name, recursiveLookup, true);
|
---|
107 | }
|
---|
108 | public IItem GetVariableValue(string name, bool recursiveLookup, bool throwOnError) {
|
---|
109 | IVariable variable;
|
---|
110 | if (myVariables.TryGetValue(name, out variable)) {
|
---|
111 | return variable.Value;
|
---|
112 | } else {
|
---|
113 | if (recursiveLookup && (parent != null))
|
---|
114 | return parent.GetVariableValue(name, recursiveLookup, throwOnError);
|
---|
115 | else {
|
---|
116 | if (throwOnError)
|
---|
117 | throw new ArgumentException("Variable " + name + " not found");
|
---|
118 | else
|
---|
119 | return null;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | public void AddSubScope(IScope scope) {
|
---|
125 | scope.SetParent(this);
|
---|
126 | mySubScopes.Add(scope);
|
---|
127 | OnSubScopeAdded(scope, mySubScopes.Count - 1);
|
---|
128 | }
|
---|
129 | public void RemoveSubScope(IScope scope) {
|
---|
130 | int index = mySubScopes.IndexOf(scope);
|
---|
131 | if (mySubScopes.Remove(scope)) {
|
---|
132 | scope.SetParent(null);
|
---|
133 | OnSubScopeRemoved(scope, index);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | public void ReorderSubScopes(int[] sequence) {
|
---|
137 | IScope[] scopes = mySubScopes.ToArray();
|
---|
138 | mySubScopes.Clear();
|
---|
139 | for (int i = 0; i < scopes.Length; i++)
|
---|
140 | mySubScopes.Add(scopes[sequence[i]]);
|
---|
141 | OnSubScopesReordered();
|
---|
142 | }
|
---|
143 | public IScope GetScope(Guid guid) {
|
---|
144 | if (Guid == guid) return this;
|
---|
145 | else {
|
---|
146 | for (int i = 0; i < mySubScopes.Count; i++) {
|
---|
147 | IScope s = GetScope(guid);
|
---|
148 | if (s != null) return s;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | return null;
|
---|
152 | }
|
---|
153 | public IScope GetScope(string name) {
|
---|
154 | if (Name == name) return this;
|
---|
155 | else {
|
---|
156 | for (int i = 0; i < mySubScopes.Count; i++) {
|
---|
157 | IScope s = GetScope(name);
|
---|
158 | if (s != null) return s;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | public void Clear() {
|
---|
165 | string[] variableNames = new string[Variables.Count];
|
---|
166 | int i = 0;
|
---|
167 | foreach (IVariable variable in Variables) {
|
---|
168 | variableNames[i] = variable.Name;
|
---|
169 | i++;
|
---|
170 | }
|
---|
171 | for (int j = 0; j < variableNames.Length; j++)
|
---|
172 | RemoveVariable(variableNames[j]);
|
---|
173 |
|
---|
174 | while (SubScopes.Count > 0)
|
---|
175 | RemoveSubScope(SubScopes[0]);
|
---|
176 | }
|
---|
177 |
|
---|
178 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
179 | Scope clone = (Scope)base.Clone(clonedObjects);
|
---|
180 | clone.myName = Name;
|
---|
181 |
|
---|
182 | foreach (IVariable variable in myVariables.Values)
|
---|
183 | clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
|
---|
184 | for (int i = 0; i < SubScopes.Count; i++)
|
---|
185 | clone.AddSubScope((IScope)Auxiliary.Clone(SubScopes[i], clonedObjects));
|
---|
186 |
|
---|
187 | return clone;
|
---|
188 | }
|
---|
189 |
|
---|
190 | public event EventHandler<VariableEventArgs> VariableAdded;
|
---|
191 | protected virtual void OnVariableAdded(IVariable variable) {
|
---|
192 | if (VariableAdded != null)
|
---|
193 | VariableAdded(this, new VariableEventArgs(variable));
|
---|
194 | }
|
---|
195 | public event EventHandler<VariableEventArgs> VariableRemoved;
|
---|
196 | protected virtual void OnVariableRemoved(IVariable variable) {
|
---|
197 | if (VariableRemoved != null)
|
---|
198 | VariableRemoved(this, new VariableEventArgs(variable));
|
---|
199 | }
|
---|
200 | public event EventHandler<ScopeIndexEventArgs> SubScopeAdded;
|
---|
201 | protected virtual void OnSubScopeAdded(IScope scope, int index) {
|
---|
202 | if (SubScopeAdded != null)
|
---|
203 | SubScopeAdded(this, new ScopeIndexEventArgs(scope, index));
|
---|
204 | }
|
---|
205 | public event EventHandler<ScopeIndexEventArgs> SubScopeRemoved;
|
---|
206 | protected virtual void OnSubScopeRemoved(IScope scope, int index) {
|
---|
207 | if (SubScopeRemoved != null)
|
---|
208 | SubScopeRemoved(this, new ScopeIndexEventArgs(scope, index));
|
---|
209 | }
|
---|
210 | public event EventHandler SubScopesReordered;
|
---|
211 | protected virtual void OnSubScopesReordered() {
|
---|
212 | if (SubScopesReordered != null)
|
---|
213 | SubScopesReordered(this, new EventArgs());
|
---|
214 | }
|
---|
215 |
|
---|
216 | #region Persistence Methods
|
---|
217 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
|
---|
218 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
219 | XmlAttribute nameAttribute = document.CreateAttribute("Name");
|
---|
220 | nameAttribute.Value = Name.ToString();
|
---|
221 | node.Attributes.Append(nameAttribute);
|
---|
222 |
|
---|
223 | XmlNode variables = document.CreateNode(XmlNodeType.Element, "Variables", null);
|
---|
224 | foreach (IVariable variable in Variables)
|
---|
225 | variables.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
|
---|
226 | node.AppendChild(variables);
|
---|
227 |
|
---|
228 | XmlNode subScopes = document.CreateNode(XmlNodeType.Element, "SubScopes", null);
|
---|
229 | for (int i = 0; i < SubScopes.Count; i++)
|
---|
230 | subScopes.AppendChild(PersistenceManager.Persist(SubScopes[i], document, persistedObjects));
|
---|
231 | node.AppendChild(subScopes);
|
---|
232 |
|
---|
233 | return node;
|
---|
234 | }
|
---|
235 | public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
|
---|
236 | base.Populate(node, restoredObjects);
|
---|
237 | myName = node.Attributes["Name"].Value;
|
---|
238 |
|
---|
239 | XmlNode variables = node.SelectSingleNode("Variables");
|
---|
240 | foreach (XmlNode variableNode in variables.ChildNodes) {
|
---|
241 | IVariable variable = (IVariable)PersistenceManager.Restore(variableNode, restoredObjects);
|
---|
242 | AddVariable(variable);
|
---|
243 | }
|
---|
244 |
|
---|
245 | XmlNode subScopes = node.SelectSingleNode("SubScopes");
|
---|
246 | for (int i = 0; i < subScopes.ChildNodes.Count; i++) {
|
---|
247 | IScope scope = (IScope)PersistenceManager.Restore(subScopes.ChildNodes[i], restoredObjects);
|
---|
248 | AddSubScope(scope);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | #endregion
|
---|
252 | }
|
---|
253 | }
|
---|