aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend
blob: ffbca0af7bb05077ac692e38988cfd91058a086b (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
package hu.bme.mit.inf.dslreasoner.viatra2logic

import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic
import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import java.util.LinkedList
import java.util.Map
import org.eclipse.emf.common.util.Enumerator
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey
import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey
import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey
import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint
import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.ConstantValue
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall
import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.AggregatorConstraint
import org.eclipse.viatra.query.runtime.matchers.aggregators.DoubleSumOperator
import org.eclipse.viatra.query.runtime.matchers.aggregators.IntegerSumOperator
import org.eclipse.viatra.query.runtime.matchers.aggregators.LongSumOperator
import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator
import org.eclipse.viatra.query.runtime.matchers.aggregators.ExtremumOperator.Extreme
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.PatternMatchCounter
import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation

class Constraint2Logic {
	val extension LogicProblemBuilder builder = new LogicProblemBuilder
	val Ecore2Logic ecore2Logic
	val ExpressionEvaluation2Logic expressionEvaliation2Logic = new ExpressionEvaluation2Logic
	val expressionExtractor = new XExpressionExtractor
	
	new(Ecore2Logic ecore2Logic) {
		this.ecore2Logic = ecore2Logic
	}
	
	def dispatch Term transformConstraint(Equality constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		constraint.who.lookup(variable2Variable)
		== 
		constraint.withWhom.lookup(variable2Variable)
	}
	
	def dispatch Term transformConstraint(ExportedParameter constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		return null
	}
	
	def dispatch Term transformConstraint(Inequality constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		constraint.who.lookup(variable2Variable)
		!= 
		constraint.withWhom.lookup(variable2Variable)
	}
	
	def dispatch Term transformConstraint(NegativePatternCall constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val parameterSubstitution = new LinkedList
		for(index : 0..<constraint.actualParametersTuple.size) {
			val variable = constraint.actualParametersTuple.get(index) as PVariable
			parameterSubstitution += variable.lookup(variable2Variable).toTerm
		}
		
		val res = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation).call(parameterSubstitution)
		return res.Not
	}
	
	def dispatch Term transformConstraint(BinaryTransitiveClosure constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,		
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		transitiveClosure(
			constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation),
			(constraint.variablesTuple.get(0) as PVariable).lookup(variable2Variable).toTerm,
			(constraint.variablesTuple.get(1) as PVariable).lookup(variable2Variable).toTerm
		)
	}
	
	def dispatch Term transformConstraint(ConstantValue constant,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val tuple = constant.variablesTuple
		if(tuple.size == 1) {
			val variable = tuple.get(0) as PVariable
			//println(variable.name + " == " + constant.supplierKey + "["+constant.supplierKey.class.name+"]")
			val translatedConstant = transformConstantValue(constant.supplierKey,ecore2LogicTrace,viatra2LogicTrace,config);
			return variable.lookup(variable2Variable) == translatedConstant
		} else throw new AssertionError
	}
	
	private def dispatch transformConstantValue(
		Enumerator enumerator,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		ecore2Logic.Literal(ecore2LogicTrace.trace,enumerator)
	}
	private def dispatch transformConstantValue(
		Integer value,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		return value.asTerm
	}
	private def dispatch transformConstantValue(
		Boolean value,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		return value.asTerm
	}
	private def dispatch transformConstantValue(
		String value,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		return value.asTerm
	}
	private def dispatch transformConstantValue(
		Double value,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		return value.asTerm
	}
	private def dispatch transformConstantValue(
		Float value,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		return value.asTerm
	}
	private def dispatch transformConstantValue(Object other,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Viatra2LogicConfiguration config)
	{
		throw new UnsupportedOperationException('''Unknown constant «other»:«other.class.name»''')
	}
	
	def dispatch Term transformConstraint(PositivePatternCall constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		
		val parameterSubstitution = new LinkedList
		for(index : 0..<constraint.variablesTuple.size) {
			val variable = constraint.variablesTuple.get(index) as PVariable
			parameterSubstitution += variable.lookup(variable2Variable).toTerm
		}
		val res = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation).call(parameterSubstitution)
		return res
	}
	
	def dispatch Term transformConstraint(TypeConstraint constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val tuple = constraint.variablesTuple
		if(tuple.size == 1) {
			val typeConstraint = constraint.equivalentJudgement.inputKey
			if(typeConstraint instanceof EClassTransitiveInstancesKey) {
				val type = typeConstraint.emfKey
				val variable = tuple.get(0) as PVariable
				return transformTypeConstraint(type,variable,ecore2LogicTrace,variable2Variable,viatra2LogicTrace)
			} else if(typeConstraint instanceof EDataTypeInSlotsKey) {
				// If the type is a primitive type or EEnum, then instanceof is an unnecessary constraint
				return null
			}
		} else if(tuple.size == 2) {
			val type = (constraint.equivalentJudgement.inputKey as EStructuralFeatureInstancesKey).emfKey
			val src = tuple.get(0) as PVariable
			val trg = tuple.get(1) as PVariable
			return transformPathConstraint(type,src,trg,ecore2LogicTrace,variable2Variable,viatra2LogicTrace)
		} else throw new IllegalArgumentException('''unknown tuple: «tuple»''')
	}
	
	def Term transformTypeConstraint(
		EClass type,
		PVariable variable,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicTrace viatra2LogicTrace)
	{
		InstanceOf(
			variable.lookup(variable2Variable),
			ecore2Logic.TypeofEClass(ecore2LogicTrace.trace,type))
	}
	def Term transformPathConstraint(
		EStructuralFeature feature,
		PVariable src, PVariable trg,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicTrace viatra2LogicTrace)
	{
		if(feature instanceof EReference) {
			return ecore2Logic.IsInReference(ecore2LogicTrace.trace,
				src.lookup(variable2Variable),
				trg.lookup(variable2Variable),
				feature)
		} else if(feature instanceof EAttribute) {
			return ecore2Logic.IsAttributeValue(ecore2LogicTrace.trace,
				src.lookup(variable2Variable),
				trg.lookup(variable2Variable),
				feature)
		} else {
			throw new IllegalArgumentException('''Unsupported path expression: «feature.class.name»''')
		}
	}
	
	def dispatch Term transformConstraint(TypeFilterConstraint constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val tuple = constraint.variablesTuple
		if(tuple.size == 1) {
			val typeConstraint = constraint.equivalentJudgement.inputKey
			if(typeConstraint instanceof EClassTransitiveInstancesKey) {
				val type = typeConstraint.emfKey
				val variable = tuple.get(0) as PVariable
				return transformTypeConstraint(type,variable,ecore2LogicTrace,variable2Variable,viatra2LogicTrace)
			} else if(typeConstraint instanceof EDataTypeInSlotsKey) {
				// If the type is a primitive type or EEnum, then instanceof is an unnecessary constraint
				return null
			}
		} else if(tuple.size == 2) {
			val type = (constraint.equivalentJudgement.inputKey as EStructuralFeatureInstancesKey).emfKey
			val src = tuple.get(0) as PVariable
			val trg = tuple.get(1) as PVariable
			return transformPathConstraint(type,src,trg,ecore2LogicTrace,variable2Variable,viatra2LogicTrace)
		} else throw new IllegalArgumentException('''unknown tuple: «tuple»''')
	}
	
	def dispatch Term transformConstraint(AggregatorConstraint constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,		
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val logicReferred = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation)
		val parameterSubstitution = new LinkedList
		for(index : 0..<constraint.actualParametersTuple.size) {
			val term = (constraint.actualParametersTuple.get(index) as PVariable).lookup(variable2Variable)
			parameterSubstitution += term
		}
		val aggregatorIndex = constraint.aggregatedColumn
		val logicResultVariable = constraint.resultVariable.lookup(variable2Variable)
		val type = constraint.aggregator.operator
		if(type === null) {
			return Count(logicReferred,parameterSubstitution,logicResultVariable)
		} else if(type instanceof IntegerSumOperator || type instanceof DoubleSumOperator || type instanceof LongSumOperator){
			return Sum(logicReferred,parameterSubstitution,aggregatorIndex,logicResultVariable)
		} else if(type instanceof ExtremumOperator) {
			if(type.name == Extreme.MIN.name.toLowerCase) {
				return Min(logicReferred,parameterSubstitution,aggregatorIndex,logicResultVariable)
			} else if(type.name == Extreme.MAX.name.toLowerCase){
				return Max(logicReferred,parameterSubstitution,aggregatorIndex,logicResultVariable)
			} else {
				throw new UnsupportedOperationException('''Unkown Extremum aggregator type: «type.name»''')
			}
		} else {
			throw new UnsupportedOperationException('''Unkown aggregator type: «type.name»''')
		}
	}
	
	def dispatch Term transformConstraint(PatternMatchCounter constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,		
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val logicReferred = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation)
		val parameterSubstitution = new LinkedList
		for(index : 0..<constraint.actualParametersTuple.size) {
			val term = (constraint.actualParametersTuple.get(index) as PVariable).lookup(variable2Variable)
			parameterSubstitution += term
		}
		val logicResultVariable = constraint.resultVariable.lookup(variable2Variable)
		return Count(logicReferred,parameterSubstitution,logicResultVariable)
	}
	
	def dispatch Term transformConstraint(ExpressionEvaluation constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,		
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		val outputVariable = constraint.outputVariable
		val expression = expressionExtractor.extractExpression(constraint.evaluator)
		return expressionEvaliation2Logic.transformEval(outputVariable,expression,variable2Variable)		
	}
	
	def dispatch Term transformConstraint(PConstraint constraint,
		TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace,
		Viatra2LogicTrace viatra2LogicTrace,		
		Map<PVariable, Variable> variable2Variable,
		Viatra2LogicConfiguration config)
	{
		throw new UnsupportedOperationException('''Unkown constraint type: «constraint.class.name»''')
	}
}