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

import java.util.HashMap
import java.util.Map
import java.util.Set
import org.eclipse.xtend.lib.annotations.Data
import org.eclipse.xtend2.lib.StringConcatenationClient

@Data abstract class AbstractNodeDescriptor {
	int dataHash

	protected def StringConcatenationClient prettyPrint() {
		'''(«dataHash»)[«class.simpleName»]'''
	}

	override toString() {
		'''
			«prettyPrint»
		'''
	}

//	@Pure
//	@Override
//	override public boolean equals(Object obj) {
//		if (this === obj)
//			return true;
//		if (obj === null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		val AbstractNodeDescriptor other = obj as AbstractNodeDescriptor;
//		if (other.dataHash != this.dataHash)
//			return false;
//		return true;
//	}
}

@Data class LocalNodeDescriptor extends AbstractNodeDescriptor {
	Set<String> types
	String id;

	new(String id, Set<String> types) {
		super(calcualteDataHash(id, types))
		this.types = types
		this.id = id
	}

	def private static calcualteDataHash(String id, Set<String> types) {
		val int prime = 31;
		var result = 0
		if (id !== null)
			result = id.hashCode();
		if (types !== null) {
			result = prime * result + types.hashCode
		}
		return result
	}

	override hashCode() {
		return this.dataHash
	}

	override equals(Object other) {
		other.class == LocalNodeDescriptor && (other as AbstractNodeDescriptor).hashCode == hashCode
	}

	override protected prettyPrint() {
		'''(«dataHash»)[«IF id !== null»id = "«id»"«IF types === null || !types.empty», «ENDIF»«ENDIF»«IF types === null»TYPES = null«ELSE»«FOR type : types SEPARATOR ", "»«type»«ENDFOR»«ENDIF»]'''
	}

	override toString() {
		'''
			«prettyPrint»
		'''
	}

//	@Pure
//	@Override
//	override public boolean equals(Object obj) {
//		if (this === obj)
//			return true;
//		if (obj === null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		val AbstractNodeDescriptor other = obj as AbstractNodeDescriptor;
//		if (other.dataHash != this.dataHash)
//			return false;
//		return true;
//	}
//	@Pure
//	override public boolean equals(Object obj) {
//		if (this === obj)
//			return true;
//		if (obj === null)
//			return false;
//		if (getClass() != obj.getClass())
//			return false;
//		if (!super.equals(obj))
//			return false;
//		val LocalNodeDescriptor other = obj as LocalNodeDescriptor;
//		if (this.clazz === null) {
//			if (other.clazz != null)
//				return false;
//		} else if (!this.clazz.equals(other.clazz))
//			return false;
//		return true;
//	}
}

@Data class IncomingRelation<FROM> {
	FROM from
	String type
}

@Data class OutgoingRelation<TO> {
	TO to
	String type
}

@Data class FurtherNodeDescriptor<NodeRep> extends AbstractNodeDescriptor {
	NodeRep previousRepresentation
	Map<IncomingRelation<NodeRep>, Integer> incomingEdges
	Map<OutgoingRelation<NodeRep>, Integer> outgoingEdges

	new(NodeRep previousRepresentation, Map<IncomingRelation<NodeRep>, Integer> incomingEdges,
		Map<OutgoingRelation<NodeRep>, Integer> outgoingEdges) {
		super(calculateDataHash(previousRepresentation, incomingEdges, outgoingEdges))
		this.previousRepresentation = previousRepresentation
		this.incomingEdges = incomingEdges
		this.outgoingEdges = outgoingEdges
	}

	static def private <NodeRep> int calculateDataHash(NodeRep previousRepresentation,
		Map<IncomingRelation<NodeRep>, Integer> incomingEdges, Map<OutgoingRelation<NodeRep>, Integer> outgoingEdges) {
		val int prime = 31;
		var int result = previousRepresentation.hashCode;
		if (incomingEdges !== null)
			result = prime * result + hashIncomingNeighborhood(incomingEdges)
		if (outgoingEdges !== null)
			result = prime * result + hashOutgoingNeighborhood(outgoingEdges)
		return result;
	}

	override hashCode() {
		return this.dataHash
	}

	override equals(Object other) {
		other.class == FurtherNodeDescriptor && (other as AbstractNodeDescriptor).hashCode == hashCode
	}

	override prettyPrint() {
		'''
dataHash»)[
			PREV = «previousRepresentation?.prettyPrint»
			«IF incomingEdges === null»
				IN null
			«ELSE»
				«FOR edge : incomingEdges.entrySet»
					IN «edge.value» «edge.key.type» = «edge.key.from.prettyPrint»
				«ENDFOR»
			«ENDIF»
			«IF outgoingEdges === null»
				OUT null
			«ELSE»
				«FOR edge : outgoingEdges.entrySet»
					OUT «edge.value» «edge.key.type» = «edge.key.to.prettyPrint»
				«ENDFOR»
			«ENDIF»
		]'''
	}

	private def StringConcatenationClient prettyPrint(NodeRep rep) {
		if (rep instanceof AbstractNodeDescriptor) {
			rep.prettyPrint
		} else {
			'''«rep»'''
		}
	}
	
	private static def <NodeRep> hashIncomingNeighborhood(Map<IncomingRelation<NodeRep>, Integer> neighborhood) {
		val int prime = 31
		var int hash = 0
		for (entry : neighborhood.entrySet) {
			val relation = entry.key
			hash += (prime * relation.from.hashCode + relation.type.hashCode).bitwiseXor(entry.value.hashCode)
		}
		hash
	}
	
	private static def <NodeRep> hashOutgoingNeighborhood(Map<OutgoingRelation<NodeRep>, Integer> neighborhood) {
		val int prime = 31
		var int hash = 0
		for (entry : neighborhood.entrySet) {
			val relation = entry.key
			hash += (prime * relation.to.hashCode + relation.type.hashCode).bitwiseXor(entry.value.hashCode)
		}
		hash
	}

	override toString() {
		'''
			«prettyPrint»
		'''
	}
}