#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Xml; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Common; namespace HeuristicLab.Core { /// /// Represents a graph of operators. /// public class OperatorGraph : ItemBase, IOperatorGraph { [Storable] private IDictionary myOperators; /// /// Gets all operators of the current instance. /// public ICollection Operators { get { return myOperators.Values; } } [Storable] private IOperator myInitialOperator; /// /// Gets or sets the initial operator (the starting one). /// /// Calls in the setter. public IOperator InitialOperator { get { return myInitialOperator; } set { if (myInitialOperator != value) { myInitialOperator = value; OnInitialOperatorChanged(); } } } /// /// Initializes a new instance of . /// public OperatorGraph() { myOperators = new Dictionary(); } /// /// Clones the current instance (deep clone). /// /// Deep clone through method of helper class /// . /// Dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned object as . public override IItem Clone(ICloner cloner) { OperatorGraph clone = new OperatorGraph(); cloner.RegisterClonedObject(this, clone); foreach (IOperator op in Operators) clone.AddOperator((IOperator)cloner.Clone(op)); if (InitialOperator != null) clone.myInitialOperator = (IOperator)cloner.Clone(InitialOperator); return clone; } /// /// Calls . public void AddOperator(IOperator op) { if (!myOperators.ContainsKey(op)) { myOperators.Add(op, op); OnOperatorAdded(op); foreach (IOperator subOperator in op.SubOperators) AddOperator(subOperator); } } /// /// Calls . public void RemoveOperator(IOperator op) { if (myOperators.ContainsKey(op)) { foreach (IOperator o in Operators) { int i = 0; while (i < o.SubOperators.Count) { if (o.SubOperators[i] == op) o.RemoveSubOperator(i); else i++; } } if (InitialOperator == op) InitialOperator = null; myOperators.Remove(op); OnOperatorRemoved(op); } } /// public void Clear() { IOperator[] ops = new IOperator[Operators.Count]; int i = 0; foreach (IOperator op in Operators) { ops[i] = op; i++; } for (int j = 0; j < ops.Length; j++) RemoveOperator(ops[j]); } /// public event EventHandler> OperatorAdded; /// /// Fires a new OperatorAdded event. /// /// The operator that has been added. protected virtual void OnOperatorAdded(IOperator op) { if (OperatorAdded != null) OperatorAdded(this, new EventArgs(op)); } /// public event EventHandler> OperatorRemoved; /// /// Fires a new OperatorRemoved event. /// /// The operator that has been removed. protected virtual void OnOperatorRemoved(IOperator op) { if (OperatorRemoved != null) OperatorRemoved(this, new EventArgs(op)); } /// public event EventHandler InitialOperatorChanged; /// /// Fires a new InitialOperatorChanged event. /// protected virtual void OnInitialOperatorChanged() { if (InitialOperatorChanged != null) InitialOperatorChanged(this, new EventArgs()); } } }