|
| 1 | +package de.monticore.codeAdaption.handler.multiIncarnation; |
| 2 | + |
| 3 | +import de.monticore.symboltable.ISymbol; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | + |
| 11 | +/** |
| 12 | + * Context information for handling multiple incarnations of the same pattern. |
| 13 | + * Tracks which incarnations are available for each reference element and which mapping they belong to. |
| 14 | + */ |
| 15 | +public class IncarnationContext { |
| 16 | + private final String mappingName; |
| 17 | + private final Map<ISymbol, List<ISymbol>> referenceToIncarnations; |
| 18 | + private final Map<ISymbol, List<ISymbol>> interfaceToImplementers; |
| 19 | + private final Map<ISymbol, StableElementKey> symbolKeys; |
| 20 | + private final ResolvedIncarnationContext resolvedContext; |
| 21 | + // concrete simple-name to grouping simple-name |
| 22 | + private Map<String, String> concreteToGroupingType = new HashMap<>(); |
| 23 | + |
| 24 | + public IncarnationContext(String mappingName, Map<ISymbol, List<ISymbol>> referenceToIncarnations) { |
| 25 | + this(mappingName, referenceToIncarnations, Map.of()); |
| 26 | + } |
| 27 | + |
| 28 | + public IncarnationContext(String mappingName, Map<ISymbol, List<ISymbol>> referenceToIncarnations, |
| 29 | + Map<ISymbol, List<ISymbol>> interfaceToImplementers) { |
| 30 | + this(mappingName, referenceToIncarnations, interfaceToImplementers, Map.of(), null); |
| 31 | + } |
| 32 | + |
| 33 | + public IncarnationContext( |
| 34 | + String mappingName, |
| 35 | + Map<ISymbol, List<ISymbol>> referenceToIncarnations, |
| 36 | + Map<ISymbol, List<ISymbol>> interfaceToImplementers, |
| 37 | + Map<ISymbol, StableElementKey> symbolKeys, |
| 38 | + ResolvedIncarnationContext resolvedContext) { |
| 39 | + this.mappingName = mappingName; |
| 40 | + this.referenceToIncarnations = referenceToIncarnations; |
| 41 | + this.interfaceToImplementers = interfaceToImplementers != null ? interfaceToImplementers : Map.of(); |
| 42 | + this.symbolKeys = symbolKeys != null ? new LinkedHashMap<>(symbolKeys) : Map.of(); |
| 43 | + this.resolvedContext = |
| 44 | + resolvedContext != null ? resolvedContext : new ResolvedIncarnationContext(mappingName); |
| 45 | + } |
| 46 | + |
| 47 | + public String getMappingName() { |
| 48 | + return mappingName; |
| 49 | + } |
| 50 | + |
| 51 | + public Map<ISymbol, List<ISymbol>> getReferenceToIncarnations() { |
| 52 | + return referenceToIncarnations; |
| 53 | + } |
| 54 | + |
| 55 | + public List<ISymbol> getIncarnations(ISymbol referenceSymbol) { |
| 56 | + if (referenceSymbol == null) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + Optional<StableElementKey> key = getStableKey(referenceSymbol); |
| 60 | + if (key.isPresent()) { |
| 61 | + List<ISymbol> resolved = |
| 62 | + resolvedContext.getIncarnations(key.get()).stream() |
| 63 | + .map(ResolvedIncarnationContext.ResolvedElement::getSymbol) |
| 64 | + .filter(java.util.Objects::nonNull) |
| 65 | + .toList(); |
| 66 | + if (!resolved.isEmpty()) { |
| 67 | + return resolved; |
| 68 | + } |
| 69 | + } |
| 70 | + List<ISymbol> incarnations = referenceToIncarnations.get(referenceSymbol); |
| 71 | + if (incarnations != null) { |
| 72 | + return incarnations; |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + public List<ResolvedIncarnationContext.ResolvedElement> getIncarnations(StableElementKey referenceKey) { |
| 78 | + return resolvedContext.getIncarnations(referenceKey); |
| 79 | + } |
| 80 | + |
| 81 | + public Optional<StableElementKey> getStableKey(ISymbol symbol) { |
| 82 | + return Optional.ofNullable(symbolKeys.get(symbol)); |
| 83 | + } |
| 84 | + |
| 85 | + public ResolvedIncarnationContext getResolvedContext() { |
| 86 | + return resolvedContext; |
| 87 | + } |
| 88 | + |
| 89 | + public boolean hasMultipleIncarnations(ISymbol referenceSymbol) { |
| 90 | + List<ISymbol> incarnations = getIncarnations(referenceSymbol); |
| 91 | + return incarnations != null && incarnations.size() > 1; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Set the computed concrete->grouping mapping (simple names). |
| 96 | + */ |
| 97 | + public void setConcreteToGroupingType(Map<String, String> map) { |
| 98 | + if (map == null) this.concreteToGroupingType = new HashMap<>(); |
| 99 | + else this.concreteToGroupingType = new HashMap<>(map); |
| 100 | + for (Map.Entry<String, String> entry : this.concreteToGroupingType.entrySet()) { |
| 101 | + resolvedContext.setGroupingMapping( |
| 102 | + StableElementKey.type(entry.getKey()), StableElementKey.type(entry.getValue())); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Find a grouping type simple name for the given concrete implementer simple name. |
| 108 | + * Returns Optional.empty() if no grouping was computed. |
| 109 | + */ |
| 110 | + public Optional<String> findGroupingTypeForImplementer(String concreteSimpleName) { |
| 111 | + if (concreteSimpleName == null) return Optional.empty(); |
| 112 | + String v = concreteToGroupingType.get(concreteSimpleName); |
| 113 | + return v == null ? Optional.empty() : Optional.of(v); |
| 114 | + } |
| 115 | + |
| 116 | + public Map<ISymbol, List<ISymbol>> getInterfaceToImplementers() { |
| 117 | + return interfaceToImplementers; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Look up the concrete type name for a reference type name. |
| 122 | + * Returns the first incarnation's name if found. |
| 123 | + */ |
| 124 | + public Optional<String> getConcreteTypeName(String refTypeName) { |
| 125 | + List<ResolvedIncarnationContext.ResolvedElement> typeIncarnations = |
| 126 | + resolvedContext.getIncarnations(StableElementKey.type(refTypeName)); |
| 127 | + if (!typeIncarnations.isEmpty()) { |
| 128 | + return Optional.of(typeIncarnations.get(0).getKey().getName()); |
| 129 | + } |
| 130 | + return Optional.empty(); |
| 131 | + } |
| 132 | + |
| 133 | + public Map<StableElementKey, List<StableElementKey>> getStableMappings() { |
| 134 | + Map<StableElementKey, List<StableElementKey>> result = new LinkedHashMap<>(); |
| 135 | + for (Map.Entry<StableElementKey, List<ResolvedIncarnationContext.ResolvedElement>> entry : |
| 136 | + resolvedContext.getTypeMappings().entrySet()) { |
| 137 | + result.put(entry.getKey(), toKeys(entry.getValue())); |
| 138 | + } |
| 139 | + for (Map.Entry<StableElementKey, List<ResolvedIncarnationContext.ResolvedElement>> entry : |
| 140 | + resolvedContext.getFieldMappings().entrySet()) { |
| 141 | + result.put(entry.getKey(), toKeys(entry.getValue())); |
| 142 | + } |
| 143 | + for (Map.Entry<StableElementKey, List<ResolvedIncarnationContext.ResolvedElement>> entry : |
| 144 | + resolvedContext.getMethodMappings().entrySet()) { |
| 145 | + result.put(entry.getKey(), toKeys(entry.getValue())); |
| 146 | + } |
| 147 | + return result; |
| 148 | + } |
| 149 | + |
| 150 | + private List<StableElementKey> toKeys(List<ResolvedIncarnationContext.ResolvedElement> elements) { |
| 151 | + List<StableElementKey> keys = new ArrayList<>(); |
| 152 | + for (ResolvedIncarnationContext.ResolvedElement element : elements) { |
| 153 | + keys.add(element.getKey()); |
| 154 | + } |
| 155 | + return keys; |
| 156 | + } |
| 157 | +} |
0 commit comments