Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2298 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 5.8 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;
27
28namespace HeuristicLab.Core {
29  /// <summary>
30  /// Represents a graph of operators.
31  /// </summary>
32  public class OperatorGraph : ItemBase, IOperatorGraph {
33
34    [Storable]
35    private IDictionary<Guid, IOperator> myOperators;
36    /// <summary>
37    /// Gets all operators of the current instance.
38    /// </summary>
39    public ICollection<IOperator> Operators {
40      get { return myOperators.Values; }
41    }
42
43    [Storable]
44    private IOperator myInitialOperator;
45    /// <summary>
46    /// Gets or sets the initial operator (the starting one).
47    /// </summary>
48    /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
49    public IOperator InitialOperator {
50      get { return myInitialOperator; }
51      set {
52        if (myInitialOperator != value) {
53          myInitialOperator = value;
54          OnInitialOperatorChanged();
55        }
56      }
57    }
58
59    /// <summary>
60    /// Initializes a new instance of <see cref="OperatorGraph"/>.
61    /// </summary>
62    public OperatorGraph() {
63      myOperators = new Dictionary<Guid, IOperator>();
64    }
65
66    /// <summary>
67    /// Creates a new instance of <see cref="OperatorGraphView"/> to represent the current instance
68    /// visually.
69    /// </summary>
70    /// <returns>The created view as <see cref="OperatorGraphView"/>.</returns>
71    public override IView CreateView() {
72      return new OperatorGraphView(this);
73    }
74
75    /// <summary>
76    /// Clones the current instance (deep clone).
77    /// </summary>
78    /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
79    /// <see cref="Auxiliary"/>.</remarks>
80    /// <param name="clonedObjects">Dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
81    /// <returns>The cloned object as <see cref="OperatorGraph"/>.</returns>
82    public override object Clone(IDictionary<Guid, object> clonedObjects) {
83      OperatorGraph clone = new OperatorGraph();
84      clonedObjects.Add(Guid, clone);
85      foreach (IOperator op in Operators)
86        clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
87      if (InitialOperator != null)
88        clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
89      return clone;
90    }
91
92    /// <inheritdoc/>
93    /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>
94    public void AddOperator(IOperator op) {
95      if (!myOperators.ContainsKey(op.Guid)) {
96        myOperators.Add(op.Guid, op);
97        OnOperatorAdded(op);
98
99        foreach (IOperator subOperator in op.SubOperators)
100          AddOperator(subOperator);
101      }
102    }
103    /// <inheritdoc/>
104    /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>
105    public void RemoveOperator(Guid guid) {
106      IOperator op = GetOperator(guid);
107      if (op != null) {
108        foreach (IOperator o in Operators) {
109          int i = 0;
110          while (i < o.SubOperators.Count) {
111            if (o.SubOperators[i] == op)
112              o.RemoveSubOperator(i);
113            else
114              i++;
115          }
116        }
117        if (InitialOperator == op)
118          InitialOperator = null;
119        myOperators.Remove(op.Guid);
120        OnOperatorRemoved(op);
121      }
122    }
123    /// <inheritdoc/>
124    public IOperator GetOperator(Guid guid) {
125      IOperator op;
126      if (myOperators.TryGetValue(guid, out op))
127        return op;
128      else
129        return null;
130    }
131    /// <inheritdoc/>
132    public void Clear() {
133      Guid[] guids = new Guid[Operators.Count];
134      int i = 0;
135      foreach (IOperator op in Operators) {
136        guids[i] = op.Guid;
137        i++;
138      }
139      for (int j = 0; j < guids.Length; j++)
140        RemoveOperator(guids[j]);
141    }
142
143    /// <inheritdoc/>
144    public event EventHandler<OperatorEventArgs> OperatorAdded;
145    /// <summary>
146    /// Fires a new <c>OperatorAdded</c> event.
147    /// </summary>
148    /// <param name="op">The operator that has been added.</param>
149    protected virtual void OnOperatorAdded(IOperator op) {
150      if (OperatorAdded != null)
151        OperatorAdded(this, new OperatorEventArgs(op));
152    }
153    /// <inheritdoc/>
154    public event EventHandler<OperatorEventArgs> OperatorRemoved;
155    /// <summary>
156    /// Fires a new <c>OperatorRemoved</c> event.
157    /// </summary>
158    /// <param name="op">The operator that has been removed.</param>
159    protected virtual void OnOperatorRemoved(IOperator op) {
160      if (OperatorRemoved != null)
161        OperatorRemoved(this, new OperatorEventArgs(op));
162    }
163    /// <inheritdoc/>
164    public event EventHandler InitialOperatorChanged;
165    /// <summary>
166    /// Fires a new <c>InitialOperatorChanged</c> event.
167    /// </summary>
168    protected virtual void OnInitialOperatorChanged() {
169      if (InitialOperatorChanged != null)
170        InitialOperatorChanged(this, new EventArgs());
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.