Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Operators/3.2/ComparatorBase.cs @ 2311

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

Refactoring of the operator architecture (#95)

File size: 3.5 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 HeuristicLab.Core;
26using HeuristicLab.Data;
27
28namespace HeuristicLab.Operators {
29  /// <summary>
30  /// Base class for operators which perform comparisons between two items.
31  /// </summary>
32  public abstract class ComparatorBase : OperatorBase {
33    /// <summary>
34    /// Initializes a new instance of <see cref="ComparatorBase"/> with three
35    /// variable infos (<c>LeftSide</c>, <c>RightSide</c> and <c>Result</c>).
36    /// </summary>
37    protected ComparatorBase() {
38      AddParameter(new Parameter("LeftSide", "Variable on the left side of the comparison", typeof(IItem), ParameterType.In));
39      AddParameter(new Parameter("RightSide", "Variable on the right side of the comparison", typeof(IItem), ParameterType.In));
40      AddParameter(new Parameter("Result", "Result of the comparison", typeof(BoolData), ParameterType.Out));
41    }
42
43    /// <summary>
44    /// Compares two items with each other and injects the <c>Result</c> variable - if it is no local one - into
45    /// the specified <paramref name="scope"/>.
46    /// </summary>
47    /// <remarks>Calls <see cref="Compare"/>.</remarks>
48    /// <exception cref="InvalidOperationException">Thrown when left side of the comparison does not
49    /// implement <see cref="IComparable"/>.</exception>
50    /// <param name="scope">The scope where to apply the compare operation.</param>
51    /// <returns><c>null</c>.</returns>
52    public override IOperation Apply(IEnvironment env, IScope scope) {
53      string resultName = env.TranslateName("Result");
54      BoolData result = scope.GetVariableValue<BoolData>(resultName, false, false);
55      if (result == null) {
56        result = new BoolData(true);
57        scope.AddVariable(new Variable(resultName, result));
58      }
59      IItem leftSide = scope.GetVariableValue<IItem>(env.TranslateName("LeftSide"), true);
60      if (!(leftSide is IComparable)) throw new InvalidOperationException("Comparator: Left side needs to be of type IComparable");
61      IComparable left = (IComparable)leftSide;
62      IItem rightSide = scope.GetVariableValue<IItem>(env.TranslateName("RightSide"), true);
63      result.Data = Compare(left, rightSide);
64      return null;
65    }
66
67    /// <summary>
68    /// Compares two variables with each other.
69    /// </summary>
70    /// <param name="left">The variable on the left side of the comparison.</param>
71    /// <param name="right">The variable on the right side of the comparison.</param>
72    /// <returns><c>true</c> if the comparison query was successful, <c>false</c> otherwise.</returns>
73    protected abstract bool Compare(IComparable left, IItem right);
74  }
75}
Note: See TracBrowser for help on using the repository browser.