forked from s-wagner/HeuristicLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComparator.cs
96 lines (90 loc) · 3.73 KB
/
Comparator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 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 <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Operators {
/// <summary>
/// An operator which compares two items.
/// </summary>
[Item("Comparator", "An operator which compares two items.")]
[StorableClass]
public sealed class Comparator : SingleSuccessorOperator {
public LookupParameter<IItem> LeftSideParameter {
get { return (LookupParameter<IItem>)Parameters["LeftSide"]; }
}
public ValueLookupParameter<IItem> RightSideParameter {
get { return (ValueLookupParameter<IItem>)Parameters["RightSide"]; }
}
private ValueParameter<Comparison> ComparisonParameter {
get { return (ValueParameter<Comparison>)Parameters["Comparison"]; }
}
public LookupParameter<BoolValue> ResultParameter {
get { return (LookupParameter<BoolValue>)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<IItem>("LeftSide", "The left side of the comparison."));
Parameters.Add(new ValueLookupParameter<IItem>("RightSide", "The right side of the comparison."));
Parameters.Add(new ValueParameter<Comparison>("Comparison", "The type of comparison.", new Comparison(Data.ComparisonType.Equal)));
Parameters.Add(new LookupParameter<BoolValue>("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();
}
}
}