Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# ignore Intellij files

data/crac/crac-io/crac-io-json/src/test/huge_crac/*
!data/crac/crac-io/crac-io-json/src/test/huge_crac/*.txt


tests/src/test/resources/files/raoresults/*.zip

# ignore Intellij files
.idea
*.iml

Expand Down
47 changes: 14 additions & 33 deletions checkstyle.xml
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Puppy Crawl//DTD Check Configuration 1.2//EN"
"http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name="Checker">
<module name="FileTabCharacter"/>

<module name="NewlineAtEndOfFile"/>

<module name="RegexpHeader">
<property name="header" value="^/\*\n * Copyright \(c\) \d{4}, RTE \(http://www\.rte-france\.com\)\n \* This Source Code Form is subject to the terms of the Mozilla Public\n * License, v\. 2\.0. If a copy of the MPL was not distributed with this\n \* file, You can obtain one at http://mozilla\.org/MPL/2\.0/.\n \*/"/>
<property name="fileExtensions" value="java"/>
</module>

<module name="RegexpMultiline">
<property name="format" value="(?:\n{2}\n+)|(?:\n{2}$)"/>
<property name="message" value="Multiple consecutive blank lines are not allowed."/>
</module>
<module name="FileTabCharacter"/>

<module name="RegexpMultiline">
<property name="format" value="\*\/\n\n+(@[A-Za-z]+\(?.*\)?)?(public|protected|private)?\s*(final)?\s*(class|interface|enum|abstract|record)"/>
<property name="message" value="No blank line allowed between Javadoc and class definition."/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<module name="NewlineAtEndOfFile">
<property name="lineSeparator" value="lf_cr_crlf"/>
</module>

<module name="RegexpSingleline">
Expand All @@ -31,11 +17,10 @@
<property name="message" value="Line has trailing spaces."/>
</module>

<module name="SuppressWarningsFilter" />

<module name="Translation"/>

<module name="TreeWalker">
<module name="SuppressWarningsHolder"/>
<module name="AbbreviationAsWordInName"/>
<module name="AbstractClassName"/>
<module name="ArrayTypeStyle"/>
Expand All @@ -47,25 +32,21 @@
<property name="allowNoEmptyLineBetweenFields" value="true"/>
<property name="allowMultipleEmptyLines" value="false"/>
<property name="allowMultipleEmptyLinesInsideClassMembers" value="false"/>
<property name="tokens" value="PACKAGE_DEF, IMPORT, CLASS_DEF, ENUM_DEF, INTERFACE_DEF, RECORD_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
<property name="tokens" value="IMPORT, CLASS_DEF, INTERFACE_DEF, STATIC_INIT, INSTANCE_INIT, METHOD_DEF, CTOR_DEF, VARIABLE_DEF"/>
</module>
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="IllegalImport"/>
<module name="Indentation"/>
<module name="JavadocType">
<property name="scope" value="private"/>
<property name="authorFormat" value="[A-Za-z._ -]+ \{@literal &lt;[A-Za-z._-]+ at [A-Za-z._-]+\.[a-z]+&gt;}"/>
<property name="allowMissingParamTags" value="true"/>
</module>
<module name="LeftCurly"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="MethodParamPad"/>
<module name="ModifierOrder"/>
<module name="MultipleVariableDeclarations"/>
<module name="MutableException"/>
<module name="NeedBraces"/>
Expand All @@ -76,10 +57,6 @@
<module name="ParameterName"/>
<module name="ParenPad"/>
<module name="RedundantImport"/>
<module name="Regexp">
<property name="format" value="@author"/>
</module>
<module name="RequireEmptyLineBeforeBlockTagGroup"/>
<module name="RightCurly"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>
Expand All @@ -90,6 +67,10 @@
<module name="UnusedImports"/>
<module name="UpperEll"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>
<module name="WhitespaceAround">
<property name="ignoreEnhancedForColon" value="false"/>
</module>
<module name="SingleSpaceSeparator"/>
</module>
</module>
<module name="SuppressWarningsFilter"/>
</module>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package com.powsybl.openrao.data.crac.api;

import static java.lang.String.format;

import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.contingency.Contingency;
import com.powsybl.iidm.network.Network;
Expand All @@ -22,6 +24,11 @@
import com.powsybl.openrao.data.crac.api.cnec.VoltageCnecAdder;
import com.powsybl.openrao.data.crac.api.io.Exporter;
import com.powsybl.openrao.data.crac.api.io.Importer;
import com.powsybl.openrao.data.crac.api.io.utils.BufferSize;
import com.powsybl.openrao.data.crac.api.io.utils.SafeFileReader;
import com.powsybl.openrao.data.crac.api.io.utils.TmpFile;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkAction;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkActionAdder;
import com.powsybl.openrao.data.crac.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.crac.api.rangeaction.CounterTradeRangeAction;
import com.powsybl.openrao.data.crac.api.rangeaction.CounterTradeRangeActionAdder;
Expand All @@ -32,14 +39,18 @@
import com.powsybl.openrao.data.crac.api.rangeaction.PstRangeAction;
import com.powsybl.openrao.data.crac.api.rangeaction.PstRangeActionAdder;
import com.powsybl.openrao.data.crac.api.rangeaction.RangeAction;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkAction;
import com.powsybl.openrao.data.crac.api.networkaction.NetworkActionAdder;

import java.io.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.time.OffsetDateTime;
import java.util.*;

import static java.lang.String.format;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

/**
* Interface to manage CRAC.
Expand Down Expand Up @@ -570,76 +581,94 @@ default boolean isRangeActionAvailableInState(RangeAction<?> rangeAction, State
* Get the CRAC format
*
* @param filename CRAC file name
* @param inputStream CRAC data
* @param filename CRAC data
* @return the CRAC format (if found)
*/
static String getCracFormat(File filename) {
var reader = SafeFileReader.create(filename, BufferSize.MEDIUM);
return Importer.findImporter(reader).getFormat();
}

@Deprecated
static String getCracFormat(String filename, InputStream inputStream) throws IOException {
byte[] bytes = getBytesFromInputStream(inputStream);
return findImporter(filename, bytes).getFormat();
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return getCracFormat(tmp.getTempFile().toFile());
}
}

/**
* Import CRAC from a file, inside a CracCreationContext
*
* @param filename CRAC file name
* @param inputStream CRAC data
* @param network the network on which the CRAC data is based
* @param cracCreationParameters extra CRAC creation parameters
* @return CracCreationContext object
*/
static CracCreationContext readWithContext(String filename, InputStream inputStream, Network network, CracCreationParameters cracCreationParameters) throws IOException {
byte[] bytes = getBytesFromInputStream(inputStream);
return findImporter(filename, bytes).importData(new ByteArrayInputStream(bytes), cracCreationParameters, network);
static CracCreationContext readWithContext(File filename, Network network, CracCreationParameters cracCreationParameters) {
var reader = SafeFileReader.create(filename, BufferSize.MEDIUM);
return Importer.findImporter(reader).importData(reader, cracCreationParameters, network);
}

private static Importer findImporter(String filename, byte[] bytes) {
return new ServiceLoaderCache<>(Importer.class).getServices().stream()
.filter(importer -> importer.exists(filename, new ByteArrayInputStream(bytes)))
.findAny()
.orElseThrow(() -> new OpenRaoException("No suitable CRAC importer found."));
@Deprecated
static CracCreationContext readWithContext(String filename, InputStream inputStream, Network network, CracCreationParameters cracCreationParameters) throws IOException {
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return readWithContext(tmp.getTempFile().toFile(), network, cracCreationParameters);
}
}

/**
* Import CRAC from a file, inside a CracCreationContext
*
* @param filename CRAC file name
* @param inputStream CRAC data
* @param network the network on which the CRAC data is based
* @return CracCreationContext object
*/
static CracCreationContext readWithContext(File filename, Network network) {
return readWithContext(filename, network, CracCreationParameters.load());
}

@Deprecated
static CracCreationContext readWithContext(String filename, InputStream inputStream, Network network) throws IOException {
return readWithContext(filename, inputStream, network, CracCreationParameters.load());
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return readWithContext(tmp.getTempFile().toFile(), network);
}
}

/**
* Import CRAC from a file
*
* @param filename CRAC file name
* @param inputStream CRAC data
* @param network the network on which the CRAC data is based
* @param cracCreationParameters extra CRAC creation parameters
* @return CRAC object
*/
static Crac read(File filename, Network network, CracCreationParameters cracCreationParameters) {
return readWithContext(filename, network, cracCreationParameters).getCrac();
}

@Deprecated
static Crac read(String filename, InputStream inputStream, Network network, CracCreationParameters cracCreationParameters) throws IOException {
return readWithContext(filename, inputStream, network, cracCreationParameters).getCrac();
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return read(tmp.getTempFile().toFile(), network, cracCreationParameters);
}
}

/**
* Import CRAC from a file
*
* @param filename CRAC file name
* @param inputStream CRAC data
* @param network the network on which the CRAC data is based
* @return CRAC object
*/
static Crac read(String filename, InputStream inputStream, Network network) throws IOException {
return read(filename, inputStream, network, CracCreationParameters.load());
static Crac read(File filename, Network network) {
return read(filename, network, CracCreationParameters.load());
}

private static byte[] getBytesFromInputStream(InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
org.apache.commons.io.IOUtils.copy(inputStream, baos);
return baos.toByteArray();
@Deprecated
static Crac read(String filename, InputStream inputStream, Network network) throws IOException {
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return read(tmp.getTempFile().toFile(), network);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package com.powsybl.openrao.data.crac.api.io;

import com.powsybl.openrao.data.crac.api.Crac;

import java.io.OutputStream;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@

package com.powsybl.openrao.data.crac.api.io;

import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.commons.OpenRaoException;
import com.powsybl.openrao.data.crac.api.CracCreationContext;
import com.powsybl.openrao.data.crac.api.io.utils.BufferSize;
import com.powsybl.openrao.data.crac.api.io.utils.SafeFileReader;
import com.powsybl.openrao.data.crac.api.io.utils.TmpFile;
import com.powsybl.openrao.data.crac.api.parameters.CracCreationParameters;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;

/**
* @author Thomas Bouquet {@literal <thomas.bouquet at rte-france.com>}
Expand All @@ -23,15 +29,41 @@ public interface Importer {
*/
String getFormat();

boolean exists(String filename, InputStream inputStream);
boolean exists(SafeFileReader inputFile);

@Deprecated
default boolean exists(String filename, InputStream inputStream) {
try (var tmp = TmpFile.create(filename, inputStream, BufferSize.MEDIUM)) {
return exists(SafeFileReader.create(tmp.getTempFile().toFile(), BufferSize.MEDIUM));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

/**
* Create a model.
*
* @param inputStream data input stream
* @param inputFile data input stream
* @param cracCreationParameters extra CRAC creation parameters
* @param network network upon which the CRAC is based
* @return the model
*/
CracCreationContext importData(InputStream inputStream, CracCreationParameters cracCreationParameters, Network network);
CracCreationContext importData(SafeFileReader inputFile, CracCreationParameters cracCreationParameters, Network network);

@Deprecated
default CracCreationContext importData(InputStream inputStream, CracCreationParameters cracCreationParameters, Network network) {
try (var tmp = TmpFile.create("importData", inputStream, BufferSize.MEDIUM)) {
return importData(SafeFileReader.create(tmp.getTempFile().toFile(), BufferSize.MEDIUM), cracCreationParameters, network);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

static Importer findImporter(SafeFileReader inputFile) {
return new ServiceLoaderCache<>(Importer.class).getServices().stream()
.filter(importer -> importer.exists(inputFile))
.findAny()
.orElseThrow(() -> new OpenRaoException("No suitable importer found."));
}

}
Loading