Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/OperatorGraph.cs @ 2546

Last change on this file since 2546 was 2526, checked in by swagner, 15 years ago

Refactored cloning (#806)

File size: 5.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28
29namespace HeuristicLab.Core {
30  /// <summary>
31  /// Represents a graph of operators.
32  /// </summary>
33  public class OperatorGraph : ItemBase, IOperatorGraph {
34
35    [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; }
42    }
43
44    [Storable]
45    private IOperator myInitialOperator;
46    /// <summary>
47    /// Gets or sets the initial operator (the starting one).
48    /// </summary>
49    /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
50    public IOperator InitialOperator {
51      get { return myInitialOperator; }
52      set {
53        if (myInitialOperator != value) {
54          myInitialOperator = value;
55          OnInitialOperatorChanged();
56        }
57      }
58    }
59
60    /// <summary>
61    /// Initializes a new instance of <see cref="OperatorGraph"/>.
62    /// </summary>
63    public OperatorGraph() {
64      myOperators = new Dictionary<IOperator, IOperator>();
65    }
66
67    /// <summary>
68    /// Clones the current instance (deep clone).
69    /// </summary>
70    /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
71    /// <see cref="Auxiliary"/>.</remarks>
72    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
73    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
74    public override IItem Clone(ICloner cloner) {
75      OperatorGraph clone = new OperatorGraph();
76      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);
81      return clone;
82    }
83
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            else
105              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    /// <inheritdoc/>
147    public event EventHandler InitialOperatorChanged;
148    /// <summary>
149    /// Fires a new <c>InitialOperatorChanged</c> event.
150    /// </summary>
151    protected virtual void OnInitialOperatorChanged() {
152      if (InitialOperatorChanged != null)
153        InitialOperatorChanged(this, new EventArgs());
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.