Skip to content

Commit

Permalink
add new mechanisms
Browse files Browse the repository at this point in the history
Signed-off-by: RALAMBOTIANA MIORA <miora.ralambotiana@rte-france.com>
  • Loading branch information
miovd committed Jan 23, 2020
1 parent 075a5c0 commit 20e2f48
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.xml.stream.XMLStreamWriter;

import java.util.function.BiConsumer;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ protected void writeRootElementAttributes(StaticVarCompensator svc, VoltageLevel

@Override
protected void writeSubElements(StaticVarCompensator svc, VoltageLevel vl, NetworkXmlWriterContext context) throws XMLStreamException {
if (!Objects.equals(svc, svc.getRegulatingTerminal().getConnectable())) {
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, REGULATING_TERMINAL, IidmXmlVersion.V_1_1, context);
TerminalRefXml.writeTerminalRef(svc.getRegulatingTerminal(), context, REGULATING_TERMINAL);
}
IidmXmlUtil.assertMinimumVersionAndRunIfNotDefault(!Objects.equals(svc, svc.getRegulatingTerminal().getConnectable()),
ROOT_ELEMENT_NAME, REGULATING_TERMINAL, IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED,
IidmXmlVersion.V_1_1, context, () -> TerminalRefXml.writeTerminalRef(svc.getRegulatingTerminal(), context, REGULATING_TERMINAL));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class ThreeWindingsTransformerXml extends AbstractTransformerXml<ThreeWindingsTr

static final String ROOT_ELEMENT_NAME = "threeWindingsTransformer";

private static final String RATIO_TAP_CHANGER_1 = "ratioTapChanger1";
private static final String PHASE_TAP_CHANGER_1 = "phaseTapChanger1";
private static final String PHASE_TAP_CHANGER_2 = "phaseTapChanger2";
private static final String PHASE_TAP_CHANGER_3 = "phaseTapChanger3";

@Override
protected String getRootElementName() {
return ROOT_ELEMENT_NAME;
Expand Down Expand Up @@ -49,15 +54,20 @@ protected void writeRootElementAttributes(ThreeWindingsTransformer twt, Substati
XmlUtil.writeDouble("ratedU1", twt.getLeg1().getRatedU(), context.getWriter());
XmlUtil.writeDouble("r2", twt.getLeg2().getR(), context.getWriter());
XmlUtil.writeDouble("x2", twt.getLeg2().getX(), context.getWriter());
XmlUtil.writeDouble("g2", twt.getLeg2().getG(), context.getWriter());
XmlUtil.writeDouble("b2", twt.getLeg2().getB(), context.getWriter());
IidmXmlUtil.writeDoubleAttributeFromMinimumVersion(ROOT_ELEMENT_NAME, "g2", twt.getLeg2().getG(), 0,
IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
IidmXmlUtil.writeDoubleAttributeFromMinimumVersion(ROOT_ELEMENT_NAME, "b2", twt.getLeg2().getB(), 0,
IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
XmlUtil.writeDouble("ratedU2", twt.getLeg2().getRatedU(), context.getWriter());
XmlUtil.writeDouble("r3", twt.getLeg3().getR(), context.getWriter());
XmlUtil.writeDouble("x3", twt.getLeg3().getX(), context.getWriter());
XmlUtil.writeDouble("g3", twt.getLeg3().getG(), context.getWriter());
XmlUtil.writeDouble("b3", twt.getLeg3().getB(), context.getWriter());
IidmXmlUtil.writeDoubleAttributeFromMinimumVersion(ROOT_ELEMENT_NAME, "g3", twt.getLeg3().getG(), 0,
IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
IidmXmlUtil.writeDoubleAttributeFromMinimumVersion(ROOT_ELEMENT_NAME, "b3", twt.getLeg3().getB(), 0,
IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
XmlUtil.writeDouble("ratedU3", twt.getLeg3().getRatedU(), context.getWriter());
XmlUtil.writeDouble("ratedU0", twt.getRatedU0(), context.getWriter());
IidmXmlUtil.writeDoubleAttributeFromMinimumVersion(ROOT_ELEMENT_NAME, "ratedU0", twt.getRatedU0(), twt.getLeg1().getRatedU(),
IidmXmlUtil.ErrorMessage.NOT_DEFAULT_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
writeNodeOrBus(1, twt.getLeg1().getTerminal(), context);
writeNodeOrBus(2, twt.getLeg2().getTerminal(), context);
writeNodeOrBus(3, twt.getLeg3().getTerminal(), context);
Expand All @@ -70,11 +80,19 @@ protected void writeRootElementAttributes(ThreeWindingsTransformer twt, Substati

@Override
protected void writeSubElements(ThreeWindingsTransformer twt, Substation s, NetworkXmlWriterContext context) throws XMLStreamException {
IidmXmlUtil.assertMinimumVersionIfNotDefault(twt.getLeg1().getRatioTapChanger() != null, ROOT_ELEMENT_NAME, RATIO_TAP_CHANGER_1,
IidmXmlUtil.ErrorMessage.NOT_NULL_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
writeRatioTapChanger(twt.getLeg1().getRatioTapChanger(), 1, context);
IidmXmlUtil.assertMinimumVersionIfNotDefault(twt.getLeg1().getPhaseTapChanger() != null, ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_1,
IidmXmlUtil.ErrorMessage.NOT_NULL_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
writePhaseTapChanger(twt.getLeg1().getPhaseTapChanger(), 1, context);
writeRatioTapChanger(twt.getLeg2().getRatioTapChanger(), 2, context);
IidmXmlUtil.assertMinimumVersionIfNotDefault(twt.getLeg2().getPhaseTapChanger() != null, ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_2,
IidmXmlUtil.ErrorMessage.NOT_NULL_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
writePhaseTapChanger(twt.getLeg2().getPhaseTapChanger(), 2, context);
writeRatioTapChanger(twt.getLeg3().getRatioTapChanger(), 3, context);
IidmXmlUtil.assertMinimumVersionIfNotDefault(twt.getLeg3().getPhaseTapChanger() != null, ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_3,
IidmXmlUtil.ErrorMessage.NOT_NULL_NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
writePhaseTapChanger(twt.getLeg3().getPhaseTapChanger(), 3, context);
if (twt.getLeg1().getCurrentLimits() != null) {
writeCurrentLimits(1, twt.getLeg1().getCurrentLimits(), context.getWriter(), context.getVersion());
Expand Down Expand Up @@ -155,31 +173,31 @@ protected void readSubElements(ThreeWindingsTransformer tx, NetworkXmlReaderCont
readCurrentLimits(3, tx.getLeg3()::newCurrentLimits, context.getReader());
break;

case "ratioTapChanger1":
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, "ratioTapChanger1", IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
case RATIO_TAP_CHANGER_1:
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, RATIO_TAP_CHANGER_1, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
readRatioTapChanger(1, tx.getLeg1(), context);
break;

case "phaseTapChanger1":
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, "phaseTapChanger1", IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
case PHASE_TAP_CHANGER_1:
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_1, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
readPhaseTapChanger(1, tx.getLeg1(), context);
break;

case "ratioTapChanger2":
readRatioTapChanger(2, tx.getLeg2(), context);
break;

case "phaseTapChanger2":
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, "phaseTapChanger2", IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
case PHASE_TAP_CHANGER_2:
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_2, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
readPhaseTapChanger(2, tx.getLeg2(), context);
break;

case "ratioTapChanger3":
readRatioTapChanger(3, tx.getLeg3(), context);
break;

case "phaseTapChanger3":
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, "phaseTapChanger3", IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
case PHASE_TAP_CHANGER_3:
IidmXmlUtil.assertMinimumVersion(ROOT_ELEMENT_NAME, PHASE_TAP_CHANGER_3, IidmXmlUtil.ErrorMessage.NOT_SUPPORTED, IidmXmlVersion.V_1_1, context);
readPhaseTapChanger(3, tx.getLeg3(), context);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@
package com.powsybl.iidm.xml.util;

import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.xml.XmlUtil;
import com.powsybl.iidm.xml.AbstractNetworkXmlContext;
import com.powsybl.iidm.xml.IidmXmlVersion;
import com.powsybl.iidm.xml.NetworkXmlReaderContext;
import com.powsybl.iidm.xml.NetworkXmlWriterContext;

import javax.xml.stream.XMLStreamException;
import java.util.Objects;

/**
* @author Miora Ralambotiana <miora.ralambotiana at rte-france.com>
*/
public final class IidmXmlUtil {

public interface IidmXmlRunnable {
void run() throws XMLStreamException;
}

public enum ErrorMessage {
NOT_SUPPORTED("not supported"),
MANDATORY("mandatory");
MANDATORY("mandatory"),
NOT_NULL_NOT_SUPPORTED("not null and not supported"),
NOT_DEFAULT_NOT_SUPPORTED("not defined as default and not supported");

private String message;

Expand All @@ -38,6 +49,34 @@ public static <C extends AbstractNetworkXmlContext> void assertMinimumVersion(St
}
}

/**
* Assert that the context's IIDM-XML version equals or is more recent than a given IIDM-XML version if the value of an attribute or the state of an equipment
* is not default (interpretable for previous versions).
* If not, throw an exception with a given type of error message.
*/
public static <C extends AbstractNetworkXmlContext> void assertMinimumVersionIfNotDefault(boolean valueIsNotDefault, String rootElementName,
String elementName, ErrorMessage type, IidmXmlVersion minVersion,
C context) {
if (valueIsNotDefault) {
assertMinimumVersion(rootElementName, elementName, type, minVersion, context);
}
}

/**
* Assert that the context's IIDM-XML version equals or is more recent than a given IIDM-XML version if the value of an attribute or the state of an equipment
* is not default (interpretable for previous versions).
* If not, throw an exception with a given type of error message.
* If the value is not default and no exception has been thrown, run a given runnable.
*/
public static <C extends AbstractNetworkXmlContext> void assertMinimumVersionAndRunIfNotDefault(boolean valueIsNotDefault, String rootElementName,
String elementName, ErrorMessage type, IidmXmlVersion minVersion,
C context, IidmXmlRunnable runnable) throws XMLStreamException {
if (valueIsNotDefault) {
assertMinimumVersion(rootElementName, elementName, type, minVersion, context);
runnable.run();
}
}

/**
* Assert that the context's IIDM-XML version is strictly older than a given IIDM-XML version.
* If not, throw an exception with a given type of error message.
Expand All @@ -50,7 +89,7 @@ public static <C extends AbstractNetworkXmlContext> void assertStrictMaximumVers
}

/**
* Read an attribute which is <b>mandatory</b> from a given minimum IIDM-XML version. <br>
* Read a double attribute which is <b>mandatory</b> from a given minimum IIDM-XML version. <br>
* If the context's IIDM-XML version is strictly older than the given minimum IIDM-XML version, the attribute <b>should not exist</b> (else an exception is thrown).
* In this case, return Double.NaN <br>
* If the context's IIDM-XML version equals or is more recent than the given minimum IIDM-XML version, the attribute <b>must exist</b> (else an exception is thrown).
Expand All @@ -61,7 +100,7 @@ public static double readDoubleAttributeFromMinimumVersion(String rootElementNam
}

/**
* Read an attribute which is <b>mandatory</b> from a given minimum IIDM-XML version. <br>
* Read a double attribute which is <b>mandatory</b> from a given minimum IIDM-XML version. <br>
* If the context's IIDM-XML version is strictly older than the given minimum IIDM-XML version, the attribute <b>should not exist</b> (else an exception is thrown).
* In this case, return a given defaultValue. <br>
* If the context's IIDM-XML version equals or is more recent than the given minimum IIDM-XML version, the attribute <b>must exist</b> (else an exception is thrown).
Expand All @@ -77,6 +116,20 @@ public static double readDoubleAttributeFromMinimumVersion(String rootElementNam
return attributeStr == null ? defaultValue : Double.valueOf(attributeStr);
}

/**
* Write a <b>mandatory</b> double attribute from a given minimum IIDM-XML version.<br>
* If the context's IIDM-XML version is strictly older than the given minimum IIDM-XML version, the attribute's value <b>should be default</b>
* (else an exception is thrown).
*/
public static void writeDoubleAttributeFromMinimumVersion(String rootElementName, String attributeName, double value, double defaultValue,
ErrorMessage type, IidmXmlVersion minVersion, NetworkXmlWriterContext context) throws XMLStreamException {
if (context.getVersion().compareTo(minVersion) >= 0) {
XmlUtil.writeDouble(attributeName, value, context.getWriter());
} else {
assertMinimumVersionIfNotDefault(!Objects.equals(value, defaultValue), rootElementName, attributeName, type, minVersion, context);
}
}

private IidmXmlUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void readFaultyVersionRegulatingSvcFile() {
@Test
public void writeFaultyVersionRegulatingSvcFile() throws IOException {
exception.expect(PowsyblException.class);
exception.expectMessage("staticVarCompensator.regulatingTerminal is not supported for IIDM-XML version 1.0. " +
exception.expectMessage("staticVarCompensator.regulatingTerminal is not defined as default and not supported for IIDM-XML version 1.0. " +
"IIDM-XML version should be >= 1.1");
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
NetworkXml.write(SvcTestCaseFactory.createWithRemoteRegulatingTerminal(), new ExportOptions().setVersion("1.0"), os);
Expand Down

0 comments on commit 20e2f48

Please sign in to comment.