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

import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder
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.SymbolicDeclaration
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.viatrasolver.partialinterpretationlanguage.partial2logicannotations.Partial2logicannotationsFactory
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRelationInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RelationLink
import java.util.ArrayList
import java.util.HashMap
import java.util.HashSet
import java.util.Map
import java.util.Set
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.lib.annotations.Data

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement
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.RealElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.StringElement

@Data class PartialInterpretation2Logic_Trace {
	Map<DefinedElement,DefinedElement> new2Old = new HashMap
	
	Map<TypeDeclaration, TypeDefinition> definedPart = new HashMap
	Map<TypeDeclaration, TypeDeclaration> undefinedPart = new HashMap
	Set<Type> originalTypes = new HashSet
	Set<TypeDeclaration> splittedTypes = new HashSet
}

class PartialInterpretation2Logic {
	val extension LogiclanguageFactory factory = LogiclanguageFactory.eINSTANCE
	val extension LogicProblemBuilder builder = new LogicProblemBuilder
	
	def transformPartialIntepretation2Logic(LogicProblem p, PartialInterpretation i) {
		val trace = new PartialInterpretation2Logic_Trace()
		trace.originalTypes.addAll(p.types)
		if(! i.newElements.empty) {
			
			// Elements adding
			addExistingElementToProblem(p,i,trace)
			
			// Types
			for(partialTypeDeclaration : i.partialtypeinterpratation.filter(PartialComplexTypeInterpretation)) {
				splitTypeIntoTwo(p,partialTypeDeclaration,trace)
			}
			connectSplittings(p,trace)
			
			// Relations
			for(partialRelationInterpretation : i.partialrelationinterpretation) {
				//println(partialRelationInterpretation.interpretationOf.name)
				relationLinksToAssertion(p,partialRelationInterpretation,trace)
			}
		}
	}
	
	private def addExistingElementToProblem(LogicProblem p, PartialInterpretation i,PartialInterpretation2Logic_Trace trace) {
//		val newElements = new ArrayList(i.newElements)
//		var newElementIndex = 1
//		for(newElement : newElements) {
//			newElement.name = '''o «newElementIndex++»'''
//			p.elements += newElement
//		}
		p.elements += i.newElements.filter[!it instanceof PrimitiveElement]
	}
	
	private def splitTypeIntoTwo(LogicProblem p, PartialComplexTypeInterpretation partialTypeDeclaration,PartialInterpretation2Logic_Trace trace) {
		if(!partialTypeDeclaration.elements.empty) {
			val declaration = partialTypeDeclaration.interpretationOf
			
			val definedPart = createTypeDefinition => [
				it.name = '''«declaration.name» DefinedPart'''
				it.elements += partialTypeDeclaration.elements
				it.isAbstract = declaration.isIsAbstract
			]
			val undefinedPart = createTypeDeclaration => [
				it.name = '''«declaration.name» UndefinedPart'''
				it.isAbstract = declaration.isIsAbstract
			]
			declaration.isAbstract = true
			
			trace.definedPart.put(declaration,definedPart)
			trace.undefinedPart.put(declaration,undefinedPart)
			trace.splittedTypes.add(declaration)
			p.add(definedPart)
			p.add(undefinedPart)
			Supertype(definedPart,declaration)
			Supertype(undefinedPart,declaration)
			
			for(containment : p.containmentHierarchies) {
				if(containment.typesOrderedInHierarchy.contains(declaration)) {
					containment.typesOrderedInHierarchy += definedPart
					containment.typesOrderedInHierarchy += undefinedPart
				}
			}
		}
	}
	
	private def connectSplittings(LogicProblem p,PartialInterpretation2Logic_Trace trace)  {
		val originalTypes = p.types.filter[originalType(trace)].toList
		for(type : originalTypes) {
			val superTypes = new ArrayList(type.supertypes.filter[originalType(trace)].toList)
			for(supertype : superTypes) {
				if(type.isSplitted(trace)) {
					if(supertype.isSplitted(trace)) {
						Supertype((type as TypeDeclaration).lookup(trace.definedPart), (supertype as TypeDeclaration).lookup(trace.definedPart))
						Supertype((type as TypeDeclaration).lookup(trace.undefinedPart), (supertype as TypeDeclaration).lookup(trace.undefinedPart))
					} else {
						// Do nothing
					}
				} else {
					if(supertype.isSplitted(trace)) {
						Supertype(type, (supertype as TypeDeclaration).lookup(trace.undefinedPart))
					} else if(supertype instanceof TypeDefinition) {
						// Do nothing
					}
				}
			}
		}
	}
	
	private def originalType(Type type,PartialInterpretation2Logic_Trace trace) {
		return trace.originalTypes.contains(type)
	}
	private def isSplitted(Type t, PartialInterpretation2Logic_Trace trace) {
		trace.splittedTypes.contains(t)
	}
	
	private def relationLinksToAssertion(LogicProblem p, PartialRelationInterpretation r,PartialInterpretation2Logic_Trace trace) {
		val relation = r.interpretationOf
		val links = r.relationlinks
		if(links.size == 0) {
			// No assertion for empty relations
			return
		} else {
			val term = if(links.size == 1) {
				createLink(links.head,relation)
			} else {
				links.map[link|createLink(link,relation)].And
			}
			val assertion = Assertion('''PartialInterpretation «r.interpretationOf.name»''',term)
			val ^annotation= Partial2logicannotationsFactory.eINSTANCE.createPartialModelRelation2Assertion
			^annotation.target = assertion
			^annotation.targetRelation = relation
			//^annotation.links += links.map[EcoreUtil.copy(it)]
			//val error= assertion.eAllContents.toIterable.filter(SymbolicValue).filter[it.symbolicReference === null] 
			//error.forEach[println("error")]
			p.add(assertion)
			p.annotations+= ^annotation
		}
	}
	
	def private createLink(RelationLink link, SymbolicDeclaration relationDeclaration) {
		if(link instanceof BinaryElementRelationLink) {
			if((link.param1 !== null) && (link.param2 !== null)) {
				return createSymbolicValue=>[
					it.symbolicReference=relationDeclaration
					it.parameterSubstitutions += createValueInLink(link.param1)
					it.parameterSubstitutions += createValueInLink(link.param2)
				]
			} else {
				throw new IllegalArgumentException
			}
		} else throw new UnsupportedOperationException
	}
	
	def private dispatch createValueInLink(BooleanElement element) {
		return element.value.asTerm
	}
	def private dispatch createValueInLink(IntegerElement element) {
		return element.value.asTerm
	}
	def private dispatch createValueInLink(RealElement element) {
		return element.value.asTerm
	}
	def private dispatch createValueInLink(StringElement element) {
		return element.value.asTerm
	}
	def private dispatch createValueInLink(DefinedElement element) {
		return createSymbolicValue => [it.symbolicReference = element]
	}
}