aboutsummaryrefslogtreecommitdiffstats
path: root/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/LookupHandler.xtend
blob: 3a06dcc3bd4f78d2098c6110f87ae5d8dcdcd350 (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
package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft

import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.LookupDefinition
import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.MappingDefinition
import org.eclipse.viatra.query.runtime.api.IPatternMatch
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher

class LookupHandler {
	val int[] argumentIndices
	val ViatraQueryMatcher<? extends IPatternMatch> lookedUpMatcher

	new(MappingDefinition mappingDefinition, LookupDefinition lookupDefinition,
		ViatraQueryMatcher<? extends IPatternMatch> lookedUpMatcher) {
		if (lookupDefinition.eContainer != mappingDefinition) {
			throw new IllegalArgumentException("lookupDefinition must be contained in mappingDefinition")
		}
		val argumentCount = lookupDefinition.arguments.size
		if (argumentCount != lookedUpMatcher.parameterNames.length) {
			throw new IllegalArgumentException(
				"lookupDefinition (name: " + lookupDefinition.mapping?.pattern?.name +
					") must have as many arguments as lookedUpMatcher (name: " + lookedUpMatcher.patternName + ")")
		}
		argumentIndices = newIntArrayOfSize(argumentCount)
		for (var int i = 0; i < argumentCount; i++) {
			val argument = lookupDefinition.arguments.get(i)
			val argumentIndex = mappingDefinition.parameters.indexOf(argument)
			argumentIndices.set(i, argumentIndex)
		}
		this.lookedUpMatcher = lookedUpMatcher
	}

	def lookupForMatch(ComponentFaultTreeTrace faultTreeTrace, IPatternMatch match) {
		val lookedUpMatch = createLookedUpMatch(match)
		faultTreeTrace.lookup(lookedUpMatch)
	}

	private def createLookedUpMatch(IPatternMatch match) {
		val lookedUpMatch = lookedUpMatcher.newEmptyMatch
		val argumentCount = argumentIndices.length
		for (var int i = 0; i < argumentCount; i++) {
			val argumentIndex = argumentIndices.get(i)
			var argumentValue = match.get(argumentIndex)
			lookedUpMatch.set(i, argumentValue)
		}
		lookedUpMatch
	}
}