Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Constraints/3.3/VariableComparisonConstraint.cs @ 4062

Last change on this file since 4062 was 1823, checked in by epitzer, 15 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 5.7 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;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Constraints {
31  /// <summary>
32  /// Constraint that compares variables in a <see cref="ConstrainedItemList"/>.
33  /// </summary>
34  public class VariableComparisonConstraint : ConstraintBase {
35
36    [Storable]
37    private StringData leftVarName;
38    /// <summary>
39    /// Gets or sets the variable name of the left item to compare.
40    /// </summary>
41    public StringData LeftVarName {
42      get { return leftVarName; }
43      set { leftVarName = value; }
44    }
45
46    [Storable]
47    private StringData rightVarName;
48    /// <summary>
49    /// Gets or sets the variable name of the right item to compare.
50    /// </summary>
51    public StringData RightVarName {
52      get { return rightVarName; }
53      set { rightVarName = value; }
54    }
55
56    [Storable]
57    private IntData comparer;
58    /// <summary>
59    /// Gets or sets the comparer.
60    /// </summary>
61    public IntData Comparer {
62      get { return comparer; }
63      set { comparer = value; }
64    }
65
66    /// <inheritdoc select="summary"/>
67    public override string Description {
68      get {
69        return @"Compares variables in a ConstrainedItemList";
70      }
71    }
72
73    /// <summary>
74    /// Initializes a new instance of <see cref="VariableComparisonConstraint"/>.
75    /// </summary>
76    public VariableComparisonConstraint() {
77      leftVarName = new StringData();
78      rightVarName = new StringData();
79      comparer = new IntData(-1);
80    }
81
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>
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)) {
101          right = tmp.Value;
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
123    /// <summary>
124    /// Creates a new instance of <see cref="VariableComparisonConstraintView"/> to represent the current
125    /// instance visually.
126    /// </summary>
127    /// <returns>The created view as <see cref="VariableComparisonConstraintView"/>.</returns>
128    public override IView CreateView() {
129      return new VariableComparisonConstraintView(this);
130    }
131
132    /// <summary>
133    /// Clones the current instance (deep clone).
134    /// </summary>
135    /// <remarks>Deep clone through <see cref="Auxiliary.Clone"/> method of helper class
136    /// <see cref="Auxiliary"/>.</remarks>
137    /// <param name="clonedObjects">Dictionary of all already clone objects. (Needed to avoid cycles.)</param>
138    /// <returns>The cloned object as <see cref="VariableComparisonConstraint"/>.</returns>
139    public override object Clone(IDictionary<Guid, object> clonedObjects) {
140      VariableComparisonConstraint clone = new VariableComparisonConstraint();
141      clonedObjects.Add(Guid, clone);
142      clone.LeftVarName = (StringData)Auxiliary.Clone(LeftVarName, clonedObjects);
143      clone.RightVarName = (StringData)Auxiliary.Clone(RightVarName, clonedObjects);
144      clone.Comparer = (IntData)Auxiliary.Clone(Comparer, clonedObjects);
145      return clone;
146    }
147  }
148}
Note: See TracBrowser for help on using the repository browser.