Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Constraints/3.3/VariableComparisonConstraint.cs @ 2520

Last change on this file since 2520 was 2520, checked in by swagner, 15 years ago

Implemented first draft of MainForm support in HeuristicLab.Core/HeuristicLab.Core.Views and all other depending plugins (#770)

File size: 5.3 KB
RevLine 
[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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
[1823]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]29
30namespace HeuristicLab.Constraints {
[1176]31  /// <summary>
32  /// Constraint that compares variables in a <see cref="ConstrainedItemList"/>.
33  /// </summary>
[2]34  public class VariableComparisonConstraint : ConstraintBase {
[1672]35
36    [Storable]
[2]37    private StringData leftVarName;
[1176]38    /// <summary>
39    /// Gets or sets the variable name of the left item to compare.
40    /// </summary>
[2]41    public StringData LeftVarName {
42      get { return leftVarName; }
43      set { leftVarName = value; }
44    }
45
[1672]46    [Storable]
[2]47    private StringData rightVarName;
[1176]48    /// <summary>
49    /// Gets or sets the variable name of the right item to compare.
50    /// </summary>
[2]51    public StringData RightVarName {
52      get { return rightVarName; }
53      set { rightVarName = value; }
54    }
55
[1672]56    [Storable]
[2]57    private IntData comparer;
[1176]58    /// <summary>
59    /// Gets or sets the comparer.
60    /// </summary>
[2]61    public IntData Comparer {
62      get { return comparer; }
63      set { comparer = value; }
64    }
65
[1176]66    /// <inheritdoc select="summary"/>
[2]67    public override string Description {
68      get {
[175]69        return @"Compares variables in a ConstrainedItemList";
[2]70      }
71    }
72
[1176]73    /// <summary>
74    /// Initializes a new instance of <see cref="VariableComparisonConstraint"/>.
75    /// </summary>
[2]76    public VariableComparisonConstraint() {
77      leftVarName = new StringData();
78      rightVarName = new StringData();
79      comparer = new IntData(-1);
80    }
81
[1176]82    /// <summary>
83    /// Checks whether the given element fulfills the current constraint.
84    /// </summary>
85    /// <exception cref="InvalidOperationException">Thrown when the data is no <c>ConstrainedItemList</c>.</exception>
86    /// <exception cref="InvalidCastException">Thrown when the left varible is not of type <c>IComparable</c>.</exception>
87    /// <exception cref="InvalidOperationException">Thrown when the comparer is undefined.</exception>
88    /// <param name="data">The item to check.</param>
89    /// <returns><c>true</c> if the constraint could be fulfilled, <c>false</c> otherwise.</returns>
[2]90    public override bool Check(IItem data) {
91      ConstrainedItemList list = (data as ConstrainedItemList);
92      if (list == null) throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Can only be applied on ConstrainedItemLists");
93      IComparable left = null;
94      IItem right = null;
95      foreach (IItem item in list) {
96        Variable tmp = (item as Variable);
97        if (tmp != null && tmp.Name.Equals(leftVarName.Data)) {
98          left = (tmp.Value as IComparable);
99          if (left == null) throw new InvalidCastException("ERROR in VariableComparisonConstraint: Value of the variable on the left side needs to be of type IComparable");
100        } else if (tmp != null && tmp.Name.Equals(rightVarName.Data)) {
[175]101          right = tmp.Value;
[2]102        }
103      }
104      if (left != null && right != null) {
105        switch (comparer.Data) {
106          case 0:
107            return left.CompareTo(right) < 0;
108          case 1:
109            return left.CompareTo(right) <= 0;
110          case 2:
111            return left.CompareTo(right) == 0;
112          case 3:
113            return left.CompareTo(right) >= 0;
114          case 4:
115            return left.CompareTo(right) > 0;
116          default:
117            throw new InvalidOperationException("ERROR in VariableComparisonConstraint: Comparer undefined");
118        }
119      }
120      return true;
121    }
122
[1176]123    /// <summary>
124    /// Clones the current instance (deep clone).
125    /// </summary>
126    /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
127    /// <see cref="Auxiliary"/>.</remarks>
128    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
129    /// <returns>The cloned object as <see cref="VariableComparisonConstraint"/>.</returns>
[2]130    public override object Clone(IDictionary<Guid, object> clonedObjects) {
131      VariableComparisonConstraint clone = new VariableComparisonConstraint();
132      clonedObjects.Add(Guid, clone);
133      clone.LeftVarName = (StringData)Auxiliary.Clone(LeftVarName, clonedObjects);
134      clone.RightVarName = (StringData)Auxiliary.Clone(RightVarName, clonedObjects);
135      clone.Comparer = (IntData)Auxiliary.Clone(Comparer, clonedObjects);
136      return clone;
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.