#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;
using HeuristicLab.Data;
namespace HeuristicLab.Constraints {
///
/// Constraint that compares variables in a .
///
public class VariableComparisonConstraint : ConstraintBase {
private StringData leftVarName;
///
/// Gets or sets the variable name of the left item to compare.
///
public StringData LeftVarName {
get { return leftVarName; }
set { leftVarName = value; }
}
private StringData rightVarName;
///
/// Gets or sets the variable name of the right item to compare.
///
public StringData RightVarName {
get { return rightVarName; }
set { rightVarName = value; }
}
private IntData comparer;
///
/// Gets or sets the comparer.
///
public IntData Comparer {
get { return comparer; }
set { comparer = value; }
}
///
public override string Description {
get {
return @"Compares variables in a ConstrainedItemList";
}
}
///
/// Initializes a new instance of .
///
public VariableComparisonConstraint() {
leftVarName = new StringData();
rightVarName = new StringData();
comparer = new IntData(-1);
}
///
/// Checks whether the given element fulfills the current constraint.
///
/// Thrown when the data is no ConstrainedItemList.
/// Thrown when the left varible is not of type IComparable.
/// Thrown when the comparer is undefined.
/// The item to check.
/// true if the constraint could be fulfilled, false otherwise.
public override bool Check(IItem data) {
ConstrainedItemList list = (data as ConstrainedItemList);
if (list == null) throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Can only be applied on ConstrainedItemLists");
IComparable left = null;
IItem right = null;
foreach (IItem item in list) {
Variable tmp = (item as Variable);
if (tmp != null && tmp.Name.Equals(leftVarName.Data)) {
left = (tmp.Value as IComparable);
if (left == null) throw new InvalidCastException("ERROR in VariableComparisonConstraint: Value of the variable on the left side needs to be of type IComparable");
} else if (tmp != null && tmp.Name.Equals(rightVarName.Data)) {
right = tmp.Value;
}
}
if (left != null && right != null) {
switch (comparer.Data) {
case 0:
return left.CompareTo(right) < 0;
case 1:
return left.CompareTo(right) <= 0;
case 2:
return left.CompareTo(right) == 0;
case 3:
return left.CompareTo(right) >= 0;
case 4:
return left.CompareTo(right) > 0;
default:
throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Comparer undefined");
}
}
return true;
}
///
/// Creates a new instance of to represent the current
/// instance visually.
///
/// The created view as .
public override IView CreateView() {
return new VariableComparisonConstraintView(this);
}
#region clone & persistence
///
/// Clones the current instance (deep clone).
///
/// Deep clone through method of helper class
/// .
/// Dictionary of all already clone objects. (Needed to avoid cycles.)
/// The cloned object as .
public override object Clone(IDictionary clonedObjects) {
VariableComparisonConstraint clone = new VariableComparisonConstraint();
clonedObjects.Add(Guid, clone);
clone.LeftVarName = (StringData)Auxiliary.Clone(LeftVarName, clonedObjects);
clone.RightVarName = (StringData)Auxiliary.Clone(RightVarName, clonedObjects);
clone.Comparer = (IntData)Auxiliary.Clone(Comparer, clonedObjects);
return clone;
}
///
/// Saves the current instance as in the specified .
///
/// The variable names and the comparer are saved as child nodes with tag names
/// LeftVarName, RightVarName and Comparer.
/// The (tag)name of the .
/// The where the data is saved.
/// 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 leftNode = PersistenceManager.Persist("LeftVarName", LeftVarName, document, persistedObjects);
node.AppendChild(leftNode);
XmlNode rightNode = PersistenceManager.Persist("RightVarName", RightVarName, document, persistedObjects);
node.AppendChild(rightNode);
XmlNode comparerNode = PersistenceManager.Persist("Comparer", Comparer, document, persistedObjects);
node.AppendChild(comparerNode);
return node;
}
///
/// Loads the persisted constraint from the specified .
///
/// The constraint must be saved in a specific way, see for
/// more information.
/// The where the instance 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);
leftVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("LeftVarName"), restoredObjects);
rightVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("RightVarName"), restoredObjects);
comparer = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Comparer"), restoredObjects);
}
#endregion
}
}