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

import com.google.common.collect.ImmutableList
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.TracedOutput
import hu.bme.mit.inf.dslreasoner.logic.model.builder.TypeScopes
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.LogiclanguageFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.PartialInterpretationInitialiser
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.Problem2PartialInterpretationTrace
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRelationInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory
import java.util.HashMap
import java.util.HashSet
import java.util.List
import java.util.Map
import java.util.Set
import org.eclipse.emf.common.util.Enumerator
import org.eclipse.emf.ecore.EAttribute
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.resource.Resource

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

class InstanceModel2PartialInterpretation {
	val extension LogiclanguageFactory factory = LogiclanguageFactory.eINSTANCE
	val extension PartialinterpretationFactory factory2 = PartialinterpretationFactory.eINSTANCE
	val Ecore2Logic ecore2Logic = new Ecore2Logic
	val partialInterpretationInitialiser = new PartialInterpretationInitialiser
	
	public def transform(
		TracedOutput<LogicProblem, Ecore2Logic_Trace> metamodelTranslationResult,
		Resource resource,
		boolean withID) 
	{
		val objectsBuilder = ImmutableList.builder
		val treeIterator = resource.allContents
		val referencesUsed = ecore2Logic.allReferencesInScope(metamodelTranslationResult.trace).toSet
		while (treeIterator.hasNext) {
			val object = treeIterator.next
			val containingReference = object.eContainmentFeature
			if (containingReference === null || referencesUsed.contains(containingReference)) {
				objectsBuilder.add(object)
			} else {
				treeIterator.prune
			}
		}
		val objects = objectsBuilder.build
		return transform(metamodelTranslationResult,objects,withID)
	}
	
	public def transform(
		TracedOutput<LogicProblem, Ecore2Logic_Trace> metamodelTranslationResult,
		List<EObject> objects,
		boolean withID) 
	{
		val problem = metamodelTranslationResult.output
		val ecore2LogicTrace = metamodelTranslationResult.trace
		
		val referencesUsed = ecore2Logic.allReferencesInScope(ecore2LogicTrace).toSet
		val attributesUsed = ecore2Logic.allAttributesInScope(ecore2LogicTrace).toSet
		
		val typeScope = createTypeScopesFromKnownAttributeValues(objects,attributesUsed)
		val tracedOutput = partialInterpretationInitialiser.initialisePartialInterpretation(problem, typeScope)
		val partialInterpretation = tracedOutput.output
		val partialInterpretationTrace = tracedOutput.trace
		
		val Map<EObject,DefinedElement> object2DefinedElement = new HashMap
	
		// EObject -> DefinedElement
		for(objectIndex : 0..<objects.size) {
			val object = objects.get(objectIndex)
			val element = createDefinedElement => [
				it.name = if(withID) 
					{ '''o «objectIndex+1»''' } else 
					{ null }
			]
			
			// Add element to interpretation
			partialInterpretation.newElements += element
			
			// Define the types
			val type = ecore2Logic.TypeofEClass(ecore2LogicTrace,object.eClass) as TypeDeclaration
			val interpretation = type.lookup(partialInterpretationTrace.type2Interpretation)
			interpretation.elements += element
			interpretation.supertypeInterpretation.forEach[elements += element]
			
			// Trace handling
			object2DefinedElement.put(object, element)
		}
		
		for(source : objects) {
			// Translating the references
			for(reference : source.eClass.EAllReferences.filter[
				referencesUsed.contains(it) && !it.derived]) 
			{
				val type = ecore2Logic.relationOfReference(ecore2LogicTrace,reference)
				val interpretation = type.lookup(partialInterpretationTrace.relation2Interpretation)
				val sourceElement = source.lookup(object2DefinedElement)
				if(reference.isMany) {
					val listOfTargets = source.eGet(reference) as List<? extends EObject>
					for(target : listOfTargets) {
						if(target !== null && object2DefinedElement.containsKey(target)) {
							val targetElement = target.lookup(object2DefinedElement)
							translateLink(interpretation,sourceElement,targetElement)
						}
					}
				} else {
					val target = source.eGet(reference) as EObject
					if(target !== null && object2DefinedElement.containsKey(target)) {
						val targetElement = target.lookup(object2DefinedElement)
						translateLink(interpretation,sourceElement,targetElement)
					}
				}
			}
			
			// Transforming the attributes
			for(attribute : source.eClass.EAllAttributes.filter[attributesUsed.contains(it) && !it.derived]) {
				val type = ecore2Logic.relationOfAttribute(ecore2LogicTrace,attribute)
				val interpretation = type.lookup(partialInterpretationTrace.relation2Interpretation)
				val sourceElement = source.lookup(object2DefinedElement)
				if(attribute.isMany) {
					val listOfTargets = source.eGet(attribute) as List<? extends EObject>
					for(target : listOfTargets) {
						val value = translateValue(target,ecore2LogicTrace,partialInterpretationTrace)
						if(value !== null) {
							translateLink(interpretation,sourceElement,value)
						}
					}
				} else {
					val target = source.eGet(attribute)
					if(target !== null) {
						val value = translateValue(target,ecore2LogicTrace,partialInterpretationTrace)
						if(value !== null) {
							translateLink(interpretation,sourceElement,value)
						}
					}
				}
			}
		}
		
		return partialInterpretation
	}
	
	private def createTypeScopesFromKnownAttributeValues(List<EObject> objects, Set<EAttribute> attributesUsed) {
		val Set<Integer> integers = new HashSet
		val Set<Double> reals = new HashSet
		val Set<String> strings = new HashSet
		for(object: objects) {
			for(attribute : object.eClass.EAllAttributes.filter[attributesUsed.contains(it)]) {
				val value = object.eGet(attribute)
				if(value !== null) {
					if(value instanceof List<?>) {
						for(v : value) {
							shortValue(v,integers,reals,strings)
						}
					} else {
						shortValue(value,integers,reals,strings)
					}
				}
			}
		}
		return new TypeScopes => [
			it.knownIntegers += integers
			it.knownReals += reals
			it.knownStrings += strings
		]
	}
	private def dispatch shortValue(Boolean value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		// Do nothing
	}
	private def dispatch shortValue(Integer value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		integers += value
	}
	private def dispatch shortValue(Short value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		integers += Integer.valueOf(value)
	}
	private def dispatch shortValue(Float value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		reals += Double.valueOf(value)
	}
	private def dispatch shortValue(Double value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		reals += value
	}
	private def dispatch shortValue(String value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		strings += value
	}
	private def dispatch shortValue(Void value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		// Do nothing
	}
	private def dispatch shortValue(Object value, Set<Integer> integers, Set<Double> reals, Set<String> strings) {
		// Do nothing
	}
	
	protected def translateLink(PartialRelationInterpretation interpretation, DefinedElement source, DefinedElement target) {
		interpretation.relationlinks += createBinaryElementRelationLink => [it.param1 = source it.param2 = target]
	}
	
	dispatch protected def translateValue(Enumerator value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		val term = this.ecore2Logic.Literal(ecore2LogicTrace,value)
		if(term instanceof DefinedElement) {
			return term
		}
		else throw new AssertionError('''term should be a defined element?''')
	}
	
	dispatch protected def translateValue(Boolean value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		value.lookup(partialInterpretationTrace.primitiveValues.booleanMap)
	}
	
	dispatch protected def translateValue(Integer value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		value.lookup(partialInterpretationTrace.primitiveValues.integerMap)
	}
	
	dispatch protected def translateValue(Short value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		Integer.valueOf(value).lookup(partialInterpretationTrace.primitiveValues.integerMap)
	}
	
	dispatch protected def translateValue(Double value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		value.lookup(partialInterpretationTrace.primitiveValues.realMap)
	}
	
	dispatch protected def translateValue(Float value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		Double.valueOf(value).lookup(partialInterpretationTrace.primitiveValues.realMap)
	}
	
	dispatch protected def translateValue(String value, Ecore2Logic_Trace ecore2LogicTrace, Problem2PartialInterpretationTrace partialInterpretationTrace) {
		value.lookup(partialInterpretationTrace.primitiveValues.stringMap)
	}
}