#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.Core;
namespace HeuristicLab.Operators {
///
/// Class to inject local variables into the scope.
///
public class VariableInjector : OperatorBase {
private Dictionary variableVariableInfoTable;
private Dictionary variableInfoVariableTable;
///
public override string Description {
get { return @"TODO\r\nOperator description still missing ..."; }
}
///
/// Initializes a new instance of .
///
public VariableInjector()
: base() {
variableVariableInfoTable = new Dictionary();
variableInfoVariableTable = new Dictionary();
}
///
/// Clones the current instance (deep clone).
///
/// Deep clone performed with of helper class
/// .
/// Dictionary of all already cloned objects. (Needed to avoid cycles.)
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
VariableInjector clone = new VariableInjector();
clonedObjects.Add(Guid, clone);
clone.Name = Name;
foreach (IVariable variable in Variables)
clone.AddVariable((IVariable)Auxiliary.Clone(variable, clonedObjects));
return clone;
}
///
/// Adds the specified to the current instance to get injected.
///
/// The variable to add.
/// Calls private method and
/// of base class .
public override void AddVariable(IVariable variable) {
base.AddVariable(variable);
CreateVariableInfo(variable);
}
///
/// Removes a variable with the specified from the current injector.
///
/// Calls private method and
/// of base class .
/// The name of the
public override void RemoveVariable(string name) {
DeleteVariableInfo(name);
base.RemoveVariable(name);
}
///
/// Adds the specified variables to the given (and removes them first,
/// if they already exist in the current scope).
///
/// The scope where to inject the variables.
/// null.
public override IOperation Apply(IScope scope) {
foreach (IVariable variable in Variables) {
if (scope.GetVariable(variable.Name) != null)
scope.RemoveVariable(variable.Name);
scope.AddVariable((IVariable)variable.Clone());
}
return null;
}
///
/// Creates a new instance of to display the current instance.
///
/// The created view as .
public override IView CreateView() {
return new VariableInjectorView(this);
}
private void CreateVariableInfo(IVariable variable) {
IVariableInfo info = new VariableInfo(Guid.NewGuid().ToString(), "Injected variable", variable.Value.GetType(), VariableKind.New);
info.ActualName = variable.Name;
AddVariableInfo(info);
variableVariableInfoTable.Add(variable, info);
variableInfoVariableTable.Add(info, variable);
info.ActualNameChanged += new EventHandler(VariableInfo_ActualNameChanged);
variable.NameChanged += new EventHandler(Variable_NameChanged);
}
private void DeleteVariableInfo(string name) {
IVariable variable = GetVariable(name);
if (variable != null) {
IVariableInfo info = variableVariableInfoTable[variable];
RemoveVariableInfo(info.FormalName);
variableVariableInfoTable.Remove(variable);
variableInfoVariableTable.Remove(info);
info.ActualNameChanged -= new EventHandler(VariableInfo_ActualNameChanged);
variable.NameChanged -= new EventHandler(Variable_NameChanged);
}
}
#region VariableInfo and Variable Events
private void VariableInfo_ActualNameChanged(object sender, EventArgs e) {
IVariableInfo info = (IVariableInfo)sender;
IVariable variable = variableInfoVariableTable[info];
variable.Name = info.ActualName;
}
private void Variable_NameChanged(object sender, EventArgs e) {
IVariable variable = (IVariable)sender;
IVariableInfo info = variableVariableInfoTable[variable];
info.ActualName = variable.Name;
}
#endregion
#region Persistence Methods
///
/// Saves the current instance as in the specified .
/// Variable infos are not persisted!
///
/// Calls of base class .
/// 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);
// variable infos should not be persisted
XmlNode infosNode = node.SelectSingleNode("VariableInfos");
infosNode.RemoveAll();
return node;
}
#endregion
}
}