aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend
blob: dca10baf610f8b1055cf1169cbfa83e852092fb8 (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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules

import com.google.common.collect.ImmutableList
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.ComplexTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
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.PrimitiveTypeReference
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.StringTypeReference
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationStatistics
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality.ScopePropagator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.GeneratedPatterns
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.ObjectCreationPrecondition
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 java.lang.reflect.Field
import java.util.HashMap
import java.util.Iterator
import java.util.LinkedHashMap
import java.util.LinkedList
import java.util.List
import java.util.Map
import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.GenericPatternMatch
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.rete.matcher.ReteBackendFactory
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule
import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor
import org.eclipse.xtext.xbase.lib.Functions.Function0

class RefinementRuleProvider {
	val extension BatchTransformationRuleFactory factory = new BatchTransformationRuleFactory
	val extension PartialinterpretationFactory factory2 = PartialinterpretationFactory.eINSTANCE
	val extension LogiclanguageFactory factory3 = LogiclanguageFactory.eINSTANCE

	def canonizeName(String name) {
		return name.replace(' ', '_')
	}

	def createUnitPrulePropagator(LogicProblem p, PartialInterpretation i, GeneratedPatterns patterns,
		ScopePropagator scopePropagator, ModelGenerationStatistics statistics) {
		new UnitRulePropagator(p, i, this, scopePropagator, patterns.mustRelationPropagationQueries, statistics)
	}

	def LinkedHashMap<ObjectCreationPrecondition, BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>>> createObjectRefinementRules(
		LogicProblem p,
		PartialInterpretation i,
		GeneratedPatterns patterns,
		UnitRulePropagator unitRulePropagator,
		boolean nameNewElement,
		ModelGenerationStatistics statistics
	) {
		val res = new LinkedHashMap
		val recursiveObjectCreation = recursiveObjectCreation(p, i)
		for (LHSEntry : patterns.refineObjectQueries.entrySet) {
			val containmentRelation = LHSEntry.key.containmentRelation
			val inverseRelation = LHSEntry.key.inverseContainment
			val type = LHSEntry.key.newType
			val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>>
			val rule = createObjectCreationRule(p, containmentRelation, inverseRelation, type,
				recursiveObjectCreation.get(type), lhs, nameNewElement, unitRulePropagator, statistics)
			res.put(LHSEntry.key, rule)
		}
		return res
	}

	def private createObjectCreationRule(LogicProblem p, Relation containmentRelation, Relation inverseRelation,
		Type type, List<ObjectCreationInterpretationData> recursiveObjectCreations,
		IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, boolean nameNewElement,
		UnitRulePropagator unitRulePropagator, ModelGenerationStatistics statistics) {
		val name = '''addObject_«type.name.canonizeName»«IF containmentRelation!==null»_by_«containmentRelation.name.canonizeName»«ENDIF»'''
		val ruleBuilder = factory.createRule(lhs).name(name)
		if (containmentRelation !== null) {
			if (inverseRelation !== null) {
				ruleBuilder.action [ match |
					statistics.incrementTransformationCount
//					println(name)
					val startTime = System.nanoTime
					// val problem = match.get(0) as LogicProblem
					val interpretation = match.get(1) as PartialInterpretation
					val relationInterpretation = match.get(2) as PartialRelationInterpretation
					val inverseRelationInterpretation = match.get(3) as PartialRelationInterpretation
					val typeInterpretation = match.get(4) as PartialComplexTypeInterpretation
					val container = match.get(5) as DefinedElement
					createObjectActionWithContainmentAndInverse(
						nameNewElement,
						interpretation,
						typeInterpretation,
						container,
						relationInterpretation,
						inverseRelationInterpretation,
						[createDefinedElement],
						recursiveObjectCreations,
						unitRulePropagator
					)
					statistics.addExecutionTime(System.nanoTime - startTime)

					unitRulePropagator.propagate
				]
			} else {
				ruleBuilder.action [ match |
					statistics.incrementTransformationCount
//					println(name)
					val startTime = System.nanoTime
					// val problem = match.get(0) as LogicProblem
					val interpretation = match.get(1) as PartialInterpretation
					val relationInterpretation = match.get(2) as PartialRelationInterpretation
					val typeInterpretation = match.get(3) as PartialComplexTypeInterpretation
					val container = match.get(4) as DefinedElement

					createObjectActionWithContainment(
						nameNewElement,
						interpretation,
						typeInterpretation,
						container,
						relationInterpretation,
						[createDefinedElement],
						recursiveObjectCreations,
						unitRulePropagator
					)
					statistics.addExecutionTime(System.nanoTime - startTime)

					unitRulePropagator.propagate
				]
			}
		} else {
			ruleBuilder.action [ match |
				statistics.incrementTransformationCount
//				println(name)
				val startTime = System.nanoTime
				// val problem = match.get(0) as LogicProblem
				val interpretation = match.get(1) as PartialInterpretation
				val typeInterpretation = match.get(2) as PartialComplexTypeInterpretation

				createObjectAction(
					nameNewElement,
					interpretation,
					typeInterpretation,
					[createDefinedElement],
					recursiveObjectCreations,
					unitRulePropagator
				)
				statistics.addExecutionTime(System.nanoTime - startTime)

				unitRulePropagator.propagate
			]
		}
		return ruleBuilder.build
	}

	def private recursiveObjectCreation(LogicProblem p, PartialInterpretation i) {
		val Map<Type, List<ObjectCreationInterpretationData>> recursiveObjectCreation = new HashMap
		for (type : p.types) {
			recursiveObjectCreation.put(type, new LinkedList)
		}

		val containmentReferences = p.containmentHierarchies.head.containmentRelations

		for (relationInterpretation : i.partialrelationinterpretation) {
			val relation = relationInterpretation.interpretationOf
			val lowermultiplicities = p.annotations.filter(LowerMultiplicityAssertion).filter[it.relation === relation]
			if ((!lowermultiplicities.empty)) {
				val number = lowermultiplicities.head.lower
				if (number > 0) {
					val sourceTypeInterpretation = getTypeInterpretation(i, relation,
						0) as PartialComplexTypeInterpretation
					val subtypeInterpretations = i.partialtypeinterpratation.filter(PartialComplexTypeInterpretation).
						filter [
							it === sourceTypeInterpretation ||
								it.supertypeInterpretation.contains(sourceTypeInterpretation)
						]

					if (containmentReferences.contains(relation)) {
						val targetTypeInterpretation = getTypeInterpretation(i, relation, 1)
						val targetType = (targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf
						if ((!targetType.isIsAbstract) && (targetType.supertypes.empty)) {
							val inverseAnnotation = p.annotations.filter(InverseRelationAssertion).filter [
								it.inverseA === relation || it.inverseB === relation
							]
							if (!inverseAnnotation.empty) {
								val onlyInverseAnnotation = if (inverseAnnotation.head.inverseA === relation) {
										inverseAnnotation.head.inverseB
									} else {
										inverseAnnotation.head.inverseA
									}
								val inverseRelationInterpretation = i.partialrelationinterpretation.filter [
									it.interpretationOf === onlyInverseAnnotation
								].head
								for (subTypeInterpretation : subtypeInterpretations) {
									for (var times = 0; times < number; times++) {
										recursiveObjectCreation.get(subTypeInterpretation.interpretationOf) +=
											new ObjectCreationInterpretationData(
												i,
												targetTypeInterpretation,
												relationInterpretation,
												inverseRelationInterpretation,
												targetTypeInterpretation.getTypeConstructor
											)
									}
								}
							} else {
								for (subTypeInterpretation : subtypeInterpretations) {
									for (var times = 0; times < number; times++) {
										recursiveObjectCreation.get(subTypeInterpretation.interpretationOf) +=
											new ObjectCreationInterpretationData(
												i,
												targetTypeInterpretation,
												relationInterpretation,
												null,
												targetTypeInterpretation.getTypeConstructor
											)
									}
								}
							}
						}
					} else if (relation.parameters.get(1) instanceof PrimitiveTypeReference) {
						val targetTypeInterpretation = getTypeInterpretation(i, relation, 1)
						for (subTypeInterpretation : subtypeInterpretations) {
							for (var times = 0; times < number; times++) {
								recursiveObjectCreation.get(subTypeInterpretation.interpretationOf) +=
									new ObjectCreationInterpretationData(
										i,
										targetTypeInterpretation,
										relationInterpretation,
										null,
										targetTypeInterpretation.getTypeConstructor
									)
							}
						}
					}
				}
			}
		}

		// Doing the recursion
		var objectCreations = new LinkedList(recursiveObjectCreation.values.flatten.toList)
		for (objectCreation : objectCreations) {
			val newInterpretation = objectCreation.typeInterpretation
			if (newInterpretation instanceof PartialComplexTypeInterpretation) {
				val newlyCreatedType = newInterpretation.interpretationOf
				if (recursiveObjectCreation.containsKey(newlyCreatedType)) {
					val actionsWhenTypeCreated = recursiveObjectCreation.get(newlyCreatedType)
					objectCreation.recursiveConstructors += actionsWhenTypeCreated
				}
			}
		}

		// checking acyclicity
		for (objectCreation : objectCreations) {
			var reachable = objectCreation.recursiveConstructors
			do {
				if (reachable.contains(objectCreation)) {
					throw new IllegalArgumentException('''Cicrle in the containment!''')
				} else {
					reachable = reachable.map[it.recursiveConstructors].flatten.toList
				}
			} while (!reachable.empty)
		}

		return recursiveObjectCreation
	}

	private def getTypeInterpretation(PartialInterpretation i, RelationDeclaration relation, int index) {
		val typeReference = relation.parameters.get(index)
		return getTypeInterpretation(i, typeReference)

	}

	private dispatch def getTypeInterpretation(PartialInterpretation i, ComplexTypeReference reference) {
		return i.partialtypeinterpratation.filter(PartialComplexTypeInterpretation).filter [
			it.getInterpretationOf == reference.referred
		].head
	}

	private dispatch def getTypeInterpretation(PartialInterpretation i, BoolTypeReference reference) {
		return i.partialtypeinterpratation.filter(PartialBooleanInterpretation).head
	}

	private dispatch def getTypeInterpretation(PartialInterpretation i, IntTypeReference reference) {
		return i.partialtypeinterpratation.filter(PartialIntegerInterpretation).head
	}

	private dispatch def getTypeInterpretation(PartialInterpretation i, RealTypeReference reference) {
		return i.partialtypeinterpratation.filter(PartialRealInterpretation).head
	}

	private dispatch def getTypeInterpretation(PartialInterpretation i, StringTypeReference reference) {
		return i.partialtypeinterpratation.filter(PartialStringInterpretation).head
	}

	private dispatch def Function0<DefinedElement> getTypeConstructor(PartialComplexTypeInterpretation reference) {
		[createDefinedElement]
	}

	private dispatch def Function0<DefinedElement> getTypeConstructor(PartialBooleanInterpretation reference) {
		[createBooleanElement]
	}

	private dispatch def Function0<DefinedElement> getTypeConstructor(PartialIntegerInterpretation reference) {
		[createIntegerElement]
	}

	private dispatch def Function0<DefinedElement> getTypeConstructor(PartialRealInterpretation reference) {
		[createRealElement]
	}

	private dispatch def Function0<DefinedElement> getTypeConstructor(PartialStringInterpretation reference) {
		[createStringElement]
	}

	def createRelationRefinementRules(GeneratedPatterns patterns, UnitRulePropagator unitRulePropagator,
		ModelGenerationStatistics statistics) {
		val res = new LinkedHashMap
		for (LHSEntry : patterns.refineRelationQueries.entrySet) {
			val declaration = LHSEntry.key.key
			val inverseReference = LHSEntry.key.value
			val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>>
			val rule = createRelationRefinementRule(declaration, inverseReference, lhs, unitRulePropagator, statistics)
			res.put(LHSEntry.key, rule)
		}
		return res
	}

	def private BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>> createRelationRefinementRule(
		RelationDeclaration declaration, Relation inverseRelation,
		IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, UnitRulePropagator unitRulePropagator,
		ModelGenerationStatistics statistics) {
		val name = '''addRelation_«declaration.name.canonizeName»«IF inverseRelation !== null»_and_«inverseRelation.name.canonizeName»«ENDIF»'''
		val ruleBuilder = factory.createRule(lhs).name(name)
		if (inverseRelation === null) {
			ruleBuilder.action [ match |
				statistics.incrementTransformationCount
//				println(name)
				val startTime = System.nanoTime
				createRelationLinkAction(match, unitRulePropagator)
				statistics.addExecutionTime(System.nanoTime - startTime)

				unitRulePropagator.propagate
			]
		} else {
			ruleBuilder.action [ match |
				statistics.incrementTransformationCount
//				println(name)
				val startTime = System.nanoTime
				createRelationLinkWithInverse(match, unitRulePropagator)
				statistics.addExecutionTime(System.nanoTime - startTime)

				unitRulePropagator.propagate
			]
		}

		return ruleBuilder.build
	}

	// ///////////////////////
	// Actions
	// ///////////////////////
	protected def void createObjectAction(boolean nameNewElement, ObjectCreationInterpretationData data,
		DefinedElement container, UnitRulePropagator unitRulePropagator) {
		if (data.containerInterpretation !== null) {
			if (data.containerInverseInterpretation !== null) {
				createObjectActionWithContainmentAndInverse(
					nameNewElement,
					data.interpretation,
					data.typeInterpretation,
					container,
					data.containerInterpretation,
					data.containerInverseInterpretation,
					data.constructor,
					data.recursiveConstructors,
					unitRulePropagator
				)
			} else {
				createObjectActionWithContainment(
					nameNewElement,
					data.interpretation,
					data.typeInterpretation,
					container,
					data.containerInterpretation,
					data.constructor,
					data.recursiveConstructors,
					unitRulePropagator
				)
			}
		} else {
			createObjectAction(
				nameNewElement,
				data.interpretation,
				data.typeInterpretation,
				data.constructor,
				data.recursiveConstructors,
				unitRulePropagator
			)
		}

	}

	protected def createObjectActionWithContainmentAndInverse(
		boolean nameNewElement,
		PartialInterpretation interpretation,
		PartialTypeInterpratation typeInterpretation,
		DefinedElement container,
		PartialRelationInterpretation relationInterpretation,
		PartialRelationInterpretation inverseRelationInterpretation,
		Function0<DefinedElement> constructor,
		List<ObjectCreationInterpretationData> recursiceObjectCreations,
		UnitRulePropagator unitRulePropagator
	) {
		val newElement = constructor.apply
		if (nameNewElement) {
			newElement.name = '''new «interpretation.newElements.size»'''
		}

		// Types
		typeInterpretation.elements += newElement
		if (typeInterpretation instanceof PartialComplexTypeInterpretation) {
			typeInterpretation.supertypeInterpretation.forEach[it.elements += newElement]
		}
		// ContainmentRelation
		val newLink1 = factory2.createBinaryElementRelationLink => [it.param1 = container it.param2 = newElement]
		relationInterpretation.relationlinks += newLink1
		// Inverse Containment
		val newLink2 = factory2.createBinaryElementRelationLink => [it.param1 = newElement it.param2 = container]
		inverseRelationInterpretation.relationlinks += newLink2

		// Scope propagation
		unitRulePropagator.decrementTypeScope(typeInterpretation)
		unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf)
		unitRulePropagator.addedToRelation(inverseRelationInterpretation.interpretationOf)

		// Existence
		interpretation.newElements += newElement

		// Do recursive object creation
		for (newConstructor : recursiceObjectCreations) {
			createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator)
		}

		return newElement
	}

	protected def createObjectActionWithContainment(
		boolean nameNewElement,
		PartialInterpretation interpretation,
		PartialTypeInterpratation typeInterpretation,
		DefinedElement container,
		PartialRelationInterpretation relationInterpretation,
		Function0<DefinedElement> constructor,
		List<ObjectCreationInterpretationData> recursiceObjectCreations,
		UnitRulePropagator unitRulePropagator
	) {
		val newElement = constructor.apply
		if (nameNewElement) {
			newElement.name = '''new «interpretation.newElements.size»'''
		}

		// Types
		typeInterpretation.elements += newElement
		if (typeInterpretation instanceof PartialComplexTypeInterpretation) {
			typeInterpretation.supertypeInterpretation.forEach[it.elements += newElement]
		}
		// ContainmentRelation
		val newLink = factory2.createBinaryElementRelationLink => [it.param1 = container it.param2 = newElement]
		relationInterpretation.relationlinks += newLink
		unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf)

		// Scope propagation
		unitRulePropagator.decrementTypeScope(typeInterpretation)

		// Existence
		interpretation.newElements += newElement

		// Do recursive object creation
		for (newConstructor : recursiceObjectCreations) {
			createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator)
		}

		return newElement
	}

	protected def createObjectAction(boolean nameNewElement, PartialInterpretation interpretation,
		PartialTypeInterpratation typeInterpretation, Function0<DefinedElement> constructor,
		List<ObjectCreationInterpretationData> recursiceObjectCreations, UnitRulePropagator unitRulePropagator) {
		val newElement = constructor.apply
		if (nameNewElement) {
			newElement.name = '''new «interpretation.newElements.size»'''
		}

		// Types
		typeInterpretation.elements += newElement
		if (typeInterpretation instanceof PartialComplexTypeInterpretation) {
			typeInterpretation.supertypeInterpretation.forEach[it.elements += newElement]
		}

		// Scope propagation
		unitRulePropagator.decrementTypeScope(typeInterpretation)

		// Existence
		interpretation.newElements += newElement

		// Do recursive object creation
		for (newConstructor : recursiceObjectCreations) {
			createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator)
		}

		return newElement
	}

	protected def createRelationLinkAction(IPatternMatch match, UnitRulePropagator unitRulePropagator) {
		// val problem = match.get(0) as LogicProblem
		// val interpretation = match.get(1) as PartialInterpretation
		val relationInterpretation = match.get(2) as PartialRelationInterpretation
		val src = match.get(3) as DefinedElement
		val trg = match.get(4) as DefinedElement
		createRelationLinkAction(src, trg, relationInterpretation, unitRulePropagator)
	}

	protected def void createRelationLinkAction(DefinedElement src, DefinedElement trg,
		PartialRelationInterpretation relationInterpretation, UnitRulePropagator unitRulePropagator) {
		val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg]
		relationInterpretation.relationlinks += link
		unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf)
	}

	protected def void createRelationLinkWithInverse(IPatternMatch match, UnitRulePropagator unitRulePropagator) {
		// val problem = match.get(0) as LogicProblem
		// val interpretation = match.get(1) as PartialInterpretation
		val relationInterpretation = match.get(2) as PartialRelationInterpretation
		val inverseInterpretation = match.get(3) as PartialRelationInterpretation
		val src = match.get(4) as DefinedElement
		val trg = match.get(5) as DefinedElement
		createRelationLinkWithInverse(src, trg, relationInterpretation, inverseInterpretation, unitRulePropagator)
	}

	protected def void createRelationLinkWithInverse(DefinedElement src, DefinedElement trg,
		PartialRelationInterpretation relationInterpretation, PartialRelationInterpretation inverseInterpretation,
		UnitRulePropagator unitRulePropagator) {
		val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg]
		relationInterpretation.relationlinks += link
		val inverseLink = createBinaryElementRelationLink => [it.param1 = trg it.param2 = src]
		inverseInterpretation.relationlinks += inverseLink
		unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf)
		unitRulePropagator.addedToRelation(inverseInterpretation.interpretationOf)
	}

	static class UnitRulePropagator {
		val LogicProblem p
		val PartialInterpretation i
		val RefinementRuleProvider refinementRuleProvider
		var AdvancedViatraQueryEngine queryEngine
		var Field delayMessageDelivery
		val ScopePropagator scopePropagator
		val List<AbstractMustRelationPropagator<? extends IPatternMatch>> propagators
		val ModelGenerationStatistics statistics

		new(LogicProblem p, PartialInterpretation i, RefinementRuleProvider refinementRuleProvider,
			ScopePropagator scopePropagator,
			Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustRelationPropagationQueries,
			ModelGenerationStatistics statistics) {
			this.p = p
			this.i = i
			this.refinementRuleProvider = refinementRuleProvider
			queryEngine = ViatraQueryEngine.on(new EMFScope(i)) as AdvancedViatraQueryEngine
			delayMessageDelivery = queryEngine.class.getDeclaredField("delayMessageDelivery")
			delayMessageDelivery.accessible = true
			this.scopePropagator = scopePropagator
			propagators = ImmutableList.copyOf(mustRelationPropagationQueries.entrySet.map [ entry |
				val matcher = queryEngine.getMatcher(entry.value)
				getPropagator(entry.key.key, entry.key.value, matcher)
			])
			this.statistics = statistics
		}

		def decrementTypeScope(PartialTypeInterpratation partialTypeInterpratation) {
			scopePropagator.decrementTypeScope(partialTypeInterpratation)
		}

		def addedToRelation(Relation r) {
			scopePropagator.addedToRelation(r)
		}

		def propagate() {
			var boolean changed
			do {
				val scopeChanged = propagateScope()
				val mustChanged = propagateMustRelations()
				changed = scopeChanged || mustChanged
			} while (changed)
		}

		protected def flushQueryEngine() {
			if (queryEngine.updatePropagationDelayed) {
				delayMessageDelivery.setBoolean(queryEngine, false)
				queryEngine.getQueryBackend(ReteBackendFactory.INSTANCE).flushUpdates
				delayMessageDelivery.setBoolean(queryEngine, true)
			}
		}

		protected def propagateScope() {
			if (scopePropagator.scopePropagationNeeded) {
				if (scopePropagator.queryEngineFlushRequiredBeforePropagation) {
					flushQueryEngine()
				}
				val propagatorStartTime = System.nanoTime
				scopePropagator.propagateAllScopeConstraints()
				statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime)
				true
			} else {
				false
			}
		}

		protected def propagateMustRelations() {
			if (propagators.empty) {
				return false
			}
			flushQueryEngine()
			val propagatorStartTime = System.nanoTime
			var changed = false
			for (propagator : propagators) {
				changed = propagator.propagate(p, i, refinementRuleProvider, this) || changed
			}
			statistics.addMustRelationPropagationTime(System.nanoTime - propagatorStartTime)
			changed
		}

		private static def <T extends IPatternMatch> getPropagator(Relation relation, Relation inverseRelation,
			ViatraQueryMatcher<T> matcher) {
			if (inverseRelation === null) {
				new MustRelationPropagator(matcher)
			} else if (relation == inverseRelation) {
				new MustRelationPropagatorWithSelfInverse(matcher)
			} else {
				new MustRelationPropagatorWithInverse(matcher)
			}
		}

		@FinalFieldsConstructor
		private static abstract class AbstractMustRelationPropagator<T extends IPatternMatch> {
			val ViatraQueryMatcher<T> matcher

			def propagate(LogicProblem p, PartialInterpretation i, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator) {
				val iterator = getIterator(p, i)
				if (!iterator.hasNext) {
					return false
				}
				iterate(iterator, refinementRuleProvider, unitRulePropagator)
				true
			}

			def iterate(Iterator<T> iterator, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator) {
				while (iterator.hasNext) {
					doPropagate(iterator.next, refinementRuleProvider, unitRulePropagator)
				}
			}

			protected def getIterator(LogicProblem p, PartialInterpretation i) {
				val partialMatch = matcher.newEmptyMatch
				partialMatch.set(0, p)
				partialMatch.set(1, i)
				matcher.streamAllMatches(partialMatch).iterator
			}

			protected def void doPropagate(T match, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator)
		}

		private static class MustRelationPropagator<T extends IPatternMatch> extends AbstractMustRelationPropagator<T> {
			new(ViatraQueryMatcher<T> matcher) {
				super(matcher)
			}

			override protected doPropagate(T match, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator) {
				refinementRuleProvider.createRelationLinkAction(match, unitRulePropagator)
			}
		}

		private static class MustRelationPropagatorWithInverse<T extends IPatternMatch> extends AbstractMustRelationPropagator<T> {
			new(ViatraQueryMatcher<T> matcher) {
				super(matcher)
			}

			override protected doPropagate(T match, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator) {
				refinementRuleProvider.createRelationLinkWithInverse(match, unitRulePropagator)
			}
		}

		private static class MustRelationPropagatorWithSelfInverse<T extends IPatternMatch> extends MustRelationPropagatorWithInverse<T> {
			new(ViatraQueryMatcher<T> matcher) {
				super(matcher)
			}

			override iterate(Iterator<T> iterator, RefinementRuleProvider refinementRuleProvider,
				UnitRulePropagator unitRulePropagator) {
				val pairs = newHashSet
				while (iterator.hasNext) {
					val match = iterator.next
					val src = match.get(4) as DefinedElement
					val trg = match.get(5) as DefinedElement
					if (!pairs.contains(trg -> src)) {
						pairs.add(src -> trg)
						doPropagate(match, refinementRuleProvider, unitRulePropagator)
					}
				}
			}
		}
	}
}

@Data
class ObjectCreationInterpretationData {
	PartialInterpretation interpretation
	PartialTypeInterpratation typeInterpretation
	PartialRelationInterpretation containerInterpretation
	PartialRelationInterpretation containerInverseInterpretation
	Function0<DefinedElement> constructor
	List<ObjectCreationInterpretationData> recursiveConstructors = new LinkedList
}