1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableSet
import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.TransfomedViatraQuery
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.CbcPolyhedronSolver
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.LinearTypeConstraintHint
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.MultiplicityGoalConstraintCalculator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.PolyhedronScopePropagator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.RelationConstraintCalculator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagatorStrategy
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.TypeHierarchyScopePropagator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.Z3PolyhedronSolver
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.GeneratedPatterns
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.ModalPatternQueries
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternProvider
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.UnitPropagationPatternGenerator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules.GoalConstraintProvider
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules.RefinementRuleProvider
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
import java.util.Collection
import java.util.List
import java.util.Map
import java.util.Set
import org.eclipse.viatra.query.runtime.api.GenericQueryGroup
import org.eclipse.viatra.query.runtime.api.IPatternMatch
import org.eclipse.viatra.query.runtime.api.IQuerySpecification
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.query.runtime.emf.EMFScope
import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule
import org.eclipse.xtend.lib.annotations.Data
class ModelGenerationStatistics {
public var long transformationExecutionTime = 0
synchronized def addExecutionTime(long amount) {
transformationExecutionTime += amount
}
public var long scopePropagationTime = 0
synchronized def addScopePropagationTime(long amount) {
scopePropagationTime += amount
}
public var long preliminaryTypeAnalisisTime = 0
public var int decisionsTried = 0
synchronized def incrementDecisionCount() {
decisionsTried++
}
public var int transformationInvocations
synchronized def incrementTransformationCount() {
transformationInvocations++
}
public var int scopePropagatorInvocations
synchronized def incrementScopePropagationCount() {
scopePropagatorInvocations++
}
public var int scopePropagatorSolverInvocations
synchronized def incrementScopePropagationSolverCount() {
scopePropagatorSolverInvocations++
}
}
@Data class ModelGenerationMethod {
ModelGenerationStatistics statistics
Collection<? extends BatchTransformationRule<?, ?>> objectRefinementRules
Collection<? extends BatchTransformationRule<?, ?>> relationRefinementRules
List<MultiplicityGoalConstraintCalculator> unfinishedMultiplicities
Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> unfinishedWF
Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> invalidWF
Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustUnitPropagationPreconditions
Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> currentUnitPropagationPreconditions
Map<String, ModalPatternQueries> modalRelationQueries
Collection<? extends IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> allPatterns
}
enum TypeInferenceMethod {
Generic,
PreliminaryAnalysis
}
class ModelGenerationMethodProvider {
val PatternProvider patternProvider = new PatternProvider
val RefinementRuleProvider refinementRuleProvider = new RefinementRuleProvider
val GoalConstraintProvider goalConstraintProvider = new GoalConstraintProvider
val relationConstraintCalculator = new RelationConstraintCalculator
def ModelGenerationMethod createModelGenerationMethod(
LogicProblem logicProblem,
PartialInterpretation emptySolution,
ReasonerWorkspace workspace,
boolean nameNewElements,
TypeInferenceMethod typeInferenceMethod,
boolean calculateObjectCreationCosts,
ScopePropagatorStrategy scopePropagatorStrategy,
Collection<LinearTypeConstraintHint> hints,
Collection<UnitPropagationPatternGenerator> unitPropagationPatternGenerators,
DocumentationLevel debugLevel
) {
val statistics = new ModelGenerationStatistics
val writeFiles = (debugLevel === DocumentationLevel.NORMAL || debugLevel === DocumentationLevel.FULL)
val Set<PQuery> existingQueries = logicProblem.relations.map[annotations].flatten.filter(TransfomedViatraQuery).
map[it.patternPQuery as PQuery].toSet
val relationConstraints = relationConstraintCalculator.calculateRelationConstraints(logicProblem)
val queries = patternProvider.generateQueries(logicProblem, emptySolution, statistics, existingQueries,
workspace, typeInferenceMethod, scopePropagatorStrategy, relationConstraints, hints,
unitPropagationPatternGenerators, writeFiles)
val scopePropagator = createScopePropagator(scopePropagatorStrategy, emptySolution, hints, queries, statistics)
scopePropagator.propagateAllScopeConstraints
val objectRefinementRules = refinementRuleProvider.createObjectRefinementRules(logicProblem, emptySolution,
queries, scopePropagator, nameNewElements, statistics)
val relationRefinementRules = refinementRuleProvider.createRelationRefinementRules(queries, scopePropagator,
statistics)
val unfinishedMultiplicities = goalConstraintProvider.getUnfinishedMultiplicityQueries(logicProblem, queries,
calculateObjectCreationCosts)
val unfinishedWF = queries.getUnfinishedWFQueries.values
val modalRelationQueriesBuilder = ImmutableMap.builder
for (entry : queries.modalRelationQueries.entrySet) {
val annotation = entry.key.annotations.filter(TransfomedViatraQuery).head
if (annotation !== null) {
modalRelationQueriesBuilder.put(annotation.patternFullyQualifiedName, entry.value)
}
}
val modalRelationQueries = modalRelationQueriesBuilder.build
val invalidWF = queries.getInvalidWFQueries.values
val mustUnitPropagationPreconditions = queries.getMustUnitPropagationPreconditionPatterns
val currentUnitPropagationPreconditions = queries.getCurrentUnitPropagationPreconditionPatterns
val queriesToPrepare = ImmutableSet.builder.addAll(queries.refineObjectQueries.values).addAll(
queries.refineTypeQueries.values).addAll(queries.refinerelationQueries.values).addAll(queries.
multiplicityConstraintQueries.values.flatMap[allQueries]).addAll(queries.unfinishedWFQueries.values).addAll(
queries.invalidWFQueries.values).addAll(queries.mustUnitPropagationPreconditionPatterns.values).addAll(
queries.currentUnitPropagationPreconditionPatterns.values).add(queries.hasElementInContainmentQuery).build
val queryEngine = ViatraQueryEngine.on(new EMFScope(emptySolution))
GenericQueryGroup.of(queriesToPrepare).prepare(queryEngine)
return new ModelGenerationMethod(
statistics,
objectRefinementRules.values,
relationRefinementRules.values,
unfinishedMultiplicities,
unfinishedWF,
invalidWF,
mustUnitPropagationPreconditions,
currentUnitPropagationPreconditions,
modalRelationQueries,
queries.allQueries
)
}
private def createScopePropagator(ScopePropagatorStrategy scopePropagatorStrategy,
PartialInterpretation emptySolution, Collection<LinearTypeConstraintHint> hints, GeneratedPatterns queries,
ModelGenerationStatistics statistics) {
if (!hints.empty && !(scopePropagatorStrategy instanceof ScopePropagatorStrategy.Polyhedral)) {
throw new IllegalArgumentException("Only the Polyhedral scope propagator strategy can use hints.")
}
switch (scopePropagatorStrategy) {
case ScopePropagatorStrategy.None,
case ScopePropagatorStrategy.Basic:
new ScopePropagator(emptySolution, statistics)
case ScopePropagatorStrategy.BasicTypeHierarchy:
new TypeHierarchyScopePropagator(emptySolution, statistics)
ScopePropagatorStrategy.Polyhedral: {
val types = queries.refineObjectQueries.keySet.map[newType].toSet
val allPatternsByName = queries.allQueries.toMap[fullyQualifiedName]
val solver = switch (scopePropagatorStrategy.solver) {
case Z3Integer:
new Z3PolyhedronSolver(false, scopePropagatorStrategy.timeoutSeconds)
case Z3Real:
new Z3PolyhedronSolver(true, scopePropagatorStrategy.timeoutSeconds)
case Cbc:
new CbcPolyhedronSolver(false, scopePropagatorStrategy.timeoutSeconds, true)
case Clp:
new CbcPolyhedronSolver(true, scopePropagatorStrategy.timeoutSeconds, true)
default:
throw new IllegalArgumentException("Unknown polyhedron solver: " +
scopePropagatorStrategy.solver)
}
new PolyhedronScopePropagator(emptySolution, statistics, types, queries.multiplicityConstraintQueries,
queries.hasElementInContainmentQuery, allPatternsByName, hints, solver,
scopePropagatorStrategy.requiresUpperBoundIndexing, scopePropagatorStrategy.updateHeuristic)
}
default:
throw new IllegalArgumentException("Unknown scope propagator strategy: " + scopePropagatorStrategy)
}
}
}
|