diff options
author | 2023-06-17 02:10:30 +0200 | |
---|---|---|
committer | 2023-06-17 02:12:19 +0200 | |
commit | de351479641196a85b21875fcab4f6527779b2ff (patch) | |
tree | a792c9a8a7c41e3c1822d44dcf218a9ee135afd0 /subprojects/store-query/src/main | |
parent | refactor(query): structural equality matcher (diff) | |
download | refinery-de351479641196a85b21875fcab4f6527779b2ff.tar.gz refinery-de351479641196a85b21875fcab4f6527779b2ff.tar.zst refinery-de351479641196a85b21875fcab4f6527779b2ff.zip |
fix: further Dnf tests and fixes
Diffstat (limited to 'subprojects/store-query/src/main')
11 files changed, 56 insertions, 28 deletions
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/Constraint.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/Constraint.java index e841da9e..c0995e53 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/Constraint.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/Constraint.java | |||
@@ -32,7 +32,9 @@ public interface Constraint { | |||
32 | return equals(other); | 32 | return equals(other); |
33 | } | 33 | } |
34 | 34 | ||
35 | String toReferenceString(); | 35 | default String toReferenceString() { |
36 | return name(); | ||
37 | } | ||
36 | 38 | ||
37 | default CallLiteral call(CallPolarity polarity, List<Variable> arguments) { | 39 | default CallLiteral call(CallPolarity polarity, List<Variable> arguments) { |
38 | return new CallLiteral(polarity, this, arguments); | 40 | return new CallLiteral(polarity, this, arguments); |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/ClausePostProcessor.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/ClausePostProcessor.java index dd45ecd4..b5e7092b 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/ClausePostProcessor.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/ClausePostProcessor.java | |||
@@ -106,7 +106,7 @@ class ClausePostProcessor { | |||
106 | private Set<NodeVariable> getEquivalentVariables(NodeVariable variable) { | 106 | private Set<NodeVariable> getEquivalentVariables(NodeVariable variable) { |
107 | var representative = getRepresentative(variable); | 107 | var representative = getRepresentative(variable); |
108 | if (!representative.equals(variable)) { | 108 | if (!representative.equals(variable)) { |
109 | throw new IllegalStateException("NodeVariable %s already has a representative %s" | 109 | throw new AssertionError("NodeVariable %s already has a representative %s" |
110 | .formatted(variable, representative)); | 110 | .formatted(variable, representative)); |
111 | } | 111 | } |
112 | return equivalencePartition.computeIfAbsent(variable, key -> { | 112 | return equivalencePartition.computeIfAbsent(variable, key -> { |
@@ -249,7 +249,7 @@ class ClausePostProcessor { | |||
249 | 249 | ||
250 | private void bindVariable(Variable input) { | 250 | private void bindVariable(Variable input) { |
251 | if (!remainingInputs.remove(input)) { | 251 | if (!remainingInputs.remove(input)) { |
252 | throw new IllegalStateException("Already processed input %s of literal %s".formatted(input, literal)); | 252 | throw new AssertionError("Already processed input %s of literal %s".formatted(input, literal)); |
253 | } | 253 | } |
254 | if (allInputsBound()) { | 254 | if (allInputsBound()) { |
255 | addToAllInputsBoundQueue(); | 255 | addToAllInputsBoundQueue(); |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java index dcf7611d..8e38ca6b 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java | |||
@@ -223,7 +223,8 @@ public final class DnfBuilder { | |||
223 | } else if (result instanceof ClausePostProcessor.ConstantResult constantResult) { | 223 | } else if (result instanceof ClausePostProcessor.ConstantResult constantResult) { |
224 | switch (constantResult) { | 224 | switch (constantResult) { |
225 | case ALWAYS_TRUE -> { | 225 | case ALWAYS_TRUE -> { |
226 | return List.of(new DnfClause(Set.of(), List.of())); | 226 | var inputVariables = getInputVariables(); |
227 | return List.of(new DnfClause(inputVariables, List.of())); | ||
227 | } | 228 | } |
228 | case ALWAYS_FALSE -> { | 229 | case ALWAYS_FALSE -> { |
229 | // Skip this clause because it can never match. | 230 | // Skip this clause because it can never match. |
@@ -248,4 +249,14 @@ public final class DnfBuilder { | |||
248 | } | 249 | } |
249 | return Collections.unmodifiableMap(mutableParameterInfoMap); | 250 | return Collections.unmodifiableMap(mutableParameterInfoMap); |
250 | } | 251 | } |
252 | |||
253 | private Set<Variable> getInputVariables() { | ||
254 | var inputParameters = new LinkedHashSet<Variable>(); | ||
255 | for (var parameter : parameters) { | ||
256 | if (parameter.getDirection() == ParameterDirection.IN) { | ||
257 | inputParameters.add(parameter.getVariable()); | ||
258 | } | ||
259 | } | ||
260 | return Collections.unmodifiableSet(inputParameters); | ||
261 | } | ||
251 | } | 262 | } |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/exceptions/IncompatibleParameterDirectionException.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/exceptions/IncompatibleParameterDirectionException.java deleted file mode 100644 index 52da20ae..00000000 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/exceptions/IncompatibleParameterDirectionException.java +++ /dev/null | |||
@@ -1,16 +0,0 @@ | |||
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.exceptions; | ||
7 | |||
8 | public class IncompatibleParameterDirectionException extends RuntimeException { | ||
9 | public IncompatibleParameterDirectionException(String message) { | ||
10 | super(message); | ||
11 | } | ||
12 | |||
13 | public IncompatibleParameterDirectionException(String message, Throwable cause) { | ||
14 | super(message, cause); | ||
15 | } | ||
16 | } | ||
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AbstractCallLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AbstractCallLiteral.java index ed7d3401..8ef8e8b4 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AbstractCallLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AbstractCallLiteral.java | |||
@@ -19,6 +19,8 @@ public abstract class AbstractCallLiteral implements Literal { | |||
19 | private final Set<Variable> inArguments; | 19 | private final Set<Variable> inArguments; |
20 | private final Set<Variable> outArguments; | 20 | private final Set<Variable> outArguments; |
21 | 21 | ||
22 | // Use exhaustive switch over enums. | ||
23 | @SuppressWarnings("squid:S1301") | ||
22 | protected AbstractCallLiteral(Constraint target, List<Variable> arguments) { | 24 | protected AbstractCallLiteral(Constraint target, List<Variable> arguments) { |
23 | int arity = target.arity(); | 25 | int arity = target.arity(); |
24 | if (arguments.size() != arity) { | 26 | if (arguments.size() != arity) { |
@@ -59,14 +61,14 @@ public abstract class AbstractCallLiteral implements Literal { | |||
59 | 61 | ||
60 | private static void checkInOutUnifiable(Variable argument) { | 62 | private static void checkInOutUnifiable(Variable argument) { |
61 | if (!argument.isUnifiable()) { | 63 | if (!argument.isUnifiable()) { |
62 | throw new IllegalArgumentException("Arguments %s cannot appear with both %s and %s direction" | 64 | throw new IllegalArgumentException("Argument %s cannot appear with both %s and %s direction" |
63 | .formatted(argument, ParameterDirection.IN, ParameterDirection.OUT)); | 65 | .formatted(argument, ParameterDirection.IN, ParameterDirection.OUT)); |
64 | } | 66 | } |
65 | } | 67 | } |
66 | 68 | ||
67 | private static void checkDuplicateOutUnifiable(Variable argument) { | 69 | private static void checkDuplicateOutUnifiable(Variable argument) { |
68 | if (!argument.isUnifiable()) { | 70 | if (!argument.isUnifiable()) { |
69 | throw new IllegalArgumentException("Arguments %s cannot be bound multiple times".formatted(argument)); | 71 | throw new IllegalArgumentException("Argument %s cannot be bound multiple times".formatted(argument)); |
70 | } | 72 | } |
71 | } | 73 | } |
72 | 74 | ||
@@ -87,12 +89,17 @@ public abstract class AbstractCallLiteral implements Literal { | |||
87 | 89 | ||
88 | @Override | 90 | @Override |
89 | public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) { | 91 | public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) { |
90 | return getArgumentsOfDirection(ParameterDirection.IN); | 92 | var inputVariables = new LinkedHashSet<>(getArgumentsOfDirection(ParameterDirection.OUT)); |
93 | inputVariables.retainAll(positiveVariablesInClause); | ||
94 | inputVariables.addAll(getArgumentsOfDirection(ParameterDirection.IN)); | ||
95 | return Collections.unmodifiableSet(inputVariables); | ||
91 | } | 96 | } |
92 | 97 | ||
93 | @Override | 98 | @Override |
94 | public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) { | 99 | public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) { |
95 | return Set.of(); | 100 | var privateVariables = new LinkedHashSet<>(getArgumentsOfDirection(ParameterDirection.OUT)); |
101 | privateVariables.removeAll(positiveVariablesInClause); | ||
102 | return Collections.unmodifiableSet(privateVariables); | ||
96 | } | 103 | } |
97 | 104 | ||
98 | @Override | 105 | @Override |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AggregationLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AggregationLiteral.java index b2fec430..3a5eb5c7 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AggregationLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/AggregationLiteral.java | |||
@@ -61,6 +61,14 @@ public class AggregationLiteral<R, T> extends AbstractCallLiteral { | |||
61 | } | 61 | } |
62 | 62 | ||
63 | @Override | 63 | @Override |
64 | public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) { | ||
65 | if (positiveVariablesInClause.contains(inputVariable)) { | ||
66 | throw new IllegalArgumentException("Aggregation variable %s must not be bound".formatted(inputVariable)); | ||
67 | } | ||
68 | return super.getInputVariables(positiveVariablesInClause); | ||
69 | } | ||
70 | |||
71 | @Override | ||
64 | public Literal reduce() { | 72 | public Literal reduce() { |
65 | var reduction = getTarget().getReduction(); | 73 | var reduction = getTarget().getReduction(); |
66 | return switch (reduction) { | 74 | return switch (reduction) { |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java index 27d8ad60..29772aee 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/literal/CallLiteral.java | |||
@@ -49,6 +49,22 @@ public final class CallLiteral extends AbstractCallLiteral implements CanNegate< | |||
49 | } | 49 | } |
50 | 50 | ||
51 | @Override | 51 | @Override |
52 | public Set<Variable> getInputVariables(Set<? extends Variable> positiveVariablesInClause) { | ||
53 | if (polarity.isPositive()) { | ||
54 | return getArgumentsOfDirection(ParameterDirection.IN); | ||
55 | } | ||
56 | return super.getInputVariables(positiveVariablesInClause); | ||
57 | } | ||
58 | |||
59 | @Override | ||
60 | public Set<Variable> getPrivateVariables(Set<? extends Variable> positiveVariablesInClause) { | ||
61 | if (polarity.isPositive()) { | ||
62 | return Set.of(); | ||
63 | } | ||
64 | return super.getPrivateVariables(positiveVariablesInClause); | ||
65 | } | ||
66 | |||
67 | @Override | ||
52 | public Literal reduce() { | 68 | public Literal reduce() { |
53 | var reduction = getTarget().getReduction(); | 69 | var reduction = getTarget().getReduction(); |
54 | var negatedReduction = polarity.isPositive() ? reduction : reduction.negate(); | 70 | var negatedReduction = polarity.isPositive() ? reduction : reduction.negate(); |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/term/Parameter.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/Parameter.java index 0fe297ab..e5a0cdf1 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/term/Parameter.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/term/Parameter.java | |||
@@ -9,7 +9,7 @@ import java.util.Objects; | |||
9 | import java.util.Optional; | 9 | import java.util.Optional; |
10 | 10 | ||
11 | public class Parameter { | 11 | public class Parameter { |
12 | public static final Parameter NODE_IN_OUT = new Parameter(null, ParameterDirection.OUT); | 12 | public static final Parameter NODE_OUT = new Parameter(null, ParameterDirection.OUT); |
13 | 13 | ||
14 | private final Class<?> dataType; | 14 | private final Class<?> dataType; |
15 | private final ParameterDirection direction; | 15 | private final ParameterDirection direction; |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/AbstractFunctionView.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/AbstractFunctionView.java index c6f3dd43..c1f9d688 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/AbstractFunctionView.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/AbstractFunctionView.java | |||
@@ -105,7 +105,7 @@ public abstract class AbstractFunctionView<T> extends SymbolView<T> { | |||
105 | 105 | ||
106 | private static List<Parameter> createParameters(int symbolArity, Parameter outParameter) { | 106 | private static List<Parameter> createParameters(int symbolArity, Parameter outParameter) { |
107 | var parameters = new Parameter[symbolArity + 1]; | 107 | var parameters = new Parameter[symbolArity + 1]; |
108 | Arrays.fill(parameters, Parameter.NODE_IN_OUT); | 108 | Arrays.fill(parameters, Parameter.NODE_OUT); |
109 | parameters[symbolArity] = outParameter; | 109 | parameters[symbolArity] = outParameter; |
110 | return List.of(parameters); | 110 | return List.of(parameters); |
111 | } | 111 | } |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/NodeFunctionView.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/NodeFunctionView.java index e9785c67..fcf11506 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/NodeFunctionView.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/NodeFunctionView.java | |||
@@ -11,7 +11,7 @@ import tools.refinery.store.tuple.Tuple1; | |||
11 | 11 | ||
12 | public final class NodeFunctionView extends AbstractFunctionView<Tuple1> { | 12 | public final class NodeFunctionView extends AbstractFunctionView<Tuple1> { |
13 | public NodeFunctionView(Symbol<Tuple1> symbol, String name) { | 13 | public NodeFunctionView(Symbol<Tuple1> symbol, String name) { |
14 | super(symbol, name, Parameter.NODE_IN_OUT); | 14 | super(symbol, name, Parameter.NODE_OUT); |
15 | } | 15 | } |
16 | 16 | ||
17 | public NodeFunctionView(Symbol<Tuple1> symbol) { | 17 | public NodeFunctionView(Symbol<Tuple1> symbol) { |
diff --git a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/TuplePreservingView.java b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/TuplePreservingView.java index 7e5b7788..6bc5a708 100644 --- a/subprojects/store-query/src/main/java/tools/refinery/store/query/view/TuplePreservingView.java +++ b/subprojects/store-query/src/main/java/tools/refinery/store/query/view/TuplePreservingView.java | |||
@@ -76,7 +76,7 @@ public abstract class TuplePreservingView<T> extends SymbolView<T> { | |||
76 | 76 | ||
77 | private static List<Parameter> createParameters(int arity) { | 77 | private static List<Parameter> createParameters(int arity) { |
78 | var parameters = new Parameter[arity]; | 78 | var parameters = new Parameter[arity]; |
79 | Arrays.fill(parameters, Parameter.NODE_IN_OUT); | 79 | Arrays.fill(parameters, Parameter.NODE_OUT); |
80 | return List.of(parameters); | 80 | return List.of(parameters); |
81 | } | 81 | } |
82 | } | 82 | } |