Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/ComparatorBase.cs @ 929

Last change on this file since 929 was 801, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Operators namespace (#331)

File size: 3.6 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      AddVariableInfo(new VariableInfo("LeftSide", "Variable on the left side of the comparison", typeof(IItem), VariableKind.In));
39      AddVariableInfo(new VariableInfo("RightSide", "Variable on the right side of the comparison", typeof(IItem), VariableKind.In));
40      AddVariableInfo(new VariableInfo("Result", "Result of the comparison", typeof(BoolData), VariableKind.Out | VariableKind.New));
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(IScope scope) {
53      BoolData result = GetVariableValue<BoolData>("Result", scope, false, false);
54      if (result == null) {
55        result = new BoolData(true);
56        IVariableInfo info = GetVariableInfo("Result");
57        if (info.Local)
58          AddVariable(new Variable(info.ActualName, result));
59        else
60          scope.AddVariable(new Variable(scope.TranslateName(info.FormalName), result));
61      }
62      IItem leftSide = GetVariableValue<IItem>("LeftSide", scope, true);
63      if (!(leftSide is IComparable)) throw new InvalidOperationException("Comparator: Left side needs to be of type IComparable");
64      IComparable left = (IComparable)leftSide;
65      IItem rightSide = GetVariableValue<IItem>("RightSide", scope, true);
66      result.Data = Compare(left, rightSide);
67      return null;
68    }
69
70    /// <summary>
71    /// Compares two variables with each other.
72    /// </summary>
73    /// <param name="left">The variable on the left side of the comparison.</param>
74    /// <param name="right">The variable on the right side of the comparison.</param>
75    /// <returns><c>true</c> if the comparison query was successful, <c>false</c> otherwise.</returns>
76    protected abstract bool Compare(IComparable left, IItem right);
77  }
78}
Note: See TracBrowser for help on using the repository browser.