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