Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Groovy post-processor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu BAGUE committed Sep 1, 2017
1 parent 612140f commit 68e314b
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
4 changes: 4 additions & 0 deletions iidm/iidm-converter-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
Expand Down
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);
}
}
}
}
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) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public void test() throws IOException {
// Test with a custom script name
script = platformConfig.getConfigDir().resolve("custom-script.js");
Files.copy(getClass().getResourceAsStream("/import-post-processor.js"), script);
test(platformConfig);
moduleConfig.setStringProperty("script", script.toAbsolutePath().toString());
test(platformConfig);
}
Expand Down
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()
}

0 comments on commit 68e314b

Please sign in to comment.