aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/PairwiseNeighbourhoodBasedStateCoderFactory.xtend
blob: 84e798f2c6b41b60e48f8a2a52d3dc43a7a62393 (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
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.AbstractNodeDescriptor
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.NeighbourhoodOptions
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PairwiseNeighbourhoodRepresentation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2PairwiseNeighbourhoodRepresentation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import java.util.ArrayList
import org.eclipse.viatra.query.runtime.api.IPatternMatch

class PairwiseNeighbourhoodBasedStateCoderFactory extends AbstractNeighbourhoodBasedStateCoderFactory {
	new() {
	}

	new(NeighbourhoodOptions options) {
		super(options)
	}

	override protected doCreateStateCoder(NeighbourhoodOptions options) {
		new PairwiseNeighbourhoodBasedPartialInterpretationStateCoder(options)
	}
}

class PairwiseNeighbourhoodBasedPartialInterpretationStateCoder extends AbstractNeighbourhoodBasedPartialInterpretationStateCoder {
	val calculator = new PartialInterpretation2PairwiseNeighbourhoodRepresentation(
		new PartialInterpretation2ImmutableTypeLattice)
	var PairwiseNeighbourhoodRepresentation<? extends AbstractNodeDescriptor> representation

	new(NeighbourhoodOptions options) {
		super(options)
	}

	override protected isRefreshNeeded() {
		representation === null
	}

	override protected doRefreshStateCodes(PartialInterpretation target, NeighbourhoodOptions options) {
		representation = calculator.createRepresentation(target, options)
	}

	override protected doCreateActivationCode(IPatternMatch match) {
		val size = match.specification.parameters.size
		val res = new ArrayList(size * size)
		for (var int i = 0; i < size; i++) {
			val a = match.get(i)
			for (var int j = 0; j < size; j++) {
				val b = match.get(j)
				res.add(getPairwiseRepresentation(a, b))
			}
		}
		match.specification.fullyQualifiedName -> res.hashCode
	}

	private def getPairwiseRepresentation(Object a, Object b) {
		if (b instanceof DefinedElement) {
			if (a instanceof DefinedElement) {
				representation.getPairwiseRepresentation(a, b)
			} else {
				representation.getBasicRepresentation(b)
			}
		} else {
			getFallbackCode(b)
		}
	}

	override protected doCreateStateCode() {
		representation.modelRepresentation.hashCode
	}

	override protected doInvalidate() {
		representation = null
	}
}