From 80d58380bfa5230e2b97c375387d2608dfda5a53 Mon Sep 17 00:00:00 2001 From: Romain Courtier Date: Wed, 4 Dec 2024 12:24:59 +0100 Subject: [PATCH] Create final caches and factorize the building of caches Signed-off-by: Romain Courtier --- .../cgmes/model/AbstractCgmesModel.java | 183 +++++------------- 1 file changed, 53 insertions(+), 130 deletions(-) diff --git a/cgmes/cgmes-model/src/main/java/com/powsybl/cgmes/model/AbstractCgmesModel.java b/cgmes/cgmes-model/src/main/java/com/powsybl/cgmes/model/AbstractCgmesModel.java index 3ff812f979b..cdc74cac22e 100644 --- a/cgmes/cgmes-model/src/main/java/com/powsybl/cgmes/model/AbstractCgmesModel.java +++ b/cgmes/cgmes-model/src/main/java/com/powsybl/cgmes/model/AbstractCgmesModel.java @@ -25,6 +25,31 @@ */ public abstract class AbstractCgmesModel implements CgmesModel { + private final Properties properties; + private String baseName; + + // Caches + private final Map cachedGroupedTransformerEnds = new HashMap<>(); + private final Map cachedGroupedRatioTapChangers = new HashMap<>(); + private final Map cachedGroupedRatioTapChangerTablePoints = new HashMap<>(); + private final Map cachedGroupedPhaseTapChangers = new HashMap<>(); + private final Map cachedGroupedPhaseTapChangerTablePoints = new HashMap<>(); + private final Map cachedGroupedShuntCompensatorPoints = new HashMap<>(); + private final Map cachedGroupedCurveData = new HashMap<>(); + + private Map cachedTerminals; + private Map cachedContainers; + private Map cachedBaseVoltages; + protected boolean cachedNodes = false; + protected PropertyBags cachedConnectivityNodes; + protected PropertyBags cachedTopologicalNodes; + private Map cachedNodesById; + // equipmentId, sequenceNumber, terminalId + private Map cachedDcTerminals; + + private static final Logger LOG = LoggerFactory.getLogger(AbstractCgmesModel.class); + private static final String SUBSTATION = "Substation"; + protected AbstractCgmesModel() { // FIXME(Luma) we must remove properties from here. They are not used! this.properties = new Properties(); @@ -35,59 +60,38 @@ public Properties getProperties() { return this.properties; } - @Override - public PropertyBags nonlinearShuntCompensatorPoints(String shuntId) { - if (cachedGroupedShuntCompensatorPoints == null) { - cachedGroupedShuntCompensatorPoints = computeGroupedShuntCompensatorPoints(); - } - return cachedGroupedShuntCompensatorPoints.getOrDefault(shuntId, new PropertyBags()); - } - @Override public PropertyBags transformerEnds(String transformerId) { - if (cachedGroupedTransformerEnds == null) { - cachedGroupedTransformerEnds = computeGroupedTransformerEnds(); - } return cachedGroupedTransformerEnds.getOrDefault(transformerId, new PropertyBags()); } @Override public PropertyBags ratioTapChangers(String transformerId) { - if (cachedGroupedRatioTapChangers == null) { - cachedGroupedRatioTapChangers = computeGroupedRatioTapChangers(); - } return cachedGroupedRatioTapChangers.getOrDefault(transformerId, new PropertyBags()); } @Override public PropertyBags ratioTapChangerTablePoints(String tableId) { - if (cachedGroupedRatioTapChangerTablePoints == null) { - cachedGroupedRatioTapChangerTablePoints = computeGroupedRatioTapChangerTablePoints(); - } return cachedGroupedRatioTapChangerTablePoints.getOrDefault(tableId, new PropertyBags()); } @Override public PropertyBags phaseTapChangers(String transformerId) { - if (cachedGroupedPhaseTapChangers == null) { - cachedGroupedPhaseTapChangers = computeGroupedPhaseTapChangers(); - } return cachedGroupedPhaseTapChangers.getOrDefault(transformerId, new PropertyBags()); } @Override public PropertyBags phaseTapChangerTablePoints(String tableId) { - if (cachedGroupedPhaseTapChangerTablePoints == null) { - cachedGroupedPhaseTapChangerTablePoints = computeGroupedPhaseTapChangerTablePoints(); - } return cachedGroupedPhaseTapChangerTablePoints.getOrDefault(tableId, new PropertyBags()); } + @Override + public PropertyBags nonlinearShuntCompensatorPoints(String shuntId) { + return cachedGroupedShuntCompensatorPoints.getOrDefault(shuntId, new PropertyBags()); + } + @Override public PropertyBags curveData(String curveId) { - if (cachedGroupedCurveData == null) { - cachedGroupedCurveData = computeGroupedCurveData(); - } return cachedGroupedCurveData.getOrDefault(curveId, new PropertyBags()); } @@ -198,83 +202,6 @@ private CgmesContainer container(CgmesTerminal t, boolean nodeBreaker) { return (containerId == null) ? null : container(containerId); } - private Map computeGroupedShuntCompensatorPoints() { - Map groupedShuntCompensatorPoints = new HashMap<>(); - nonlinearShuntCompensatorPoints() - .forEach(point -> { - String shuntCompensator = point.getId("Shunt"); - groupedShuntCompensatorPoints.computeIfAbsent(shuntCompensator, bag -> new PropertyBags()) - .add(point); - }); - return groupedShuntCompensatorPoints; - } - - private Map computeGroupedTransformerEnds() { - Map groupedTransformerEnds = new HashMap<>(); - transformerEnds() - .forEach(end -> { - String transformerId = end.getId(CgmesNames.POWER_TRANSFORMER); - groupedTransformerEnds.computeIfAbsent(transformerId, x -> new PropertyBags()) - .add(end); - }); - return groupedTransformerEnds; - } - - private Map computeGroupedRatioTapChangers() { - Map groupedRatioTapChangers = new HashMap<>(); - ratioTapChangers() - .forEach(rtc -> { - String transformerId = rtc.getId(CgmesNames.POWER_TRANSFORMER); - groupedRatioTapChangers.computeIfAbsent(transformerId, bag -> new PropertyBags()) - .add(rtc); - }); - return groupedRatioTapChangers; - } - - private Map computeGroupedRatioTapChangerTablePoints() { - Map groupedRatioTapChangerTablePoints = new HashMap<>(); - ratioTapChangerTablePoints() - .forEach(point -> { - String tableId = point.getId("RatioTapChangerTable"); - groupedRatioTapChangerTablePoints.computeIfAbsent(tableId, bag -> new PropertyBags()) - .add(point); - }); - return groupedRatioTapChangerTablePoints; - } - - private Map computeGroupedPhaseTapChangers() { - Map groupedPhaseTapChangers = new HashMap<>(); - phaseTapChangers() - .forEach(ptc -> { - String transformerId = ptc.getId(CgmesNames.POWER_TRANSFORMER); - groupedPhaseTapChangers.computeIfAbsent(transformerId, bag -> new PropertyBags()) - .add(ptc); - }); - return groupedPhaseTapChangers; - } - - private Map computeGroupedPhaseTapChangerTablePoints() { - Map groupedPhaseTapChangerTablePoints = new HashMap<>(); - phaseTapChangerTablePoints() - .forEach(point -> { - String tableId = point.getId("PhaseTapChangerTable"); - groupedPhaseTapChangerTablePoints.computeIfAbsent(tableId, bag -> new PropertyBags()) - .add(point); - }); - return groupedPhaseTapChangerTablePoints; - } - - private Map computeGroupedCurveData() { - Map groupedCurveData = new HashMap<>(); - curveData() - .forEach(data -> { - String curveId = data.getId("ReactiveCapabilityCurve"); - groupedCurveData.computeIfAbsent(curveId, bag -> new PropertyBags()) - .add(data); - }); - return groupedCurveData; - } - protected void cacheNodes() { if (!cachedNodes) { cachedConnectivityNodes = connectivityNodes(); @@ -367,14 +294,17 @@ public void read(ReadOnlyDataSource ds, ReportNode reportNode) { throw new CgmesModelException(msg, e); } } + buildCaches(); } protected void invalidateCaches() { - cachedGroupedShuntCompensatorPoints = null; - cachedGroupedTransformerEnds = null; - cachedGroupedRatioTapChangers = null; - cachedGroupedPhaseTapChangers = null; - cachedGroupedCurveData = null; + cachedGroupedTransformerEnds.clear(); + cachedGroupedRatioTapChangers.clear(); + cachedGroupedRatioTapChangerTablePoints.clear(); + cachedGroupedPhaseTapChangers.clear(); + cachedGroupedPhaseTapChangerTablePoints.clear(); + cachedGroupedShuntCompensatorPoints.clear(); + cachedGroupedCurveData.clear(); cachedTerminals = null; cachedContainers = null; cachedBaseVoltages = null; @@ -385,27 +315,20 @@ protected void invalidateCaches() { cachedDcTerminals = null; } - private final Properties properties; - private String baseName; - - // Caches - private Map cachedGroupedShuntCompensatorPoints; - private Map cachedGroupedTransformerEnds; - private Map cachedGroupedRatioTapChangers; - private Map cachedGroupedRatioTapChangerTablePoints; - private Map cachedGroupedPhaseTapChangers; - private Map cachedGroupedPhaseTapChangerTablePoints; - private Map cachedGroupedCurveData; - private Map cachedTerminals; - private Map cachedContainers; - private Map cachedBaseVoltages; - protected boolean cachedNodes = false; - protected PropertyBags cachedConnectivityNodes; - protected PropertyBags cachedTopologicalNodes; - private Map cachedNodesById; - // equipmentId, sequenceNumber, terminalId - private Map cachedDcTerminals; + private void buildCaches() { + buildCache(cachedGroupedTransformerEnds, transformerEnds(), CgmesNames.POWER_TRANSFORMER); + buildCache(cachedGroupedRatioTapChangers, ratioTapChangers(), CgmesNames.POWER_TRANSFORMER); + buildCache(cachedGroupedRatioTapChangerTablePoints, ratioTapChangerTablePoints(), CgmesNames.RATIO_TAP_CHANGER_TABLE); + buildCache(cachedGroupedPhaseTapChangers, phaseTapChangers(), CgmesNames.POWER_TRANSFORMER); + buildCache(cachedGroupedPhaseTapChangerTablePoints, phaseTapChangerTablePoints(), CgmesNames.PHASE_TAP_CHANGER_TABLE); + buildCache(cachedGroupedShuntCompensatorPoints, nonlinearShuntCompensatorPoints(), "Shunt"); + buildCache(cachedGroupedCurveData, curveData(), "ReactiveCapabilityCurve"); + } - private static final Logger LOG = LoggerFactory.getLogger(AbstractCgmesModel.class); - private static final String SUBSTATION = "Substation"; + private void buildCache(Map cache, PropertyBags ps, String groupName) { + ps.forEach(p -> { + String groupId = p.getId(groupName); + cache.computeIfAbsent(groupId, b -> new PropertyBags()).add(p); + }); + } }