Skip to content

Commit

Permalink
deserialize ts
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bouquet <[email protected]>
  • Loading branch information
bqth29 committed Jan 24, 2025
1 parent 5c70d5f commit b1ae3eb
Show file tree
Hide file tree
Showing 9 changed files with 750 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.google.common.base.Suppliers;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.util.ServiceLoaderCache;

import java.time.OffsetDateTime;
import java.util.List;

/**
Expand All @@ -22,14 +24,26 @@
*/
public interface CracFactory {

/**
* Create a {@code Crac} object.
*
* @param id: ID to assign to the created Crac.
* @param name: Name to assign to the created Crac.
* @param timestamp: Timestamp of validity of the Crac.
* @return the created {@code Crac} instance.
*/
Crac create(String id, String name, OffsetDateTime timestamp);

/**
* Create a {@code Crac} object.
*
* @param id: ID to assign to the created Crac.
* @param name: Name to assign to the created Crac.
* @return the created {@code Crac} instance.
*/
Crac create(String id, String name);
default Crac create(String id, String name) {
return create(id, name, null);
}

/**
* Create a {@code Crac} object. Name will be equal to id.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

import com.google.auto.service.AutoService;

import java.time.OffsetDateTime;

/**
* Mock CracFactory implementation, for unit tests only
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
*/
@AutoService(CracFactory.class)
public class MockCracFactory1 implements CracFactory {
@Override
public Crac create(String id, String name, OffsetDateTime timestamp) {
return null;
}

@Override
public Crac create(String id, String name) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@

import com.google.auto.service.AutoService;

import java.time.OffsetDateTime;

/**
* Mock CracFactory implementation, for unit tests only
* @author Peter Mitri {@literal <peter.mitri at rte-france.com>}
*/
@AutoService(CracFactory.class)
public class MockCracFactory2 implements CracFactory {
@Override
public Crac create(String id, String name, OffsetDateTime timestamp) {
return null;
}

@Override
public Crac create(String id, String name) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.powsybl.openrao.data.crac.api.CracFactory;
import com.google.auto.service.AutoService;

import java.time.OffsetDateTime;

/**
* Crac Factory implementation.
*
Expand All @@ -26,6 +28,11 @@ public String getName() {
return NAME;
}

@Override
public Crac create(String id, String name, OffsetDateTime timestamp) {
return new CracImpl(id, name, timestamp);
}

@Override
public Crac create(String id, String name) {
return new CracImpl(id, name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class JsonSerializationConstants {
private JsonSerializationConstants() {
}

public static final String CRAC_IO_VERSION = "2.6";
public static final String CRAC_IO_VERSION = "2.7";
/*
v1.1: addition of switchPairs
v1.2: addition of injectionRangeAction
Expand All @@ -62,6 +62,7 @@ private JsonSerializationConstants() {
v2.4: new names for onConstraint and cnecId, side left/right -> one/two
v2.5: elementary actions have new type coming from core remedial actions
v2.6: addition of activation-cost and variation-costs for remedial actions
v2.7: addition of timestamp
*/

// headers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.OffsetDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -69,8 +70,15 @@ public Crac deserialize(JsonParser jsonParser, DeserializationContext deserializ
throw new OpenRaoException(String.format("The JSON Crac must contain a %s field after the %s field", JsonSerializationConstants.NAME, JsonSerializationConstants.ID));
}
String name = jsonParser.nextTextValue();
// TODO: deserialize timestamp
Crac crac = cracFactory.create(id, name);
JsonToken nextToken = jsonParser.nextToken();
OffsetDateTime timestamp;
if (jsonParser.getCurrentName().equals(JsonSerializationConstants.TIMESTAMP)) {
timestamp = OffsetDateTime.parse(jsonParser.nextTextValue());
nextToken = jsonParser.nextToken();
} else {
timestamp = null;
}
Crac crac = cracFactory.create(id, name, timestamp);
if (JsonSerializationConstants.getPrimaryVersionNumber(version) < 2) {
crac.newInstant("preventive", InstantKind.PREVENTIVE)
.newInstant("outage", InstantKind.OUTAGE)
Expand All @@ -81,7 +89,7 @@ public Crac deserialize(JsonParser jsonParser, DeserializationContext deserializ
Map<String, String> deserializedNetworkElementsNamesPerId = null;

// deserialize the following lines of the Crac
while (jsonParser.nextToken() != JsonToken.END_OBJECT) {
while (nextToken != JsonToken.END_OBJECT) {
switch (jsonParser.getCurrentName()) {
case JsonSerializationConstants.NETWORK_ELEMENTS_NAME_PER_ID:
jsonParser.nextToken();
Expand Down Expand Up @@ -151,6 +159,7 @@ public Crac deserialize(JsonParser jsonParser, DeserializationContext deserializ
default:
throw new OpenRaoException("Unexpected field in Crac: " + jsonParser.getCurrentName());
}
nextToken = jsonParser.nextToken();
}
return crac;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema#",
"$id": "https://www.powsybl.orgopen-rao-crac-schema=crac-v2.6.schema",
"title": "CRAC",
"description": "Contingency list, Remedial Actions and additional Constraints",
"type": "object",
"properties": {
"type": {
"const": "CRAC"
},
"version": {
"const": "2.7"
},
"info": {
"type": "string"
},
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"timestamp": {
"type": "string"
},
"instants": {
"type": "array",
"items": {
"$ref": "resource:/schemas/instant/instant-v2.0.json"
}
},
"ra-usage-limits-per-instant": {
"type": "array",
"items": {
"$ref": "resource:/schemas/ra-usage-limits-per-instant/ra-usage-limits-per-instant-v2.1.json"
}
},
"networkElementsNamePerId": {
"type": "object",
"additionalProperties": {
"type": "string"
}
},
"contingencies": {
"type": "array",
"items": {
"$ref": "resource:/schemas/contingency/contingency-v1.0.json"
}
},
"flowCnecs": {
"type": "array",
"items": {
"items": {
"$ref": "resource:/schemas/flow-cnec/flow-cnec-v2.4.json"
}
}
},
"angleCnecs": {
"type": "array",
"items": {
"items": {
"$ref": "resource:/schemas/angle-cnec/angle-cnec-v1.4.json"
}
}
},
"voltageCnecs": {
"type": "array",
"items": {
"items": {
"$ref": "resource:/schemas/voltage-cnec/voltage-cnec-v1.5.json"
}
}
},
"pstRangeActions": {
"type": "array",
"items": {
"$ref": "resource:/schemas/pst-range-action/pst-range-action-v2.6.json"
}
},
"hvdcRangeActions": {
"type": "array",
"items": {
"$ref": "resource:/schemas/hvdc-range-action/hvdc-range-action-v2.6.json"
}
},
"injectionRangeActions": {
"type": "array",
"items": {
"$ref": "resource:/schemas/injection-range-action/injection-range-action-v2.6.json"
}
},
"counterTradeRangeActions": {
"type": "array",
"items": {
"$ref": "resource:/schemas/counter-trade-range-action/counter-trade-range-action-v2.6.json"
}
},
"networkActions": {
"type": "array",
"items": {
"$ref": "resource:/schemas/network-action/network-action-v2.6.json"
}
}
},
"additionalProperties": false,
"required": [
"type",
"version",
"id",
"name"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -354,9 +356,19 @@ void importV2Point6Test() {

Crac crac = new JsonImport().importData(cracFile, CracCreationParameters.load(), network, null).getCrac();
assertEquals(7, crac.getNetworkActions().size());
assertTrue(crac.getTimestamp().isEmpty());
testContentOfV2Point6Crac(crac);
}

@Test
void importV2Point7Test() {
InputStream cracFile = getClass().getResourceAsStream("/retrocompatibility/v2/crac-v2.7.json");

Crac crac = new JsonImport().importData(cracFile, CracCreationParameters.load(), network, null).getCrac();
assertEquals(7, crac.getNetworkActions().size());
testContentOfV2Point7Crac(crac);
}

private void testContentOfV1Point0Crac(Crac crac) {
Instant preventiveInstant = crac.getInstant("preventive");
Instant autoInstant = crac.getInstant("auto");
Expand Down Expand Up @@ -878,4 +890,11 @@ private void testContentOfV2Point6Crac(Crac crac) {
assertEquals(Optional.of(15000.0), crac.getRangeAction("counterTradeRange1Id").getVariationCost(VariationDirection.UP));
assertEquals(Optional.of(18000.0), crac.getRangeAction("counterTradeRange1Id").getVariationCost(VariationDirection.DOWN));
}

private void testContentOfV2Point7Crac(Crac crac) {
testContentOfV2Point6Crac(crac);
Optional<OffsetDateTime> timestamp = crac.getTimestamp();
assertTrue(timestamp.isPresent());
assertEquals(OffsetDateTime.of(2025, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC), timestamp.get());
}
}
Loading

0 comments on commit b1ae3eb

Please sign in to comment.