SimpleContingencyDbFacade implements ContingencyDbFacade { @Override public synchronized List<List<Action>> getCurativeActions(Contingency contingency, List<LimitViolation> limitViolations) { Objects.requireNonNull(contingency, "contingency is null"); LOGGER.info("Network {}: getting curative actions for contingency {}", network.getId(), contingency.getId()); List<List<Action>> curativeActions = new ArrayList<>(); for (ActionsContingenciesAssociation association : contingenciesActionsDbClient.getActionsCtgAssociations(network)) { if (!association.getContingenciesId().contains(contingency.getId())) { continue; } if (!constraintsMatch(association, limitViolations)) { continue; } for (String actionId : association.getActionsId()) { Action action = contingenciesActionsDbClient.getAction(actionId, network); if (action != null) { curativeActions.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(); curativeActions.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 contingency {}", network.getId(), actionId, contingency.getId()); } } } } LOGGER.info("Network {}: found {} curative actions for contingency {}", network.getId(), curativeActions.size(), contingency.getId()); return curativeActions; } 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 testGetCurativeActions() throws Exception { Network mockNetwork = NetworkFactory.create("mockNetwork", "test"); String mockContingencyId = "mockContingency"; Contingency mockContingency = new Contingency(mockContingencyId, new BranchContingency("mockLine")); String mockActionId = "mockAction"; Action mockAction = new ActionImpl(mockActionId, true, true, new SwitchClosingAction("mockVoltageLevel", "mockSwitch")); Constraint mockConstraint = new ConstraintImpl("mockline", 0, ConstraintType.BRANCH_OVERLOAD); ActionsContingenciesAssociation mockAssociation = new ActionsContingenciesAssociationImpl( Arrays.asList(mockContingencyId), Arrays.asList(mockConstraint), Arrays.asList(mockActionId)); ContingenciesAndActionsDatabaseClient mockContingenciesActionsDbClient = Mockito.mock(ContingenciesAndActionsDatabaseClient.class); Mockito.when(mockContingenciesActionsDbClient.getActionsCtgAssociations(mockNetwork)).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.getCurativeActions(mockContingency, null)); }
@Test public void testGetCurativeActionsWithActionPlans() throws Exception { Network mockNetwork = NetworkFactory.create("mockNetwork", "test"); String mockContingencyId = "mockContingency"; Contingency mockContingency = new Contingency(mockContingencyId, new BranchContingency("mockLine")); String mockActionId = "mockAction"; Action mockAction = new ActionImpl(mockActionId, true, true, new SwitchClosingAction("mockVoltageLevel", "mockSwitch")); Map<BigInteger, String> mockOptionActions = ImmutableMap.of(new BigInteger("1"), mockActionId); LogicalExpressionImpl mockLogicalExpression = new LogicalExpressionImpl(); mockLogicalExpression.setOperator(new UnaryOperator(mockActionId)); ActionPlanOption mockActionPlanOption = new OptionImpl(new BigInteger("1"), mockLogicalExpression, mockOptionActions); Map<BigInteger, ActionPlanOption> mockPriorityOptions = ImmutableMap.of(new BigInteger("1"), mockActionPlanOption); String mockActionPlanId = "mockActionPlan"; ActionPlan mockActionPlan = new ActionPlanImpl(mockActionPlanId, "", Collections.emptyList(), mockPriorityOptions); Constraint mockConstraint = new ConstraintImpl("mockline", 0, ConstraintType.BRANCH_OVERLOAD); ActionsContingenciesAssociation mockAssociation = new ActionsContingenciesAssociationImpl( Arrays.asList(mockContingencyId), Arrays.asList(mockConstraint), Arrays.asList(mockActionPlanId)); ContingenciesAndActionsDatabaseClient mockContingenciesActionsDbClient = Mockito.mock(ContingenciesAndActionsDatabaseClient.class); Mockito.when(mockContingenciesActionsDbClient.getActionsCtgAssociations(mockNetwork)).thenReturn(Arrays.asList(mockAssociation)); Mockito.when(mockContingenciesActionsDbClient.getAction(mockActionPlanId, mockNetwork)).thenReturn(null); Mockito.when(mockContingenciesActionsDbClient.getActionPlan(mockActionPlanId)).thenReturn(mockActionPlan); Mockito.when(mockContingenciesActionsDbClient.getAction(mockActionId, mockNetwork)).thenReturn(mockAction); ContingencyDbFacade contingencyDbFacade = new SimpleContingencyDbFacade(mockContingenciesActionsDbClient, mockNetwork); assertEquals(Arrays.asList(Collections.singletonList(mockAction)), contingencyDbFacade.getCurativeActions(mockContingency, null)); }
@Test public void testGetCurativeActionsWithViolations() 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); List<LimitViolation> mockLimitViolations = Arrays.asList(mockLimitViolation); String mockContingencyId = "mockContingency"; Contingency mockContingency = new Contingency(mockContingencyId, new BranchContingency("mockLine")); 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( Arrays.asList(mockContingencyId), Arrays.asList(mockConstraint), Arrays.asList(mockActionId)); ContingenciesAndActionsDatabaseClient mockContingenciesActionsDbClient = Mockito.mock(ContingenciesAndActionsDatabaseClient.class); Mockito.when(mockContingenciesActionsDbClient.getActionsCtgAssociations(mockNetwork)).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.getCurativeActions(mockContingency, mockLimitViolations)); }