Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.3/OperatorGraph.cs @ 3744

Last change on this file since 3744 was 2474, checked in by swagner, 15 years ago

Implemented generic EventArgs (#796)

File size: 5.9 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<Guid, 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<Guid, IOperator>();
65    }
66
67    /// <summary>
68    /// Creates a new instance of <see cref="OperatorGraphView"/> to represent the current instance
69    /// visually.
70    /// </summary>
71    /// <returns>The created view as <see cref="OperatorGraphView"/>.</returns>
72    public override IView CreateView() {
73      return new OperatorGraphView(this);
74    }
75
76    /// <summary>
77    /// Clones the current instance (deep clone).
78    /// </summary>
79    /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
80    /// <see cref="Auxiliary"/>.</remarks>
81    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
82    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
83    public override object Clone(IDictionary<Guid, object> clonedObjects) {
84      OperatorGraph clone = new OperatorGraph();
85      clonedObjects.Add(Guid, clone);
86      foreach (IOperator op in Operators)
87        clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
88      if (InitialOperator != null)
89        clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
90      return clone;
91    }
92
93    /// <inheritdoc/>
94    /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>
95    public void AddOperator(IOperator op) {
96      if (!myOperators.ContainsKey(op.Guid)) {
97        myOperators.Add(op.Guid, op);
98        OnOperatorAdded(op);
99
100        foreach (IOperator subOperator in op.SubOperators)
101          AddOperator(subOperator);
102      }
103    }
104    /// <inheritdoc/>
105    /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>
106    public void RemoveOperator(Guid guid) {
107      IOperator op = GetOperator(guid);
108      if (op != null) {
109        foreach (IOperator o in Operators) {
110          int i = 0;
111          while (i < o.SubOperators.Count) {
112            if (o.SubOperators[i] == op)
113              o.RemoveSubOperator(i);
114            else
115              i++;
116          }
117        }
118        if (InitialOperator == op)
119          InitialOperator = null;
120        myOperators.Remove(op.Guid);
121        OnOperatorRemoved(op);
122      }
123    }
124    /// <inheritdoc/>
125    public IOperator GetOperator(Guid guid) {
126      IOperator op;
127      if (myOperators.TryGetValue(guid, out op))
128        return op;
129      else
130        return null;
131    }
132    /// <inheritdoc/>
133    public void Clear() {
134      Guid[] guids = new Guid[Operators.Count];
135      int i = 0;
136      foreach (IOperator op in Operators) {
137        guids[i] = op.Guid;
138        i++;
139      }
140      for (int j = 0; j < guids.Length; j++)
141        RemoveOperator(guids[j]);
142    }
143
144    /// <inheritdoc/>
145    public event EventHandler<EventArgs<IOperator>> OperatorAdded;
146    /// <summary>
147    /// Fires a new <c>OperatorAdded</c> event.
148    /// </summary>
149    /// <param name="op">The operator that has been added.</param>
150    protected virtual void OnOperatorAdded(IOperator op) {
151      if (OperatorAdded != null)
152        OperatorAdded(this, new EventArgs<IOperator>(op));
153    }
154    /// <inheritdoc/>
155    public event EventHandler<EventArgs<IOperator>> OperatorRemoved;
156    /// <summary>
157    /// Fires a new <c>OperatorRemoved</c> event.
158    /// </summary>
159    /// <param name="op">The operator that has been removed.</param>
160    protected virtual void OnOperatorRemoved(IOperator op) {
161      if (OperatorRemoved != null)
162        OperatorRemoved(this, new EventArgs<IOperator>(op));
163    }
164    /// <inheritdoc/>
165    public event EventHandler InitialOperatorChanged;
166    /// <summary>
167    /// Fires a new <c>InitialOperatorChanged</c> event.
168    /// </summary>
169    protected virtual void OnInitialOperatorChanged() {
170      if (InitialOperatorChanged != null)
171        InitialOperatorChanged(this, new EventArgs());
172    }
173  }
174}
Note: See TracBrowser for help on using the repository browser.