diff options
Diffstat (limited to 'subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java')
-rw-r--r-- | subprojects/store-query/src/main/java/tools/refinery/store/query/dnf/DnfBuilder.java | 73 |
1 files changed, 18 insertions, 55 deletions
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 8e38ca6b..0f9fd366 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 | |||
@@ -5,12 +5,10 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.store.query.dnf; | 6 | package tools.refinery.store.query.dnf; |
7 | 7 | ||
8 | import tools.refinery.store.query.InvalidQueryException; | ||
8 | import tools.refinery.store.query.dnf.callback.*; | 9 | import tools.refinery.store.query.dnf.callback.*; |
9 | import tools.refinery.store.query.literal.Literal; | 10 | import tools.refinery.store.query.literal.Literal; |
10 | import tools.refinery.store.query.term.DataVariable; | 11 | import tools.refinery.store.query.term.*; |
11 | import tools.refinery.store.query.term.NodeVariable; | ||
12 | import tools.refinery.store.query.term.ParameterDirection; | ||
13 | import tools.refinery.store.query.term.Variable; | ||
14 | 12 | ||
15 | import java.util.*; | 13 | import java.util.*; |
16 | 14 | ||
@@ -62,6 +60,18 @@ public final class DnfBuilder { | |||
62 | return variable; | 60 | return variable; |
63 | } | 61 | } |
64 | 62 | ||
63 | public Variable parameter(Parameter parameter) { | ||
64 | return parameter(null, parameter); | ||
65 | } | ||
66 | |||
67 | public Variable parameter(String name, Parameter parameter) { | ||
68 | var type = parameter.tryGetType(); | ||
69 | if (type.isPresent()) { | ||
70 | return parameter(name, type.get(), parameter.getDirection()); | ||
71 | } | ||
72 | return parameter(name, parameter.getDirection()); | ||
73 | } | ||
74 | |||
65 | public DnfBuilder parameter(Variable variable) { | 75 | public DnfBuilder parameter(Variable variable) { |
66 | return parameter(variable, ParameterDirection.OUT); | 76 | return parameter(variable, ParameterDirection.OUT); |
67 | } | 77 | } |
@@ -88,7 +98,7 @@ public final class DnfBuilder { | |||
88 | public DnfBuilder symbolicParameter(SymbolicParameter symbolicParameter) { | 98 | public DnfBuilder symbolicParameter(SymbolicParameter symbolicParameter) { |
89 | var variable = symbolicParameter.getVariable(); | 99 | var variable = symbolicParameter.getVariable(); |
90 | if (!parameterVariables.add(variable)) { | 100 | if (!parameterVariables.add(variable)) { |
91 | throw new IllegalArgumentException("Variable %s is already on the parameter list %s" | 101 | throw new InvalidQueryException("Variable %s is already on the parameter list %s" |
92 | .formatted(variable, parameters)); | 102 | .formatted(variable, parameters)); |
93 | } | 103 | } |
94 | parameters.add(symbolicParameter); | 104 | parameters.add(symbolicParameter); |
@@ -129,7 +139,7 @@ public final class DnfBuilder { | |||
129 | } | 139 | } |
130 | 140 | ||
131 | public <T> DnfBuilder clause(Class<T> type1, ClauseCallback1Data1<T> callback) { | 141 | public <T> DnfBuilder clause(Class<T> type1, ClauseCallback1Data1<T> callback) { |
132 | return clause(callback.toLiterals(Variable.of("v1", type1))); | 142 | return clause(callback.toLiterals(Variable.of("d1", type1))); |
133 | } | 143 | } |
134 | 144 | ||
135 | public DnfBuilder clause(ClauseCallback2Data0 callback) { | 145 | public DnfBuilder clause(ClauseCallback2Data0 callback) { |
@@ -206,57 +216,10 @@ public final class DnfBuilder { | |||
206 | } | 216 | } |
207 | 217 | ||
208 | public Dnf build() { | 218 | public Dnf build() { |
209 | var postProcessedClauses = postProcessClauses(); | 219 | var postProcessor = new DnfPostProcessor(parameters, clauses); |
220 | var postProcessedClauses = postProcessor.postProcessClauses(); | ||
210 | return new Dnf(name, Collections.unmodifiableList(parameters), | 221 | return new Dnf(name, Collections.unmodifiableList(parameters), |
211 | Collections.unmodifiableList(functionalDependencies), | 222 | Collections.unmodifiableList(functionalDependencies), |
212 | Collections.unmodifiableList(postProcessedClauses)); | 223 | Collections.unmodifiableList(postProcessedClauses)); |
213 | } | 224 | } |
214 | |||
215 | private List<DnfClause> postProcessClauses() { | ||
216 | var parameterInfoMap = getParameterInfoMap(); | ||
217 | var postProcessedClauses = new ArrayList<DnfClause>(clauses.size()); | ||
218 | for (var literals : clauses) { | ||
219 | var postProcessor = new ClausePostProcessor(parameterInfoMap, literals); | ||
220 | var result = postProcessor.postProcessClause(); | ||
221 | if (result instanceof ClausePostProcessor.ClauseResult clauseResult) { | ||
222 | postProcessedClauses.add(clauseResult.clause()); | ||
223 | } else if (result instanceof ClausePostProcessor.ConstantResult constantResult) { | ||
224 | switch (constantResult) { | ||
225 | case ALWAYS_TRUE -> { | ||
226 | var inputVariables = getInputVariables(); | ||
227 | return List.of(new DnfClause(inputVariables, List.of())); | ||
228 | } | ||
229 | case ALWAYS_FALSE -> { | ||
230 | // Skip this clause because it can never match. | ||
231 | } | ||
232 | default -> throw new IllegalStateException("Unexpected ClausePostProcessor.ConstantResult: " + | ||
233 | constantResult); | ||
234 | } | ||
235 | } else { | ||
236 | throw new IllegalStateException("Unexpected ClausePostProcessor.Result: " + result); | ||
237 | } | ||
238 | } | ||
239 | return postProcessedClauses; | ||
240 | } | ||
241 | |||
242 | private Map<Variable, ClausePostProcessor.ParameterInfo> getParameterInfoMap() { | ||
243 | var mutableParameterInfoMap = new LinkedHashMap<Variable, ClausePostProcessor.ParameterInfo>(); | ||
244 | int arity = parameters.size(); | ||
245 | for (int i = 0; i < arity; i++) { | ||
246 | var parameter = parameters.get(i); | ||
247 | mutableParameterInfoMap.put(parameter.getVariable(), | ||
248 | new ClausePostProcessor.ParameterInfo(parameter.getDirection(), i)); | ||
249 | } | ||
250 | return Collections.unmodifiableMap(mutableParameterInfoMap); | ||
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 | } | ||
262 | } | 225 | } |