diff options
Diffstat (limited to 'subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/util/OrderingCompareAgent.java')
-rw-r--r-- | subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/util/OrderingCompareAgent.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/util/OrderingCompareAgent.java b/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/util/OrderingCompareAgent.java new file mode 100644 index 00000000..20fd35dd --- /dev/null +++ b/subprojects/interpreter-rete/src/main/java/tools/refinery/interpreter/rete/util/OrderingCompareAgent.java | |||
@@ -0,0 +1,97 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | |||
10 | package tools.refinery.interpreter.rete.util; | ||
11 | |||
12 | import java.util.Comparator; | ||
13 | |||
14 | /** | ||
15 | * Comparing agent for an ordering. Terminology: the "preferred" item will register as LESS. | ||
16 | * | ||
17 | * @author Gabor Bergmann | ||
18 | * | ||
19 | */ | ||
20 | public abstract class OrderingCompareAgent<T> { | ||
21 | protected T a; | ||
22 | protected T b; | ||
23 | |||
24 | /** | ||
25 | * @param a | ||
26 | * @param b | ||
27 | */ | ||
28 | protected OrderingCompareAgent(T a, T b) { | ||
29 | super(); | ||
30 | this.a = a; | ||
31 | this.b = b; | ||
32 | } | ||
33 | |||
34 | int result = 0; | ||
35 | |||
36 | protected abstract void doCompare(); | ||
37 | |||
38 | /** | ||
39 | * @return the result | ||
40 | */ | ||
41 | public int compare() { | ||
42 | doCompare(); | ||
43 | return result; | ||
44 | } | ||
45 | |||
46 | // COMPARISON HELPERS | ||
47 | protected boolean isUnknown() { | ||
48 | return result == 0; | ||
49 | } | ||
50 | |||
51 | /** | ||
52 | * @pre result == 0 | ||
53 | */ | ||
54 | protected boolean consider(int partial) { | ||
55 | if (isUnknown()) | ||
56 | result = partial; | ||
57 | return isUnknown(); | ||
58 | } | ||
59 | |||
60 | protected boolean swallowBoolean(boolean x) { | ||
61 | return x; | ||
62 | } | ||
63 | |||
64 | // PREFERENCE FUNCTIONS | ||
65 | protected static int dontCare() { | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | protected static int preferTrue(boolean b1, boolean b2) { | ||
70 | return (b1 ^ b2) ? (b1 ? -1 : +1) : 0; | ||
71 | } | ||
72 | |||
73 | protected static int preferFalse(boolean b1, boolean b2) { | ||
74 | return (b1 ^ b2) ? (b2 ? -1 : +1) : 0; | ||
75 | } | ||
76 | |||
77 | protected static <U> int preferLess(Comparable<U> c1, U c2) { | ||
78 | return c1.compareTo(c2); | ||
79 | } | ||
80 | |||
81 | protected static <U> int preferLess(U c1, U c2, Comparator<U> comp) { | ||
82 | return comp.compare(c1, c2); | ||
83 | } | ||
84 | |||
85 | protected static <U> int preferMore(Comparable<U> c1, U c2) { | ||
86 | return reverse(c1.compareTo(c2)); | ||
87 | } | ||
88 | |||
89 | protected static <U> int preferMore(U c1, U c2, Comparator<U> comp) { | ||
90 | return reverse(comp.compare(c1, c2)); | ||
91 | } | ||
92 | |||
93 | private static int reverse(int value) { | ||
94 | return Integer.compare(0, value); | ||
95 | } | ||
96 | |||
97 | } | ||