diff options
author | 2019-02-07 18:24:45 +0100 | |
---|---|---|
committer | 2019-02-19 19:18:25 +0100 | |
commit | 6f88c4bd4e91728cea62505e893b0ce300baf7fc (patch) | |
tree | fef4d5cf7d28b0e4fd59a3b7181e60a2f236d235 /Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu | |
parent | Extract component fault tree model (diff) | |
download | VIATRA-Generator-6f88c4bd4e91728cea62505e893b0ce300baf7fc.tar.gz VIATRA-Generator-6f88c4bd4e91728cea62505e893b0ce300baf7fc.tar.zst VIATRA-Generator-6f88c4bd4e91728cea62505e893b0ce300baf7fc.zip |
Ecore2Cft transformation
Diffstat (limited to 'Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu')
9 files changed, 348 insertions, 0 deletions
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/cft2ft/Cft2FtTransformation.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/cft2ft/Cft2FtTransformation.xtend new file mode 100644 index 00000000..caaffc3c --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/cft2ft/Cft2FtTransformation.xtend | |||
@@ -0,0 +1,10 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.cft2ft | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.ComponentFaultTree | ||
4 | |||
5 | class Cft2FtTransformation { | ||
6 | def createFaultTree(ComponentFaultTree componentFaultTree) { | ||
7 | // TODO | ||
8 | throw new UnsupportedOperationException() | ||
9 | } | ||
10 | } \ No newline at end of file | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentFaultTreeTrace.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentFaultTreeTrace.xtend new file mode 100644 index 00000000..7a3e377b --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentFaultTreeTrace.xtend | |||
@@ -0,0 +1,39 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.CftFactory | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.ComponentDefinition | ||
5 | import java.util.Map | ||
6 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
7 | import org.eclipse.xtend.lib.annotations.Accessors | ||
8 | |||
9 | class ComponentFaultTreeTrace { | ||
10 | @Accessors val componentFaultTree = CftFactory.eINSTANCE.createComponentFaultTree | ||
11 | |||
12 | val nameGenerator = new ComponentNameGenerator | ||
13 | val Map<IPatternMatch, ComponentInstanceTrace> componentInstancesMap = newHashMap | ||
14 | |||
15 | def instantiateComponent(IPatternMatch patternMatch, ComponentDefinition componenDefinition) { | ||
16 | if (componentInstancesMap.containsKey(patternMatch)) { | ||
17 | throw new IllegalArgumentException("Already instantiated component for match: " + patternMatch) | ||
18 | } | ||
19 | val componentTrace = new ComponentInstanceTrace(componentFaultTree, componenDefinition, nameGenerator) | ||
20 | componentInstancesMap.put(patternMatch, componentTrace) | ||
21 | componentTrace | ||
22 | } | ||
23 | |||
24 | def setTopLevel(ComponentInstanceTrace trace) { | ||
25 | if (componentFaultTree.topEvent !== null) { | ||
26 | throw new IllegalArgumentException("Top level component already set") | ||
27 | } | ||
28 | val outputs = trace.outputs | ||
29 | if (outputs.size !== 1) { | ||
30 | throw new IllegalArgumentException("Top level component must have 1 output, got " + outputs.size + | ||
31 | " instead") | ||
32 | } | ||
33 | componentFaultTree.topEvent = outputs.head | ||
34 | } | ||
35 | |||
36 | def lookup(IPatternMatch patternMatch) { | ||
37 | componentInstancesMap.get(patternMatch) | ||
38 | } | ||
39 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentInstanceTrace.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentInstanceTrace.xtend new file mode 100644 index 00000000..7353bfe5 --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentInstanceTrace.xtend | |||
@@ -0,0 +1,51 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import com.google.common.collect.Maps | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.CftFactory | ||
5 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.ComponentDefinition | ||
6 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.ComponentFaultTree | ||
7 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.EventDeclaration | ||
8 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.InputEvent | ||
9 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.Output | ||
10 | import java.util.Map | ||
11 | |||
12 | class ComponentInstanceTrace { | ||
13 | val componentInstance = CftFactory.eINSTANCE.createComponent | ||
14 | val Map<InputEvent, InputTrace> inputEventsMap | ||
15 | val Map<EventDeclaration, Output> outputEventsMap | ||
16 | |||
17 | protected new(ComponentFaultTree faultTree, ComponentDefinition componentDefinition, | ||
18 | ComponentNameGenerator nameGenerator) { | ||
19 | componentInstance.componentDefinition = componentDefinition | ||
20 | componentInstance.name = nameGenerator.nextName(componentDefinition) | ||
21 | inputEventsMap = Maps.newHashMapWithExpectedSize(componentDefinition.inputEvents.size) | ||
22 | for (inputEvent : componentDefinition.inputEvents) { | ||
23 | val inputTrace = new InputTrace(componentInstance, inputEvent) | ||
24 | inputEventsMap.put(inputEvent, inputTrace) | ||
25 | } | ||
26 | outputEventsMap = Maps.newHashMapWithExpectedSize(componentDefinition.outputEvents.size) | ||
27 | for (outputEvent : componentDefinition.outputEvents) { | ||
28 | val output = CftFactory.eINSTANCE.createOutput | ||
29 | output.eventDeclaration = outputEvent | ||
30 | componentInstance.outputs += output | ||
31 | outputEventsMap.put(outputEvent, output) | ||
32 | } | ||
33 | faultTree.components += componentInstance | ||
34 | } | ||
35 | |||
36 | def void assign(EventDeclaration inputEvent, ComponentInstanceTrace sourceComponent, EventDeclaration outputEvent) { | ||
37 | val inputTrace = inputEventsMap.get(inputEvent) | ||
38 | if (inputTrace === null) { | ||
39 | throw new IllegalArgumentException("Unknown input: " + inputEvent) | ||
40 | } | ||
41 | val output = sourceComponent.outputEventsMap.get(outputEvent) | ||
42 | if (output === null) { | ||
43 | throw new IllegalArgumentException("Unknown output: " + outputEvent) | ||
44 | } | ||
45 | inputTrace.assign(output) | ||
46 | } | ||
47 | |||
48 | protected def getOutputs() { | ||
49 | componentInstance.outputs | ||
50 | } | ||
51 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentNameGenerator.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentNameGenerator.xtend new file mode 100644 index 00000000..71d40a9b --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/ComponentNameGenerator.xtend | |||
@@ -0,0 +1,16 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.ComponentDefinition | ||
4 | import java.util.Map | ||
5 | |||
6 | class ComponentNameGenerator { | ||
7 | static val DEFAULT_NAME = "__unnamed" | ||
8 | |||
9 | val Map<ComponentDefinition, Integer> instanceCounts = newHashMap | ||
10 | |||
11 | def nextName(ComponentDefinition componentDefinition) { | ||
12 | val instanceCount = instanceCounts.getOrDefault(componentDefinition, 0) | ||
13 | instanceCounts.put(componentDefinition, instanceCount + 1) | ||
14 | (componentDefinition.name ?: DEFAULT_NAME) + instanceCount | ||
15 | } | ||
16 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/Ecore2CftTransformation.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/Ecore2CftTransformation.xtend new file mode 100644 index 00000000..36aac5e1 --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/Ecore2CftTransformation.xtend | |||
@@ -0,0 +1,27 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import com.google.common.collect.ImmutableList | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.TransformationDefinition | ||
5 | import java.util.List | ||
6 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | ||
7 | |||
8 | class Ecore2CftTransformation { | ||
9 | val List<MappingHandler> mappingHandlers | ||
10 | |||
11 | new(TransformationDefinition transformationDefinition, ViatraQueryEngine viatraQueryEngine) { | ||
12 | val mappingQueries = new MappingQueries(transformationDefinition, viatraQueryEngine) | ||
13 | mappingHandlers = ImmutableList.copyOf(transformationDefinition.mappingDefinitions.map [ mappingDefinition | | ||
14 | new MappingHandler(mappingDefinition, mappingQueries) | ||
15 | ]) | ||
16 | } | ||
17 | |||
18 | def createComponentFaultTree() { | ||
19 | val trace = new ComponentFaultTreeTrace | ||
20 | for (handler : mappingHandlers) { | ||
21 | handler.instantiateComponents(trace) | ||
22 | } | ||
23 | for (handler : mappingHandlers) { | ||
24 | handler.instantiateConnections(trace) | ||
25 | } | ||
26 | } | ||
27 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/InputTrace.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/InputTrace.xtend new file mode 100644 index 00000000..c529a09b --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/InputTrace.xtend | |||
@@ -0,0 +1,29 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.CftFactory | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.Component | ||
5 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.Connection | ||
6 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.Input | ||
7 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.InputEvent | ||
8 | import hu.bme.mit.inf.dslreasoner.faulttree.model.cft.Output | ||
9 | import java.util.Map | ||
10 | |||
11 | class InputTrace { | ||
12 | val Input input = CftFactory.eINSTANCE.createInput | ||
13 | val Map<Output, Connection> connectionsMap = newHashMap | ||
14 | |||
15 | protected new(Component component, InputEvent inputEvent) { | ||
16 | input.inputEvent = inputEvent | ||
17 | component.inputs += input | ||
18 | } | ||
19 | |||
20 | def void assign(Output output) { | ||
21 | val connection = connectionsMap.get(output) | ||
22 | if (connection === null) { | ||
23 | val newConnection = CftFactory.eINSTANCE.createConnection | ||
24 | newConnection.output = output | ||
25 | input.incomingConnections += newConnection | ||
26 | connectionsMap.put(output, newConnection) | ||
27 | } | ||
28 | } | ||
29 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/LookupHandler.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/LookupHandler.xtend new file mode 100644 index 00000000..3a06dcc3 --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/LookupHandler.xtend | |||
@@ -0,0 +1,47 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.LookupDefinition | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.MappingDefinition | ||
5 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
6 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | ||
7 | |||
8 | class LookupHandler { | ||
9 | val int[] argumentIndices | ||
10 | val ViatraQueryMatcher<? extends IPatternMatch> lookedUpMatcher | ||
11 | |||
12 | new(MappingDefinition mappingDefinition, LookupDefinition lookupDefinition, | ||
13 | ViatraQueryMatcher<? extends IPatternMatch> lookedUpMatcher) { | ||
14 | if (lookupDefinition.eContainer != mappingDefinition) { | ||
15 | throw new IllegalArgumentException("lookupDefinition must be contained in mappingDefinition") | ||
16 | } | ||
17 | val argumentCount = lookupDefinition.arguments.size | ||
18 | if (argumentCount != lookedUpMatcher.parameterNames.length) { | ||
19 | throw new IllegalArgumentException( | ||
20 | "lookupDefinition (name: " + lookupDefinition.mapping?.pattern?.name + | ||
21 | ") must have as many arguments as lookedUpMatcher (name: " + lookedUpMatcher.patternName + ")") | ||
22 | } | ||
23 | argumentIndices = newIntArrayOfSize(argumentCount) | ||
24 | for (var int i = 0; i < argumentCount; i++) { | ||
25 | val argument = lookupDefinition.arguments.get(i) | ||
26 | val argumentIndex = mappingDefinition.parameters.indexOf(argument) | ||
27 | argumentIndices.set(i, argumentIndex) | ||
28 | } | ||
29 | this.lookedUpMatcher = lookedUpMatcher | ||
30 | } | ||
31 | |||
32 | def lookupForMatch(ComponentFaultTreeTrace faultTreeTrace, IPatternMatch match) { | ||
33 | val lookedUpMatch = createLookedUpMatch(match) | ||
34 | faultTreeTrace.lookup(lookedUpMatch) | ||
35 | } | ||
36 | |||
37 | private def createLookedUpMatch(IPatternMatch match) { | ||
38 | val lookedUpMatch = lookedUpMatcher.newEmptyMatch | ||
39 | val argumentCount = argumentIndices.length | ||
40 | for (var int i = 0; i < argumentCount; i++) { | ||
41 | val argumentIndex = argumentIndices.get(i) | ||
42 | var argumentValue = match.get(argumentIndex) | ||
43 | lookedUpMatch.set(i, argumentValue) | ||
44 | } | ||
45 | lookedUpMatch | ||
46 | } | ||
47 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend new file mode 100644 index 00000000..643af5c4 --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend | |||
@@ -0,0 +1,91 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import com.google.common.collect.ImmutableMap | ||
4 | import com.google.common.collect.Maps | ||
5 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.LookupDefinition | ||
6 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.MappingDefinition | ||
7 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.Variable | ||
8 | import java.util.Map | ||
9 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
10 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | ||
11 | |||
12 | class MappingHandler { | ||
13 | val ViatraQueryMatcher<? extends IPatternMatch> matcher | ||
14 | val MappingDefinition mappingDefinition | ||
15 | val Map<LookupDefinition, LookupHandler> lookupHandlers | ||
16 | |||
17 | new(MappingDefinition mappingDefinition, MappingQueries mappingQueries) { | ||
18 | matcher = mappingQueries.getMatcher(mappingDefinition) | ||
19 | this.mappingDefinition = mappingDefinition | ||
20 | val variables = newHashSet | ||
21 | for (assignment : mappingDefinition.assignments) { | ||
22 | variables += assignment.input.component | ||
23 | variables += assignment.output.component | ||
24 | } | ||
25 | lookupHandlers = ImmutableMap.copyOf(variables.filter(LookupDefinition).toMap([it], [ lookupDefinition | | ||
26 | mappingQueries.createLookupHandler(mappingDefinition, lookupDefinition) | ||
27 | ])) | ||
28 | } | ||
29 | |||
30 | def instantiateComponents(ComponentFaultTreeTrace faultTreeTrace) { | ||
31 | if (!hasComponentInstace) { | ||
32 | return | ||
33 | } | ||
34 | matcher.forEachMatch [ match | | ||
35 | val componentTrace = faultTreeTrace.instantiateComponent(match, componentDefinition) | ||
36 | if (isTopLevel) { | ||
37 | faultTreeTrace.topLevel = componentTrace | ||
38 | } | ||
39 | ] | ||
40 | } | ||
41 | |||
42 | def instantiateConnections(ComponentFaultTreeTrace faultTreeTrace) { | ||
43 | if (!hasConnections) { | ||
44 | return | ||
45 | } | ||
46 | matcher.forEachMatch [ match | | ||
47 | val lookedUpComponents = lookupComponents(faultTreeTrace, match) | ||
48 | for (assignment : mappingDefinition.assignments) { | ||
49 | val input = assignment.input | ||
50 | val inputComponent = lookedUpComponents.get(input.component) | ||
51 | val output = assignment.output | ||
52 | val outputComponent = lookedUpComponents.get(output.component) | ||
53 | if (inputComponent !== null && outputComponent !== null) { | ||
54 | inputComponent.assign(input.event, outputComponent, output.event) | ||
55 | } | ||
56 | } | ||
57 | ] | ||
58 | } | ||
59 | |||
60 | private def Map<Variable, ComponentInstanceTrace> lookupComponents(ComponentFaultTreeTrace faultTreeTrace, | ||
61 | IPatternMatch match) { | ||
62 | val lookedUpComponents = Maps.newHashMapWithExpectedSize(lookupHandlers.size + 1) | ||
63 | if (hasComponentInstace) { | ||
64 | val componentInstance = faultTreeTrace.lookup(match) | ||
65 | lookedUpComponents.put(mappingDefinition.componentInstance, componentInstance) | ||
66 | } | ||
67 | for (pair : lookupHandlers.entrySet) { | ||
68 | val componentInstance = pair.value.lookupForMatch(faultTreeTrace, match) | ||
69 | if (componentInstance !== null) { | ||
70 | lookedUpComponents.put(pair.key, componentInstance) | ||
71 | } | ||
72 | } | ||
73 | lookedUpComponents | ||
74 | } | ||
75 | |||
76 | private def getComponentDefinition() { | ||
77 | mappingDefinition.componentInstance?.componentType | ||
78 | } | ||
79 | |||
80 | private def hasComponentInstace() { | ||
81 | componentDefinition !== null | ||
82 | } | ||
83 | |||
84 | private def isTopLevel() { | ||
85 | mappingDefinition.topLevel | ||
86 | } | ||
87 | |||
88 | private def hasConnections() { | ||
89 | !mappingDefinition.assignments.empty | ||
90 | } | ||
91 | } | ||
diff --git a/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingQueries.xtend b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingQueries.xtend new file mode 100644 index 00000000..6683b3f8 --- /dev/null +++ b/Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingQueries.xtend | |||
@@ -0,0 +1,38 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft | ||
2 | |||
3 | import com.google.common.collect.ImmutableMap | ||
4 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.LookupDefinition | ||
5 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.MappingDefinition | ||
6 | import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.TransformationDefinition | ||
7 | import java.util.Map | ||
8 | import org.eclipse.viatra.query.patternlanguage.emf.specification.SpecificationBuilder | ||
9 | import org.eclipse.viatra.query.runtime.api.GenericQueryGroup | ||
10 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
11 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | ||
12 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | ||
13 | |||
14 | class MappingQueries { | ||
15 | val Map<MappingDefinition, ViatraQueryMatcher<? extends IPatternMatch>> matchersMap | ||
16 | |||
17 | new(TransformationDefinition transformationDefinition, ViatraQueryEngine viatraQueryEngine) { | ||
18 | val specificationBuilder = new SpecificationBuilder | ||
19 | val querySpecificationsMap = transformationDefinition.mappingDefinitions.toMap([it], [ | ||
20 | specificationBuilder.getOrCreateSpecification(pattern) | ||
21 | ]) | ||
22 | GenericQueryGroup.of(querySpecificationsMap.values).prepare(viatraQueryEngine) | ||
23 | matchersMap = ImmutableMap.copyOf(querySpecificationsMap.mapValues[getMatcher(viatraQueryEngine)]) | ||
24 | } | ||
25 | |||
26 | def getMatcher(MappingDefinition mappingDefinition) { | ||
27 | val matcher = matchersMap.get(mappingDefinition) | ||
28 | if (matcher === null) { | ||
29 | throw new IllegalArgumentException("Unknown mapping definition: " + mappingDefinition) | ||
30 | } | ||
31 | matcher | ||
32 | } | ||
33 | |||
34 | def createLookupHandler(MappingDefinition mappingDefinition, LookupDefinition lookupDefinition) { | ||
35 | val lookedUpMatcher = getMatcher(lookupDefinition.mapping) | ||
36 | new LookupHandler(mappingDefinition, lookupDefinition, lookedUpMatcher) | ||
37 | } | ||
38 | } | ||