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

Commit

Permalink
Fix bug 108 remove binded dsl extension methods in script task
Browse files Browse the repository at this point in the history
  • Loading branch information
yichen88 committed Sep 20, 2017
1 parent bd740fa commit f66ab30
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
10 changes: 8 additions & 2 deletions action/action-dsl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>eu.itesla_project</groupId>
<artifactId>action</artifactId>
Expand Down Expand Up @@ -62,7 +62,7 @@
<artifactId>iidm-api</artifactId>
<version>${project.version}</version>
</dependency>


<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -97,6 +97,12 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>action-util</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,17 @@ class ActionDslLoader extends DslLoader {
// contingencies
binding.contingency = { String id, Closure<Void> closure ->
def cloned = closure.clone()
ContingencySpec spec = new ContingencySpec()
cloned.delegate = spec
ContingencySpec contingencySpec = new ContingencySpec()
cloned.delegate = contingencySpec
cloned()
if (!spec.equipments) {
if (!contingencySpec.equipments) {
throw new RuntimeException("'equipments' field is not set")
}
if (spec.equipments.length == 0) {
if (contingencySpec.equipments.length == 0) {
throw new RuntimeException("'equipments' field is empty")
}
def elements = []
for (String equipment : spec.equipments) {
for (String equipment : contingencySpec.equipments) {
Identifiable identifiable = network.getIdentifiable(equipment)
if (identifiable == null) {
throw new ActionDslException("Equipment '" + equipment + "' of contingency '" + id + "' not found")
Expand All @@ -148,18 +148,18 @@ class ActionDslLoader extends DslLoader {
// rules
binding.rule = { String id, Closure<Void> closure ->
def cloned = closure.clone()
RuleSpec spec = new RuleSpec()
cloned.delegate = spec
RuleSpec ruleSpec = new RuleSpec()
cloned.delegate = ruleSpec
cloned()
if (!spec.when) {
if (!ruleSpec.when) {
throw new RuntimeException("'when' field is not set")
}
if (spec.apply.length == 0) {
if (ruleSpec.apply.length == 0) {
throw new RuntimeException("'apply' field is empty")
}
Rule rule = new Rule(id, new ExpressionCondition(spec.when), spec.life, spec.apply)
if (spec.description) {
rule.setDescription(spec.description)
Rule rule = new Rule(id, new ExpressionCondition(ruleSpec.when), ruleSpec.life, ruleSpec.apply)
if (ruleSpec.description) {
rule.setDescription(ruleSpec.description)
}
rulesDb.addRule(rule)
LOGGER.debug("Found rule '{}'", id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package eu.itesla_project.action.dsl.task

import com.google.auto.service.AutoService
import eu.itesla_project.action.dsl.ActionDslException
import eu.itesla_project.action.dsl.DslConstants
import eu.itesla_project.action.dsl.spi.DslTaskExtension
import eu.itesla_project.computation.ComputationManager
Expand All @@ -27,7 +28,11 @@ class ScriptDslTaskExtension implements DslTaskExtension, DslConstants {
binding.setVariable("computationManager", computationManager)
binding.setVariable(SCRIPT_IS_RUNNING, true)
try {
closure.owner.delegate.metaClass = null
closure.resolveStrategy = Closure.OWNER_ONLY
closure.call()
} catch (MissingMethodException e) {
throw new ActionDslException(e)
} finally {
binding.setVariable(SCRIPT_IS_RUNNING, null)
binding.setVariable("network", oldNetwork)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ public ActionDslException(String message) {
public ActionDslException(String message, Throwable cause) {
super(message, cause);
}

public ActionDslException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import groovy.lang.GroovyCodeSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand All @@ -22,6 +23,9 @@
*/
public class ActionDslLoaderTest {

@org.junit.Rule
public final ExpectedException exception = ExpectedException.none();

private Network network;

@Before
Expand Down Expand Up @@ -54,4 +58,22 @@ public void test() {
assertEquals("action description", action.getDescription());
assertEquals(0, action.getTasks().size());
}

@Test
public void testDslExtension() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/actions2.groovy"))).load(network);
Action another = actionDb.getAction("anotherAction");
exception.expect(RuntimeException.class);
exception.expectMessage("Switch 'switchId' not found");
another.run(network, null);
}

@Test
public void testUnvalidate() {
ActionDb actionDb = new ActionDslLoader(new GroovyCodeSource(getClass().getResource("/actions2.groovy"))).load(network);
Action someAction = actionDb.getAction("someAction");
someAction.getId();
exception.expect(ActionDslException.class);
someAction.run(network, null);
}
}
21 changes: 21 additions & 0 deletions action/action-dsl/src/test/resources/actions2.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
rule ('Memoriser_Prise_Init_TD_Boutre') {
when !contingencyOccurred()
life 1
apply 'someAction'
}

action ('someAction') {
description 'asdf'
tasks {
script {
transformer('NGEN_NHV1').r = 3
closeSwitch('switchId')
}
}
}

action ('anotherAction') {
tasks {
closeSwitch('switchId')
}
}

0 comments on commit f66ab30

Please sign in to comment.