diff options
Diffstat (limited to 'subprojects/language/src/main/java/tools/refinery/language/expressions/AbstractTermInterpreter.java')
-rw-r--r-- | subprojects/language/src/main/java/tools/refinery/language/expressions/AbstractTermInterpreter.java | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/expressions/AbstractTermInterpreter.java b/subprojects/language/src/main/java/tools/refinery/language/expressions/AbstractTermInterpreter.java new file mode 100644 index 00000000..2530b707 --- /dev/null +++ b/subprojects/language/src/main/java/tools/refinery/language/expressions/AbstractTermInterpreter.java | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2024 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.language.expressions; | ||
7 | |||
8 | import tools.refinery.language.model.problem.BinaryOp; | ||
9 | import tools.refinery.language.model.problem.UnaryOp; | ||
10 | import tools.refinery.language.typesystem.AggregatorName; | ||
11 | import tools.refinery.language.typesystem.DataExprType; | ||
12 | |||
13 | import java.util.*; | ||
14 | |||
15 | // This class is used to configure term interpreters by clients with various arguments. | ||
16 | @SuppressWarnings("SameParameterValue") | ||
17 | public abstract class AbstractTermInterpreter implements TermInterpreter { | ||
18 | private final Map<DataExprType, DataExprType> negations = new HashMap<>(); | ||
19 | private final Map<UnaryKey, DataExprType> unaryOperators = new HashMap<>(); | ||
20 | private final Set<DataExprType> comparisons = new HashSet<>(); | ||
21 | private final Set<DataExprType> ranges = new HashSet<>(); | ||
22 | private final Map<BinaryKey, DataExprType> binaryOperators = new HashMap<>(); | ||
23 | private final Set<CastKey> casts = new HashSet<>(); | ||
24 | private final Map<AggregatorKey, DataExprType> aggregators = new HashMap<>(); | ||
25 | |||
26 | protected AbstractTermInterpreter() { | ||
27 | } | ||
28 | |||
29 | protected void addNegation(DataExprType type, DataExprType result) { | ||
30 | negations.put(type, result); | ||
31 | } | ||
32 | |||
33 | protected void addNegation(DataExprType type) { | ||
34 | addNegation(type, type); | ||
35 | } | ||
36 | |||
37 | protected void addUnaryOperator(UnaryOp op, DataExprType type, DataExprType result) { | ||
38 | unaryOperators.put(new UnaryKey(op, type), result); | ||
39 | } | ||
40 | |||
41 | protected void addUnaryOperator(UnaryOp op, DataExprType type) { | ||
42 | addUnaryOperator(op, type, type); | ||
43 | } | ||
44 | |||
45 | protected void addComparison(DataExprType type) { | ||
46 | comparisons.add(type); | ||
47 | } | ||
48 | |||
49 | protected void addRange(DataExprType type) { | ||
50 | ranges.add(type); | ||
51 | } | ||
52 | |||
53 | protected void addBinaryOperator(BinaryOp op, DataExprType leftType, DataExprType rightType, DataExprType result) { | ||
54 | binaryOperators.put(new BinaryKey(op, leftType, rightType), result); | ||
55 | } | ||
56 | |||
57 | protected void addBinaryOperator(BinaryOp op, DataExprType type) { | ||
58 | addBinaryOperator(op, type, type, type); | ||
59 | } | ||
60 | |||
61 | protected void addCast(DataExprType fromType, DataExprType toType) { | ||
62 | if (fromType.equals(toType)) { | ||
63 | throw new IllegalArgumentException("The fromType and toType of a cast operator must be different"); | ||
64 | } | ||
65 | casts.add(new CastKey(fromType, toType)); | ||
66 | } | ||
67 | |||
68 | protected void addAggregator(AggregatorName aggregator, DataExprType type, DataExprType result) { | ||
69 | aggregators.put(new AggregatorKey(aggregator, type), result); | ||
70 | } | ||
71 | |||
72 | protected void addAggregator(AggregatorName aggregator, DataExprType type) { | ||
73 | addAggregator(aggregator, type, type); | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public Optional<DataExprType> getNegationType(DataExprType type) { | ||
78 | return Optional.ofNullable(negations.get(type)); | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public Optional<DataExprType> getUnaryOperationType(UnaryOp op, DataExprType type) { | ||
83 | if (unaryOperators.isEmpty()) { | ||
84 | return Optional.empty(); | ||
85 | } | ||
86 | return Optional.ofNullable(unaryOperators.get(new UnaryKey(op, type))); | ||
87 | } | ||
88 | |||
89 | @Override | ||
90 | public boolean isComparisonSupported(DataExprType type) { | ||
91 | return comparisons.contains(type); | ||
92 | } | ||
93 | |||
94 | @Override | ||
95 | public boolean isRangeSupported(DataExprType type) { | ||
96 | return ranges.contains(type); | ||
97 | } | ||
98 | |||
99 | @Override | ||
100 | public Optional<DataExprType> getBinaryOperationType(BinaryOp op, DataExprType leftType, DataExprType rightType) { | ||
101 | if (binaryOperators.isEmpty()) { | ||
102 | return Optional.empty(); | ||
103 | } | ||
104 | return Optional.ofNullable(binaryOperators.get(new BinaryKey(op, leftType, rightType))); | ||
105 | } | ||
106 | |||
107 | @Override | ||
108 | public Optional<DataExprType> getAggregationType(AggregatorName aggregator, DataExprType type) { | ||
109 | if (aggregators.isEmpty()) { | ||
110 | return Optional.empty(); | ||
111 | } | ||
112 | return Optional.ofNullable(aggregators.get(new AggregatorKey(aggregator, type))); | ||
113 | } | ||
114 | |||
115 | @Override | ||
116 | public boolean isCastSupported(DataExprType fromType, DataExprType toType) { | ||
117 | return casts.contains(new CastKey(fromType, toType)); | ||
118 | } | ||
119 | |||
120 | private record UnaryKey(UnaryOp op, DataExprType type) { | ||
121 | } | ||
122 | |||
123 | private record BinaryKey(BinaryOp op, DataExprType leftType, DataExprType rightType) { | ||
124 | } | ||
125 | |||
126 | private record CastKey(DataExprType fromType, DataExprType toType) { | ||
127 | } | ||
128 | |||
129 | private record AggregatorKey(AggregatorName aggregator, DataExprType type) { | ||
130 | } | ||
131 | } | ||