|
6 | 6 |
|
7 | 7 | package edu.ie3.simona.api.mapping; |
8 | 8 |
|
| 9 | +import edu.ie3.datamodel.io.naming.timeseries.ColumnScheme; |
| 10 | +import edu.ie3.datamodel.models.input.container.GridContainer; |
| 11 | +import edu.ie3.datamodel.models.value.Value; |
9 | 12 | import java.util.*; |
10 | | -import java.util.stream.Collectors; |
11 | | -import java.util.stream.Stream; |
12 | 13 |
|
13 | 14 | /** Contains the mapping between SIMONA uuid, the external id and the data type the assets hold */ |
14 | 15 | public class ExtEntityMapping { |
15 | 16 |
|
16 | | - private final Map<DataType, List<ExtEntityEntry>> extEntities; |
| 17 | + private final EnumMap<DataType, List<UUID>> extAssets = new EnumMap<>(DataType.class); |
| 18 | + private final Map<UUID, Class<? extends Value>> primaryMapping = new HashMap<>(); |
17 | 19 |
|
18 | | - public ExtEntityMapping(List<ExtEntityEntry> extEntityEntryList) { |
19 | | - this.extEntities = |
20 | | - extEntityEntryList.stream().collect(Collectors.groupingBy(ExtEntityEntry::dataType)); |
| 20 | + // asset lists |
| 21 | + private final Set<UUID> gridAssets = new HashSet<>(); |
| 22 | + private final Set<UUID> participants = new HashSet<>(); |
| 23 | + private final Set<UUID> ems = new HashSet<>(); |
| 24 | + |
| 25 | + private final Map<UUID, String> uuidToId = new HashMap<>(); |
| 26 | + private final Map<String, UUID> idToUuid = new HashMap<>(); |
| 27 | + |
| 28 | + public ExtEntityMapping(List<ExtEntityEntry> entries) { |
| 29 | + entries.forEach( |
| 30 | + entry -> { |
| 31 | + DataType dataType = entry.dataType(); |
| 32 | + UUID uuid = entry.uuid(); |
| 33 | + String id = entry.id(); |
| 34 | + |
| 35 | + entry |
| 36 | + .columnScheme() |
| 37 | + .ifPresent(scheme -> primaryMapping.put(uuid, scheme.getValueClass())); |
| 38 | + |
| 39 | + // override mappings |
| 40 | + uuidToId.put(uuid, id); |
| 41 | + idToUuid.put(id, uuid); |
| 42 | + |
| 43 | + extAssets.computeIfAbsent(dataType, k -> new ArrayList<>()).add(uuid); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a mapping based on the uuids and ids found in the provided grid. |
| 49 | + * |
| 50 | + * @param grid that contains some mapping information |
| 51 | + */ |
| 52 | + public ExtEntityMapping(GridContainer grid) { |
| 53 | + // handling of grid assets |
| 54 | + grid.getRawGrid() |
| 55 | + .allEntitiesAsList() |
| 56 | + .forEach( |
| 57 | + asset -> { |
| 58 | + UUID uuid = asset.getUuid(); |
| 59 | + String id = asset.getId(); |
| 60 | + |
| 61 | + // add to mappings |
| 62 | + uuidToId.put(uuid, id); |
| 63 | + idToUuid.put(id, uuid); |
| 64 | + |
| 65 | + // add to asset list |
| 66 | + gridAssets.add(asset.getUuid()); |
| 67 | + }); |
| 68 | + |
| 69 | + // handling of participants and ems |
| 70 | + grid.getSystemParticipants() |
| 71 | + .allEntitiesAsList() |
| 72 | + .forEach( |
| 73 | + participant -> { |
| 74 | + UUID uuid = participant.getUuid(); |
| 75 | + String id = participant.getId(); |
| 76 | + |
| 77 | + // add to mappings |
| 78 | + uuidToId.put(uuid, id); |
| 79 | + idToUuid.put(id, uuid); |
| 80 | + |
| 81 | + // add to asset list |
| 82 | + participants.add(participant.getUuid()); |
| 83 | + |
| 84 | + // add ems |
| 85 | + participant |
| 86 | + .getControllingEm() |
| 87 | + .ifPresent( |
| 88 | + em -> { |
| 89 | + UUID emUuid = em.getUuid(); |
| 90 | + String emId = em.getId(); |
| 91 | + |
| 92 | + // add to mappings |
| 93 | + uuidToId.put(emUuid, emId); |
| 94 | + idToUuid.put(emId, emUuid); |
| 95 | + |
| 96 | + // add to asset list |
| 97 | + ems.add(emUuid); |
| 98 | + }); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Used to copy another mapping. |
| 104 | + * |
| 105 | + * @param mapping that should be copied |
| 106 | + */ |
| 107 | + private ExtEntityMapping(ExtEntityMapping mapping) { |
| 108 | + this.extAssets.putAll(mapping.extAssets); |
| 109 | + this.primaryMapping.putAll(mapping.primaryMapping); |
| 110 | + this.gridAssets.addAll(mapping.gridAssets); |
| 111 | + this.participants.addAll(mapping.participants); |
| 112 | + this.ems.addAll(mapping.ems); |
| 113 | + this.uuidToId.putAll(mapping.uuidToId); |
| 114 | + this.idToUuid.putAll(mapping.idToUuid); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Method to add entities that should be used for the data connection. |
| 119 | + * |
| 120 | + * <p>Note: This method can override the existing uuid-id mapping! |
| 121 | + * |
| 122 | + * @param included a list of entries |
| 123 | + * @return an updated mapping |
| 124 | + */ |
| 125 | + public ExtEntityMapping include(List<ExtEntityEntry> included) { |
| 126 | + ExtEntityMapping copy = new ExtEntityMapping(this); |
| 127 | + copy.includeEntries(included); |
| 128 | + return copy; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Method to specify entities that should be used for the data connection. The ids are matched |
| 133 | + * against the uuid-id mapping that is already know. |
| 134 | + * |
| 135 | + * @param included a list of ids |
| 136 | + * @return an updated mapping |
| 137 | + */ |
| 138 | + public ExtEntityMapping include( |
| 139 | + DataType dataType, List<String> included, Optional<ColumnScheme> schemeOption) { |
| 140 | + ExtEntityMapping copy = new ExtEntityMapping(this); |
| 141 | + List<UUID> includedUuids = |
| 142 | + included.stream().map(this::get).filter(Optional::isPresent).map(Optional::get).toList(); |
| 143 | + |
| 144 | + schemeOption.ifPresent( |
| 145 | + scheme -> includedUuids.forEach(uuid -> primaryMapping.put(uuid, scheme.getValueClass()))); |
| 146 | + addExtEntities(dataType, includedUuids); |
| 147 | + |
| 148 | + return copy; |
| 149 | + } |
| 150 | + |
| 151 | + protected void includeIds( |
| 152 | + DataType dataType, List<UUID> included, Optional<ColumnScheme> schemeOption) { |
| 153 | + schemeOption.ifPresent( |
| 154 | + scheme -> included.forEach(uuid -> primaryMapping.put(uuid, scheme.getValueClass()))); |
| 155 | + addExtEntities(dataType, included); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Method to add entities that should be used for the data connection. |
| 160 | + * |
| 161 | + * <p>Note: This method can override the existing uuid-id mapping! |
| 162 | + * |
| 163 | + * @param included a list of entries |
| 164 | + */ |
| 165 | + private void includeEntries(List<ExtEntityEntry> included) { |
| 166 | + included.forEach( |
| 167 | + entry -> { |
| 168 | + DataType dataType = entry.dataType(); |
| 169 | + UUID uuid = entry.uuid(); |
| 170 | + String id = entry.id(); |
| 171 | + |
| 172 | + entry |
| 173 | + .columnScheme() |
| 174 | + .ifPresent(scheme -> primaryMapping.put(uuid, scheme.getValueClass())); |
| 175 | + |
| 176 | + // override mappings |
| 177 | + uuidToId.put(uuid, id); |
| 178 | + idToUuid.put(id, uuid); |
| 179 | + |
| 180 | + addExtEntities(dataType, included.stream().map(ExtEntityEntry::uuid).toList()); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Method to handle adding the entities to the {@link #extAssets} map. |
| 186 | + * |
| 187 | + * @param dataType for which assets are added |
| 188 | + * @param included a list of included uuids |
| 189 | + */ |
| 190 | + private void addExtEntities(DataType dataType, List<UUID> included) { |
| 191 | + if (dataType == DataType.PRIMARY_RESULT) { |
| 192 | + extAssets.computeIfAbsent(DataType.PRIMARY, d -> new ArrayList<>()).addAll(included); |
| 193 | + extAssets.computeIfAbsent(DataType.RESULT, d -> new ArrayList<>()).addAll(included); |
| 194 | + } else { |
| 195 | + extAssets.computeIfAbsent(dataType, k -> new ArrayList<>()).addAll(included); |
| 196 | + } |
21 | 197 | } |
22 | 198 |
|
23 | 199 | /** Returns the data types of this mapping. */ |
24 | 200 | public Set<DataType> getDataTypes() { |
25 | | - return extEntities.keySet(); |
| 201 | + return extAssets.keySet(); |
| 202 | + } |
| 203 | + |
| 204 | + /** Returns a map: uuid to primary data class. */ |
| 205 | + public Map<UUID, Class<? extends Value>> getPrimaryMapping() { |
| 206 | + return Collections.unmodifiableMap(primaryMapping); |
| 207 | + } |
| 208 | + |
| 209 | + /** Returns a list of all external assets. */ |
| 210 | + public List<UUID> getAllAssets() { |
| 211 | + return extAssets.values().stream().flatMap(Collection::stream).toList(); |
26 | 212 | } |
27 | 213 |
|
28 | 214 | /** |
29 | | - * Method for getting the external entity entries for a specific data type. |
| 215 | + * Method to return all assets of a specific data type. If no external assets are set, all assets |
| 216 | + * that are provided by a grid are used. In case external entities are present, they will be used |
| 217 | + * to filter the grid assets. If no grid assets are present, only the external assets will be |
| 218 | + * returned. |
30 | 219 | * |
31 | | - * @param dataType for which entries should be returned |
32 | | - * @return a list containing all entries or an empty list |
| 220 | + * @param dataType for which assets should be returned |
| 221 | + * @return a list of uuids |
33 | 222 | */ |
34 | | - public List<ExtEntityEntry> getEntries(DataType dataType) { |
35 | | - return extEntities.getOrDefault(dataType, Collections.emptyList()); |
| 223 | + public List<UUID> getAssets(DataType dataType) { |
| 224 | + List<UUID> uuids = |
| 225 | + switch (dataType) { |
| 226 | + case PRIMARY, PRIMARY_RESULT -> new ArrayList<>(participants); |
| 227 | + case RESULT -> { |
| 228 | + List<UUID> res = new ArrayList<>(); |
| 229 | + res.addAll(gridAssets); |
| 230 | + res.addAll(participants); |
| 231 | + res.addAll(ems); |
| 232 | + yield res; |
| 233 | + } |
| 234 | + case EM -> new ArrayList<>(ems); |
| 235 | + }; |
| 236 | + |
| 237 | + if (uuids.isEmpty()) { |
| 238 | + if (dataType == DataType.PRIMARY_RESULT) { |
| 239 | + List<UUID> res = new ArrayList<>(); |
| 240 | + res.addAll(extAssets.getOrDefault(DataType.PRIMARY, Collections.emptyList())); |
| 241 | + res.addAll(extAssets.getOrDefault(DataType.RESULT, Collections.emptyList())); |
| 242 | + |
| 243 | + return res; |
| 244 | + } else { |
| 245 | + return extAssets.getOrDefault(dataType, Collections.emptyList()); |
| 246 | + } |
| 247 | + |
| 248 | + } else { |
| 249 | + List<UUID> ext = new ArrayList<>(); |
| 250 | + extAssets.values().forEach(ext::addAll); |
| 251 | + |
| 252 | + if (extAssets.isEmpty()) { |
| 253 | + return uuids; |
| 254 | + } else { |
| 255 | + return uuids.stream().filter(ext::contains).toList(); |
| 256 | + } |
| 257 | + } |
36 | 258 | } |
37 | 259 |
|
38 | 260 | /** |
39 | | - * Returns the full mapping external id to SIMONA uuid. Equals {@code |
40 | | - * getExtId2UuidMapping(DataType.values())}. |
| 261 | + * Checks if the mapping contains the given id. |
| 262 | + * |
| 263 | + * @param id to check |
| 264 | + * @return {@code true}, if a mapping is found. |
41 | 265 | */ |
42 | | - public Map<String, UUID> getExtId2UuidMapping() { |
43 | | - return extEntities.values().stream() |
44 | | - .flatMap(Collection::stream) |
45 | | - .collect(Collectors.toMap(ExtEntityEntry::id, ExtEntityEntry::uuid)); |
| 266 | + public boolean contains(String id) { |
| 267 | + return idToUuid.containsKey(id); |
46 | 268 | } |
47 | 269 |
|
48 | 270 | /** |
49 | | - * Returns the full mapping SIMONA uuid to external id. Equals {@code |
50 | | - * getExtUuid2IdMapping(DataType.values())}. |
| 271 | + * Checks if the mapping contains the given uuid. |
| 272 | + * |
| 273 | + * @param uuid to check |
| 274 | + * @return {@code true}, if a mapping is found. |
51 | 275 | */ |
52 | | - public Map<UUID, String> getExtUuid2IdMapping() { |
53 | | - return extEntities.values().stream() |
54 | | - .flatMap(Collection::stream) |
55 | | - .collect(Collectors.toMap(ExtEntityEntry::uuid, ExtEntityEntry::id)); |
| 276 | + public boolean contains(UUID uuid) { |
| 277 | + return uuidToId.containsKey(uuid); |
56 | 278 | } |
57 | 279 |
|
58 | 280 | /** |
59 | | - * Mapping external id to SIMONA uuid. |
| 281 | + * Method to convert a given id into an uuid. |
60 | 282 | * |
61 | | - * @param dataType data type the external asset expects |
62 | | - * @return mapping external id to SIMONA uuid |
| 283 | + * @param id to convert |
| 284 | + * @return the uuid or {@code null}, if no mapping exists |
63 | 285 | */ |
64 | | - public Map<String, UUID> getExtId2UuidMapping(DataType dataType) { |
65 | | - return extEntities.getOrDefault(dataType, Collections.emptyList()).stream() |
66 | | - .collect(Collectors.toMap(ExtEntityEntry::id, ExtEntityEntry::uuid)); |
| 286 | + public UUID from(String id) { |
| 287 | + return idToUuid.get(id); |
67 | 288 | } |
68 | 289 |
|
69 | 290 | /** |
70 | | - * Mapping external id to SIMONA uuid. |
| 291 | + * Method to convert a given uuid into an id. |
71 | 292 | * |
72 | | - * @param dataTypes the external asset expects |
73 | | - * @return mapping external id to SIMONA uuid |
| 293 | + * @param uuid to convert |
| 294 | + * @return the uuid or {@code null}, if no mapping exists |
74 | 295 | */ |
75 | | - public Map<String, UUID> getExtId2UuidMapping(DataType... dataTypes) { |
76 | | - return Stream.of(dataTypes) |
77 | | - .flatMap(type -> extEntities.getOrDefault(type, Collections.emptyList()).stream()) |
78 | | - .collect(Collectors.toMap(ExtEntityEntry::id, ExtEntityEntry::uuid)); |
| 296 | + public String from(UUID uuid) { |
| 297 | + return uuidToId.get(uuid); |
79 | 298 | } |
80 | 299 |
|
81 | 300 | /** |
82 | | - * Mapping SIMONA uuid to external id. |
| 301 | + * Method that tries to convert the given id into an uuid. |
83 | 302 | * |
84 | | - * @param dataType data type the external asset expects |
85 | | - * @return mapping SIMONA uuid to external id |
| 303 | + * @param id to convert |
| 304 | + * @return an option for an uuid or {@link Optional#empty()}, if no mapping exists |
86 | 305 | */ |
87 | | - public Map<UUID, String> getExtUuid2IdMapping(DataType dataType) { |
88 | | - return extEntities.getOrDefault(dataType, Collections.emptyList()).stream() |
89 | | - .collect(Collectors.toMap(ExtEntityEntry::uuid, ExtEntityEntry::id)); |
| 306 | + public Optional<UUID> get(String id) { |
| 307 | + return Optional.ofNullable(idToUuid.get(id)); |
90 | 308 | } |
91 | 309 |
|
92 | 310 | /** |
93 | | - * Mapping SIMONA uuid to external id. |
| 311 | + * Method that tries to convert the given uuid into an id. |
94 | 312 | * |
95 | | - * @param dataTypes data types the external asset expects |
96 | | - * @return mapping SIMONA uuid to external id |
| 313 | + * @param uuid to convert |
| 314 | + * @return an option for an id or {@link Optional#empty()}, if no mapping exists |
97 | 315 | */ |
98 | | - public Map<UUID, String> getExtUuid2IdMapping(DataType... dataTypes) { |
99 | | - return Stream.of(dataTypes) |
100 | | - .flatMap(type -> extEntities.getOrDefault(type, Collections.emptyList()).stream()) |
101 | | - .collect(Collectors.toMap(ExtEntityEntry::uuid, ExtEntityEntry::id)); |
| 316 | + public Optional<String> get(UUID uuid) { |
| 317 | + return Optional.ofNullable(uuidToId.get(uuid)); |
| 318 | + } |
| 319 | + |
| 320 | + /** Returns the full mapping external id to SIMONA uuid. */ |
| 321 | + public Map<String, UUID> getExtId2UuidMapping() { |
| 322 | + return Collections.unmodifiableMap(idToUuid); |
| 323 | + } |
| 324 | + |
| 325 | + /** Returns the full mapping SIMONA uuid to external id. */ |
| 326 | + public Map<UUID, String> getExtUuid2IdMapping() { |
| 327 | + return Collections.unmodifiableMap(uuidToId); |
102 | 328 | } |
103 | 329 | } |
0 commit comments