blob: 04c4950692ce9b48a3547aaf5e51856bda5bafc7 (
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
|
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
import org.eclipse.viatra.query.runtime.api.IQuerySpecification
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
val Map<IQuerySpecification<?>, String> fullyQualifiedNames = newHashMap
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
}
private def getFullyQualifiedNameCached(IQuerySpecification<?> specification) {
fullyQualifiedNames.computeIfAbsent(specification, [fullyQualifiedName])
}
override doCreateActivationCode(IPatternMatch match) {
val size = match.specification.parameters.size
var int hash = 0
val prime = 31
for (var int index = 0; index < size; index++) {
val matchArgument = match.get(index)
val code = getCode(matchArgument)
val codeNumber = if (code === null) {
0
} else {
code.hashCode
}
hash = prime * hash + codeNumber
for (var i = 0; i < index; i++) {
val number = if (matchArgument === match.get(i)) {
1
} else {
0
}
hash = prime * hash + number
}
}
match.specification.fullyQualifiedNameCached -> hash
}
def private getCode(Object o) {
switch (o) {
DefinedElement:
nodeRepresentations.get(o)
default:
getFallbackCode(o)
}
}
override doCreateStateCode() {
modelRepresentation.hashCode
}
override doInvalidate() {
nodeRepresentations = null
modelRepresentation = null
}
}
|