#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;
namespace HeuristicLab.Core {
///
/// Represents a graph of operators.
///
public class OperatorGraph : ItemBase, IOperatorGraph {
private IDictionary myOperators;
///
/// Gets all operators of the current instance.
///
public ICollection Operators {
get { return myOperators.Values; }
}
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();
}
///
/// Creates a new instance of to represent the current instance
/// visually.
///
/// The created view as .
public override IView CreateView() {
return new OperatorGraphView(this);
}
///
/// 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 object Clone(IDictionary clonedObjects) {
OperatorGraph clone = new OperatorGraph();
clonedObjects.Add(Guid, clone);
foreach (IOperator op in Operators)
clone.AddOperator((IOperator)Auxiliary.Clone(op, clonedObjects));
if (InitialOperator != null)
clone.myInitialOperator = (IOperator)Auxiliary.Clone(InitialOperator, clonedObjects);
return clone;
}
///
/// Calls .
public void AddOperator(IOperator op) {
if (!myOperators.ContainsKey(op.Guid)) {
myOperators.Add(op.Guid, op);
OnOperatorAdded(op);
foreach (IOperator subOperator in op.SubOperators)
AddOperator(subOperator);
}
}
///
/// Calls .
public void RemoveOperator(Guid guid) {
IOperator op = GetOperator(guid);
if (op != null) {
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.Guid);
OnOperatorRemoved(op);
}
}
///
public IOperator GetOperator(Guid guid) {
IOperator op;
if (myOperators.TryGetValue(guid, out op))
return op;
else
return null;
}
///
public void Clear() {
Guid[] guids = new Guid[Operators.Count];
int i = 0;
foreach (IOperator op in Operators) {
guids[i] = op.Guid;
i++;
}
for (int j = 0; j < guids.Length; j++)
RemoveOperator(guids[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 OperatorEventArgs(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 OperatorEventArgs(op));
}
///
public event EventHandler InitialOperatorChanged;
///
/// Fires a new InitialOperatorChanged event.
///
protected virtual void OnInitialOperatorChanged() {
if (InitialOperatorChanged != null)
InitialOperatorChanged(this, new EventArgs());
}
#region Persistence Methods
///
/// Saves the current instance as in the specified .
///
/// Calls of base class .
/// To save the operators of the current instance a child node is created with the tag name
/// Operators. Beyond this child node all operators are saved as child nodes themselves.
/// The initial operator is saved as child node with the tag name InitialOperator.
/// The (tag)name of the .
/// The where to save the data.
/// The dictionary of all already persisted objects.
/// (Needed to avoid cycles.)
/// The saved .
public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary persistedObjects) {
XmlNode node = base.GetXmlNode(name, document, persistedObjects);
XmlNode ops = document.CreateNode(XmlNodeType.Element, "Operators", null);
foreach (IOperator op in myOperators.Values)
ops.AppendChild(PersistenceManager.Persist(op, document, persistedObjects));
node.AppendChild(ops);
if (InitialOperator != null)
node.AppendChild(PersistenceManager.Persist("InitialOperator", InitialOperator, document, persistedObjects));
return node;
}
///
/// Loads the persisted operator graph from the specified .
///
/// See to get more information about how the graph must be saved.
/// Calls of base class .
/// The where the operator graph is saved.
/// The dictionary of all already restored objects.
/// (Needed to avoid cycles.)
public override void Populate(XmlNode node, IDictionary restoredObjects) {
base.Populate(node, restoredObjects);
XmlNode ops = node.SelectSingleNode("Operators");
for (int i = 0; i < ops.ChildNodes.Count; i++) {
XmlNode opNode = ops.ChildNodes[i];
IOperator op = (IOperator)PersistenceManager.Restore(opNode, restoredObjects);
myOperators.Add(op.Guid, op);
}
XmlNode initial = node.SelectSingleNode("InitialOperator");
if (initial != null)
myInitialOperator = (IOperator)PersistenceManager.Restore(initial, restoredObjects);
}
#endregion
}
}