aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.logic2ecore/src/hu/bme/mit/inf/dslreasoner/logic2ecore/Logic2Ecore.xtend
blob: 002399358fdc986d5136c21ae1a57d051ba48962 (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
package hu.bme.mit.inf.dslreasoner.logic2ecore

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.LogicModelInterpretation
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicStructureBuilder
import java.util.Collection
import java.util.HashMap
import java.util.HashSet
import java.util.List
import java.util.Set
import org.eclipse.emf.ecore.EClass
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EStructuralFeature

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*

class Logic2Ecore {
	private val extension LogicStructureBuilder structureBuilder = new LogicStructureBuilder
	val Ecore2Logic ecore2Logic;
	
	new (Ecore2Logic ecore2Logic) {
		this.ecore2Logic = ecore2Logic
	}
	
	def transformInterpretation(LogicModelInterpretation interpretation, Ecore2Logic_Trace forwardTrace) {
		val allClasses = ecore2Logic.allClassesInScope(forwardTrace)
		val elements = new HashSet
		val element2Classes = new HashMap
		
		for(clazz: allClasses) {
			val type = ecore2Logic.TypeofEClass(forwardTrace,clazz);
			val elementsOfClass = getElements(interpretation,type)
			for(element : elementsOfClass) {
				elements += element
				element2Classes.putOrAddToSet(element,clazz)
			}
		}
		
		val element2Object = new HashMap
		for(element: elements) {
			val clazz = element.lookup(element2Classes).mostConcreteType
			val eobject = clazz.EPackage.EFactoryInstance.create(clazz)
			element2Object.put(element,eobject)
		}
		
		val allReferences = ecore2Logic.allReferencesInScope(forwardTrace)
		for(referenceType : allReferences) {
			if(referenceType.canSetFeature) {
				for(sourceElement : elements) {
					val sourceObject = sourceElement.lookup(element2Object)
					if(referenceType.EContainingClass.isSuperTypeOf(sourceObject.eClass)) {
						for(targetElement: elements) {
							val targetObject = targetElement.lookup(element2Object)
							if(referenceType.EReferenceType.isSuperTypeOf(targetObject.eClass)) {
								val expression = ecore2Logic.IsInReference(forwardTrace,sourceElement,targetElement,referenceType)
								val linkExist = interpretation.evalAsBool(expression)
								if(linkExist) {
									if(referenceType.isMany) {
										val list = sourceObject.eGet(referenceType) as List<? super EObject>
										list+= targetObject
									} else {
										sourceObject.eSet(referenceType,targetObject)
									}
								}
							}
						}
					}
				}
			}
		}
		
		/// + Attributes
		
		return element2Object.values.root
	}
	
	private def canSetFeature(EStructuralFeature feature) {
		feature.changeable && !feature.derived
	}
	
	def  mostConcreteType(Set<EClass> classes) {
		for(candidate : classes) {
			val subtypeOfAll = classes.forall[it.isSuperTypeOf(candidate)]
			if(subtypeOfAll) {
				if(candidate.abstract || candidate.isInterface) {
					throw new AssertionError('''Object has abstract concrete type! Types: [«FOR c:classes SEPARATOR ","»«c.name»«ENDFOR»]''')
				} else {
					return candidate
				}
			}
		}
		throw new AssertionError('''Object has no unique concrete type! Types: [«FOR c:classes SEPARATOR ","»«c.name»«ENDFOR»]''')
	}
	
	def getRoot(Collection<EObject> objects) {
		val rootCandidates = objects.filter[eContainer === null]
		if(rootCandidates.size == 1) {
			return rootCandidates.head
		} else {
			println('''No single root objects: «rootCandidates.size»''')
			return rootCandidates.head
		}
	}
}