Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators/3.3/Comparator.cs @ 4501

Last change on this file since 4501 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.4 KB
RevLine 
[2757]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2757]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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Operators {
29  /// <summary>
[2794]30  /// An operator which compares two items.
[2757]31  /// </summary>
32  [Item("Comparator", "An operator which compares two items.")]
[3017]33  [StorableClass]
[2757]34  public sealed class Comparator : SingleSuccessorOperator {
35    public LookupParameter<IItem> LeftSideParameter {
36      get { return (LookupParameter<IItem>)Parameters["LeftSide"]; }
37    }
38    public ValueLookupParameter<IItem> RightSideParameter {
39      get { return (ValueLookupParameter<IItem>)Parameters["RightSide"]; }
40    }
[3048]41    private ValueParameter<Comparison> ComparisonParameter {
42      get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
[2757]43    }
[3048]44    public LookupParameter<BoolValue> ResultParameter {
45      get { return (LookupParameter<BoolValue>)Parameters["Result"]; }
[2757]46    }
[3048]47    public Comparison Comparison {
[2757]48      get { return ComparisonParameter.Value; }
49      set { ComparisonParameter.Value = value; }
50    }
51
52    public Comparator()
53      : base() {
54      Parameters.Add(new LookupParameter<IItem>("LeftSide", "The left side of the comparison."));
55      Parameters.Add(new ValueLookupParameter<IItem>("RightSide", "The right side of the comparison."));
[3048]56      Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(Data.ComparisonType.Equal)));
57      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison."));
[2757]58    }
59
[2834]60    public override IOperation Apply() {
[2757]61      IItem left = LeftSideParameter.ActualValue;
62      IItem right = RightSideParameter.ActualValue;
63      IComparable comparable = left as IComparable;
64      if (comparable == null) throw new InvalidOperationException();
65
66      int i = comparable.CompareTo(right);
67      bool b = false;
68      switch (Comparison.Value) {
[3048]69        case HeuristicLab.Data.ComparisonType.Less:
[2757]70          b = i < 0; break;
[3048]71        case HeuristicLab.Data.ComparisonType.LessOrEqual:
[2757]72          b = i <= 0; break;
[3048]73        case HeuristicLab.Data.ComparisonType.Equal:
[2757]74          b = i == 0; break;
[3048]75        case HeuristicLab.Data.ComparisonType.GreaterOrEqual:
[2831]76          b = i >= 0; break;
[3048]77        case HeuristicLab.Data.ComparisonType.Greater:
[2757]78          b = i > 0; break;
[3048]79        case HeuristicLab.Data.ComparisonType.NotEqual:
[2757]80          b = i != 0; break;
81      }
[3048]82      ResultParameter.ActualValue = new BoolValue(b);
[2757]83      return base.Apply();
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.