#region License Information /* HeuristicLab * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Operators { /// /// An operator which compares two items. /// [Item("Comparator", "An operator which compares two items.")] [StorableClass("8EEAB358-6F38-4A12-A69C-7B0931D956A0")] public sealed class Comparator : SingleSuccessorOperator { public LookupParameter LeftSideParameter { get { return (LookupParameter)Parameters["LeftSide"]; } } public ValueLookupParameter RightSideParameter { get { return (ValueLookupParameter)Parameters["RightSide"]; } } private ValueParameter ComparisonParameter { get { return (ValueParameter)Parameters["Comparison"]; } } public LookupParameter ResultParameter { get { return (LookupParameter)Parameters["Result"]; } } public Comparison Comparison { get { return ComparisonParameter.Value; } set { ComparisonParameter.Value = value; } } [StorableConstructor] private Comparator(bool deserializing) : base(deserializing) { } private Comparator(Comparator original, Cloner cloner) : base(original, cloner) { } public Comparator() : base() { Parameters.Add(new LookupParameter("LeftSide", "The left side of the comparison.")); Parameters.Add(new ValueLookupParameter("RightSide", "The right side of the comparison.")); Parameters.Add(new ValueParameter("Comparison", "The type of comparison.", new Comparison(Data.ComparisonType.Equal))); Parameters.Add(new LookupParameter("Result", "The result of the comparison.")); } public override IDeepCloneable Clone(Cloner cloner) { return new Comparator(this, cloner); } public override IOperation Apply() { IItem left = LeftSideParameter.ActualValue; IItem right = RightSideParameter.ActualValue; IComparable comparable = left as IComparable; if (comparable == null) throw new InvalidOperationException(); int i = comparable.CompareTo(right); bool b = false; switch (Comparison.Value) { case HeuristicLab.Data.ComparisonType.Less: b = i < 0; break; case HeuristicLab.Data.ComparisonType.LessOrEqual: b = i <= 0; break; case HeuristicLab.Data.ComparisonType.Equal: b = i == 0; break; case HeuristicLab.Data.ComparisonType.GreaterOrEqual: b = i >= 0; break; case HeuristicLab.Data.ComparisonType.Greater: b = i > 0; break; case HeuristicLab.Data.ComparisonType.NotEqual: b = i != 0; break; } ResultParameter.ActualValue = new BoolValue(b); return base.Apply(); } } }