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

import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.NeighbourhoodOptions
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2Hash
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2NeighbourhoodRepresentation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import java.util.ArrayList
import java.util.Map
import org.eclipse.viatra.query.runtime.api.IPatternMatch

class NeighbourhoodBasedStateCoderFactory extends AbstractNeighbourhoodBasedStateCoderFactory {
	new() {
	}

	new(NeighbourhoodOptions options) {
		super(options)
	}

	override protected doCreateStateCoder(NeighbourhoodOptions options) {
		new NeighbourhoodBasedPartialInterpretationStateCoder(new PartialInterpretation2ImmutableTypeLattice, options)
	}
}

class NeighbourhoodBasedHashStateCoderFactory extends AbstractNeighbourhoodBasedStateCoderFactory {
	new() {
	}

	new(NeighbourhoodOptions options) {
		super(options)
	}

	override protected doCreateStateCoder(NeighbourhoodOptions options) {
		new NeighbourhoodBasedPartialInterpretationStateCoder(new PartialInterpretation2Hash, options)
	}
}

class NeighbourhoodBasedPartialInterpretationStateCoder<ModelRep, NodeRep> extends AbstractNeighbourhoodBasedPartialInterpretationStateCoder {
	val PartialInterpretation2NeighbourhoodRepresentation<ModelRep, NodeRep> calculator
	var Map<DefinedElement, ? extends NodeRep> nodeRepresentations = null
	var ModelRep modelRepresentation = null

	new(PartialInterpretation2NeighbourhoodRepresentation<ModelRep, NodeRep> calculator, NeighbourhoodOptions options) {
		super(options)
		this.calculator = calculator
	}

	override protected isRefreshNeeded() {
		nodeRepresentations === null || modelRepresentation === null
	}

	override doRefreshStateCodes(PartialInterpretation target, NeighbourhoodOptions options) {
		val code = calculator.createRepresentation(target, options)
		modelRepresentation = code.modelRepresentation
		nodeRepresentations = code.nodeRepresentations
	}

	override doCreateActivationCode(IPatternMatch match) {
		val size = match.specification.parameters.size
		val res = new ArrayList(size)
		var int equivalenceHash = 0
		val prime = 31

		for (var int index = 0; index < size; index++) {
			val matchArgument = match.get(index)
			res.add(getCode(matchArgument))
			for (var i = 0; i < index; i++) {
				val number = if (matchArgument === match.get(i)) {
						1
					} else {
						0
					}
				equivalenceHash = prime * equivalenceHash + number
			}
		}

		match.specification.fullyQualifiedName -> (res -> equivalenceHash).hashCode
	}

	def private getCode(Object o) {
		switch (o) {
			DefinedElement:
				nodeRepresentations.get(o)
			default:
				getFallbackCode(o)
		}
	}

	override doCreateStateCode() {
		modelRepresentation.hashCode
	}

	override doInvalidate() {
		nodeRepresentations = null
		modelRepresentation = null
	}
}