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