SimpleContingencyDbFacade implements ContingencyDbFacade { @Override public synchronized List<List<Action>> getPreventiveActions(LimitViolation limitViolation) { Objects.requireNonNull(limitViolation, "limit violation is null"); LOGGER.info("Network {}: getting preventive actions for {} violation on equipment {}", network.getId(), limitViolation.getLimitType(), limitViolation.getSubjectId()); List<List<Action>> preventiveActions = new ArrayList<>(); if (!limitViolation.getLimitType().equals(LimitViolationType.CURRENT)) { LOGGER.warn("Network {}: no preventive actions found for {} violation on equipment {}, as just branch overload is handled, so far", network.getId(), limitViolation.getLimitType(), limitViolation.getSubjectId()); return preventiveActions; } for (ActionsContingenciesAssociation association : contingenciesActionsDbClient.getActionsCtgAssociationsByConstraint(limitViolation.getSubjectId(), ConstraintType.BRANCH_OVERLOAD)) { if (!association.getContingenciesId().isEmpty()) { continue; } for (String actionId : association.getActionsId()) { Action action = contingenciesActionsDbClient.getAction(actionId, network); if (action != null) { preventiveActions.add(Collections.singletonList(action)); } else { ActionPlan actionPlan = contingenciesActionsDbClient.getActionPlan(actionId); if (actionPlan != null) { for (ActionPlanOption option : actionPlan.getPriorityOption().values()) { if (option.getLogicalExpression().getOperator() instanceof UnaryOperator) { UnaryOperator op = (UnaryOperator) option.getLogicalExpression().getOperator(); preventiveActions.add(Collections.singletonList(contingenciesActionsDbClient.getAction(op.getActionId(), network))); } else { throw new AssertionError("Operator " + option.getLogicalExpression().getOperator().getClass() + " not yet supported"); } } } else { LOGGER.error("Network {}: action {} not found for {} violation on equipment {}", network.getId(), actionId, limitViolation.getLimitType(), limitViolation.getSubjectId()); } } } } LOGGER.info("Network {}: found {} preventive actions for {} violation on equipment {}", network.getId(), preventiveActions.size(), limitViolation.getLimitType(), limitViolation.getSubjectId()); return preventiveActions; } SimpleContingencyDbFacade(ContingenciesAndActionsDatabaseClient contingenciesActionsDbClient, Network network); @Override synchronized List<Contingency> getContingencies(); @Override synchronized List<List<Action>> getCurativeActions(Contingency contingency, List<LimitViolation> limitViolations); @Override synchronized List<List<Action>> getPreventiveActions(LimitViolation limitViolation);  }
@Test public void testGetPreventiveActions() throws Exception { Network mockNetwork = NetworkFactory.create("mockNetwork", "test"); String mockLineId = "mockline"; mockNetwork.newSubstation() .setId("mockSubstation1") .setCountry(Country.FR) .add() .newVoltageLevel() .setId("mockVoltageLevel1") .setNominalV(380) .setTopologyKind(TopologyKind.BUS_BREAKER) .setHighVoltageLimit(400) .setLowVoltageLimit(300) .add() .getBusBreakerView() .newBus() .setId("mockBus1") .add(); mockNetwork.newSubstation() .setId("mockSubstation2") .setCountry(Country.FR) .add() .newVoltageLevel() .setId("mockVoltageLevel2") .setNominalV(380) .setTopologyKind(TopologyKind.BUS_BREAKER) .setHighVoltageLimit(400) .setLowVoltageLimit(300) .add().getBusBreakerView() .newBus() .setId("mockBus2") .add(); Line mockLine = mockNetwork.newLine() .setId(mockLineId) .setVoltageLevel1("mockVoltageLevel1") .setBus1("mockBus1") .setConnectableBus1("mockBus1") .setVoltageLevel2("mockVoltageLevel2") .setBus2("mockBus2") .setConnectableBus2("mockBus2") .setR(0) .setX(0) .setG1(0) .setB1(0) .setG2(0) .setB2(0) .add(); LimitViolation mockLimitViolation = createMockLimitViolation(mockLineId); String mockActionId = "mockAction"; Action mockAction = new ActionImpl(mockActionId, true, true, new SwitchClosingAction("mockVoltageLevel", "mockSwitch")); Constraint mockConstraint = new ConstraintImpl(mockLineId, 0, ConstraintType.BRANCH_OVERLOAD); ActionsContingenciesAssociation mockAssociation = new ActionsContingenciesAssociationImpl( Collections.emptyList(), Arrays.asList(mockConstraint), Arrays.asList(mockActionId)); ContingenciesAndActionsDatabaseClient mockContingenciesActionsDbClient = Mockito.mock(ContingenciesAndActionsDatabaseClient.class); Mockito.when(mockContingenciesActionsDbClient.getActionsCtgAssociationsByConstraint(mockLineId, ConstraintType.BRANCH_OVERLOAD)) .thenReturn(Arrays.asList(mockAssociation)); Mockito.when(mockContingenciesActionsDbClient.getAction(mockActionId, mockNetwork)).thenReturn(mockAction); ContingencyDbFacade contingencyDbFacade = new SimpleContingencyDbFacade(mockContingenciesActionsDbClient, mockNetwork); assertEquals(Arrays.asList(Collections.singletonList(mockAction)), contingencyDbFacade.getPreventiveActions(mockLimitViolation)); }