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: 34094cac4a08c4508fc50ecdae87065ab2ee64e3 (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
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.*

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)
	
	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 transformBaseProperties(
		LogicProblem problem,
		PartialInterpretation emptySolution,
		Map<String,PQuery> fqn2PQuery,
		TypeAnalysisResult typeAnalysisResult
	) {
		
		return '''
			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, interpetation:PartialInterpretation) {
				PartialInterpretation.problem(interpetation,problem);
			}
				
			/////////////////////////
			// 0.1 Existence
			/////////////////////////
			private pattern mustExist(problem:LogicProblem, interpetation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpetation);
				LogicProblem.elements(problem,element);
			} or {
				find interpretation(problem,interpetation);
				PartialInterpretation.newElements(interpetation,element);
			}
			«IF problem.hasBoolean»or {
				find interpretation(problem,interpetation);
				PartialInterpretation.booleanelements(interpetation,element);
ENDIF»
			«IF problem.hasInteger»or {
				find interpretation(problem,interpetation);
				PartialInterpretation.integerelements(interpetation,element);
ENDIF»
			«IF problem.hasReal»or {
				find interpretation(problem,interpetation);
				PartialInterpretation.realelements(interpetation,element);
ENDIF»
			«IF problem.hasString»or {
				find interpretation(problem,interpetation);
				PartialInterpretation.stringelements(interpetation,element);
ENDIF»
			
			private pattern mayExist(problem:LogicProblem, interpetation:PartialInterpretation, element:DefinedElement) {
			    find mustExist(problem,interpetation,element);
			} or {
			    find interpretation(problem,interpetation);
			    neg find elementCloseWorld(interpetation);
			    PartialInterpretation.openWorldElementPrototypes(interpetation,element);
			}
			«IF problem.hasInteger»or {
				find interpretation(problem,interpetation);
				neg find integerCloseWorld(interpetation);
				PartialInterpretation.newIntegers(interpetation,element);
ENDIF»
			«IF problem.hasReal»or {
				find interpretation(problem,interpetation);
				neg find realCloseWorld(interpetation);
				PartialInterpretation.newReals(interpetation,element);
ENDIF»
			«IF problem.hasString»or {
				find interpretation(problem,interpetation);
				neg find stringCloseWorld(interpetation);
				PartialInterpretation.newStrings(interpetation,element);
ENDIF»
			
			private pattern elementCloseWorld(interpetation:PartialInterpretation) {
			    PartialInterpretation.maxNewElements(interpetation,0);
			}
			«IF problem.hasInteger»private pattern integerCloseWorld(interpetation:PartialInterpretation) {
			    PartialInterpretation.maxNewIntegers(interpetation,0);
ENDIF»
			«IF problem.hasReal»private pattern realCloseWorld(interpetation:PartialInterpretation) {
			    PartialInterpretation.maxNewReals(interpetation,0);
ENDIF»
			«IF problem.hasString»private pattern stringCloseWorld(interpetation:PartialInterpretation) {
			    PartialInterpretation.maxNewStrings(interpetation,0);
ENDIF»
			
			////////////////////////
			// 0.2 Equivalence
			////////////////////////
			pattern mayEquivalent(problem:LogicProblem, interpretation:PartialInterpretation, a: DefinedElement, b: DefinedElement) {
				find mayExist(problem,interpretation,a);
				find mayExist(problem,interpretation,b);
				a == b;
			}
			
			////////////////////////
			// 0.3 Required Patterns by TypeIndexer
			////////////////////////
			«typeIndexer.requiredQueries»
			
			//////////
			// 1. Problem-Specific Base Indexers
			//////////
			// 1.1 Type Indexers
			//////////
			// 1.1.1 primitive Type Indexers
			//////////
			pattern instanceofBoolean(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpretation);
				PartialInterpretation.booleanelements(interpretation,element);
			}
			pattern instanceofInteger(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpretation);
				PartialInterpretation.integerelements(interpretation,element);
			} or {
				find interpretation(problem,interpretation);
				PartialInterpretation.newIntegers(interpetation,element);
			}
			pattern instanceofReal(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpretation);
				PartialInterpretation.realelements(interpretation,element);
			} or {
				find interpretation(problem,interpretation);
				PartialInterpretation.newReals(interpetation,element);
			}
			pattern instanceofString(problem:LogicProblem, interpretation:PartialInterpretation, element:DefinedElement) {
				find interpretation(problem,interpretation);
				PartialInterpretation.stringelements(interpretation,element);
			} or {
				find interpretation(problem,interpretation);
				PartialInterpretation.newStrings(interpetation,element);
			}
			
			//////////
			// 1.1.2 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)»
			'''
	}
}