aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/interpreter
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2024-02-22 02:02:21 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2024-04-07 14:55:46 +0200
commit31465875054c847943e625d9e84bbbd52bc3695e (patch)
tree961b8831158e848244b841b0905f8dbd07ff1e39 /subprojects/interpreter
parentfeat(language): datatype declarations (diff)
downloadrefinery-31465875054c847943e625d9e84bbbd52bc3695e.tar.gz
refinery-31465875054c847943e625d9e84bbbd52bc3695e.tar.zst
refinery-31465875054c847943e625d9e84bbbd52bc3695e.zip
feat(query): left join for data variables
Diffstat (limited to 'subprojects/interpreter')
-rw-r--r--subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/basicdeferred/LeftJoinConstraint.java82
-rw-r--r--subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/rewriters/PBodyCopier.java13
2 files changed, 94 insertions, 1 deletions
diff --git a/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/basicdeferred/LeftJoinConstraint.java b/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/basicdeferred/LeftJoinConstraint.java
new file mode 100644
index 00000000..1c1a895e
--- /dev/null
+++ b/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/basicdeferred/LeftJoinConstraint.java
@@ -0,0 +1,82 @@
1/*******************************************************************************
2 * Copyright (c) 2010-2016, Tamas Szabo, Istvan Rath and Daniel Varro
3 * Copyright (c) 2024 The Refinery Authors <https://refinery.tools/>
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0 which is available at
6 * http://www.eclipse.org/legal/epl-v20.html.
7 * SPDX-License-Identifier: EPL-2.0
8 *******************************************************************************/
9package tools.refinery.interpreter.matchers.psystem.basicdeferred;
10
11import tools.refinery.interpreter.matchers.context.IQueryMetaContext;
12import tools.refinery.interpreter.matchers.psystem.ITypeInfoProviderConstraint;
13import tools.refinery.interpreter.matchers.psystem.PBody;
14import tools.refinery.interpreter.matchers.psystem.PVariable;
15import tools.refinery.interpreter.matchers.psystem.TypeJudgement;
16import tools.refinery.interpreter.matchers.psystem.queries.PQuery;
17import tools.refinery.interpreter.matchers.tuple.Tuple;
18import tools.refinery.interpreter.matchers.tuple.Tuples;
19
20import java.util.Collections;
21import java.util.Set;
22
23public class LeftJoinConstraint extends PatternCallBasedDeferred implements ITypeInfoProviderConstraint {
24 protected PVariable resultVariable;
25 protected int optionalColumn;
26 protected Object defaultValue;
27
28 public LeftJoinConstraint(PBody pBody, Tuple actualParametersTuple, PQuery query, PVariable resultVariable,
29 int optionalColumn, Object defaultValue) {
30 super(pBody, actualParametersTuple, query, Collections.singleton(resultVariable));
31 this.resultVariable = resultVariable;
32 this.optionalColumn = optionalColumn;
33 this.defaultValue = defaultValue;
34 }
35
36 public PVariable getResultVariable() {
37 return resultVariable;
38 }
39
40 public int getOptionalColumn() {
41 return optionalColumn;
42 }
43
44 public Object getDefaultValue() {
45 return defaultValue;
46 }
47
48 @Override
49 public Set<PVariable> getDeducedVariables() {
50 return Collections.singleton(resultVariable);
51 }
52
53 @Override
54 protected void doDoReplaceVariables(PVariable obsolete, PVariable replacement) {
55 if (resultVariable.equals(obsolete)) {
56 resultVariable = replacement;
57 }
58 }
59
60 @Override
61 protected Set<PVariable> getCandidateQuantifiedVariables() {
62 return actualParametersTuple.getDistinctElements();
63 }
64
65 @Override
66 protected String toStringRest() {
67 return query.getFullyQualifiedName() + "@" + actualParametersTuple.toString() + "->"
68 + resultVariable.toString();
69 }
70
71 @Override
72 public Set<TypeJudgement> getImpliedJudgements(IQueryMetaContext context) {
73 var optionalParameter = getReferredQuery().getParameters().get(optionalColumn);
74 var unaryType = optionalParameter.getDeclaredUnaryType();
75 if (unaryType != null && !context.isEnumerable(unaryType)) {
76 // The outer join makes the result variable non-enumerable, since the default value might not be present in
77 // the model.
78 return Set.of(new TypeJudgement(unaryType, Tuples.staticArityFlatTupleOf(resultVariable)));
79 }
80 return Set.of();
81 }
82}
diff --git a/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/rewriters/PBodyCopier.java b/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/rewriters/PBodyCopier.java
index 99350185..1e580599 100644
--- a/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/rewriters/PBodyCopier.java
+++ b/subprojects/interpreter/src/main/java/tools/refinery/interpreter/matchers/psystem/rewriters/PBodyCopier.java
@@ -137,7 +137,9 @@ public class PBodyCopier extends AbstractRewriterTraceSource {
137 } else if (constraint instanceof PatternMatchCounter) { 137 } else if (constraint instanceof PatternMatchCounter) {
138 copyPatternMatchCounterConstraint((PatternMatchCounter) constraint); 138 copyPatternMatchCounterConstraint((PatternMatchCounter) constraint);
139 } else if (constraint instanceof AggregatorConstraint) { 139 } else if (constraint instanceof AggregatorConstraint) {
140 copyAggregatorConstraint((AggregatorConstraint) constraint); 140 copyAggregatorConstraint((AggregatorConstraint) constraint);
141 } else if (constraint instanceof LeftJoinConstraint leftJoinConstraint) {
142 copyLeftJoinConstraint((LeftJoinConstraint) constraint);
141 } else if (constraint instanceof ExpressionEvaluation) { 143 } else if (constraint instanceof ExpressionEvaluation) {
142 copyExpressionEvaluationConstraint((ExpressionEvaluation) constraint); 144 copyExpressionEvaluationConstraint((ExpressionEvaluation) constraint);
143 } else { 145 } else {
@@ -256,6 +258,15 @@ public class PBodyCopier extends AbstractRewriterTraceSource {
256 constraint.getReferredQuery(), mappedResultVariable, constraint.getAggregatedColumn())); 258 constraint.getReferredQuery(), mappedResultVariable, constraint.getAggregatedColumn()));
257 } 259 }
258 260
261 protected void copyLeftJoinConstraint(LeftJoinConstraint constraint) {
262 PVariable[] mappedVariables = extractMappedVariables(constraint);
263 PVariable mappedResultVariable = variableMapping.get(constraint.getResultVariable());
264 Tuple variablesTuple = Tuples.flatTupleOf((Object[]) mappedVariables);
265 addTrace(constraint, new LeftJoinConstraint(body, variablesTuple,
266 constraint.getReferredQuery(), mappedResultVariable, constraint.getOptionalColumn(),
267 constraint.getDefaultValue()));
268 }
269
259 protected void copyExpressionEvaluationConstraint(ExpressionEvaluation expressionEvaluation) { 270 protected void copyExpressionEvaluationConstraint(ExpressionEvaluation expressionEvaluation) {
260 PVariable mappedOutputVariable = variableMapping.get(expressionEvaluation.getOutputVariable()); 271 PVariable mappedOutputVariable = variableMapping.get(expressionEvaluation.getOutputVariable());
261 addTrace(expressionEvaluation, new ExpressionEvaluation(body, 272 addTrace(expressionEvaluation, new ExpressionEvaluation(body,