[2] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Text;
|
---|
| 25 | using System.Xml;
|
---|
[1823] | 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2474] | 27 | using HeuristicLab.Common;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Core {
|
---|
[776] | 30 | /// <summary>
|
---|
| 31 | /// Represents a graph of operators.
|
---|
| 32 | /// </summary>
|
---|
[2] | 33 | public class OperatorGraph : ItemBase, IOperatorGraph {
|
---|
[1667] | 34 |
|
---|
| 35 | [Storable]
|
---|
[2526] | 36 | private IDictionary<IOperator, IOperator> myOperators;
|
---|
[776] | 37 | /// <summary>
|
---|
| 38 | /// Gets all operators of the current instance.
|
---|
| 39 | /// </summary>
|
---|
[2] | 40 | public ICollection<IOperator> Operators {
|
---|
| 41 | get { return myOperators.Values; }
|
---|
| 42 | }
|
---|
[1667] | 43 |
|
---|
| 44 | [Storable]
|
---|
[2] | 45 | private IOperator myInitialOperator;
|
---|
[776] | 46 | /// <summary>
|
---|
| 47 | /// Gets or sets the initial operator (the starting one).
|
---|
| 48 | /// </summary>
|
---|
| 49 | /// <remarks>Calls <see cref="OnInitialOperatorChanged"/> in the setter.</remarks>
|
---|
[2] | 50 | public IOperator InitialOperator {
|
---|
| 51 | get { return myInitialOperator; }
|
---|
| 52 | set {
|
---|
| 53 | if (myInitialOperator != value) {
|
---|
| 54 | myInitialOperator = value;
|
---|
| 55 | OnInitialOperatorChanged();
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[776] | 60 | /// <summary>
|
---|
| 61 | /// Initializes a new instance of <see cref="OperatorGraph"/>.
|
---|
| 62 | /// </summary>
|
---|
[2] | 63 | public OperatorGraph() {
|
---|
[2526] | 64 | myOperators = new Dictionary<IOperator, IOperator>();
|
---|
[2] | 65 | }
|
---|
| 66 |
|
---|
[776] | 67 | /// <summary>
|
---|
| 68 | /// Clones the current instance (deep clone).
|
---|
| 69 | /// </summary>
|
---|
[2526] | 70 | /// <remarks>Deep clone through <see cref="cloner.Clone"/> method of helper class
|
---|
[776] | 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>
|
---|
[2526] | 74 | public override IItem Clone(ICloner cloner) {
|
---|
[2] | 75 | OperatorGraph clone = new OperatorGraph();
|
---|
[2526] | 76 | cloner.RegisterClonedObject(this, clone);
|
---|
[2] | 77 | foreach (IOperator op in Operators)
|
---|
[2526] | 78 | clone.AddOperator((IOperator)cloner.Clone(op));
|
---|
[2] | 79 | if (InitialOperator != null)
|
---|
[2526] | 80 | clone.myInitialOperator = (IOperator)cloner.Clone(InitialOperator);
|
---|
[2] | 81 | return clone;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[776] | 84 | /// <inheritdoc/>
|
---|
| 85 | /// <remarks>Calls <see cref="OnOperatorAdded"/>.</remarks>
|
---|
[2] | 86 | public void AddOperator(IOperator op) {
|
---|
[2526] | 87 | if (!myOperators.ContainsKey(op)) {
|
---|
| 88 | myOperators.Add(op, op);
|
---|
[2] | 89 | OnOperatorAdded(op);
|
---|
| 90 |
|
---|
| 91 | foreach (IOperator subOperator in op.SubOperators)
|
---|
| 92 | AddOperator(subOperator);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[776] | 95 | /// <inheritdoc/>
|
---|
| 96 | /// <remarks>Calls <see cref="OnOperatorRemoved"/>.</remarks>
|
---|
[2526] | 97 | public void RemoveOperator(IOperator op) {
|
---|
| 98 | if (myOperators.ContainsKey(op)) {
|
---|
[2] | 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;
|
---|
[2526] | 110 | myOperators.Remove(op);
|
---|
[2] | 111 | OnOperatorRemoved(op);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
[776] | 114 | /// <inheritdoc/>
|
---|
[2] | 115 | public void Clear() {
|
---|
[2526] | 116 | IOperator[] ops = new IOperator[Operators.Count];
|
---|
[2] | 117 | int i = 0;
|
---|
| 118 | foreach (IOperator op in Operators) {
|
---|
[2526] | 119 | ops[i] = op;
|
---|
[2] | 120 | i++;
|
---|
| 121 | }
|
---|
[2526] | 122 | for (int j = 0; j < ops.Length; j++)
|
---|
| 123 | RemoveOperator(ops[j]);
|
---|
[2] | 124 | }
|
---|
| 125 |
|
---|
[776] | 126 | /// <inheritdoc/>
|
---|
[2474] | 127 | public event EventHandler<EventArgs<IOperator>> OperatorAdded;
|
---|
[776] | 128 | /// <summary>
|
---|
| 129 | /// Fires a new <c>OperatorAdded</c> event.
|
---|
| 130 | /// </summary>
|
---|
| 131 | /// <param name="op">The operator that has been added.</param>
|
---|
[2] | 132 | protected virtual void OnOperatorAdded(IOperator op) {
|
---|
| 133 | if (OperatorAdded != null)
|
---|
[2474] | 134 | OperatorAdded(this, new EventArgs<IOperator>(op));
|
---|
[2] | 135 | }
|
---|
[776] | 136 | /// <inheritdoc/>
|
---|
[2474] | 137 | public event EventHandler<EventArgs<IOperator>> OperatorRemoved;
|
---|
[776] | 138 | /// <summary>
|
---|
| 139 | /// Fires a new <c>OperatorRemoved</c> event.
|
---|
| 140 | /// </summary>
|
---|
| 141 | /// <param name="op">The operator that has been removed.</param>
|
---|
[2] | 142 | protected virtual void OnOperatorRemoved(IOperator op) {
|
---|
| 143 | if (OperatorRemoved != null)
|
---|
[2474] | 144 | OperatorRemoved(this, new EventArgs<IOperator>(op));
|
---|
[2] | 145 | }
|
---|
[776] | 146 | /// <inheritdoc/>
|
---|
[2] | 147 | public event EventHandler InitialOperatorChanged;
|
---|
[776] | 148 | /// <summary>
|
---|
| 149 | /// Fires a new <c>InitialOperatorChanged</c> event.
|
---|
| 150 | /// </summary>
|
---|
[2] | 151 | protected virtual void OnInitialOperatorChanged() {
|
---|
| 152 | if (InitialOperatorChanged != null)
|
---|
| 153 | InitialOperatorChanged(this, new EventArgs());
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | }
|
---|