Skip to content

Commit e32ef1b

Browse files
committed
adapted generator to the immutable model
1 parent c65b12d commit e32ef1b

File tree

4 files changed

+85
-99
lines changed

4 files changed

+85
-99
lines changed

plugins/hu.bme.mit.incquery.localsearch.cpp/src/hu/bme/mit/incquery/localsearch/cpp/generator/LocalSearchCppGenerator.xtend

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class LocalSearchCppGenerator {
2121

2222
def IGeneratorOutputProvider generate(String queryFileName, ResourceSet resourceSet, List<PQuery> queries) {
2323

24-
val query = new QueryStub(queryFileName)
25-
query.addClasses(resourceSet.resources.map[allContents].concat.filter(EClass).filterNull.toSet)
24+
val classes = resourceSet.resources.map[allContents].concat.filter(EClass).filterNull.toSet
2625

2726
val planCompiler = new PlanCompiler
28-
queries.forEach[planCompiler.compilePlan(it, query)]
27+
val patternStubs = queries.map[
28+
planCompiler.compilePlan(it)
29+
].flatten.toSet
2930

30-
generator.initialize(query)
31+
val queryStub = new QueryStub(queryFileName, patternStubs, classes)
32+
generator.initialize(queryStub)
3133

3234
return generator
3335
}

plugins/hu.bme.mit.incquery.localsearch.cpp/src/hu/bme/mit/incquery/localsearch/cpp/generator/planner/POperationCompiler.xtend

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hu.bme.mit.incquery.localsearch.cpp.generator.planner
22

3-
import com.google.common.base.CaseFormat
3+
import com.google.common.base.Optional
4+
import com.google.common.collect.Maps
45
import hu.bme.mit.incquery.localsearch.cpp.generator.model.CheckInstanceOfStub
56
import hu.bme.mit.incquery.localsearch.cpp.generator.model.CheckMultiNavigationStub
67
import hu.bme.mit.incquery.localsearch.cpp.generator.model.CheckSingleNavigationStub
@@ -9,8 +10,10 @@ import hu.bme.mit.incquery.localsearch.cpp.generator.model.ExtendMultiNavigation
910
import hu.bme.mit.incquery.localsearch.cpp.generator.model.ExtendSingleNavigationStub
1011
import hu.bme.mit.incquery.localsearch.cpp.generator.model.MatchingFrameStub
1112
import hu.bme.mit.incquery.localsearch.cpp.generator.model.PatternBodyStub
13+
import hu.bme.mit.incquery.localsearch.cpp.generator.model.SearchOperationStub
14+
import hu.bme.mit.incquery.localsearch.cpp.generator.model.TypeInfo
15+
import hu.bme.mit.incquery.localsearch.cpp.generator.model.VariableInfo
1216
import hu.bme.mit.incquery.localsearch.cpp.generator.planner.util.CompilerHelper
13-
import hu.bme.mit.incquery.localsearch.cpp.generator.planner.util.CompilerHelper.TypeMap
1417
import java.util.Map
1518
import java.util.Set
1619
import org.eclipse.emf.ecore.EReference
@@ -34,61 +37,47 @@ class POperationCompiler {
3437

3538
var Map<PVariable, Integer> variableMapping
3639
var Map<PConstraint, Set<Integer>> variableBindings
37-
var Map<PVariable, TypeMap> typeMapping
40+
var Map<PVariable, TypeInfo> typeMapping
3841

3942
var MatchingFrameStub matchingFrame
40-
var PatternBodyStub patternBodyStub
4143

4244

43-
def void compile(SubPlan plan, PBody pBody, Set<PVariable> boundVariables, PatternBodyStub bodyStub) {
45+
def compile(SubPlan plan, PBody pBody, Set<PVariable> boundVariables) {
4446
variableMapping = CompilerHelper::createVariableMapping(plan)
4547
typeMapping = CompilerHelper::createTypeMapping(plan)
4648
variableBindings = CompilerHelper::cacheVariableBindings(plan, variableMapping, boundVariables.map[variableMapping.get(it)].toSet)
4749

48-
matchingFrame = getMatchingFrame(plan, pBody)
49-
bodyStub.matchingFrame = matchingFrame
50+
matchingFrame = getMatchingFrame(pBody)
5051

51-
patternBodyStub = bodyStub
52-
53-
CompilerHelper::createOperationsList(plan).forEach [
54-
compile(variableMapping)
55-
]
56-
return
52+
val searchOperations = CompilerHelper::createOperationsList(plan)
53+
.map[compile]
54+
.flatten
55+
.toList
56+
return new PatternBodyStub(pBody, matchingFrame, searchOperations)
5757
}
5858

59-
private def getMatchingFrame(SubPlan plan, PBody pBody) {
59+
private def getMatchingFrame(PBody pBody) {
6060
if(frameMap.containsKey(pBody)) {
6161
frameMap.get(pBody)
6262
} else {
63-
val tmpMatchingFrame = new MatchingFrameStub
64-
frameMap.put(pBody, tmpMatchingFrame)
65-
66-
67-
typeMapping.forEach [
68-
tmpMatchingFrame.addVariable($0, $1, variableMapping.get($0))
69-
]
70-
71-
plan.body.pattern.parameters.map[plan.body.getVariableByNameChecked(it.name)].forEach [
72-
tmpMatchingFrame.setVariableKey(it)
73-
]
74-
75-
plan.body.pattern.parameters.forEach[
76-
tmpMatchingFrame.mapParameterToVariable(it, plan.body.getVariableByNameChecked(it.name))
77-
]
78-
79-
tmpMatchingFrame
63+
val variableToParameterMap = Maps::uniqueIndex(pBody.pattern.parameters) [pBody.getVariableByNameChecked(it.name)]
64+
// don't pass this to anything else or evaluate it! (Lazy evaluation!!)
65+
val variableInfos = pBody.allVariables.map[
66+
new VariableInfo(Optional::fromNullable(variableToParameterMap.get(it)), it, typeMapping.get(it), variableMapping.get(it))
67+
].toList
68+
new MatchingFrameStub(variableInfos)
8069
}
8170
}
8271

83-
def compile(POperation pOperation, Map<PVariable, Integer> variableMapping) {
72+
def compile(POperation pOperation) {
8473
switch (pOperation) {
8574
PApply: {
8675
val pConstraint = pOperation.getPConstraint
8776

8877
if(pConstraint.allBound)
89-
createCheck(pConstraint, variableMapping)
78+
return createCheck(pConstraint)
9079
else
91-
createExtend(pConstraint, variableMapping)
80+
return createExtend(pConstraint)
9281

9382
}
9483
PStart: {
@@ -98,15 +87,17 @@ class POperationCompiler {
9887
default: { // TODO: throw an error
9988
}
10089
}
90+
return #[]
10191
}
10292

103-
def dispatch void createCheck(TypeConstraint constraint, Map<PVariable, Integer> variableMapping) {
93+
def dispatch createCheck(TypeConstraint constraint) {
94+
val operations = <SearchOperationStub>newArrayList
10495
val inputKey = constraint.supplierKey
10596

10697
switch (inputKey) {
10798
EClassTransitiveInstancesKey: {
10899
val variable = constraint.getVariableInTuple(0)
109-
patternBodyStub.addSearchOperation(new CheckInstanceOfStub(matchingFrame, variable, inputKey.wrappedKey))
100+
operations += new CheckInstanceOfStub(matchingFrame, variable, inputKey.wrappedKey)
110101
}
111102
EStructuralFeatureInstancesKey: {
112103
val src = constraint.getVariableInTuple(0)
@@ -116,26 +107,31 @@ class POperationCompiler {
116107

117108
switch (relationType) {
118109
case relationType.isOneToOne:
119-
patternBodyStub.addSearchOperation(new CheckSingleNavigationStub(matchingFrame, src, trg, inputKey.wrappedKey))
110+
operations += new CheckSingleNavigationStub(matchingFrame, src, trg, inputKey.wrappedKey)
120111
case relationType.isOneToMany:
121-
patternBodyStub.addSearchOperation(new CheckMultiNavigationStub(matchingFrame, src, trg, inputKey.wrappedKey))
112+
operations += new CheckMultiNavigationStub(matchingFrame, src, trg, inputKey.wrappedKey)
122113
}
123114
}
124115
}
116+
117+
return operations
125118
}
126119

127120
// def dispatch createCheck(CheckPConstraint constraint, Map<PVariable, Integer> variableMapping, String patternName) {
128121
// pattern.addSearchOperation(new CheckExpressionStub(matchingFrame, constraint.affectedVariables, constraint.expression))
129122
// }
130123

131-
def dispatch void createCheck(ExportedParameter constraint, Map<PVariable, Integer> variableMapping) {
124+
def dispatch createCheck(ExportedParameter constraint) {
132125
// nop
126+
#[]
133127
}
134128

135-
def dispatch void createCheck(PConstraint constraint, Map<PVariable, Integer> variableMapping) {
129+
def dispatch createCheck(PConstraint constraint) {
130+
#[]
136131
}
137132

138-
def dispatch void createExtend(TypeConstraint constraint, Map<PVariable, Integer> variableMapping) {
133+
def dispatch createExtend(TypeConstraint constraint) {
134+
val operations = <SearchOperationStub>newArrayList
139135
val inputKey = constraint.supplierKey
140136

141137
// TODO : this is wasteful
@@ -147,7 +143,7 @@ class POperationCompiler {
147143
switch (inputKey) {
148144
EClassTransitiveInstancesKey: {
149145
val variable = constraint.getVariableInTuple(0)
150-
patternBodyStub.addSearchOperation(new ExtendInstanceOfStub(matchingFrame, variable, inputKey.wrappedKey))
146+
operations += new ExtendInstanceOfStub(matchingFrame, variable, inputKey.wrappedKey)
151147
}
152148
EStructuralFeatureInstancesKey: {
153149
var src = constraint.getVariableInTuple(0)
@@ -163,25 +159,28 @@ class POperationCompiler {
163159
trg = tmp
164160
key = (key as EReference).EOpposite
165161
} else if (!fromBound && !toBound) {
166-
patternBodyStub.addSearchOperation(new ExtendInstanceOfStub(matchingFrame, src, inputKey.wrappedKey.EContainingClass))
162+
operations += new ExtendInstanceOfStub(matchingFrame, src, inputKey.wrappedKey.EContainingClass)
167163
}
168164

169165
switch (key) {
170166
case key.isOneToOne:
171-
patternBodyStub.addSearchOperation(new ExtendSingleNavigationStub(matchingFrame, src, trg, key))
167+
operations += new ExtendSingleNavigationStub(matchingFrame, src, trg, key)
172168
case key.isOneToMany:
173-
patternBodyStub.addSearchOperation(new ExtendMultiNavigationStub(matchingFrame, src, trg, key))
169+
operations += new ExtendMultiNavigationStub(matchingFrame, src, trg, key)
174170
}
175171
}
176172
}
173+
return operations
177174
}
178175

179-
def dispatch void createExtend(ExportedParameter constraint, Map<PVariable, Integer> variableMapping) {
176+
def dispatch createExtend(ExportedParameter constraint) {
180177
// nop
178+
#[]
181179
}
182180

183-
def dispatch void createExtend(PConstraint constraint, Map<PVariable, Integer> variableMapping) {
181+
def dispatch createExtend(PConstraint constraint) {
184182
println("Constraint type not yet implemented: " + constraint)
183+
#[]
185184
}
186185

187186
private def allBound(PConstraint pConstraint) {

plugins/hu.bme.mit.incquery.localsearch.cpp/src/hu/bme/mit/incquery/localsearch/cpp/generator/planner/PlanCompiler.xtend

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
package hu.bme.mit.incquery.localsearch.cpp.generator.planner
22

3+
import com.google.common.collect.ImmutableSet
4+
import com.google.common.collect.Iterables
35
import hu.bme.mit.incquery.localsearch.cpp.generator.model.PatternStub
46
import java.util.List
57
import org.apache.log4j.Logger
68
import org.eclipse.viatra.query.runtime.emf.EMFQueryMetaContext
79
import org.eclipse.viatra.query.runtime.matchers.psystem.annotations.ParameterReference
810
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PDisjunction
11+
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter
912
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery
1013
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.DefaultFlattenCallPredicate
1114
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.PBodyNormalizer
1215
import org.eclipse.viatra.query.runtime.matchers.psystem.rewriters.PQueryFlattener
13-
import hu.bme.mit.incquery.localsearch.cpp.generator.model.QueryStub
14-
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PParameter
1516

1617
class PlanCompiler {
1718

19+
val PQueryFlattener flattener
20+
val PBodyNormalizer normalizer
1821

1922
extension val CPPLocalSearchRuntimeBasedStrategy strategy
2023
extension val POperationCompiler compiler
2124

22-
2325
new () {
26+
this.flattener = new PQueryFlattener(new DefaultFlattenCallPredicate)
27+
this.normalizer = new PBodyNormalizer(null, false)
28+
2429
this.strategy = new CPPLocalSearchRuntimeBasedStrategy(false)
2530
this.compiler = new POperationCompiler
2631
}
2732

28-
def compilePlan(PQuery pQuery, QueryStub queryStub) {
33+
def compilePlan(PQuery pQuery) {
2934
val bindings = pQuery.allAnnotations.filter[name == "Bind"]
30-
bindings.forEach[ binding |
35+
val boundPatternStubs = bindings.map[ binding |
3136
val boundParameters = binding.getAllValues("parameters").map [
3237
switch (it) {
3338
ParameterReference: #[it]
@@ -37,33 +42,36 @@ class PlanCompiler {
3742
pQuery.parameters.get(pQuery.getPositionOfParameter(it.name))
3843
].toSet
3944

40-
val patternStub = queryStub.addPattern(pQuery, boundParameters)
41-
pQuery.compile(patternStub, boundParameters, false)
45+
val bodies = pQuery.compile(boundParameters, false)
46+
return new PatternStub(pQuery, bodies, boundParameters)
4247
]
4348

44-
val patternStub = queryStub.addPattern(pQuery)
45-
pQuery.compile(patternStub, #{}, true)
49+
val bodies = pQuery.compile(#{}, true)
50+
val unboundPatternStub = new PatternStub(pQuery, bodies)
51+
// copy to prevent lazy evaluation
52+
return ImmutableSet::copyOf(Iterables::concat(#[unboundPatternStub], boundPatternStubs))
4653
}
4754

48-
def compile(PQuery pQuery, PatternStub patternStub, Iterable<PParameter> boundParameters, boolean checkSanity) {
49-
val flattener = new PQueryFlattener(new DefaultFlattenCallPredicate)
50-
val normalizer = new PBodyNormalizer(null, false)
55+
def compile(PQuery pQuery, Iterable<PParameter> boundParameters, boolean checkSanity) {
5156

5257
val flatDisjunction = flattener.rewrite(pQuery.disjunctBodies)
5358
val normalizedDisjunction = normalizer.rewrite(flatDisjunction)
5459

5560
if (checkSanity && !normalizedDisjunction.sensibleWithoutBinding)
56-
return
61+
return #{}
5762

5863
val normalizedBodies = normalizedDisjunction.bodies
5964

60-
normalizedBodies.forEach[pBody |
61-
val boundPVariables = boundParameters.map[pBody.getVariableByNameChecked(name)].toSet
65+
val patternBodyStubs = normalizedBodies.map[pBody |
66+
val boundPVariables = boundParameters.map[pBody.getVariableByNameChecked(name)]
67+
.toSet
6268

63-
val patternBodyStub = patternStub.addPatternBody(pBody)
6469
pBody.plan(Logger::getLogger(PlanCompiler), boundPVariables, EMFQueryMetaContext.INSTANCE, null, #{})
65-
.compile(pBody, boundPVariables, patternBodyStub)
66-
]
70+
.compile(pBody, boundPVariables)
71+
72+
].toSet
73+
74+
return patternBodyStubs
6775
}
6876

6977
/**

plugins/hu.bme.mit.incquery.localsearch.cpp/src/hu/bme/mit/incquery/localsearch/cpp/generator/planner/util/CompilerHelper.xtend

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package hu.bme.mit.incquery.localsearch.cpp.generator.planner.util
22

3-
import com.google.common.base.Objects
43
import com.google.common.collect.Iterables
54
import com.google.common.collect.Lists
65
import com.google.common.collect.Maps
76
import com.google.common.collect.Sets
7+
import hu.bme.mit.incquery.localsearch.cpp.generator.model.TypeInfo
88
import java.util.List
99
import java.util.Map
1010
import java.util.Set
@@ -22,6 +22,7 @@ import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable
2222
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter
2323
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint
2424

25+
// TODO: help... please...
2526
class CompilerHelper {
2627

2728
static def Map<PVariable, Integer> createVariableMapping(SubPlan plan) {
@@ -89,17 +90,17 @@ class CompilerHelper {
8990
return variableBindings
9091
}
9192

92-
static def Map<PVariable, TypeMap> createTypeMapping(SubPlan plan) {
93-
val Map<PVariable, TypeMap> typeMapping = Maps::newHashMap()
93+
static def Map<PVariable, TypeInfo> createTypeMapping(SubPlan plan) {
94+
val Map<PVariable, TypeInfo> typeMapping = Maps::newHashMap()
9495
var Set<PVariable> allVarialbes = plan.getAllEnforcedConstraints().map[getAffectedVariables].flatten.toSet
9596
allVarialbes.forEach[pVar |
9697
var EClass leastStrictType = getLeastStrictType(pVar)
9798
if (leastStrictType !== null) {
98-
typeMapping.put(pVar, new TypeMap(leastStrictType, getStrictestType(pVar)))
99+
typeMapping.put(pVar, new TypeInfo(leastStrictType, getStrictestType(pVar)))
99100
} else {
100101
var EDataType primitiveType = getPrimitiveType(pVar)
101102
if (primitiveType !== null) {
102-
typeMapping.put(pVar, new TypeMap(primitiveType, primitiveType))
103+
typeMapping.put(pVar, new TypeInfo(primitiveType, primitiveType))
103104
}
104105
}
105106
]
@@ -202,29 +203,5 @@ class CompilerHelper {
202203
}
203204
operationsList.add(plan.getOperation())
204205
return Lists::reverse(operationsList)
205-
}
206-
207-
static class TypeMap {
208-
final EClassifier looseType
209-
final EClassifier strictType
210-
211-
def EClassifier getLooseType() {
212-
return looseType
213-
}
214-
215-
def EClassifier getStrictType() {
216-
return strictType
217-
}
218-
219-
new(EClassifier looseType, EClassifier strictType) {
220-
super()
221-
this.looseType = looseType
222-
this.strictType = strictType
223-
}
224-
225-
override String toString() {
226-
return Objects::toStringHelper(this).add("looseType", looseType.getName()).add("strictType",
227-
strictType.getName()).toString()
228-
}
229-
}
206+
}
230207
}

0 commit comments

Comments
 (0)