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