Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • worked on parameters and operators
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;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Operators {
31  /// <summary>
32  /// Operator which increments an integer variable.
33  /// </summary>
34  [Item("Comparator", "An operator which compares two items.")]
35  [EmptyStorableClass]
36  [Creatable("Test")]
37  public sealed class Comparator : SingleSuccessorOperator {
38    public LookupParameter<IItem> LeftSideParameter {
39      get { return (LookupParameter<IItem>)Parameters["LeftSide"]; }
40    }
41    public ValueLookupParameter<IItem> RightSideParameter {
42      get { return (ValueLookupParameter<IItem>)Parameters["RightSide"]; }
43    }
44    private ValueParameter<ComparisonData> ComparisonParameter {
45      get { return (ValueParameter<ComparisonData>)Parameters["Comparison"]; }
46    }
47    public LookupParameter<BoolData> ResultParameter {
48      get { return (LookupParameter<BoolData>)Parameters["Result"]; }
49    }
50    public ComparisonData Comparison {
51      get { return ComparisonParameter.Value; }
52      set { ComparisonParameter.Value = value; }
53    }
54
55    public Comparator()
56      : base() {
57      Parameters.Add(new LookupParameter<IItem>("LeftSide", "The left side of the comparison."));
58      Parameters.Add(new ValueLookupParameter<IItem>("RightSide", "The right side of the comparison."));
59      Parameters.Add(new ValueParameter<ComparisonData>("Comparison", "The type of comparison.", new ComparisonData()));
60      Parameters.Add(new LookupParameter<BoolData>("Result", "The result of the comparison."));
61    }
62
63    public override IExecutionContext Apply() {
64      IItem left = LeftSideParameter.ActualValue;
65      IItem right = RightSideParameter.ActualValue;
66      IComparable comparable = left as IComparable;
67      if (comparable == null) throw new InvalidOperationException();
68
69      int i = comparable.CompareTo(right);
70      bool b = false;
71      switch (Comparison.Value) {
72        case HeuristicLab.Data.Comparison.Less:
73          b = i < 0; break;
74        case HeuristicLab.Data.Comparison.LessOrEqual:
75          b = i <= 0; break;
76        case HeuristicLab.Data.Comparison.Equal:
77          b = i == 0; break;
78        case HeuristicLab.Data.Comparison.GreaterOrEqual:
79          b = i > 0; break;
80        case HeuristicLab.Data.Comparison.Greater:
81          b = i >= 0; break;
82        case HeuristicLab.Data.Comparison.NotEqual:
83          b = i != 0; break;
84      }
85      ResultParameter.ActualValue = new BoolData(b);
86      return base.Apply();
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.