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 | using HeuristicLab.Collections;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Core {
|
---|
30 | /// <summary>
|
---|
31 | /// Hierarchical container of variables (and of subscopes).
|
---|
32 | /// </summary>
|
---|
33 | [Item("Scope", "A scope which contains variables and sub-scopes.")]
|
---|
34 | [Creatable("Test")]
|
---|
35 | public class Scope : NamedItem {
|
---|
36 | [Storable]
|
---|
37 | private Scope parent;
|
---|
38 |
|
---|
39 | private VariableCollection variables;
|
---|
40 | [Storable]
|
---|
41 | public VariableCollection Variables {
|
---|
42 | get { return variables; }
|
---|
43 | private set {
|
---|
44 | if (variables != null) variables.Changed -= new ChangedEventHandler(Variables_Changed);
|
---|
45 | variables = value;
|
---|
46 | if (variables != null) variables.Changed += new ChangedEventHandler(Variables_Changed);
|
---|
47 | }
|
---|
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 |
|
---|
81 | public Variable Lookup(string name, bool recursive) {
|
---|
82 | return Lookup(name, recursive, true);
|
---|
83 | }
|
---|
84 | public Variable Lookup(string name, bool recursive, bool throwOnError) {
|
---|
85 | Variable variable;
|
---|
86 |
|
---|
87 | if (this.variables.TryGetValue(name, out variable)) {
|
---|
88 | return variable;
|
---|
89 | } else if (recursive) {
|
---|
90 | Scope scope = this.parent;
|
---|
91 | while (scope != null) {
|
---|
92 | if (scope.variables.TryGetValue(name, out variable))
|
---|
93 | return variable;
|
---|
94 | scope = scope.parent;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | if (throwOnError)
|
---|
98 | throw new ArgumentException("Variable " + name + " not found");
|
---|
99 | else
|
---|
100 | return null;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <inheritdoc/>
|
---|
104 | public void Clear() {
|
---|
105 | variables.Clear();
|
---|
106 | subScopes.Clear();
|
---|
107 | }
|
---|
108 |
|
---|
109 | /// <inheritdoc/>
|
---|
110 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
111 | Scope clone = (Scope)base.Clone(cloner);
|
---|
112 | clone.parent = (Scope)cloner.Clone(parent);
|
---|
113 | clone.Variables = (VariableCollection)cloner.Clone(variables);
|
---|
114 | clone.SubScopes = (ScopeList)cloner.Clone(subScopes);
|
---|
115 | return clone;
|
---|
116 | }
|
---|
117 |
|
---|
118 | #region SubScopes Events
|
---|
119 | private void RegisterSubScopesEvents() {
|
---|
120 | if (subScopes != null) {
|
---|
121 | subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsAdded);
|
---|
122 | subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsRemoved);
|
---|
123 | subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsReplaced);
|
---|
124 | subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_CollectionReset);
|
---|
125 | subScopes.Changed += new ChangedEventHandler(SubScopes_Changed);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | private void DeregisterSubScopesEvents() {
|
---|
129 | if (subScopes != null) {
|
---|
130 | subScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsAdded);
|
---|
131 | subScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsRemoved);
|
---|
132 | subScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_ItemsReplaced);
|
---|
133 | subScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<Scope>>(SubScopes_CollectionReset);
|
---|
134 | subScopes.Changed -= new ChangedEventHandler(SubScopes_Changed);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
|
---|
138 | foreach (IndexedItem<Scope> item in e.Items)
|
---|
139 | item.Value.parent = this;
|
---|
140 | }
|
---|
141 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
|
---|
142 | foreach (IndexedItem<Scope> item in e.Items)
|
---|
143 | item.Value.parent = null;
|
---|
144 | }
|
---|
145 | private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
|
---|
146 | foreach (IndexedItem<Scope> oldItem in e.OldItems)
|
---|
147 | oldItem.Value.parent = null;
|
---|
148 | foreach (IndexedItem<Scope> item in e.Items)
|
---|
149 | item.Value.parent = this;
|
---|
150 | }
|
---|
151 | private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<Scope>> e) {
|
---|
152 | foreach (IndexedItem<Scope> oldItem in e.OldItems)
|
---|
153 | oldItem.Value.parent = null;
|
---|
154 | foreach (IndexedItem<Scope> item in e.Items)
|
---|
155 | item.Value.parent = this;
|
---|
156 | }
|
---|
157 | private void SubScopes_Changed(object sender, ChangedEventArgs e) {
|
---|
158 | OnChanged(e);
|
---|
159 | }
|
---|
160 | #endregion
|
---|
161 |
|
---|
162 | #region Variables Events
|
---|
163 | private void Variables_Changed(object sender, ChangedEventArgs e) {
|
---|
164 | OnChanged(e);
|
---|
165 | }
|
---|
166 | #endregion
|
---|
167 | }
|
---|
168 | }
|
---|