aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternGenerator.xtend
blob: 677170b8ab1720712218b8baa10fa155050b97b9 (plain) (blame)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns

import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.InverseRelationAssertion
import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.LowerMultiplicityAssertion
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDefinition
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.StringTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.DefinedByDerivedFeature
import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.TransfomedViatraQuery
import hu.bme.mit.inf.dslreasoner.viatra2logic.viatra2logicannotations.TransformedViatraWellformednessConstraint
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeAnalysisResult
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.TypeInferenceMethod
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import java.util.HashMap
import java.util.Map
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EReference
import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery
import org.eclipse.xtend.lib.annotations.Accessors

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint

@Data class PatternGeneratorResult {
	CharSequence patternText
	HashMap<PConstraint,String> constraint2MustPreconditionName
	HashMap<PConstraint,String> constraint2CurrentPreconditionName
}

class PatternGenerator {
	@Accessors(PUBLIC_GETTER) val TypeIndexer typeIndexer //= new TypeIndexer(this)
	@Accessors(PUBLIC_GETTER) val RelationDeclarationIndexer relationDeclarationIndexer = new RelationDeclarationIndexer(this)
	@Accessors(PUBLIC_GETTER) val RelationDefinitionIndexer relationDefinitionIndexer = new RelationDefinitionIndexer(this)
	@Accessors(PUBLIC_GETTER) val ContainmentIndexer containmentIndexer = new ContainmentIndexer(this)
	@Accessors(PUBLIC_GETTER) val InvalidIndexer invalidIndexer = new InvalidIndexer(this)
	@Accessors(PUBLIC_GETTER) val UnfinishedIndexer unfinishedIndexer = new UnfinishedIndexer(this)
	@Accessors(PUBLIC_GETTER) val TypeRefinementGenerator typeRefinementGenerator //= new RefinementGenerator(this)
	@Accessors(PUBLIC_GETTER) val RelationRefinementGenerator relationRefinementGenerator = new RelationRefinementGenerator(this)
	@Accessors(PUBLIC_GETTER) val UnitPropagationPreconditionGenerator unitPropagationPreconditionGenerator = new UnitPropagationPreconditionGenerator(this)
	
	public new(TypeInferenceMethod typeInferenceMethod) {
		if(typeInferenceMethod == TypeInferenceMethod.Generic) {
			this.typeIndexer = new GenericTypeIndexer(this)
			this.typeRefinementGenerator = new GenericTypeRefinementGenerator(this)
		} else if(typeInferenceMethod == TypeInferenceMethod.PreliminaryAnalysis) {
			this.typeIndexer = new TypeIndexerWithPreliminaryTypeAnalysis(this)
			this.typeRefinementGenerator = new TypeRefinementWithPreliminaryTypeAnalysis(this)
		} else {
			this.typeIndexer = null
			this.typeRefinementGenerator = null
			throw new IllegalArgumentException('''Unknown type indexing technique : «typeInferenceMethod.name»''')
		}
	}
	
	public def requiresTypeAnalysis() {
		typeIndexer.requiresTypeAnalysis || typeRefinementGenerator.requiresTypeAnalysis
	}
	
	public dispatch def referRelation(
		RelationDeclaration referred,
		String sourceVariable,
		String targetVariable,
		Modality modality,
		Map<String,PQuery> fqn2PQuery)
	{
		return this.relationDeclarationIndexer.referRelation(referred,sourceVariable,targetVariable,modality)
	}
	public dispatch def referRelation(
		RelationDefinition referred,
		String sourceVariable,
		String targetVariable,
		Modality modality,
		Map<String,PQuery> fqn2PQuery)
	{
		val pattern = referred.annotations.filter(TransfomedViatraQuery).head.patternFullyQualifiedName.lookup(fqn2PQuery)
		return this.relationDefinitionIndexer.referPattern(pattern,#[sourceVariable,targetVariable],modality,true,false)
	}
	
	def public referRelationByName(EReference reference,
		String sourceVariable,
		String targetVariable,
		Modality modality)
	{
		'''find «modality.name.toLowerCase»InRelation«canonizeName('''«reference.name» reference «reference.EContainingClass.name»''')
		»(problem,interpretation,«sourceVariable»,«targetVariable»);'''
	}
	
	def public CharSequence referAttributeByName(EAttribute attribute,
		String sourceVariable,
		String targetVariable,
		Modality modality)
	{
		'''find «modality.name.toLowerCase»InRelation«canonizeName('''«attribute.name» attribute «attribute.EContainingClass.name»''')
		»(problem,interpretation,«sourceVariable»,«targetVariable»);'''
	}
	
	public def canonizeName(String name) {
		name.split(' ').join('_')
	}
	
	public def lowerMultiplicities(LogicProblem problem) {
		problem.assertions.map[annotations].flatten.filter(LowerMultiplicityAssertion).filter[!it.relation.isDerived]
	}
	public def wfQueries(LogicProblem problem) {
		problem.assertions.map[it.annotations]
			.flatten
			.filter(TransformedViatraWellformednessConstraint)
			.map[it.query]
	}
	public def getContainments(LogicProblem p) {
		return p.containmentHierarchies.head.containmentRelations
	}
	public def getInverseRelations(LogicProblem p) {
		val inverseRelations = new HashMap
		p.annotations.filter(InverseRelationAssertion).forEach[
			inverseRelations.put(it.inverseA,it.inverseB)
			inverseRelations.put(it.inverseB,it.inverseA)
		]
		return inverseRelations
	}
	public def isRepresentative(Relation relation, Relation inverse) {
		if(inverse == null) {
			return true
		} else {
			relation.name.compareTo(inverse.name)<1
		}
	}
	
	public def isDerived(Relation relation) {
		relation.annotations.exists[it instanceof DefinedByDerivedFeature]
	}
	public def getDerivedDefinition(RelationDeclaration relation) {
		relation.annotations.filter(DefinedByDerivedFeature).head.query
	}
	
	private def allTypeReferences(LogicProblem problem) {
		problem.eAllContents.filter(TypeReference).toIterable
	}
	protected def hasBoolean(LogicProblem problem) {
		problem.allTypeReferences.exists[it instanceof BoolTypeReference]
	}
	protected def hasInteger(LogicProblem problem) {
		problem.allTypeReferences.exists[it instanceof IntTypeReference]
	}
	protected def hasReal(LogicProblem problem) {
		problem.allTypeReferences.exists[it instanceof RealTypeReference]
	}
	protected def hasString(LogicProblem problem) {
		problem.allTypeReferences.exists[it instanceof StringTypeReference]
	}
	
	public def PatternGeneratorResult transformBaseProperties(
		LogicProblem problem,
		PartialInterpretation emptySolution,
		Map<String,PQuery> fqn2PQuery,
		TypeAnalysisResult typeAnalysisResult
	) {
		val first = 
		 '''
			import epackage "http://www.bme.hu/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage"
			import epackage "http://www.bme.hu/mit/inf/dslreasoner/logic/model/problem"
			import epackage "http://www.bme.hu/mit/inf/dslreasoner/logic/model/language"
			
			//////////
			// 0. Util
			//////////
			private pattern interpretation(problem:LogicProblem, interpretation:PartialInterpretation) {
				PartialInterpretation.problem(interpretation,problem);
			}
				
			/////////////////////////
			// 0.1 Existence
			/////////////////////////
			/** [[exist(element)]]=1 */
			private pattern mustExist(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpretation);
				LogicProblem.elements(problem,element);
			} or {
				find interpretation(problem,interpretation);
				PartialInterpretation.newElements(interpretation,element);
			}
			
			/** [[exist(element)]]>=1/2 */
			private pattern mayExist(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
			    find mustExist(problem,interpretation,element);
			} or {
			    find interpretation(problem,interpretation);
			    neg find elementCloseWorld(element);
			    PartialInterpretation.openWorldElements(interpretation,element);
			}
			
			private pattern elementCloseWorld(element:DefinedElement) {
				PartialInterpretation.openWorldElements(i,element);
			    PartialInterpretation.maxNewElements(i,0);
			} or {
				Scope.targetTypeInterpretation(scope,interpretation);
				PartialTypeInterpratation.elements(interpretation,element);
				Scope.maxNewElements(scope,0);
			}
			
			////////////////////////
			// 0.2 Equivalence
			////////////////////////
			pattern mayEquivalent(problem:LogicProblem, interpretation:PartialInterpretation, a: DefinedElement, b: DefinedElement)
			// For non-primitive type. Boolean types always must equivalent or cannot equivalent
			{
				find mayExist(problem,interpretation,a);
				find mayExist(problem,interpretation,b);
				a == b;
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				IntegerElement(a);
«««				IntegerElement(b);
«««				PrimitiveElement.valueSet(a,false);
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				IntegerElement(a);
«««				IntegerElement(b);
«««				PrimitiveElement.valueSet(b,false);
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				RealElement(a);
«««				RealElement(b);
«««				PrimitiveElement.valueSet(a,false);
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				RealElement(a);
«««				RealElement(b);
«««				PrimitiveElement.valueSet(b,false);
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				StringElement(a);
«««				StringElement(b);
«««				PrimitiveElement.valueSet(a,false);
«««			} or {
«««				find mayExist(problem,interpretation,a);
«««				find mayExist(problem,interpretation,b);
«««				StringElement(a);
«««				StringElement(b);
«««				PrimitiveElement.valueSet(b,false);
			}
			
			pattern mustEquivalent(problem:LogicProblem, interpretation:PartialInterpretation, a: DefinedElement, b: DefinedElement) {
			// For non-primitive and Boolean primitive type
				find mustExist(problem,interpretation,a);
				find mustExist(problem,interpretation,b);
				a == b;
«««			} or {
«««				find mustExist(problem,interpretation,a);
«««				find mustExist(problem,interpretation,b);
«««				PrimitiveElement.valueSet(a,true);
«««				PrimitiveElement.valueSet(b,true);
«««				IntegerElement.value(a,value);
«««				IntegerElement.value(b,value);
«««			} or {
«««				find mustExist(problem,interpretation,a);
«««				find mustExist(problem,interpretation,b);
«««				PrimitiveElement.valueSet(a,true);
«««				PrimitiveElement.valueSet(b,true);
«««				RealElement.value(a,value);
«««				RealElement.value(b,value);
«««			} or {
«««				find mustExist(problem,interpretation,a);
«««				find mustExist(problem,interpretation,b);
«««				PrimitiveElement.valueSet(a,true);
«««				PrimitiveElement.valueSet(b,true);
«««				StringElement.value(a,value);
«««				StringElement.value(b,value);
			}
			
			//////////
			// 1. Problem-Specific Base Indexers
			//////////
			// 1.1 Type Indexers
			//////////
			// 1.1.1 Required Patterns by TypeIndexer
			//////////
			«typeIndexer.requiredQueries»
			//////////
			// 1.1.2 primitive Type Indexers
			//////////
			// Currently unused. Refer primitive types as:
			// > PrimitiveElement(element)
			// specific types are referred as:
			// > BooleanElement(variableName)
			// > IntegerElement(variableName)
			// > RealElement(variableName)
			// > StringElement(variableName)
			// And their value as
			// > BooleanElement.value(variableName,value)
			// > IntegerElement.value(variableName,value)
			// > RealElement.value(variableName,value)
			// > StringElement.value(variableName,value)
			// Whether a value is set is defined by:
			// > PrimitiveElement.valueSet(variableName,isFilled);
			//////////
			// 1.1.3 domain-specific Type Indexers
			//////////
			«typeIndexer.generateInstanceOfQueries(problem,emptySolution,typeAnalysisResult)»
			
			//////////
			// 1.2 Relation Declaration Indexers
			//////////
			«relationDeclarationIndexer.generateRelationIndexers(problem,problem.relations.filter(RelationDeclaration),fqn2PQuery)»
			
			//////////
			// 1.3 Relation Definition Indexers
			//////////
			«relationDefinitionIndexer.generateRelationDefinitions(problem,problem.relations.filter(RelationDefinition),fqn2PQuery)»
			
			//////////
			// 1.4 Containment Indexer
			//////////
			«containmentIndexer.transformContainment(problem,problem.relations,fqn2PQuery)»
			
			//////////
			// 2. Invalidation Indexers
			//////////
			// 2.1 Invalidated by WF Queries
			//////////
			«invalidIndexer.generateInvalidatedByWfQueries(problem,fqn2PQuery)»
			
			//////////
			// 3. Unfinishedness Indexers
			//////////
			// 3.1 Unfinishedness Measured by Multiplicity
			//////////
			«unfinishedIndexer.generateUnfinishedMultiplicityQueries(problem,fqn2PQuery)»
			
			//////////
			// 3.2 Unfinishedness Measured by WF Queries
			//////////
			«unfinishedIndexer.generateUnfinishedWfQueries(problem,fqn2PQuery)»
			
			//////////
			// 4. Refinement Indexers
			//////////
			// 4.1 Object constructors
			//////////
			«typeRefinementGenerator.generateRefineObjectQueries(problem,emptySolution,typeAnalysisResult)»
			
			//////////
			// 4.2 Type refinement
			//////////
			«typeRefinementGenerator.generateRefineTypeQueries(problem,emptySolution,typeAnalysisResult)»
			
			//////////
			// 4.3 Relation refinement
			//////////
			«relationRefinementGenerator.generateRefineReference(problem)»
			
			//////////
			// 5 Unit Propagations
			//////////
			'''
			val up = unitPropagationPreconditionGenerator.generateUnitPropagationRules(problem,problem.relations.filter(RelationDefinition),fqn2PQuery)
			val second = up.definitions
			return new PatternGeneratorResult(first+second,up.constraint2MustPreconditionName,up.constraint2CurrentPreconditionName)
	}
}