1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Parameters;
|
---|
27 | using HeuristicLab.Persistence;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Operators {
|
---|
30 | /// <summary>
|
---|
31 | /// An operator which compares two items.
|
---|
32 | /// </summary>
|
---|
33 | [Item("Comparator", "An operator which compares two items.")]
|
---|
34 | [StorableType("caae008a-ce1f-4528-8493-70d9b22c13c7")]
|
---|
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 | }
|
---|
42 | private ValueParameter<Comparison> ComparisonParameter {
|
---|
43 | get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
|
---|
44 | }
|
---|
45 | public LookupParameter<BoolValue> ResultParameter {
|
---|
46 | get { return (LookupParameter<BoolValue>)Parameters["Result"]; }
|
---|
47 | }
|
---|
48 | public Comparison Comparison {
|
---|
49 | get { return ComparisonParameter.Value; }
|
---|
50 | set { ComparisonParameter.Value = value; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | [StorableConstructor]
|
---|
54 | private Comparator(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
55 | private Comparator(Comparator original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | }
|
---|
58 | public Comparator()
|
---|
59 | : base() {
|
---|
60 | Parameters.Add(new LookupParameter<IItem>("LeftSide", "The left side of the comparison."));
|
---|
61 | Parameters.Add(new ValueLookupParameter<IItem>("RightSide", "The right side of the comparison."));
|
---|
62 | Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(Data.ComparisonType.Equal)));
|
---|
63 | Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison."));
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
67 | return new Comparator(this, cloner);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override IOperation Apply() {
|
---|
71 | IItem left = LeftSideParameter.ActualValue;
|
---|
72 | IItem right = RightSideParameter.ActualValue;
|
---|
73 | IComparable comparable = left as IComparable;
|
---|
74 | if (comparable == null) throw new InvalidOperationException();
|
---|
75 |
|
---|
76 | int i = comparable.CompareTo(right);
|
---|
77 | bool b = false;
|
---|
78 | switch (Comparison.Value) {
|
---|
79 | case HeuristicLab.Data.ComparisonType.Less:
|
---|
80 | b = i < 0; break;
|
---|
81 | case HeuristicLab.Data.ComparisonType.LessOrEqual:
|
---|
82 | b = i <= 0; break;
|
---|
83 | case HeuristicLab.Data.ComparisonType.Equal:
|
---|
84 | b = i == 0; break;
|
---|
85 | case HeuristicLab.Data.ComparisonType.GreaterOrEqual:
|
---|
86 | b = i >= 0; break;
|
---|
87 | case HeuristicLab.Data.ComparisonType.Greater:
|
---|
88 | b = i > 0; break;
|
---|
89 | case HeuristicLab.Data.ComparisonType.NotEqual:
|
---|
90 | b = i != 0; break;
|
---|
91 | }
|
---|
92 | ResultParameter.ActualValue = new BoolValue(b);
|
---|
93 | return base.Apply();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|