diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/quasitree/JoinCandidate.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/quasitree/JoinCandidate.java | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/quasitree/JoinCandidate.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/quasitree/JoinCandidate.java new file mode 100644 index 00000000..45350099 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/quasitree/JoinCandidate.java | |||
@@ -0,0 +1,162 @@ | |||
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.rete.construction.quasitree; | ||
11 | |||
12 | import java.util.ArrayList; | ||
13 | import java.util.Collections; | ||
14 | import java.util.List; | ||
15 | import java.util.Map; | ||
16 | import java.util.Set; | ||
17 | import java.util.stream.Collectors; | ||
18 | import java.util.stream.Stream; | ||
19 | |||
20 | import tools.refinery.viatra.runtime.matchers.planning.SubPlan; | ||
21 | import tools.refinery.viatra.runtime.matchers.planning.SubPlanFactory; | ||
22 | import tools.refinery.viatra.runtime.matchers.planning.helpers.FunctionalDependencyHelper; | ||
23 | import tools.refinery.viatra.runtime.matchers.planning.operations.PJoin; | ||
24 | import tools.refinery.viatra.runtime.matchers.psystem.PConstraint; | ||
25 | import tools.refinery.viatra.runtime.matchers.psystem.PVariable; | ||
26 | import tools.refinery.viatra.runtime.matchers.psystem.analysis.QueryAnalyzer; | ||
27 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
28 | |||
29 | /** | ||
30 | * @author Gabor Bergmann | ||
31 | * | ||
32 | */ | ||
33 | class JoinCandidate { | ||
34 | private QueryAnalyzer analyzer; | ||
35 | |||
36 | SubPlan primary; | ||
37 | SubPlan secondary; | ||
38 | |||
39 | Set<PVariable> varPrimary; | ||
40 | Set<PVariable> varSecondary; | ||
41 | Set<PVariable> varCommon; | ||
42 | |||
43 | List<PConstraint> consPrimary; | ||
44 | List<PConstraint> consSecondary; | ||
45 | |||
46 | |||
47 | JoinCandidate(SubPlan primary, SubPlan secondary, QueryAnalyzer analyzer) { | ||
48 | super(); | ||
49 | this.primary = primary; | ||
50 | this.secondary = secondary; | ||
51 | this.analyzer = analyzer; | ||
52 | |||
53 | varPrimary = getPrimary().getVisibleVariables(); | ||
54 | varSecondary = getSecondary().getVisibleVariables(); | ||
55 | varCommon = CollectionsFactory.createSet(varPrimary); | ||
56 | varCommon.retainAll(varSecondary); | ||
57 | |||
58 | consPrimary = new ArrayList<PConstraint>(primary.getAllEnforcedConstraints()); | ||
59 | Collections.sort(consPrimary, TieBreaker.CONSTRAINT_COMPARATOR); | ||
60 | consSecondary = new ArrayList<PConstraint>(secondary.getAllEnforcedConstraints()); | ||
61 | Collections.sort(consSecondary, TieBreaker.CONSTRAINT_COMPARATOR); | ||
62 | } | ||
63 | |||
64 | |||
65 | |||
66 | /** | ||
67 | * @return the a | ||
68 | */ | ||
69 | public SubPlan getPrimary() { | ||
70 | return primary; | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * @return the b | ||
75 | */ | ||
76 | public SubPlan getSecondary() { | ||
77 | return secondary; | ||
78 | } | ||
79 | |||
80 | public SubPlan getJoinedPlan(SubPlanFactory factory) { | ||
81 | // check special cases first | ||
82 | if (isTrivial()) | ||
83 | return primary; | ||
84 | if (isSubsumption()) | ||
85 | return | ||
86 | (consPrimary.size() > consSecondary.size()) ? primary : secondary; | ||
87 | |||
88 | |||
89 | // default case | ||
90 | return factory.createSubPlan(new PJoin(), primary, secondary); | ||
91 | } | ||
92 | |||
93 | @Override | ||
94 | public String toString() { | ||
95 | return primary.toString() + " |x| " + secondary.toString(); | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * @return the varPrimary | ||
100 | */ | ||
101 | public Set<PVariable> getVarPrimary() { | ||
102 | return varPrimary; | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * @return the varSecondary | ||
107 | */ | ||
108 | public Set<PVariable> getVarSecondary() { | ||
109 | return varSecondary; | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * @return constraints of primary, sorted according to {@link TieBreaker#CONSTRAINT_COMPARATOR}. | ||
114 | */ | ||
115 | public List<PConstraint> getConsPrimary() { | ||
116 | return consPrimary; | ||
117 | } | ||
118 | /** | ||
119 | * @return constraints of secondary, sorted according to {@link TieBreaker#CONSTRAINT_COMPARATOR}. | ||
120 | */ | ||
121 | public List<PConstraint> getConsSecondary() { | ||
122 | return consSecondary; | ||
123 | } | ||
124 | |||
125 | |||
126 | |||
127 | public boolean isTrivial() { | ||
128 | return getPrimary().equals(getSecondary()); | ||
129 | } | ||
130 | |||
131 | public boolean isSubsumption() { | ||
132 | return consPrimary.containsAll(consSecondary) || consSecondary.containsAll(consPrimary); | ||
133 | } | ||
134 | |||
135 | public boolean isCheckOnly() { | ||
136 | return varPrimary.containsAll(varSecondary) || varSecondary.containsAll(varPrimary); | ||
137 | } | ||
138 | |||
139 | public boolean isDescartes() { | ||
140 | return Collections.disjoint(varPrimary, varSecondary); | ||
141 | } | ||
142 | |||
143 | private Boolean heath; | ||
144 | |||
145 | // it is a Heath-join iff common variables functionally determine either all primary or all secondary variables | ||
146 | public boolean isHeath() { | ||
147 | if (heath == null) { | ||
148 | Set<PConstraint> union = Stream.concat( | ||
149 | primary.getAllEnforcedConstraints().stream(), | ||
150 | secondary.getAllEnforcedConstraints().stream() | ||
151 | ).collect(Collectors.toSet()); | ||
152 | Map<Set<PVariable>, Set<PVariable>> dependencies = | ||
153 | analyzer.getFunctionalDependencies(union, false); | ||
154 | // does varCommon determine either varPrimary or varSecondary? | ||
155 | Set<PVariable> varCommonClosure = FunctionalDependencyHelper.closureOf(varCommon, dependencies); | ||
156 | |||
157 | heath = varCommonClosure.containsAll(varPrimary) || varCommonClosure.containsAll(varSecondary); | ||
158 | } | ||
159 | return heath; | ||
160 | } | ||
161 | |||
162 | } | ||