Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/3.3/OperatorGraph.cs @ 2033

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

Refactoring of the operator architecture (#95)

File size: 5.4 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 IList<IOperator> myOperators;
36    /// <summary>
37    /// Gets all operators of the current instance.
38    /// </summary>
39    public ICollection<IOperator> Operators {
40      get { return myOperators; }
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 List<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<long, object> clonedObjects) {
83      OperatorGraph clone = new OperatorGraph();
84      clonedObjects.Add(clone.Id, 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.Contains(op)) {
96        myOperators.Add(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(IOperator op) {
106      if (Operators.Contains(op)) {
107        foreach (IOperator o in Operators) {
108          int i = 0;
109          while (i < o.SubOperators.Count) {
110            if (o.SubOperators[i] == op)
111              o.RemoveSubOperator(i);
112            else
113              i++;
114          }
115        }
116        if (InitialOperator == op)
117          InitialOperator = null;
118        myOperators.Remove(op);
119        OnOperatorRemoved(op);
120      }
121    }
122    /// <inheritdoc/>
123    public void Clear() {
124      while (myOperators.Count > 0)
125        RemoveOperator(myOperators[0]);
126    }
127
128    /// <inheritdoc/>
129    public event EventHandler<OperatorEventArgs> OperatorAdded;
130    /// <summary>
131    /// Fires a new <c>OperatorAdded</c> event.
132    /// </summary>
133    /// <param name="op">The operator that has been added.</param>
134    protected virtual void OnOperatorAdded(IOperator op) {
135      if (OperatorAdded != null)
136        OperatorAdded(this, new OperatorEventArgs(op));
137    }
138    /// <inheritdoc/>
139    public event EventHandler<OperatorEventArgs> OperatorRemoved;
140    /// <summary>
141    /// Fires a new <c>OperatorRemoved</c> event.
142    /// </summary>
143    /// <param name="op">The operator that has been removed.</param>
144    protected virtual void OnOperatorRemoved(IOperator op) {
145      if (OperatorRemoved != null)
146        OperatorRemoved(this, new OperatorEventArgs(op));
147    }
148    /// <inheritdoc/>
149    public event EventHandler InitialOperatorChanged;
150    /// <summary>
151    /// Fires a new <c>InitialOperatorChanged</c> event.
152    /// </summary>
153    protected virtual void OnInitialOperatorChanged() {
154      if (InitialOperatorChanged != null)
155        InitialOperatorChanged(this, new EventArgs());
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.