Changeset 2653 for trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs
- Timestamp:
- 01/20/10 05:00:28 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs
r2526 r2653 24 24 using System.Text; 25 25 using System.Xml; 26 using System.Linq; 26 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 27 28 using HeuristicLab.Common; 29 using HeuristicLab.Collections; 28 30 29 31 namespace HeuristicLab.Core { … … 31 33 /// Represents a graph of operators. 32 34 /// </summary> 33 public class OperatorGraph : ItemBase, IOperatorGraph { 34 35 [Item("OperatorGraph", "Represents a graph of operators.")] 36 [Creatable("Test")] 37 public class OperatorGraph : ItemBase { 38 private OperatorSet operators; 39 /// <summary> 40 /// Gets all operators of the current instance. 41 /// </summary> 35 42 [Storable] 36 private IDictionary<IOperator, IOperator> myOperators; 37 /// <summary> 38 /// Gets all operators of the current instance. 39 /// </summary> 40 public ICollection<IOperator> Operators { 41 get { return myOperators.Values; } 43 public OperatorSet Operators { 44 get { return operators; } 45 private set { 46 DeregisterOperatorsEvents(); 47 operators = value; 48 RegisterOperatorsEvents(); 49 } 42 50 } 43 51 44 52 [Storable] 45 private IOperator myInitialOperator;53 private IOperator initialOperator; 46 54 /// <summary> 47 55 /// Gets or sets the initial operator (the starting one). … … 49 57 /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks> 50 58 public IOperator InitialOperator { 51 get { return myInitialOperator; }59 get { return initialOperator; } 52 60 set { 53 if (myInitialOperator != value) { 54 myInitialOperator = value; 61 if (initialOperator != value) { 62 if (value != null) Operators.Add(value); 63 initialOperator = value; 55 64 OnInitialOperatorChanged(); 56 65 } … … 62 71 /// </summary> 63 72 public OperatorGraph() { 64 myOperators = new Dictionary<IOperator, IOperator>(); 73 Operators = new OperatorSet(); 74 initialOperator = null; 65 75 } 66 76 … … 72 82 /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param> 73 83 /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns> 74 public override I Item Clone(ICloner cloner) {84 public override IDeepCloneable Clone(Cloner cloner) { 75 85 OperatorGraph clone = new OperatorGraph(); 76 86 cloner.RegisterClonedObject(this, clone); 77 foreach (IOperator op in Operators) 78 clone.AddOperator((IOperator)cloner.Clone(op)); 79 if (InitialOperator != null) 80 clone.myInitialOperator = (IOperator)cloner.Clone(InitialOperator); 87 clone.Operators = (OperatorSet)cloner.Clone(operators); 88 clone.initialOperator = (IOperator)cloner.Clone(initialOperator); 81 89 return clone; 82 90 } 83 91 84 /// <inheritdoc/>85 /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>86 public void AddOperator(IOperator op) {87 if (!myOperators.ContainsKey(op)) {88 myOperators.Add(op, op);89 OnOperatorAdded(op);90 91 foreach (IOperator subOperator in op.SubOperators)92 AddOperator(subOperator);93 }94 }95 /// <inheritdoc/>96 /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>97 public void RemoveOperator(IOperator op) {98 if (myOperators.ContainsKey(op)) {99 foreach (IOperator o in Operators) {100 int i = 0;101 while (i < o.SubOperators.Count) {102 if (o.SubOperators[i] == op)103 o.RemoveSubOperator(i);104 else105 i++;106 }107 }108 if (InitialOperator == op)109 InitialOperator = null;110 myOperators.Remove(op);111 OnOperatorRemoved(op);112 }113 }114 /// <inheritdoc/>115 public void Clear() {116 IOperator[] ops = new IOperator[Operators.Count];117 int i = 0;118 foreach (IOperator op in Operators) {119 ops[i] = op;120 i++;121 }122 for (int j = 0; j < ops.Length; j++)123 RemoveOperator(ops[j]);124 }125 126 /// <inheritdoc/>127 public event EventHandler<EventArgs<IOperator>> OperatorAdded;128 /// <summary>129 /// Fires a new <c>OperatorAdded</c> event.130 /// </summary>131 /// <param name="op">The operator that has been added.</param>132 protected virtual void OnOperatorAdded(IOperator op) {133 if (OperatorAdded != null)134 OperatorAdded(this, new EventArgs<IOperator>(op));135 }136 /// <inheritdoc/>137 public event EventHandler<EventArgs<IOperator>> OperatorRemoved;138 /// <summary>139 /// Fires a new <c>OperatorRemoved</c> event.140 /// </summary>141 /// <param name="op">The operator that has been removed.</param>142 protected virtual void OnOperatorRemoved(IOperator op) {143 if (OperatorRemoved != null)144 OperatorRemoved(this, new EventArgs<IOperator>(op));145 }146 92 /// <inheritdoc/> 147 93 public event EventHandler InitialOperatorChanged; … … 152 98 if (InitialOperatorChanged != null) 153 99 InitialOperatorChanged(this, new EventArgs()); 154 } 100 OnChanged(); 101 } 102 103 #region Operators Events 104 private void AddOperator(IOperator op) { 105 RegisterOperatorEvents(op); 106 foreach (IParameter param in op.Parameters) 107 AddParameter(param); 108 } 109 private void RemoveOperator(IOperator op) { 110 foreach (IParameter param in op.Parameters) 111 RemoveParameter(param); 112 DeregisterOperatorEvents(op); 113 114 // remove edges to removed operator 115 var opParams = from o in Operators 116 from p in o.Parameters 117 where p is IOperatorParameter 118 where (((IOperatorParameter)p).Value != null) && (((IOperatorParameter)p).Value == op) 119 select (IOperatorParameter)p; 120 foreach (IOperatorParameter opParam in opParams) 121 opParam.Value = null; 122 } 123 private void AddParameter(IParameter param) { 124 IOperatorParameter opParam = param as IOperatorParameter; 125 if (opParam != null) { 126 RegisterOperatorParameterEvents(opParam); 127 if (opParam.Value != null) Operators.Add(opParam.Value); 128 } 129 } 130 private void RemoveParameter(IParameter param) { 131 IOperatorParameter opParam = param as IOperatorParameter; 132 if (opParam != null) { 133 DeregisterOperatorParameterEvents(opParam); 134 } 135 } 136 137 private void RegisterOperatorsEvents() { 138 if (operators != null) { 139 operators.Changed += new ChangedEventHandler(Operators_Changed); 140 operators.ItemsAdded += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded); 141 operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved); 142 operators.CollectionReset += new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset); 143 } 144 } 145 private void DeregisterOperatorsEvents() { 146 if (operators != null) { 147 operators.Changed -= new ChangedEventHandler(Operators_Changed); 148 operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsAdded); 149 operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IOperator>(Operators_ItemsRemoved); 150 operators.CollectionReset -= new CollectionItemsChangedEventHandler<IOperator>(Operators_CollectionReset); 151 } 152 } 153 private void RegisterOperatorEvents(IOperator op) { 154 op.Parameters.ItemsAdded += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded); 155 op.Parameters.ItemsRemoved += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved); 156 op.Parameters.ItemsReplaced += new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced); 157 op.Parameters.CollectionReset += new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset); 158 } 159 private void DeregisterOperatorEvents(IOperator op) { 160 op.Parameters.ItemsAdded -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsAdded); 161 op.Parameters.ItemsRemoved -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsRemoved); 162 op.Parameters.ItemsReplaced -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_ItemsReplaced); 163 op.Parameters.CollectionReset -= new CollectionItemsChangedEventHandler<IParameter>(Parameters_CollectionReset); 164 } 165 private void RegisterOperatorParameterEvents(IOperatorParameter opParam) { 166 opParam.ValueChanged += new EventHandler(opParam_ValueChanged); 167 } 168 private void DeregisterOperatorParameterEvents(IOperatorParameter opParam) { 169 opParam.ValueChanged -= new EventHandler(opParam_ValueChanged); 170 } 171 172 private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IOperator> e) { 173 foreach (IOperator op in e.Items) 174 AddOperator(op); 175 } 176 private void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IOperator> e) { 177 foreach (IOperator op in e.Items) 178 RemoveOperator(op); 179 if (!Operators.Contains(InitialOperator)) InitialOperator = null; 180 } 181 private void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IOperator> e) { 182 foreach (IOperator op in e.OldItems) 183 RemoveOperator(op); 184 foreach (IOperator op in e.Items) 185 AddOperator(op); 186 if (!Operators.Contains(InitialOperator)) InitialOperator = null; 187 } 188 private void Parameters_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IParameter> e) { 189 foreach (IParameter param in e.Items) 190 AddParameter(param); 191 } 192 private void Parameters_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IParameter> e) { 193 foreach (IParameter param in e.Items) 194 RemoveParameter(param); 195 } 196 private void Parameters_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IParameter> e) { 197 foreach (IParameter param in e.OldItems) 198 RemoveParameter(param); 199 foreach (IParameter param in e.Items) 200 AddParameter(param); 201 } 202 private void Parameters_CollectionReset(object sender, CollectionItemsChangedEventArgs<IParameter> e) { 203 foreach (IParameter param in e.OldItems) 204 RemoveParameter(param); 205 foreach (IParameter param in e.Items) 206 AddParameter(param); 207 } 208 private void opParam_ValueChanged(object sender, EventArgs e) { 209 IOperatorParameter opParam = (IOperatorParameter)sender; 210 if (opParam.Value != null) Operators.Add(opParam.Value); 211 } 212 private void Operators_Changed(object sender, ChangedEventArgs e) { 213 OnChanged(e); 214 } 215 #endregion 155 216 } 156 217 }
Note: See TracChangeset
for help on using the changeset viewer.