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 System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using System.Linq;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Collections;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Core {
|
---|
32 | /// <summary>
|
---|
33 | /// Represents a graph of operators.
|
---|
34 | /// </summary>
|
---|
35 | [Item("OperatorGraph", "Represents a graph of operators.")]
|
---|
36 | [Creatable("Algorithms")]
|
---|
37 | public class OperatorGraph : Item {
|
---|
38 | private OperatorSet operators;
|
---|
39 | /// <summary>
|
---|
40 | /// Gets all operators of the current instance.
|
---|
41 | /// </summary>
|
---|
42 | [Storable]
|
---|
43 | public OperatorSet Operators {
|
---|
44 | get { return operators; }
|
---|
45 | private set {
|
---|
46 | DeregisterOperatorsEvents();
|
---|
47 | operators = value;
|
---|
48 | RegisterOperatorsEvents();
|
---|
49 | }
|
---|
50 | }
|
---|
51 |
|
---|
52 | [Storable]
|
---|
53 | private IOperator initialOperator;
|
---|
54 | /// <summary>
|
---|
55 | /// Gets or sets the initial operator (the starting one).
|
---|
56 | /// </summary>
|
---|
57 | /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
|
---|
58 | public IOperator InitialOperator {
|
---|
59 | get { return initialOperator; }
|
---|
60 | set {
|
---|
61 | if (initialOperator != value) {
|
---|
62 | if (value != null) Operators.Add(value);
|
---|
63 | initialOperator = value;
|
---|
64 | OnInitialOperatorChanged();
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable]
|
---|
70 | private IDeepCloneable visualizationInfo;
|
---|
71 | /// <summary>
|
---|
72 | /// Gets or sets the visualizationInfo.
|
---|
73 | /// </summary>
|
---|
74 | /// /// <remarks>The VisualizationInfo can only be set once and fires afterwards and InvalidOperationException</remarks>
|
---|
75 | public IDeepCloneable VisualizationInfo {
|
---|
76 | get { return visualizationInfo; }
|
---|
77 | set {
|
---|
78 | if (visualizationInfo != null)
|
---|
79 | throw new InvalidOperationException("The value of the property VisualizationInfo is already set and cannot be set again.");
|
---|
80 | visualizationInfo = value;
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Initializes a new instance of <see cref="OperatorGraph"/>.
|
---|
86 | /// </summary>
|
---|
87 | public OperatorGraph() {
|
---|
88 | Operators = new OperatorSet();
|
---|
89 | initialOperator = null;
|
---|
90 | visualizationInfo = null;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /// <summary>
|
---|
94 | /// Clones the current instance (deep clone).
|
---|
95 | /// </summary>
|
---|
96 | /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
|
---|
97 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
98 | /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
|
---|
99 | /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
|
---|
100 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
101 | OperatorGraph clone = new OperatorGraph();
|
---|
102 | cloner.RegisterClonedObject(this, clone);
|
---|
103 | clone.Operators = (OperatorSet)cloner.Clone(operators);
|
---|
104 | clone.initialOperator = (IOperator)cloner.Clone(initialOperator);
|
---|
105 | return clone;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /// <inheritdoc/>
|
---|
109 | public event EventHandler InitialOperatorChanged;
|
---|
110 | /// <summary>
|
---|
111 | /// Fires a new <c>InitialOperatorChanged</c> event.
|
---|
112 | /// </summary>
|
---|
113 | protected virtual void OnInitialOperatorChanged() {
|
---|
114 | if (InitialOperatorChanged != null)
|
---|
115 | InitialOperatorChanged(this, EventArgs.Empty);
|
---|
116 | OnChanged();
|
---|
117 | }
|
---|
118 |
|
---|
119 | #region Operators Events
|
---|
120 | private void AddOperator(IOperator op) {
|
---|
121 | RegisterOperatorEvents(op);
|
---|
122 | foreach (IParameter param in op.Parameters)
|
---|
123 | AddParameter(param);
|
---|
124 | }
|
---|
125 | private void RemoveOperator(IOperator op) {
|
---|
126 | foreach (IParameter param in op.Parameters)
|
---|
127 | RemoveParameter(param);
|
---|
128 | DeregisterOperatorEvents(op);
|
---|
129 |
|
---|
130 | // remove edges to removed operator
|
---|
131 | var opParams = from o in Operators
|
---|
132 | from p in o.Parameters
|
---|
133 | where p is IValueParameter<IOperator>
|
---|
134 | where (((IValueParameter<IOperator>)p).Value != null) && (((IValueParameter<IOperator>)p).Value == op)
|
---|
135 | select (IValueParameter<IOperator>)p;
|
---|
136 | foreach (IValueParameter<IOperator> opParam in opParams)
|
---|
137 | opParam.Value = null;
|
---|
138 | }
|
---|
139 | private void AddParameter(IParameter param) {
|
---|
140 | IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
|
---|
141 | if (opParam != null) {
|
---|
142 | RegisterOperatorParameterEvents(opParam);
|
---|
143 | if (opParam.Value != null) Operators.Add(opParam.Value);
|
---|
144 | }
|
---|
145 | }
|
---|
146 | private void RemoveParameter(IParameter param) {
|
---|
147 | IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
|
---|
148 | if (opParam != null) {
|
---|
149 | DeregisterOperatorParameterEvents(opParam);
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void RegisterOperatorsEvents() {
|
---|
154 | if (operators != null) {
|
---|
155 | operators.Changed += new ChangedEventHandler(Operators_Changed);
|
---|
156 | operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
|
---|
157 | operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
|
---|
158 | operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
|
---|
159 | foreach (IOperator op in operators) {
|
---|
160 | RegisterOperatorEvents(op);
|
---|
161 | foreach (IParameter param in op.Parameters) {
|
---|
162 | IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
|
---|
163 | if (opParam != null) RegisterOperatorParameterEvents(opParam);
|
---|
164 | }
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | private void DeregisterOperatorsEvents() {
|
---|
169 | if (operators != null) {
|
---|
170 | foreach (IOperator op in operators) {
|
---|
171 | foreach (IParameter param in op.Parameters) {
|
---|
172 | IValueParameter<IOperator> opParam = param as IValueParameter<IOperator>;
|
---|
173 | if (opParam != null) DeregisterOperatorParameterEvents(opParam);
|
---|
174 | }
|
---|
175 | DeregisterOperatorEvents(op);
|
---|
176 | }
|
---|
177 | operators.Changed -= new ChangedEventHandler(Operators_Changed);
|
---|
178 | operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded);
|
---|
179 | operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved);
|
---|
180 | operators.CollectionReset -= new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | private void RegisterOperatorEvents(IOperator op) {
|
---|
184 | op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
|
---|
185 | op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
|
---|
186 | op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
|
---|
187 | op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
|
---|
188 | }
|
---|
189 | private void DeregisterOperatorEvents(IOperator op) {
|
---|
190 | op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded);
|
---|
191 | op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved);
|
---|
192 | op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced);
|
---|
193 | op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset);
|
---|
194 | }
|
---|
195 | private void RegisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
|
---|
196 | opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
|
---|
197 | }
|
---|
198 | private void DeregisterOperatorParameterEvents(IValueParameter<IOperator> opParam) {
|
---|
199 | opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
|
---|
200 | }
|
---|
201 |
|
---|
202 | private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
203 | foreach (IOperator op in e.Items)
|
---|
204 | AddOperator(op);
|
---|
205 | }
|
---|
206 | private void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
207 | foreach (IOperator op in e.Items)
|
---|
208 | RemoveOperator(op);
|
---|
209 | if (!Operators.Contains(InitialOperator)) InitialOperator = null;
|
---|
210 | }
|
---|
211 | private void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IOperator> e) {
|
---|
212 | foreach (IOperator op in e.OldItems)
|
---|
213 | RemoveOperator(op);
|
---|
214 | foreach (IOperator op in e.Items)
|
---|
215 | AddOperator(op);
|
---|
216 | if (!Operators.Contains(InitialOperator)) InitialOperator = null;
|
---|
217 | }
|
---|
218 | private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
219 | foreach (IParameter param in e.Items)
|
---|
220 | AddParameter(param);
|
---|
221 | }
|
---|
222 | private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
223 | foreach (IParameter param in e.Items)
|
---|
224 | RemoveParameter(param);
|
---|
225 | }
|
---|
226 | private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
227 | foreach (IParameter param in e.OldItems)
|
---|
228 | RemoveParameter(param);
|
---|
229 | foreach (IParameter param in e.Items)
|
---|
230 | AddParameter(param);
|
---|
231 | }
|
---|
232 | private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) {
|
---|
233 | foreach (IParameter param in e.OldItems)
|
---|
234 | RemoveParameter(param);
|
---|
235 | foreach (IParameter param in e.Items)
|
---|
236 | AddParameter(param);
|
---|
237 | }
|
---|
238 | private void opParam_ValueChanged(object sender, EventArgs e) {
|
---|
239 | IValueParameter<IOperator> opParam = (IValueParameter<IOperator>)sender;
|
---|
240 | if (opParam.Value != null) Operators.Add(opParam.Value);
|
---|
241 | }
|
---|
242 | private void Operators_Changed(object sender, ChangedEventArgs e) {
|
---|
243 | OnChanged(e);
|
---|
244 | }
|
---|
245 | #endregion
|
---|
246 | }
|
---|
247 | }
|
---|