diff options
Diffstat (limited to 'subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/TypeJudgement.java')
-rw-r--r-- | subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/TypeJudgement.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/TypeJudgement.java b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/TypeJudgement.java new file mode 100644 index 00000000..4447b225 --- /dev/null +++ b/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/TypeJudgement.java | |||
@@ -0,0 +1,153 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2015, Bergmann Gabor, Istvan Rath 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 | package tools.refinery.viatra.runtime.matchers.psystem; | ||
10 | |||
11 | import java.util.Collection; | ||
12 | import java.util.HashSet; | ||
13 | import java.util.List; | ||
14 | import java.util.Map; | ||
15 | import java.util.stream.Collectors; | ||
16 | import java.util.Set; | ||
17 | |||
18 | import tools.refinery.viatra.runtime.matchers.context.IInputKey; | ||
19 | import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext; | ||
20 | import tools.refinery.viatra.runtime.matchers.context.InputKeyImplication; | ||
21 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint; | ||
22 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.TypeConstraint; | ||
23 | import tools.refinery.viatra.runtime.matchers.tuple.Tuple; | ||
24 | import tools.refinery.viatra.runtime.matchers.tuple.Tuples; | ||
25 | |||
26 | /** | ||
27 | * A judgement that means that the given tuple of variables will represent a tuple of values that is a member of the extensional relation identified by the given input key. | ||
28 | * @author Bergmann Gabor | ||
29 | * | ||
30 | */ | ||
31 | public class TypeJudgement { | ||
32 | |||
33 | private IInputKey inputKey; | ||
34 | private Tuple variablesTuple; | ||
35 | /** | ||
36 | * @param inputKey | ||
37 | * @param variablesTuple | ||
38 | */ | ||
39 | public TypeJudgement(IInputKey inputKey, Tuple variablesTuple) { | ||
40 | super(); | ||
41 | this.inputKey = inputKey; | ||
42 | this.variablesTuple = variablesTuple; | ||
43 | } | ||
44 | public IInputKey getInputKey() { | ||
45 | return inputKey; | ||
46 | } | ||
47 | public Tuple getVariablesTuple() { | ||
48 | return variablesTuple; | ||
49 | } | ||
50 | @Override | ||
51 | public int hashCode() { | ||
52 | final int prime = 31; | ||
53 | int result = 1; | ||
54 | result = prime * result | ||
55 | + ((inputKey == null) ? 0 : inputKey.hashCode()); | ||
56 | result = prime * result | ||
57 | + ((variablesTuple == null) ? 0 : variablesTuple.hashCode()); | ||
58 | return result; | ||
59 | } | ||
60 | @Override | ||
61 | public boolean equals(Object obj) { | ||
62 | if (this == obj) | ||
63 | return true; | ||
64 | if (obj == null) | ||
65 | return false; | ||
66 | if (!(obj instanceof TypeJudgement)) | ||
67 | return false; | ||
68 | TypeJudgement other = (TypeJudgement) obj; | ||
69 | if (inputKey == null) { | ||
70 | if (other.inputKey != null) | ||
71 | return false; | ||
72 | } else if (!inputKey.equals(other.inputKey)) | ||
73 | return false; | ||
74 | if (variablesTuple == null) { | ||
75 | if (other.variablesTuple != null) | ||
76 | return false; | ||
77 | } else if (!variablesTuple.equals(other.variablesTuple)) | ||
78 | return false; | ||
79 | return true; | ||
80 | } | ||
81 | |||
82 | public Set<TypeJudgement> getDirectlyImpliedJudgements(IQueryMetaContext context) { | ||
83 | Set<TypeJudgement> results = new HashSet<TypeJudgement>(); | ||
84 | results.add(this); | ||
85 | |||
86 | Collection<InputKeyImplication> implications = context.getImplications(this.inputKey); | ||
87 | for (InputKeyImplication inputKeyImplication : implications) { | ||
88 | results.add( | ||
89 | transcribeImplication(inputKeyImplication) | ||
90 | ); | ||
91 | } | ||
92 | |||
93 | return results; | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * @since 1.6 | ||
98 | */ | ||
99 | public Set<TypeJudgement> getWeakenedAlternativeJudgements(IQueryMetaContext context) { | ||
100 | Set<TypeJudgement> results = new HashSet<TypeJudgement>(); | ||
101 | |||
102 | Collection<InputKeyImplication> implications = context.getWeakenedAlternatives(this.inputKey); | ||
103 | for (InputKeyImplication inputKeyImplication : implications) { | ||
104 | results.add( | ||
105 | transcribeImplication(inputKeyImplication) | ||
106 | ); | ||
107 | } | ||
108 | |||
109 | return results; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * @since 2.0 | ||
114 | */ | ||
115 | public Map<TypeJudgement, Set<TypeJudgement>> getConditionalImpliedJudgements(IQueryMetaContext context) { | ||
116 | return context.getConditionalImplications(this.inputKey).entrySet().stream().collect(Collectors.toMap( | ||
117 | entry -> transcribeImplication(entry.getKey()), | ||
118 | entry -> entry.getValue().stream().map(this::transcribeImplication).collect(Collectors.toSet()))); | ||
119 | } | ||
120 | |||
121 | |||
122 | |||
123 | private TypeJudgement transcribeImplication(InputKeyImplication inputKeyImplication) { | ||
124 | return new TypeJudgement( | ||
125 | inputKeyImplication.getImpliedKey(), | ||
126 | transcribeVariablesToTuple(inputKeyImplication.getImpliedIndices()) | ||
127 | ); | ||
128 | } | ||
129 | private Tuple transcribeVariablesToTuple(List<Integer> indices) { | ||
130 | Object[] elements = new Object[indices.size()]; | ||
131 | for (int i = 0; i < indices.size(); ++i) | ||
132 | elements[i] = variablesTuple.get(indices.get(i)); | ||
133 | return Tuples.flatTupleOf(elements); | ||
134 | } | ||
135 | |||
136 | @Override | ||
137 | public String toString() { | ||
138 | return "TypeJudgement:" + inputKey.getPrettyPrintableName() + "@" + variablesTuple.toString(); | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Creates this judgement as a direct type constraint in the given PBody under construction. | ||
143 | * <p> pre: the variables tuple must be formed of variables of that PBody. | ||
144 | * @since 1.6 | ||
145 | */ | ||
146 | public PConstraint createConstraintFor(PBody pBody) { | ||
147 | if (inputKey.isEnumerable()) { | ||
148 | return new TypeConstraint(pBody, variablesTuple, inputKey); | ||
149 | } else { | ||
150 | return new TypeFilterConstraint(pBody, variablesTuple, inputKey); | ||
151 | } | ||
152 | } | ||
153 | } | ||