aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ScopePropagator.xtend
blob: 8012776fc12b057edc7be3c3f7efcf7bb39f9145 (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
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra

import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope
import java.util.HashMap
import java.util.Map
import java.util.Set
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation
import java.util.HashSet
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation

class ScopePropagator {
	PartialInterpretation partialInterpretation
	Map<PartialTypeInterpratation,Scope> type2Scope
	
	val Map<Scope, Set<Scope>> superScopes
	val Map<Scope, Set<Scope>> subScopes
	
	public new(PartialInterpretation p) {
		partialInterpretation = p
		type2Scope = new HashMap
		for(scope : p.scopes) {
			type2Scope.put(scope.targetTypeInterpretation,scope)
		}
		
		superScopes = new HashMap
		subScopes = new HashMap
		for(scope : p.scopes) {
			superScopes.put(scope,new HashSet)
			subScopes.put(scope,new HashSet)
		}
		
		for(scope : p.scopes) {
			val target = scope.targetTypeInterpretation
			if(target instanceof PartialComplexTypeInterpretation) {
				val supertypeInterpretations = target.supertypeInterpretation
				for(supertypeInterpretation : supertypeInterpretations) {
					val supertypeScope = type2Scope.get(supertypeInterpretation)
					superScopes.get(scope).add(supertypeScope)
					subScopes.get(supertypeScope).add(scope)
				}
			}
		}
	}
	
	def public propagateAllScopeConstraints() {
		var boolean hadChanged
		do{
			hadChanged = false
			for(superScopeEntry : superScopes.entrySet) {
				val sub = superScopeEntry.key
				hadChanged = propagateLowerLimitUp(sub,partialInterpretation) || hadChanged
				hadChanged = propagateUpperLimitDown(sub,partialInterpretation) || hadChanged
				for(sup: superScopeEntry.value) {
					hadChanged = propagateLowerLimitUp(sub,sup) || hadChanged
					hadChanged = propagateUpperLimitDown(sub,sup) || hadChanged
				}
			}
		} while(hadChanged)
//		println('''All constraints are propagated.''')
	}
	
	def public propagateAdditionToType(PartialTypeInterpratation t) {
		val isPrimitive = t instanceof PartialPrimitiveInterpretation || t === null
		if(!isPrimitive) {
	//		println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''')
			val targetScope = type2Scope.get(t)
			if(targetScope!==null) {
				targetScope.removeOne
				val sups = superScopes.get(targetScope)
				sups.forEach[removeOne]
			}
			
			
			if(this.partialInterpretation.minNewElements > 0 ) {
				this.partialInterpretation.minNewElements = this.partialInterpretation.minNewElements-1
			}
			if(this.partialInterpretation.maxNewElements > 0) {
				this.partialInterpretation.maxNewElements = this.partialInterpretation.maxNewElements-1
			} else if(this.partialInterpretation.maxNewElements === 0) {
				this.partialInterpretation.maxNewElements = 0
				//throw new IllegalArgumentException('''Inconsistent object creation: lower node limit is 0!''')
			}
			
	//		subScopes.get(targetScope).forEach[propagateUpperLimitDown(it,targetScope)]
	//		for(sup: sups) {
	//			subScopes.get(sup).forEach[propagateUpperLimitDown(it,sup)]
	//		}
	//		for(scope : type2Scope.values) {
	//			propagateUpperLimitDown(scope,partialInterpretation)
	//		}
	
			propagateAllScopeConstraints
			
	//		println('''Target Scope: «targetScope.minNewElements» - «targetScope.maxNewElements»''')
	//		println(''' «this.partialInterpretation.minNewElements» - «this.partialInterpretation.maxNewElements»''')
	//		this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')]
	//		println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''')
		}
	}
	
	private def propagateLowerLimitUp(Scope subScope, Scope superScope) {
		if(subScope.minNewElements>superScope.minNewElements) {
//			println('''
//			«(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> «(superScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»
//			superScope.minNewElements «superScope.minNewElements» = subScope.minNewElements «subScope.minNewElements»
//			''')
			superScope.minNewElements = subScope.minNewElements
			return true
		} else {
			return false
		}
	}
	
	private def propagateUpperLimitDown(Scope subScope, Scope superScope) {
		if(superScope.maxNewElements>=0 && (superScope.maxNewElements<subScope.maxNewElements || subScope.maxNewElements<0)) {
//			println('''
//			«(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> «(superScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»
//			subScope.maxNewElements «subScope.maxNewElements» = superScope.maxNewElements «superScope.maxNewElements»
//			''')
			subScope.maxNewElements = superScope.maxNewElements
			return true
		} else {
			return false
		}
	}
	
	private def propagateLowerLimitUp(Scope subScope, PartialInterpretation p) {
		if(subScope.minNewElements>p.minNewElements) {
//			println('''
//			«(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> nodes
//			p.minNewElements «p.minNewElements» = subScope.minNewElements «subScope.minNewElements»
//			''')
			p.minNewElements = subScope.minNewElements
			return true
		} else {
			return false
		}
	}
	
	private def propagateUpperLimitDown(Scope subScope, PartialInterpretation p) {
		if(p.maxNewElements>=0 && (p.maxNewElements<subScope.maxNewElements || subScope.maxNewElements<0)) {
//			println('''
//			«(subScope.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name» -> nodes
//			subScope.maxNewElements «subScope.maxNewElements» = p.maxNewElements «p.maxNewElements»
//			''')
			subScope.maxNewElements = p.maxNewElements
			return true
		} else {
			return false
		}
	}
	private def removeOne(Scope scope) {
		if(scope.maxNewElements===0) {
			scope.maxNewElements=0
			//throw new IllegalArgumentException('''Inconsistent object creation: «scope.targetTypeInterpretation»''')
		} else if(scope.maxNewElements>0) {
			scope.maxNewElements= scope.maxNewElements-1
		}
		if(scope.minNewElements>0) {
			scope.minNewElements= scope.minNewElements-1
		}
	}
}