diff options
Diffstat (limited to 'Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend')
-rw-r--r-- | Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend | 91 |
1 files changed, 91 insertions, 0 deletions
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 | } | ||