blob: 7bc949a814d67bc54bf4f59807fff9365845bb7c (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*******************************************************************************
* Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-v20.html.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package tools.refinery.viatra.runtime.matchers.psystem.basicdeferred;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext;
import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.psystem.TypeJudgement;
import tools.refinery.viatra.runtime.matchers.psystem.VariableDeferredPConstraint;
/**
* @author Gabor Bergmann
*
*/
public abstract class BaseTypeSafeConstraint extends
VariableDeferredPConstraint {
protected Set<PVariable> inputVariables;
protected PVariable outputVariable;
public PVariable getOutputVariable() {
return outputVariable;
}
/**
* @param pBody
* @param inputVariables
* @param outputVariable null iff no output (check-only)
*/
public BaseTypeSafeConstraint(PBody pBody,
Set<PVariable> inputVariables, final PVariable outputVariable) {
super(pBody,
(outputVariable == null) ?
inputVariables :
Stream.concat(inputVariables.stream(), Stream.of(outputVariable)).collect(Collectors.toSet())
);
this.inputVariables = inputVariables;
this.outputVariable = outputVariable;
}
@Override
public Set<PVariable> getDeducedVariables() {
if (outputVariable == null)
return Collections.emptySet();
else
return Collections.singleton(outputVariable);
}
@Override
public Set<PVariable> getDeferringVariables() {
return inputVariables;
}
@Override
public boolean isReadyAt(SubPlan plan, IQueryMetaContext context) {
if (super.isReadyAt(plan, context)) {
return checkTypeSafety(plan, context) == null;
}
return false;
}
/**
* Checks whether all type restrictions are already enforced on affected variables.
*
* @param plan
* @return a variable whose type safety is not enforced yet, or null if the plan is typesafe
*/
public PVariable checkTypeSafety(SubPlan plan, IQueryMetaContext context) {
Set<TypeJudgement> impliedJudgements = plan.getAllImpliedTypeJudgements(context);
for (PVariable pVariable : inputVariables) {
Set<TypeJudgement> allTypeRestrictionsForVariable = pBody.getAllUnaryTypeRestrictions(context).get(pVariable);
if (allTypeRestrictionsForVariable != null && !impliedJudgements.containsAll(allTypeRestrictionsForVariable))
return pVariable;
}
return null;
}
@Override
protected void doReplaceVariable(PVariable obsolete, PVariable replacement) {
if (inputVariables.remove(obsolete))
inputVariables.add(replacement);
if (outputVariable == obsolete)
outputVariable = replacement;
}
}
|