This repository has been archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Groovy post-processor implementation
- Loading branch information
Mathieu BAGUE
committed
Sep 1, 2017
1 parent
612140f
commit 68e314b
Showing
5 changed files
with
159 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...converter-api/src/main/java/eu/itesla_project/iidm/import_/GroovyScriptPostProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* Copyright (c) 2017, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package eu.itesla_project.iidm.import_; | ||
|
||
import com.google.auto.service.AutoService; | ||
import eu.itesla_project.commons.config.ModuleConfig; | ||
import eu.itesla_project.commons.config.PlatformConfig; | ||
import eu.itesla_project.computation.ComputationManager; | ||
import eu.itesla_project.iidm.network.Network; | ||
import groovy.lang.Binding; | ||
import groovy.lang.GroovyShell; | ||
import org.codehaus.groovy.control.CompilerConfiguration; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.Reader; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author Mathieu Bague <mathieu.bague@rte-france.com> | ||
*/ | ||
@AutoService(ImportPostProcessor.class) | ||
public class GroovyScriptPostProcessor implements ImportPostProcessor { | ||
|
||
public static final String NAME = "groovyScript"; | ||
|
||
public static final String DEFAULT_SCRIPT_NAME = "import-post-processor.groovy"; | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(GroovyScriptPostProcessor.class); | ||
|
||
private Path script; | ||
|
||
public GroovyScriptPostProcessor() { | ||
this(PlatformConfig.defaultConfig()); | ||
} | ||
|
||
public GroovyScriptPostProcessor(PlatformConfig platformConfig) { | ||
Objects.requireNonNull(platformConfig); | ||
|
||
Path defaultScript = platformConfig.getConfigDir().resolve(DEFAULT_SCRIPT_NAME); | ||
|
||
ModuleConfig config = platformConfig.getModuleConfigIfExists("groovy-post-processor"); | ||
if (config != null) { | ||
script = config.getPathProperty("script", defaultScript); | ||
} else { | ||
script = defaultScript; | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public void process(Network network, ComputationManager computationManager) throws Exception { | ||
if (Files.exists(script)) { | ||
LOGGER.debug("Execute groovy post processor {}", script); | ||
try (Reader reader = Files.newBufferedReader(script)) { | ||
CompilerConfiguration conf = new CompilerConfiguration(); | ||
|
||
Binding binding = new Binding(); | ||
binding.setVariable("network", network); | ||
binding.setVariable("computationManager", computationManager); | ||
|
||
GroovyShell shell = new GroovyShell(binding, conf); | ||
shell.evaluate(reader); | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...erter-api/src/test/java/eu/itesla_project/iidm/import_/GroovyScriptPostProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* Copyright (c) 2017, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package eu.itesla_project.iidm.import_; | ||
|
||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import eu.itesla_project.commons.config.InMemoryPlatformConfig; | ||
import eu.itesla_project.commons.config.MapModuleConfig; | ||
import eu.itesla_project.commons.config.PlatformConfig; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* @author Mathieu Bague <mathieu.bague@rte-france.com> | ||
*/ | ||
public class GroovyScriptPostProcessorTest { | ||
|
||
private FileSystem fileSystem; | ||
|
||
@Before | ||
public void setUp() throws IOException { | ||
fileSystem = Jimfs.newFileSystem(Configuration.unix()); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
fileSystem.close(); | ||
} | ||
|
||
@Test | ||
public void test() throws IOException { | ||
InMemoryPlatformConfig platformConfig = new InMemoryPlatformConfig(fileSystem); | ||
Path script = platformConfig.getConfigDir().resolve(GroovyScriptPostProcessor.DEFAULT_SCRIPT_NAME); | ||
Files.copy(getClass().getResourceAsStream("/import-post-processor.groovy"), script); | ||
test(platformConfig); | ||
|
||
// Test with a custom script name | ||
script = platformConfig.getConfigDir().resolve("custom-script.groovy"); | ||
Files.copy(getClass().getResourceAsStream("/import-post-processor.groovy"), script); | ||
MapModuleConfig moduleConfig = platformConfig.createModuleConfig("groovy-post-processor"); | ||
moduleConfig.setStringProperty("script", script.toAbsolutePath().toString()); | ||
test(platformConfig); | ||
} | ||
|
||
private void test(PlatformConfig platformConfig) { | ||
GroovyScriptPostProcessor processor = new GroovyScriptPostProcessor(platformConfig); | ||
assertEquals("groovyScript", processor.getName()); | ||
|
||
try { | ||
processor.process(null, null); | ||
fail(); | ||
} catch (Exception ignored) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
iidm/iidm-converter-api/src/test/resources/import-post-processor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Copyright (c) 2017, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
if (network == null) { | ||
throw new NullPointerException() | ||
} |