[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;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Constraints {
|
---|
[1176] | 30 | /// <summary>
|
---|
| 31 | /// Constraint that compares variables in a <see cref="ConstrainedItemList"/>.
|
---|
| 32 | /// </summary>
|
---|
[2] | 33 | public class VariableComparisonConstraint : ConstraintBase {
|
---|
| 34 | private StringData leftVarName;
|
---|
[1176] | 35 | /// <summary>
|
---|
| 36 | /// Gets or sets the variable name of the left item to compare.
|
---|
| 37 | /// </summary>
|
---|
[2] | 38 | public StringData LeftVarName {
|
---|
| 39 | get { return leftVarName; }
|
---|
| 40 | set { leftVarName = value; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | private StringData rightVarName;
|
---|
[1176] | 44 | /// <summary>
|
---|
| 45 | /// Gets or sets the variable name of the right item to compare.
|
---|
| 46 | /// </summary>
|
---|
[2] | 47 | public StringData RightVarName {
|
---|
| 48 | get { return rightVarName; }
|
---|
| 49 | set { rightVarName = value; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | private IntData comparer;
|
---|
[1176] | 53 | /// <summary>
|
---|
| 54 | /// Gets or sets the comparer.
|
---|
| 55 | /// </summary>
|
---|
[2] | 56 | public IntData Comparer {
|
---|
| 57 | get { return comparer; }
|
---|
| 58 | set { comparer = value; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[1176] | 61 | /// <inheritdoc select="summary"/>
|
---|
[2] | 62 | public override string Description {
|
---|
| 63 | get {
|
---|
[175] | 64 | return @"Compares variables in a ConstrainedItemList";
|
---|
[2] | 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[1176] | 68 | /// <summary>
|
---|
| 69 | /// Initializes a new instance of <see cref="VariableComparisonConstraint"/>.
|
---|
| 70 | /// </summary>
|
---|
[2] | 71 | public VariableComparisonConstraint() {
|
---|
| 72 | leftVarName = new StringData();
|
---|
| 73 | rightVarName = new StringData();
|
---|
| 74 | comparer = new IntData(-1);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
[1176] | 77 | /// <summary>
|
---|
| 78 | /// Checks whether the given element fulfills the current constraint.
|
---|
| 79 | /// </summary>
|
---|
| 80 | /// <exception cref="InvalidOperationException">Thrown when the data is no <c>ConstrainedItemList</c>.</exception>
|
---|
| 81 | /// <exception cref="InvalidCastException">Thrown when the left varible is not of type <c>IComparable</c>.</exception>
|
---|
| 82 | /// <exception cref="InvalidOperationException">Thrown when the comparer is undefined.</exception>
|
---|
| 83 | /// <param name="data">The item to check.</param>
|
---|
| 84 | /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
|
---|
[2] | 85 | public override bool Check(IItem data) {
|
---|
| 86 | ConstrainedItemList list = (data as ConstrainedItemList);
|
---|
| 87 | if (list == null) throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Can only be applied on ConstrainedItemLists");
|
---|
| 88 | IComparable left = null;
|
---|
| 89 | IItem right = null;
|
---|
| 90 | foreach (IItem item in list) {
|
---|
| 91 | Variable tmp = (item as Variable);
|
---|
| 92 | if (tmp != null && tmp.Name.Equals(leftVarName.Data)) {
|
---|
| 93 | left = (tmp.Value as IComparable);
|
---|
| 94 | if (left == null) throw new InvalidCastException("ERROR in VariableComparisonConstraint: Value of the variable on the left side needs to be of type IComparable");
|
---|
| 95 | } else if (tmp != null && tmp.Name.Equals(rightVarName.Data)) {
|
---|
[175] | 96 | right = tmp.Value;
|
---|
[2] | 97 | }
|
---|
| 98 | }
|
---|
| 99 | if (left != null && right != null) {
|
---|
| 100 | switch (comparer.Data) {
|
---|
| 101 | case 0:
|
---|
| 102 | return left.CompareTo(right) < 0;
|
---|
| 103 | case 1:
|
---|
| 104 | return left.CompareTo(right) <= 0;
|
---|
| 105 | case 2:
|
---|
| 106 | return left.CompareTo(right) == 0;
|
---|
| 107 | case 3:
|
---|
| 108 | return left.CompareTo(right) >= 0;
|
---|
| 109 | case 4:
|
---|
| 110 | return left.CompareTo(right) > 0;
|
---|
| 111 | default:
|
---|
| 112 | throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Comparer undefined");
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | return true;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[1176] | 118 | /// <summary>
|
---|
| 119 | /// Creates a new instance of <see cref="VariableComparisonConstraintView"/> to represent the current
|
---|
| 120 | /// instance visually.
|
---|
| 121 | /// </summary>
|
---|
| 122 | /// <returns>The created view as <see cref="VariableComparisonConstraintView"/>.</returns>
|
---|
[2] | 123 | public override IView CreateView() {
|
---|
| 124 | return new VariableComparisonConstraintView(this);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | #region clone & persistence
|
---|
[1176] | 128 | /// <summary>
|
---|
| 129 | /// Clones the current instance (deep clone).
|
---|
| 130 | /// </summary>
|
---|
| 131 | /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
|
---|
| 132 | /// <see cref="Auxiliary"/>.</remarks>
|
---|
| 133 | /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
|
---|
| 134 | /// <returns>The cloned object as <see cref="VariableComparisonConstraint"/>.</returns>
|
---|
[2] | 135 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
| 136 | VariableComparisonConstraint clone = new VariableComparisonConstraint();
|
---|
| 137 | clonedObjects.Add(Guid, clone);
|
---|
| 138 | clone.LeftVarName = (StringData)Auxiliary.Clone(LeftVarName, clonedObjects);
|
---|
| 139 | clone.RightVarName = (StringData)Auxiliary.Clone(RightVarName, clonedObjects);
|
---|
| 140 | clone.Comparer = (IntData)Auxiliary.Clone(Comparer, clonedObjects);
|
---|
| 141 | return clone;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[1176] | 144 | /// <summary>
|
---|
| 145 | /// Saves the current instance as <see cref="XmlNode"/> in the specified <paramref name="document"/>.
|
---|
| 146 | /// </summary>
|
---|
| 147 | /// <remarks>The variable names and the comparer are saved as child nodes with tag names
|
---|
| 148 | /// <c>LeftVarName</c>, <c>RightVarName</c> and <c>Comparer</c>.</remarks>
|
---|
| 149 | /// <param name="name">The (tag)name of the <see cref="XmlNode"/>.</param>
|
---|
| 150 | /// <param name="document">The <see cref="XmlDocument"/> where the data is saved.</param>
|
---|
| 151 | /// <param name="persistedObjects">The dictionary of all already persisted objects.
|
---|
| 152 | /// (Needed to avoid cycles.)</param>
|
---|
| 153 | /// <returns>The saved <see cref="XmlNode"/>.</returns>
|
---|
[2] | 154 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
| 155 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
| 156 | XmlNode leftNode = PersistenceManager.Persist("LeftVarName", LeftVarName, document, persistedObjects);
|
---|
| 157 | node.AppendChild(leftNode);
|
---|
| 158 | XmlNode rightNode = PersistenceManager.Persist("RightVarName", RightVarName, document, persistedObjects);
|
---|
| 159 | node.AppendChild(rightNode);
|
---|
| 160 | XmlNode comparerNode = PersistenceManager.Persist("Comparer", Comparer, document, persistedObjects);
|
---|
| 161 | node.AppendChild(comparerNode);
|
---|
| 162 | return node;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
[1176] | 165 | /// <summary>
|
---|
| 166 | /// Loads the persisted constraint from the specified <paramref name="node"/>.
|
---|
| 167 | /// </summary>
|
---|
| 168 | /// <remarks>The constraint must be saved in a specific way, see <see cref="GetXmlNode"/> for
|
---|
| 169 | /// more information.</remarks>
|
---|
| 170 | /// <param name="node">The <see cref="XmlNode"/> where the instance is saved.</param>
|
---|
| 171 | /// <param name="restoredObjects">The dictionary of all already restored objects.
|
---|
| 172 | /// (Needed to avoid cycles.)</param>
|
---|
[2] | 173 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
| 174 | base.Populate(node, restoredObjects);
|
---|
| 175 | leftVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("LeftVarName"), restoredObjects);
|
---|
| 176 | rightVarName = (StringData)PersistenceManager.Restore(node.SelectSingleNode("RightVarName"), restoredObjects);
|
---|
| 177 | comparer = (IntData)PersistenceManager.Restore(node.SelectSingleNode("Comparer"), restoredObjects);
|
---|
| 178 | }
|
---|
| 179 | #endregion
|
---|
| 180 | }
|
---|
| 181 | }
|
---|