diff options
Diffstat (limited to 'subprojects/interpreter/src')
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 | *******************************************************************************/ | ||
9 | package tools.refinery.interpreter.matchers.psystem.basicdeferred; | ||
10 | |||
11 | import tools.refinery.interpreter.matchers.context.IQueryMetaContext; | ||
12 | import tools.refinery.interpreter.matchers.psystem.ITypeInfoProviderConstraint; | ||
13 | import tools.refinery.interpreter.matchers.psystem.PBody; | ||
14 | import tools.refinery.interpreter.matchers.psystem.PVariable; | ||
15 | import tools.refinery.interpreter.matchers.psystem.TypeJudgement; | ||
16 | import tools.refinery.interpreter.matchers.psystem.queries.PQuery; | ||
17 | import tools.refinery.interpreter.matchers.tuple.Tuple; | ||
18 | import tools.refinery.interpreter.matchers.tuple.Tuples; | ||
19 | |||
20 | import java.util.Collections; | ||
21 | import java.util.Set; | ||
22 | |||
23 | public 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, |