Skip to content

Commit

Permalink
Read base URI if present during CGMES triplestore import and apply to…
Browse files Browse the repository at this point in the history
… all files (#2197)

Signed-off-by: VEDELAGO MIORA <[email protected]>

Co-authored-by: Luma <[email protected]>
  • Loading branch information
miovd and zamarrenolm authored Jul 18, 2022
1 parent 752f3fc commit 993a98e
Show file tree
Hide file tree
Showing 7 changed files with 2,305 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ public final class CgmesConformity1ModifiedCatalog {
private CgmesConformity1ModifiedCatalog() {
}

public static TestGridModelResources microGridBaseCaseBEExplicitBase() {
String base = ENTSOE_CONFORMITY_1_MODIFIED
+ "/MicroGrid/BaseCase/BC_BE_v2_explicitBase/";
String baseOriginal = ENTSOE_CONFORMITY_1
+ "/MicroGrid/BaseCase/CGMES_v2.4.15_MicroGridTestConfiguration_BC_BE_v2/";
String baseBoundary = ENTSOE_CONFORMITY_1
+ "/MicroGrid/BaseCase/CGMES_v2.4.15_MicroGridTestConfiguration_BD_v2/";
return new TestGridModelResources(
"MicroGrid-BaseCase-BE-explicitBase",
null,
new ResourceSet(base,
"MicroGridTestConfiguration_BC_BE_EQ_V2.xml"),
new ResourceSet(baseOriginal,
"MicroGridTestConfiguration_BC_BE_SSH_V2.xml",
"MicroGridTestConfiguration_BC_BE_SV_V2.xml",
"MicroGridTestConfiguration_BC_BE_TP_V2.xml"),
new ResourceSet(baseBoundary, "MicroGridTestConfiguration_EQ_BD.xml",
"MicroGridTestConfiguration_TP_BD.xml"));
}

public static TestGridModelResources microGridBaseCaseBERatioPhaseTapChangerTabular() {
String base = ENTSOE_CONFORMITY_1_MODIFIED
+ "/MicroGrid/BaseCase/BC_BE_v2_rtc_ptc_tabular/";
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void tearDown() throws IOException {
fileSystem.close();
}

@Test
public void microBEExplicitBase() {
Network network = new CgmesImport()
.importData(CgmesConformity1ModifiedCatalog.microGridBaseCaseBEExplicitBase().dataSource(), NetworkFactory.findDefault(), null);
assertNotNull(network);
}

@Test
public void microBERatioPhaseTabularTest() {
Network network = new CgmesImport()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static com.powsybl.cgmes.model.CgmesNamespace.*;
Expand Down Expand Up @@ -57,12 +58,24 @@ public boolean existsCim14() {
}

public String baseName() {
// Build an absolute IRI from the data source base name
String name = dataSource.getBaseName().toLowerCase();
if (name.isEmpty()) {
name = "default-cgmes-model";
}
return "http://" + name;
// Get the base URI if present, else build an absolute URI from the data source base name
return names().stream()
.map(n -> {
try (InputStream is = dataSource.newInputStream(n)) {
return NamespaceReader.base(is);
} catch (IOException x) {
throw new UncheckedIOException(x);
}
})
.filter(Objects::nonNull)
.findFirst()
.orElseGet(() -> {
String name = dataSource.getBaseName().toLowerCase();
if (name.isEmpty()) {
name = "default-cgmes-model";
}
return "http://" + name;
});
}

public Set<String> names() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,26 @@ public static Set<String> namespaces1(InputStream is) throws XMLStreamException
return found;
}

public static String base(InputStream is) {
XMLStreamReader xmlsr;
try {
xmlsr = XML_INPUT_FACTORY_SUPPLIER.get().createXMLStreamReader(is);
try {
while (xmlsr.hasNext()) {
int eventType = xmlsr.next();
if (eventType == XMLStreamConstants.START_ELEMENT) {
return xmlsr.getAttributeValue(null, "base");
}
}
} finally {
xmlsr.close();
XmlUtil.gcXmlInputFactory(XML_INPUT_FACTORY_SUPPLIER.get());
}
return null;
} catch (XMLStreamException e) {
throw new CgmesModelException("base", e);
}
}

private static final Supplier<XMLInputFactory> XML_INPUT_FACTORY_SUPPLIER = Suppliers.memoize(XMLInputFactory::newInstance);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (c) 2022, 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 com.powsybl.cgmes.model;

import org.junit.Test;

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
* @author Miora Vedelago <miora.ralambotiana at rte-france.com>
*/
public class NamespaceReaderTest {

@Test
public void base() throws IOException {
try (InputStream is = getClass().getResourceAsStream("/empty_cim16_EQ_with_explicitBase.xml")) {
assertEquals("http://example.com", NamespaceReader.base(is));
}
}

@Test
public void baseNull() throws IOException {
try (InputStream is = getClass().getResourceAsStream("/empty_cim16_EQ.xml")) {
assertNull(NamespaceReader.base(is));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:cim="http://iec.ch/TC57/2013/CIM-schema-cim16#" xml:base="http://example.com">
</rdf:RDF>

0 comments on commit 993a98e

Please sign in to comment.