aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/PartialInterpretationInitialiser.xtend
blob: 28fc6e311d603054e770d4f90a88d1b2940d6f88 (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
package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage

import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput
import hu.bme.mit.inf.dslreasoner.logic.model.builder.TypeScopes
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.And
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.ComplexTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Exists
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.IntTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguageFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealTypeReference
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.StringLiteral
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.SymbolicValue
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDefinition
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.logic.model.patterns.SupertypeStar
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partial2logicannotations.PartialModelRelation2Assertion
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BooleanElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialBooleanInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialIntegerInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRealInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRelationInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialStringInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.StringElement
import java.util.HashMap
import java.util.Map
import java.util.SortedSet
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.emf.EMFScope
import org.eclipse.xtend.lib.annotations.Data

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeReference

@Data class Problem2PartialInterpretationTrace {
	Map<TypeDeclaration, PartialComplexTypeInterpretation> type2Interpretation
	PrimitiveValueTrace primitiveValues
	Map<RelationDeclaration, PartialRelationInterpretation> relation2Interpretation
}
@Data class PrimitiveValueTrace {
	PartialBooleanInterpretation booleanInterpretation
	Map<Boolean,BooleanElement> booleanMap
	PartialIntegerInterpretation integerInterpretation
	Map<Integer,IntegerElement> integerMap
	PartialRealInterpretation realInterpretation
	Map<Double,RealElement> realMap
	PartialStringInterpretation stringInterpretation
	Map<String,StringElement> stringMap
}

/**
 * Initializer class for an empty partial interpretation.
 */
class PartialInterpretationInitialiser {
	val extension PartialinterpretationFactory factory = PartialinterpretationFactory.eINSTANCE
	val extension LogiclanguageFactory factory2 = LogiclanguageFactory.eINSTANCE
	
	/**
	 * Initialises an empty partial interpretation from a logic problem and a scope.
	 */
	def TracedOutput<PartialInterpretation,Problem2PartialInterpretationTrace> initialisePartialInterpretation(
		LogicProblem problem,
		TypeScopes typeScopes) 
	{
		val res = createPartialInterpretation
		res.problem = problem
			
		// Initialise primitive elements
		val booleanType = initBooleans(res)
		val integerType = initIntegers(res, typeScopes.knownIntegers, typeScopes.minNewIntegers, typeScopes.maxNewIntegers)
		val realType = initReals(res, typeScopes.knownReals, typeScopes.minNewReals, typeScopes.maxNewReals)
		val stringType= initStrings(res,typeScopes.knownStrings, typeScopes.minNewStrings, typeScopes.maxNewStrings)
		val primitiveTrace = createPrimitiveTrace(booleanType, integerType, realType, stringType)
		// Initialise complex elements
		
		val type2Interpretation = initElements(res,
			typeScopes.minNewElementsByType,
			typeScopes.maxNewElementsByType,
			typeScopes.minNewElements,
			typeScopes.maxNewElements)
			
		// Initialise relations
		val relation2Interpretation = initRelations(res,primitiveTrace)
		
		val trace = new Problem2PartialInterpretationTrace(type2Interpretation,primitiveTrace,relation2Interpretation)
		return new TracedOutput(res,trace)
	}
	
	def createPrimitiveTrace(
		PartialBooleanInterpretation booleanType,
		PartialIntegerInterpretation integerType,
		PartialRealInterpretation realType,
		PartialStringInterpretation stringType)
	{
		return new PrimitiveValueTrace(
			booleanType,
			booleanType.elements.filter(BooleanElement).filter[it.isValueSet].toMap[value],
			integerType,
			integerType.elements.filter(IntegerElement).filter[it.isValueSet].toMap[value],
			realType,
			realType.elements.filter(RealElement).filter[it.isValueSet].toMap[value],
			stringType,
			stringType.elements.filter(StringElement).filter[it.isValueSet].toMap[value]
		)
	}
	
	def protected initBooleans(PartialInterpretation partialInterpretation) {
		val booleanInterpretation = createPartialBooleanInterpretation
		partialInterpretation.partialtypeinterpratation += booleanInterpretation
		
		val trueElement = createBooleanElement => [it.name = "true" it.value = true it.valueSet = true]
		booleanInterpretation.elements += trueElement
		partialInterpretation.newElements+=trueElement
		
		val falseElement = createBooleanElement => [it.name = "false" it.value = false it.valueSet = true]
		booleanInterpretation.elements += falseElement
		partialInterpretation.newElements+=falseElement
		
		return booleanInterpretation
	}
	
	def protected initIntegers(PartialInterpretation partialInterpretation, SortedSet<Integer> knownIntegers, int minNewIntegers, int maxNewIntegers) {
		val integerInterpretation = createPartialIntegerInterpretation
		partialInterpretation.partialtypeinterpratation += integerInterpretation
		
		for(knownInteger : knownIntegers) {
			val integerElement = createIntegerElement => [it.name = knownInteger.toString it.value = knownInteger it.valueSet = true]
			integerInterpretation.elements += integerElement
			partialInterpretation.newElements += integerElement
		}
		if(maxNewIntegers>0) {
			val newElements = createIntegerElement => [it.name = "New Integers" it.valueSet = false]
			partialInterpretation.openWorldElements += newElements
			integerInterpretation.elements += newElements
		}
		
		return integerInterpretation
	}
	
	def protected initReals(PartialInterpretation partialInterpretation, SortedSet<Double> knownReals, int minNewReals, int maxNewReals) {
		val realInterpretation = createPartialRealInterpretation
		partialInterpretation.partialtypeinterpratation += realInterpretation
		
		for(knownReal : knownReals) {
			val realElement = createRealElement => [it.name = knownReal.toString it.value = knownReal it.valueSet = true]
			realInterpretation.elements += realElement
			partialInterpretation.newElements += realElement
		}
		if(maxNewReals>0) {
			val newElements = createRealElement => [it.name = "New Reals" it.valueSet = false]
			partialInterpretation.openWorldElements += newElements
			realInterpretation.elements += newElements
		}
		return realInterpretation
	}
	
	def protected initStrings(PartialInterpretation partialInterpretation, SortedSet<String> knownStrings, int minNewStrings, int maxNewStrings) {
		val stringInterpretation = createPartialStringInterpretation
		partialInterpretation.partialtypeinterpratation += stringInterpretation
		
		for(knownString : knownStrings) {
			val stringElement = createStringElement => [it.name = '''"«knownString»"''' it.value = knownString it.valueSet = true]
			stringInterpretation.elements += stringElement
			partialInterpretation.newElements += stringElement
		}
		if(maxNewStrings>0) {
			val newElements = createStringElement => [it.name = "New Strings" it.valueSet = false]
			partialInterpretation.openWorldElements += newElements
			stringInterpretation.elements += newElements
		}
		
		return stringInterpretation
	}
	
	def protected initElements(PartialInterpretation interpretation,
		Map<Type, Integer> minNewElementsByType, Map<Type, Integer> maxNewElementsByType,
		int minNewElements, int maxNewElements)
	{
		val Map<TypeDeclaration, PartialComplexTypeInterpretation> type2Interpretation = new HashMap
		val engine = ViatraQueryEngine.on(new EMFScope(interpretation.problem))
		// Elements
		interpretation.minNewElements = minNewElements
		interpretation.maxNewElements = maxNewElements
		// elements from problem are included
		if(maxNewElements != 0) {
			val newElements = createDefinedElement => [it.name = "New Objects"]
			interpretation.openWorldElements += newElements 
		}
		
		for(typeDeclaration : interpretation.problem.types.filter(TypeDeclaration)) {
			val typeInterpretation = typeDeclaration.initialisePartialTypeInterpretation(engine)
			interpretation.partialtypeinterpratation += typeInterpretation
			type2Interpretation.put(typeDeclaration,typeInterpretation)
			interpretation.scopes += initialiseTypeScope(typeInterpretation, minNewElementsByType.get(typeDeclaration),maxNewElementsByType.get(typeDeclaration))
			
		}
		interpretation.problem.connectSuperypes(type2Interpretation)
		return type2Interpretation
	}
	
	def private initialiseTypeScope(PartialTypeInterpratation interpretation, Integer min, Integer max) {
		val res = createScope
		res.targetTypeInterpretation = interpretation
		res.minNewElements = min ?: 0
		res.maxNewElements = max ?: -1
		return res
	}
	
	def private connectSuperypes(LogicProblem problem, Map<TypeDeclaration, PartialComplexTypeInterpretation> trace) {
		for(typeDeclaration : problem.types.filter(TypeDeclaration)) {
			val supertypes = typeDeclaration.<Type>transitiveClosurePlus[it.supertypes]
			for(supertype : supertypes.filter(TypeDeclaration)) {
				typeDeclaration.lookup(trace).supertypeInterpretation += supertype.lookup(trace)
				//println('''«typeDeclaration.name» --> «supertype.name»''')
			}
		}
	}
	
	def private Map<RelationDeclaration, PartialRelationInterpretation> initRelations(PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		val Map<RelationDeclaration, PartialRelationInterpretation> relation2Interpretation = new HashMap
		val definedRelationDeclarations = interpretation.problem.relations.filter(RelationDefinition).map[defines]
		val undefinedRelationDeclarations = interpretation.problem.relations.filter(RelationDeclaration).filter[
			declared | !definedRelationDeclarations.exists[defined | defined === declared]
		]
		for(relation : undefinedRelationDeclarations) {
			val partialInterpretation = relation.initialisePartialRelationInterpretation
			interpretation.partialrelationinterpretation += partialInterpretation
			relation2Interpretation.put(relation,partialInterpretation)
		}
		for(pMR2A : interpretation.problem.annotations.filter(PartialModelRelation2Assertion)) {
			val relation = pMR2A.targetRelation
			val r = relation.lookup(relation2Interpretation)
			val assertion = pMR2A.target.value
			val links = if(assertion instanceof SymbolicValue) {
				#[assertion]
			} else if(assertion instanceof And){
				val res = assertion.operands.filter(SymbolicValue)
				if(res.size != assertion.operands.size) {
					throw new UnsupportedOperationException('''Assertion describing partial model of "«r.interpretationOf.name»" contains unsupported constructs''')
				} else {
					res
				}
			} else {
				throw new UnsupportedOperationException('''Assertion describing partial model of "«r.interpretationOf.name»" contains unsupported constructs''')
			}
			for(link:links) {
				r.relationlinks += createLink(interpretation, link,trace)
			}
		}
		
//		interpretation.partialfunctioninterpretation += interpretation.problem.functions.filter(FunctionDeclaration)
//			.map[initialisePartialFunctionInterpretation(trace)]
//		interpretation.partialconstantinterpretation += interpretation.problem.constants.filter(ConstantDeclaration)
//			.map[initialisePartialConstantDeclaration(trace)]

		return relation2Interpretation
	}
	def private createLink(PartialInterpretation interpretation, SymbolicValue v, PrimitiveValueTrace trace) {
		val translatedValues = v.parameterSubstitutions.map[getElement(interpretation, trace)].toList
		if(translatedValues.size == 1) {
			return createUnaryElementRelationLink => [it.param1 = translatedValues.get(0)]
		} else if(translatedValues.size == 2) {
			return createBinaryElementRelationLink => [it.param1 = translatedValues.get(0) it.param2 = translatedValues.get(1)]
		} else {
			val res = createNaryRelationLink
			for(i : 0..<translatedValues.size) {
				res.elements += createNaryRelationLinkElement => [
					it.index = i
					it.param = translatedValues.get(i)
				]
			}
			return res
		}
	}

	def private dispatch getElement(SymbolicValue element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		return element.symbolicReference as DefinedElement
	}
	def private dispatch getElement(BoolLiteral element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		element.value.lookup(trace.booleanMap)
	}
	def private dispatch getElement(IntLiteral element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		element.value.lookup(trace.integerMap)
	}
	def private dispatch getElement(RealLiteral element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		element.value.lookup(trace.realMap)
	}
	def private dispatch getElement(StringLiteral element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		element.value.lookup(trace.stringMap)
	}
	def private dispatch getElement(Exists element, PartialInterpretation interpretation, PrimitiveValueTrace trace) {
		val type = element.quantifiedVariables.get(0).range
		val e = createUnknownValueElement(type)
		interpretation.newElements += e
		return e
	}
	
	def private initialisePartialTypeInterpretation(TypeDeclaration t, ViatraQueryEngine engine) {
		val supertypeMatcher = SupertypeStar.Matcher.on(engine)
		val res = createPartialComplexTypeInterpretation => [
			it.interpretationOf = t
			it.elements += supertypeMatcher.getAllValuesOfsubtype(t)
				.filter(TypeDefinition)
				.map[elements].flatten
		]
		return res
	}
	
	def private initialisePartialRelationInterpretation(RelationDeclaration r) {
		val res = createPartialRelationInterpretation => [
			it.interpretationOf = r
			if(r.parameters.size==2) {
				it.param1 = r.parameters.get(0)
				it.param2 = r.parameters.get(1)
			} else throw new UnsupportedOperationException
		]
		return res
	}
		
	def private dispatch createUnknownValueElement(BoolTypeReference element) {
		createRealElement => [it.valueSet = false]
	}
	def private dispatch createUnknownValueElement(IntTypeReference element) {
		createRealElement => [it.valueSet = false]
	}
	def private dispatch createUnknownValueElement(RealTypeReference element) {
		createRealElement => [it.valueSet = false]
	}
	def private dispatch createUnknownValueElement(ComplexTypeReference element) {
		//TODO Enum handling
	}
	
}