Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Constraints/3.2/VariableComparisonConstraint.cs @ 6004

Last change on this file since 6004 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 7.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28
29namespace HeuristicLab.Constraints {
30  /// <summary>
31  /// Constraint that compares variables in a <see cref="ConstrainedItemList"/>.
32  /// </summary>
33  public class VariableComparisonConstraint : ConstraintBase {
34    private StringData leftVarName;
35    /// <summary>
36    /// Gets or sets the variable name of the left item to compare.
37    /// </summary>
38    public StringData LeftVarName {
39      get { return leftVarName; }
40      set { leftVarName = value; }
41    }
42
43    private StringData rightVarName;
44    /// <summary>
45    /// Gets or sets the variable name of the right item to compare.
46    /// </summary>
47    public StringData RightVarName {
48      get { return rightVarName; }
49      set { rightVarName = value; }
50    }
51
52    private IntData comparer;
53    /// <summary>
54    /// Gets or sets the comparer.
55    /// </summary>
56    public IntData Comparer {
57      get { return comparer; }
58      set { comparer = value; }
59    }
60
61    /// <inheritdoc select="summary"/>
62    public override string Description {
63      get {
64        return @"Compares variables in a ConstrainedItemList";
65      }
66    }
67
68    /// <summary>
69    /// Initializes a new instance of <see cref="VariableComparisonConstraint"/>.
70    /// </summary>
71    public VariableComparisonConstraint() {
72      leftVarName = new StringData();
73      rightVarName = new StringData();
74      comparer = new IntData(-1);
75    }
76
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>
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)) {
96          right = tmp.Value;
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
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>
123    public override IView CreateView() {
124      return new VariableComparisonConstraintView(this);
125    }
126
127    #region clone & persistence
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>
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
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>
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
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>
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}
Note: See TracBrowser for help on using the repository browser.