@@ -4,15 +4,20 @@ import com.google.common.collect.ImmutableSet
4
4
import com.google.common.collect.Iterables
5
5
import hu.bme.mit.incquery.localsearch.cpp.generator.model.PatternStub
6
6
import java.util.List
7
+ import java.util.Set
8
+ import java.util.concurrent.atomic.AtomicInteger
7
9
import org.apache.log4j.Logger
8
10
import org.eclipse.viatra.query.runtime.emf.EMFQueryMetaContext
11
+ import org.eclipse.viatra.query.runtime.matchers.psystem.PBody
12
+ import org.eclipse.viatra.query.runtime.matchers.psystem.annotations.PAnnotation
9
13
import org.eclipse.viatra.query.runtime.matchers.psystem.annotations.ParameterReference
10
14
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PDisjunction
11
15
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter
12
16
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery
13
17
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.DefaultFlattenCallPredicate
14
18
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.PBodyNormalizer
15
19
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.PQueryFlattener
20
+ import java.util.Collection
16
21
17
22
class PlanCompiler {
18
23
@@ -32,46 +37,53 @@ class PlanCompiler {
32
37
33
38
def compilePlan (PQuery pQuery ) {
34
39
val frameRegistry = new MatchingFrameRegistry
40
+
41
+ val flatDisjunction = flattener. rewrite(pQuery. disjunctBodies)
42
+ val normalizedDisjunction = normalizer. rewrite(flatDisjunction)
43
+
44
+ val normalizedBodies = normalizedDisjunction. bodies. toList
45
+
35
46
val bindings = pQuery. allAnnotations. filter[name == " Bind" ]
36
47
val boundPatternStubs = bindings. map[ binding |
37
- val boundParameters = binding. getAllValues(" parameters" ). map [
38
- switch (it ) {
39
- ParameterReference : #[it ]
40
- List<ParameterReference > : it
41
- }
42
- ]. flatten. map[
43
- pQuery. parameters. get(pQuery. getPositionOfParameter(it . name))
44
- ]. toSet
48
+ val boundParameters = getBoundParameters(binding, pQuery)
45
49
46
- val bodies = pQuery . compile(boundParameters, false , frameRegistry)
50
+ val bodies = normalizedBodies . compile(boundParameters, frameRegistry)
47
51
return new PatternStub (pQuery, bodies, boundParameters)
48
52
]
49
53
50
- val bodies = pQuery. compile(#{}, true , frameRegistry)
54
+ val bodies = if (normalizedDisjunction. sensibleWithoutBinding) {
55
+ normalizedBodies. compile(#{}, frameRegistry)
56
+ } else {
57
+ #{}
58
+ }
51
59
val unboundPatternStub = new PatternStub (pQuery, bodies)
52
60
// copy to prevent lazy evaluation
53
61
return ImmutableSet :: copyOf(Iterables :: concat(#[unboundPatternStub], boundPatternStubs))
54
62
}
63
+
64
+ private def getBoundParameters (PAnnotation binding , PQuery pQuery ) {
65
+ binding. getAllValues(" parameters" ). map [
66
+ switch (it ) {
67
+ ParameterReference : #[it ]
68
+ List<ParameterReference > : it
69
+ }
70
+ ]. flatten. map[
71
+ pQuery. parameters. get(pQuery. getPositionOfParameter(it . name))
72
+ ]. toSet
73
+ }
55
74
56
- def compile (PQuery pQuery , Iterable<PParameter > boundParameters , boolean checkSanity , MatchingFrameRegistry frameRegistry ) {
57
-
58
- val flatDisjunction = flattener. rewrite(pQuery. disjunctBodies)
59
- val normalizedDisjunction = normalizer. rewrite(flatDisjunction)
60
-
61
- if (checkSanity && ! normalizedDisjunction. sensibleWithoutBinding)
62
- return #{}
63
-
64
- val normalizedBodies = normalizedDisjunction. bodies
75
+ def compile (List<PBody > normalizedBodies , Iterable<PParameter > boundParameters , MatchingFrameRegistry frameRegistry ) {
65
76
77
+ val AtomicInteger counter = new AtomicInteger (0 )
66
78
val patternBodyStubs = normalizedBodies. map[pBody |
67
79
val boundPVariables = boundParameters. map[pBody. getVariableByNameChecked(name)]
68
80
. toSet
69
81
70
- val acceptor = new CPPSearchOperationAcceptor (frameRegistry)
82
+ val acceptor = new CPPSearchOperationAcceptor (counter . getAndIncrement, frameRegistry)
71
83
pBody. plan(Logger :: getLogger(PlanCompiler ), boundPVariables, EMFQueryMetaContext . INSTANCE , null , #{})
72
84
. compile(pBody, boundPVariables, acceptor)
73
85
74
- return acceptor. patternBodyStub
86
+ return acceptor. patternBodyStub
75
87
]. toSet
76
88
77
89
return patternBodyStubs
0 commit comments