GroovyDslContingenciesProvider extends AbstractDslContingenciesProvider { @Override public List<Contingency> getContingencies(Network network) { return new ContingencyDslLoader(script).load(network); } GroovyDslContingenciesProvider(final Path path);  GroovyDslContingenciesProvider(final InputStream input); @Override List<Contingency> getContingencies(Network network);  }
@Test public void test() throws IOException { writeToDslFile("contingency('c1') {", " equipments 'NHV1_NHV2_1'", "}"); List<Contingency> contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertEquals(1, contingencies.size()); Contingency contingency = contingencies.get(0); assertEquals("c1", contingency.getId()); assertEquals(0, contingency.getExtensions().size()); assertEquals(1, contingency.getElements().size()); ContingencyElement element = contingency.getElements().iterator().next(); assertTrue(element instanceof BranchContingency); assertEquals("NHV1_NHV2_1", element.getId()); }
@Test public void testOrder() throws IOException { writeToDslFile("contingency('c1') {", " equipments 'NHV1_NHV2_1'", "}", "contingency('c2') {", " equipments 'NHV1_NHV2_2'", "}"); List<Contingency> contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertEquals(2, contingencies.size()); assertEquals("c1", contingencies.get(0).getId()); assertEquals("c2", contingencies.get(1).getId()); }
@Test public void testAutomaticList() throws IOException { writeToDslFile("for (l in network.lines) {", " contingency(l.id) {", " equipments l.id", " }", "}"); List<Contingency> contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertEquals(2, contingencies.size()); assertEquals(Sets.newHashSet("NHV1_NHV2_1", "NHV1_NHV2_2"), getContingenciesNames(contingencies)); }
@Test public void testFactory() throws IOException { ContingenciesProviderFactory factory = new GroovyDslContingenciesProviderFactory(); String dsl = createAllBranchesDsl(); InputStream inputStreamDsl = new ByteArrayInputStream(dsl.getBytes(StandardCharsets.UTF_8)); ContingenciesProvider providerFromStream = factory.create(inputStreamDsl); assertTrue(providerFromStream instanceof GroovyDslContingenciesProvider); List<Contingency> contingenciesFromStream = providerFromStream.getContingencies(network); assertEquals(4, contingenciesFromStream.size()); try (Writer writer = Files.newBufferedWriter(dslFile, StandardCharsets.UTF_8)) { writer.write(dsl); } ContingenciesProvider providerFromFile = factory.create(dslFile); assertTrue(providerFromFile instanceof GroovyDslContingenciesProvider); List<Contingency> contingenciesFromFile = providerFromFile.getContingencies(network); assertEquals(4, contingenciesFromFile.size()); assertEquals(getContingenciesNames(contingenciesFromFile), getContingenciesNames(contingenciesFromStream)); }
@Test public void reuseProvider() { ContingenciesProviderFactory factory = new GroovyDslContingenciesProviderFactory(); InputStream inputStreamDsl = new ByteArrayInputStream(createAllBranchesDsl().getBytes(StandardCharsets.UTF_8)); ContingenciesProvider provider = factory.create(inputStreamDsl); assertTrue(provider instanceof GroovyDslContingenciesProvider); List<Contingency> contingencies1 = provider.getContingencies(network); assertEquals(4, contingencies1.size()); List<Contingency> contingencies2 = provider.getContingencies(network); assertEquals(4, contingencies2.size()); assertEquals(getContingenciesNames(contingencies1), getContingenciesNames(contingencies2)); }
@Test public void withComparison() throws IOException { writeToDslFile("for (l in network.lines) {", " if (l.terminal1.voltageLevel.nominalV >= 380) {", " contingency(l.id) { equipments l.id }", " }", "}"); List<Contingency> contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertEquals(2, contingencies.size()); assertEquals(Sets.newHashSet("NHV1_NHV2_1", "NHV1_NHV2_2"), getContingenciesNames(contingencies)); writeToDslFile("for (l in network.lines) {", " if (l.terminal1.voltageLevel.nominalV >= 500) {", " contingency(l.id) { equipments l.id }", " }", "}"); contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertTrue(contingencies.isEmpty()); }
@Test public void testExtension() throws IOException { writeToDslFile( "contingency('test') {", " equipments 'NHV1_NHV2_1'", " probability {", " base 0.1", " tsName 'myTs'", " }", "}"); List<Contingency> contingencies = new GroovyDslContingenciesProvider(dslFile) .getContingencies(network); assertEquals(1, contingencies.size()); assertEquals(1, contingencies.get(0).getExtensions().size()); assertEquals("myTs", contingencies.get(0).getExtension(ProbabilityContingencyExtension.class).getProbabilityTimeSeriesRef()); }