From 6f88c4bd4e91728cea62505e893b0ce300baf7fc Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 7 Feb 2019 18:24:45 +0100 Subject: Ecore2Cft transformation --- .../transformation/ecore2cft/MappingHandler.xtend | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend (limited to 'Stochastic/hu.bme.mit.inf.dslreasoner.faulttree.transformation/src/hu/bme/mit/inf/dslreasoner/faulttree/transformation/ecore2cft/MappingHandler.xtend') 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 @@ +package hu.bme.mit.inf.dslreasoner.faulttree.transformation.ecore2cft + +import com.google.common.collect.ImmutableMap +import com.google.common.collect.Maps +import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.LookupDefinition +import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.MappingDefinition +import hu.bme.mit.inf.dslreasoner.faulttree.components.cftLanguage.Variable +import java.util.Map +import org.eclipse.viatra.query.runtime.api.IPatternMatch +import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher + +class MappingHandler { + val ViatraQueryMatcher matcher + val MappingDefinition mappingDefinition + val Map lookupHandlers + + new(MappingDefinition mappingDefinition, MappingQueries mappingQueries) { + matcher = mappingQueries.getMatcher(mappingDefinition) + this.mappingDefinition = mappingDefinition + val variables = newHashSet + for (assignment : mappingDefinition.assignments) { + variables += assignment.input.component + variables += assignment.output.component + } + lookupHandlers = ImmutableMap.copyOf(variables.filter(LookupDefinition).toMap([it], [ lookupDefinition | + mappingQueries.createLookupHandler(mappingDefinition, lookupDefinition) + ])) + } + + def instantiateComponents(ComponentFaultTreeTrace faultTreeTrace) { + if (!hasComponentInstace) { + return + } + matcher.forEachMatch [ match | + val componentTrace = faultTreeTrace.instantiateComponent(match, componentDefinition) + if (isTopLevel) { + faultTreeTrace.topLevel = componentTrace + } + ] + } + + def instantiateConnections(ComponentFaultTreeTrace faultTreeTrace) { + if (!hasConnections) { + return + } + matcher.forEachMatch [ match | + val lookedUpComponents = lookupComponents(faultTreeTrace, match) + for (assignment : mappingDefinition.assignments) { + val input = assignment.input + val inputComponent = lookedUpComponents.get(input.component) + val output = assignment.output + val outputComponent = lookedUpComponents.get(output.component) + if (inputComponent !== null && outputComponent !== null) { + inputComponent.assign(input.event, outputComponent, output.event) + } + } + ] + } + + private def Map lookupComponents(ComponentFaultTreeTrace faultTreeTrace, + IPatternMatch match) { + val lookedUpComponents = Maps.newHashMapWithExpectedSize(lookupHandlers.size + 1) + if (hasComponentInstace) { + val componentInstance = faultTreeTrace.lookup(match) + lookedUpComponents.put(mappingDefinition.componentInstance, componentInstance) + } + for (pair : lookupHandlers.entrySet) { + val componentInstance = pair.value.lookupForMatch(faultTreeTrace, match) + if (componentInstance !== null) { + lookedUpComponents.put(pair.key, componentInstance) + } + } + lookedUpComponents + } + + private def getComponentDefinition() { + mappingDefinition.componentInstance?.componentType + } + + private def hasComponentInstace() { + componentDefinition !== null + } + + private def isTopLevel() { + mappingDefinition.topLevel + } + + private def hasConnections() { + !mappingDefinition.assignments.empty + } +} -- cgit v1.2.3-54-g00ecf