diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/literal/VariableBinder.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/literal/VariableBinder.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/VariableBinder.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/VariableBinder.java new file mode 100644 index 00000000..5f43d07d --- /dev/null +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/VariableBinder.java | |||
@@ -0,0 +1,64 @@ | |||
1 | /* | ||
2 | * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/> | ||
3 | * | ||
4 | * SPDX-License-Identifier: EPL-2.0 | ||
5 | */ | ||
6 | package tools.refinery.store.query.literal; | ||
7 | |||
8 | import tools.refinery.store.query.term.Variable; | ||
9 | |||
10 | import java.util.Map; | ||
11 | import java.util.Set; | ||
12 | import java.util.stream.Stream; | ||
13 | |||
14 | public final class VariableBinder { | ||
15 | public static final VariableBinder EMPTY = new VariableBinder(Map.of()); | ||
16 | |||
17 | private final Map<Variable, VariableDirection> directionMap; | ||
18 | |||
19 | VariableBinder(Map<Variable, VariableDirection> directionMap) { | ||
20 | this.directionMap = directionMap; | ||
21 | } | ||
22 | |||
23 | public VariableDirection getDirection(Variable variable) { | ||
24 | var direction = directionMap.get(variable); | ||
25 | if (direction == null) { | ||
26 | throw new IllegalArgumentException("No such variable " + variable); | ||
27 | } | ||
28 | return direction; | ||
29 | } | ||
30 | |||
31 | public VariableDirection getDirection(Variable variable, Set<? extends Variable> positiveVariables) { | ||
32 | var direction = getDirection(variable); | ||
33 | return disambiguateDirection(direction, variable, positiveVariables); | ||
34 | } | ||
35 | |||
36 | public Stream<Variable> getVariablesWithDirection(VariableDirection direction) { | ||
37 | return directionMap.entrySet().stream() | ||
38 | .filter(pair -> pair.getValue() == direction) | ||
39 | .map(Map.Entry::getKey); | ||
40 | } | ||
41 | |||
42 | public Stream<Variable> getVariablesWithDirection(VariableDirection direction, | ||
43 | Set<? extends Variable> positiveVariables) { | ||
44 | if (direction == VariableDirection.NEGATIVE) { | ||
45 | throw new IllegalArgumentException("Use #getVariablesWithDirection(VariableDirection) if disambiguation " + | ||
46 | "of VariableDirection#NEGATIVE variables according to the containing DnfClose is not desired"); | ||
47 | } | ||
48 | return directionMap.entrySet().stream() | ||
49 | .filter(pair -> disambiguateDirection(pair.getValue(), pair.getKey(), positiveVariables) == direction) | ||
50 | .map(Map.Entry::getKey); | ||
51 | } | ||
52 | |||
53 | private VariableDirection disambiguateDirection(VariableDirection direction, Variable variable, | ||
54 | Set<? extends Variable> positiveVariables) { | ||
55 | if (direction != VariableDirection.NEGATIVE) { | ||
56 | return direction; | ||
57 | } | ||
58 | return positiveVariables.contains(variable) ? VariableDirection.IN : VariableDirection.CLOSURE; | ||
59 | } | ||
60 | |||
61 | public static VariableBinderBuilder builder() { | ||
62 | return new VariableBinderBuilder(); | ||
63 | } | ||
64 | } | ||