diff --git a/cimxml/src/main/java/de/soptim/opencgmes/cimxml/parser/ParserCIMXML_StAX_SR.java b/cimxml/src/main/java/de/soptim/opencgmes/cimxml/parser/ParserCIMXML_StAX_SR.java index eda150eb..e089d3bc 100644 --- a/cimxml/src/main/java/de/soptim/opencgmes/cimxml/parser/ParserCIMXML_StAX_SR.java +++ b/cimxml/src/main/java/de/soptim/opencgmes/cimxml/parser/ParserCIMXML_StAX_SR.java @@ -1500,7 +1500,7 @@ private void emitInitialBaseAndNamespacesDetermineCimNamespaceAndSetBaseIfNeeded // Determine CIM version. versionOfCIMXML = prefixURI; if (!CimNamespaceFactoryRegistry.hasProfileFactory(prefixURI)) { - RDFXMLparseWarning("The provided 'cim' namespace: " + prefixURI + " is not registered in CimNamespaceMapper.", location()); + RDFXMLparseWarning("The provided 'cim' namespace: " + prefixURI + " is not registered in " + CimNamespaceFactoryRegistry.class.getSimpleName() + ".", location()); } if (ReaderCIMXML_StAX_SR.TRACE) { trace.printf("CIM version of CIMXML: %s\n", versionOfCIMXML); diff --git a/cimxml/src/main/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStd.java b/cimxml/src/main/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStd.java index 97c65933..76fa5b4d 100644 --- a/cimxml/src/main/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStd.java +++ b/cimxml/src/main/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStd.java @@ -27,6 +27,7 @@ import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import org.apache.jena.datatypes.RDFDatatype; +import org.apache.jena.datatypes.TypeMapper; import org.apache.jena.datatypes.xsd.XSDDatatype; import org.apache.jena.datatypes.xsd.impl.RDFLangString; import org.apache.jena.graph.Graph; @@ -46,41 +47,38 @@ public class CimProfileRegistryStd implements CimProfileRegistry { private static final Query typedPropertiesQuery = QueryFactory.create( """ - PREFIX rdf: - PREFIX rdfs: - PREFIX cims: - - SELECT ?rdfType ?property ?cimDatatype ?primitiveType ?referenceType - WHERE - { - { - ?property rdfs:domain ?rdfType; - rdfs:range ?referenceType; - OPTIONAL { - ?property cims:AssociationUsed ?associationUsed - } - FILTER(!BOUND(?associationUsed) || ?associationUsed = "Yes") - } - UNION - { - ?property rdfs:domain ?rdfType; - cims:dataType ?cimDatatype. - { - ?cimDatatype cims:stereotype "CIMDatatype". - [] rdfs:domain ?cimDatatype; - rdfs:label ?label; - #rdfs:label "value"; - cims:dataType/cims:stereotype "Primitive"; - cims:dataType/rdfs:label ?primitiveType. - FILTER (!bound(?label) || str(?label) = "value") - } - UNION + PREFIX cims: + PREFIX rdf: + PREFIX rdfs: + PREFIX xsd: + + SELECT ?rdfType ?property ?cimDatatype ?primitiveType ?referenceType + WHERE { - ?cimDatatype cims:stereotype "Primitive"; - rdfs:label ?primitiveType. + ?rdfType rdf:type rdfs:Class . + ?property rdf:type rdf:Property ; + rdfs:domain ?rdfType . + { + ?property rdfs:range ?referenceType. + FILTER NOT EXISTS { ?property cims:AssociationUsed "No" } # Filter out associations that are not used as properties + } + UNION + { + ?property cims:dataType ?cimDatatype. + { + ?cimDatatype cims:stereotype "Primitive"; + rdfs:label ?primitiveType. + } + UNION + { + ?cimDatatype cims:stereotype "CIMDatatype". + BIND(IRI(CONCAT(STR(?cimDatatype), ".value")) AS ?parentTypeValue) + ?parentTypeValue rdfs:domain ?cimDatatype; + cims:dataType/cims:stereotype "Primitive"; + cims:dataType/rdfs:label ?primitiveType. + } + } } - } - } """); private final ErrorHandler errorHandler; private final Map, CimProfile> multiVersionIriProfiles = new ConcurrentHashMap<>(); @@ -323,14 +321,21 @@ private Map getTypedProperties(Graph g) { final var cimDatatype = vars.get("cimDatatype"); final var primitiveType = vars.get("primitiveType"); final var referenceType = vars.get("referenceType"); + final var rdfDataType = primitiveType != null + // retrieve CIM specific primitive type mapping + ? getXsdDatatype(primitiveType.getLiteralLexicalForm()) + // if no cimDatatype is given, but a referenceType is given + : cimDatatype == null && referenceType != null + // then this may be an XSD datatype defined as rdf:range + // if it cannot be mapped, we treat it as a reference type + ? TypeMapper.getInstance().getTypeByName(referenceType.getURI()) + : null; map.put(property, new PropertyInfo( rdfType, property, cimDatatype, - primitiveType != null - ? getXsdDatatype(primitiveType.getLiteralLexicalForm()) - : null, - referenceType)); + rdfDataType, + rdfDataType != null ? null : referenceType)); }); return Collections.unmodifiableMap(map); } diff --git a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStdTest.java b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStdTest.java deleted file mode 100644 index 672589bc..00000000 --- a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/CimProfileRegistryStdTest.java +++ /dev/null @@ -1,382 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package de.soptim.opencgmes.cimxml.rdfs; - -import de.soptim.opencgmes.cimxml.graph.CimProfile; -import de.soptim.opencgmes.cimxml.parser.RdfXmlParser; -import de.soptim.opencgmes.cimxml.parser.ReaderCIMXML_StAX_SR; -import de.soptim.opencgmes.cimxml.parser.system.StreamCimXmlToDatasetGraph; -import org.apache.jena.datatypes.xsd.XSDDatatype; -import org.apache.jena.graph.NodeFactory; -import org.junit.Test; - -import java.io.StringReader; -import java.util.Set; - -import static org.junit.Assert.*; - -public class CimProfileRegistryStdTest { - - @Test - public void registerProfileWithOneClassAndTwoSimpleProperties() { - final var rdfxml = """ - - - - - MYCUST - - 1.1.0 - - - - - - - - - - - - - - - - - - - - - - - - - Float - Primitive - - - - - String - Primitive - - - - """; - - final var parser = new ReaderCIMXML_StAX_SR(); - final var streamRDF = new StreamCimXmlToDatasetGraph(); - - parser.read(new StringReader(rdfxml), streamRDF); - - var graph = streamRDF.getCimDatasetGraph().getDefaultGraph(); - - var profile = CimProfile.wrap(graph); - - var registry = new CimProfileRegistryStd(); - registry.register(profile); - - var owlVersionIRIs = Set.of(NodeFactory.createURI("http://example.org/MyCustom/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - - assertTrue(registry.getRegisteredProfiles().contains(profile)); - assertEquals(1, registry.getRegisteredProfiles().size()); - - var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); - assertNotNull(properties); - assertEquals(2, properties.size()); - - var floatProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"); - assertTrue(properties.containsKey(floatProperty)); - var propertyInfo = properties.get(floatProperty); - assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); - assertEquals(floatProperty, propertyInfo.property()); - assertEquals(XSDDatatype.XSDfloat, propertyInfo.primitiveType()); - assertNull(propertyInfo.referenceType()); - - var textProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.textProperty"); - assertTrue(properties.containsKey(textProperty)); - propertyInfo = properties.get(textProperty); - assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); - assertEquals(textProperty, propertyInfo.property()); - assertEquals(XSDDatatype.XSDstring, propertyInfo.primitiveType()); - assertNull(propertyInfo.referenceType()); - } - - @Test - public void registerPofileWithMultipleVersionIRIs() { - final var rdfxml = """ - - - - - MYCUST - - - - - - """; - - - var profile = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); - - var registry = new CimProfileRegistryStd(); - registry.register(profile); - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomCore/1/1"), - NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomCore/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/AnyOtherProfile/1/1")); - - assertFalse(registry.containsProfile(owlVersionIRIs)); - assertTrue(registry.getPropertiesAndDatatypes(owlVersionIRIs).isEmpty()); - } - } - - @Test - public void registerProfilesWithSameVersionIris() { - final var rdfxml = """ - - - - - MYCUST - - - - - """; - - - var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); - var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); - - - var registry = new CimProfileRegistryStd(); - registry.register(profileA); - - assertThrows(IllegalArgumentException.class, () -> registry.register(profileB)); - } - - @Test - public void registerProfilesWithMultipleSameVersionIris() { - final var rdfxml = """ - - - - - MYCUST - - - - - - """; - - var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); - var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); - - - var registry = new CimProfileRegistryStd(); - registry.register(profileA); - - assertThrows(IllegalArgumentException.class, () -> registry.register(profileB)); - } - - @Test - public void registerProfilesWithSingleAndMultiVersionIrisMixed() { - final var rdfxmlProfileA = """ - - - - - MYCUST - - - - - - - - - - - - - - - - - - - - Float - Primitive - - - - """; - - final var rdfxmlProfileB = """ - - - - - MYCUST - - - - - - - - - - - - - - - - - - - Float - Primitive - - - - """; - - var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxmlProfileA)); - var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxmlProfileB)); - - - var registry = new CimProfileRegistryStd(); - registry.register(profileA); - registry.register(profileB); - - //add in different order - registry = new CimProfileRegistryStd(); - registry.register(profileB); - registry.register(profileA); - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomCore/1/1"), - NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); - assertNotNull(properties); - assertTrue(properties.containsKey(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"))); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomCore/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); - assertNotNull(properties); - assertTrue(properties.containsKey(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassB.floatProperty"))); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); - - assertTrue(registry.containsProfile(owlVersionIRIs)); - var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); - assertNotNull(properties); - assertTrue(properties.containsKey(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"))); - } - - { - var owlVersionIRIs = Set.of( - NodeFactory.createURI("http://example.org/AnyOtherProfile/1/1")); - - assertFalse(registry.containsProfile(owlVersionIRIs)); - assertTrue(registry.getPropertiesAndDatatypes(owlVersionIRIs).isEmpty()); - } - } - -} \ No newline at end of file diff --git a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStd.java b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStd.java new file mode 100644 index 00000000..7ee01cca --- /dev/null +++ b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStd.java @@ -0,0 +1,384 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.soptim.opencgmes.cimxml.rdfs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import de.soptim.opencgmes.cimxml.graph.CimProfile; +import de.soptim.opencgmes.cimxml.parser.RdfXmlParser; +import de.soptim.opencgmes.cimxml.parser.ReaderCIMXML_StAX_SR; +import de.soptim.opencgmes.cimxml.parser.system.StreamCimXmlToDatasetGraph; +import java.io.StringReader; +import java.util.Set; +import org.apache.jena.datatypes.xsd.XSDDatatype; +import org.apache.jena.graph.NodeFactory; +import org.junit.Test; + +public class TestCimProfileRegistryStd { + + @Test + public void registerProfileWithOneClassAndTwoSimpleProperties() { + final var rdfxml = """ + + + + + MYCUST + + 1.1.0 + + + + + + + + + + + + + + + + + + + + + + + + + Float + Primitive + + + + + String + Primitive + + + + """; + + final var parser = new ReaderCIMXML_StAX_SR(); + final var streamRDF = new StreamCimXmlToDatasetGraph(); + + parser.read(new StringReader(rdfxml), streamRDF); + + var graph = streamRDF.getCimDatasetGraph().getDefaultGraph(); + + var profile = CimProfile.wrap(graph); + + var registry = new CimProfileRegistryStd(); + registry.register(profile); + + var owlVersionIRIs = Set.of(NodeFactory.createURI("http://example.org/MyCustom/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + + assertTrue(registry.getRegisteredProfiles().contains(profile)); + assertEquals(1, registry.getRegisteredProfiles().size()); + + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertEquals(2, properties.size()); + + var floatProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"); + assertTrue(properties.containsKey(floatProperty)); + var propertyInfo = properties.get(floatProperty); + assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); + assertEquals(floatProperty, propertyInfo.property()); + assertEquals(XSDDatatype.XSDfloat, propertyInfo.primitiveType()); + assertNull(propertyInfo.referenceType()); + + var textProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.textProperty"); + assertTrue(properties.containsKey(textProperty)); + propertyInfo = properties.get(textProperty); + assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); + assertEquals(textProperty, propertyInfo.property()); + assertEquals(XSDDatatype.XSDstring, propertyInfo.primitiveType()); + assertNull(propertyInfo.referenceType()); + } + + @Test + public void registerProfileWithMultipleVersionIRIs() { + final var rdfxml = """ + + + + + MYCUST + + + + + + """; + + var profile = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); + + var registry = new CimProfileRegistryStd(); + registry.register(profile); + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomCore/1/1"), + NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomCore/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + assertNotNull(registry.getPropertiesAndDatatypes(owlVersionIRIs)); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/AnyOtherProfile/1/1")); + + assertFalse(registry.containsProfile(owlVersionIRIs)); + assert (registry.getPropertiesAndDatatypes(owlVersionIRIs).isEmpty()); + } + } + + @Test + public void registerProfilesWithSameVersionIris() { + final var rdfxml = """ + + + + + MYCUST + + + + + """; + + var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); + var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); + + var registry = new CimProfileRegistryStd(); + registry.register(profileA); + + assertThrows(IllegalArgumentException.class, () -> registry.register(profileB)); + } + + @Test + public void registerProfilesWithMultipleSameVersionIris() { + final var rdfxml = """ + + + + + MYCUST + + + + + + """; + + var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); + var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxml)); + + var registry = new CimProfileRegistryStd(); + registry.register(profileA); + + assertThrows(IllegalArgumentException.class, () -> registry.register(profileB)); + } + + @Test + public void registerProfilesWithSingleAndMultiVersionIrisMixed() { + final var rdfxmlProfileA = """ + + + + + MYCUST + + + + + + + + + + + + + + + + + + + + Float + Primitive + + + + """; + + final var rdfxmlProfileB = """ + + + + + MYCUST + + + + + + + + + + + + + + + + + + + Float + Primitive + + + + """; + + var profileA = new RdfXmlParser().parseCimProfile(new StringReader(rdfxmlProfileA)); + var profileB = new RdfXmlParser().parseCimProfile(new StringReader(rdfxmlProfileB)); + + var registry = new CimProfileRegistryStd(); + registry.register(profileA); + registry.register(profileB); + + //add in different order + registry = new CimProfileRegistryStd(); + registry.register(profileB); + registry.register(profileA); + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomCore/1/1"), + NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertTrue(properties.containsKey( + NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"))); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomCore/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertTrue(properties.containsKey( + NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassB.floatProperty"))); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/MyCustomOperation/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertTrue(properties.containsKey( + NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"))); + } + + { + var owlVersionIRIs = Set.of( + NodeFactory.createURI("http://example.org/AnyOtherProfile/1/1")); + + assertFalse(registry.containsProfile(owlVersionIRIs)); + assertTrue(registry.getPropertiesAndDatatypes(owlVersionIRIs).isEmpty()); + } + } + +} \ No newline at end of file diff --git a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStdTypeMapping.java b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStdTypeMapping.java new file mode 100644 index 00000000..58f557e8 --- /dev/null +++ b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/rdfs/TestCimProfileRegistryStdTypeMapping.java @@ -0,0 +1,203 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.soptim.opencgmes.cimxml.rdfs; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import de.soptim.opencgmes.cimxml.graph.CimProfile; +import de.soptim.opencgmes.cimxml.parser.ReaderCIMXML_StAX_SR; +import de.soptim.opencgmes.cimxml.parser.system.StreamCimXmlToDatasetGraph; +import java.io.StringReader; +import java.util.Set; +import org.apache.jena.datatypes.BaseDatatype; +import org.apache.jena.datatypes.TypeMapper; +import org.apache.jena.datatypes.xsd.XSDDatatype; +import org.apache.jena.graph.NodeFactory; +import org.junit.Test; + +public class TestCimProfileRegistryStdTypeMapping { + + /** + * The test is identical to + * de.soptim.opencgmes.cimxml.rdfs.TestCimProfileRegistryStd#registerProfileWithOneClassAndTwoSimpleProperties() + * except that not CIM primitive datatypes are referenced using 'cims:dataType', except the XML + * Schema datatypes are referenced directly using 'rdfs:range'. + */ + @Test + public void registerProfileWithOneClassAndTwoXSDProperties() { + final var rdfxml = """ + + + + + MYCUST + + 1.1.0 + + + + + + + + + + + + + + + + + + + + + + + + """; + + final var parser = new ReaderCIMXML_StAX_SR(); + final var streamRDF = new StreamCimXmlToDatasetGraph(); + + parser.read(new StringReader(rdfxml), streamRDF); + + var graph = streamRDF.getCimDatasetGraph().getDefaultGraph(); + + var profile = CimProfile.wrap(graph); + + var registry = new CimProfileRegistryStd(); + registry.register(profile); + + var owlVersionIRIs = Set.of(NodeFactory.createURI("http://example.org/MyCustom/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + + assertTrue(registry.getRegisteredProfiles().contains(profile)); + assertEquals(1, registry.getRegisteredProfiles().size()); + + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertEquals(2, properties.size()); + + var floatProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.floatProperty"); + assertTrue(properties.containsKey(floatProperty)); + var propertyInfo = properties.get(floatProperty); + assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); + assertEquals(floatProperty, propertyInfo.property()); + assertEquals(XSDDatatype.XSDfloat, propertyInfo.primitiveType()); + assertNull(propertyInfo.referenceType()); + + var textProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.textProperty"); + assertTrue(properties.containsKey(textProperty)); + propertyInfo = properties.get(textProperty); + assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), propertyInfo.rdfType()); + assertEquals(textProperty, propertyInfo.property()); + assertEquals(XSDDatatype.XSDstring, propertyInfo.primitiveType()); + assertNull(propertyInfo.referenceType()); + } + + @Test + public void registerProfileUsingCustomDataType() { + final var rdfxml = """ + + + + + MYCUST + + 1.1.0 + + + + + + + + + + + + + + + + + """; + + var semVerDataType = new BaseDatatype("https://semver.org#v2_0_0"); + var typeMapper = TypeMapper.getInstance(); + try { + typeMapper.registerDatatype(semVerDataType); + + final var parser = new ReaderCIMXML_StAX_SR(); + final var streamRDF = new StreamCimXmlToDatasetGraph(); + + parser.read(new StringReader(rdfxml), streamRDF); + + var graph = streamRDF.getCimDatasetGraph().getDefaultGraph(); + + var profile = CimProfile.wrap(graph); + + var registry = new CimProfileRegistryStd(); + registry.register(profile); + + var owlVersionIRIs = Set.of(NodeFactory.createURI("http://example.org/MyCustom/1/1")); + + assertTrue(registry.containsProfile(owlVersionIRIs)); + + assertTrue(registry.getRegisteredProfiles().contains(profile)); + assertEquals(1, registry.getRegisteredProfiles().size()); + + var properties = registry.getPropertiesAndDatatypes(owlVersionIRIs); + assertNotNull(properties); + assertEquals(1, properties.size()); + + var versionProperty = NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA.version"); + assertTrue(properties.containsKey(versionProperty)); + var propertyInfo = properties.get(versionProperty); + assertEquals(NodeFactory.createURI("http://iec.ch/TC57/CIM100#ClassA"), + propertyInfo.rdfType()); + assertEquals(versionProperty, propertyInfo.property()); + assertEquals(semVerDataType, propertyInfo.primitiveType()); + assertNull(propertyInfo.referenceType()); + } finally { + typeMapper.unregisterDatatype(semVerDataType); + } + } + +} \ No newline at end of file diff --git a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/CimDatasetGraphTest.java b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/CimDatasetGraphTest.java deleted file mode 100644 index ef537f0c..00000000 --- a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/CimDatasetGraphTest.java +++ /dev/null @@ -1,230 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package de.soptim.opencgmes.cimxml.sparql.core; - -import de.soptim.opencgmes.cimxml.parser.CimXmlParser; -import org.apache.jena.graph.NodeFactory; -import org.apache.jena.riot.system.ErrorHandlerFactory; -import org.junit.Test; - -import java.io.StringReader; - -import static org.junit.Assert.*; - -public class CimDatasetGraphTest { - - @Test - public void fullModelToSingleGraph() { - final var rdfxml = """ - - - - http://soptim.de/CIM/MyProfile/1.1 - - - My Custom Equipment - - - """; - - var model = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) - .parseCimModel(new StringReader(rdfxml)); - - var fullGraph = model.fullModelToSingleGraph(); - assertNotNull(fullGraph); - assertEquals(4, fullGraph.size()); - - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:08984e27-811f-4042-9125-1531ae0de0f6"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/61970-552/ModelDescription/1#FullModel") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:08984e27-811f-4042-9125-1531ae0de0f6"), - NodeFactory.createURI("http://iec.ch/TC57/61970-552/ModelDescription/1#Model.profile"), - NodeFactory.createLiteralString("http://soptim.de/CIM/MyProfile/1.1") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:f67fc354-9e39-4191-a456-67537399bc48"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyEquipment") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:f67fc354-9e39-4191-a456-67537399bc48"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), - NodeFactory.createLiteralString("My Custom Equipment") - )); - } - - @Test - public void differenceModelToFullModel() { - final var rdfXmlFullModel = """ - - - - http://soptim.de/CIM/MyProfile/1.1 - - - Name of my element - A - - - Name of new element to remove entirely - property of new element to remove - - - Name of element to remain - property of new element to remain - - - """; - - final var rdfxmlDifferenceModel = """ - - - - http://soptim.de/CIM/MyProfile/1.1 - urn:uuid:d4336345-ad68-4566-afab-d9798ec5ca86 - - - - - Name of my element - - - - - - - - - B - - - - - Name of new element to add - property of new element - - - - - - - - - A - - - - - Name of new element to remove entirely - property of new element to remove - - - - - - - """; - - var predecessorFullModel = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) - .parseCimModel(new StringReader(rdfXmlFullModel)); - var differenceModel = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) - .parseCimModel(new StringReader(rdfxmlDifferenceModel)); - - var fullGraph = differenceModel.differenceModelToFullModel(predecessorFullModel); - assertNotNull(fullGraph); - assertEquals(9, fullGraph.size()); - // the element to remain unchanged - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), - NodeFactory.createLiteralString("Name of element to remain") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), - NodeFactory.createLiteralString("property of new element to remain") - )); - - // the unchanged properties - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), - NodeFactory.createLiteralString("Name of my element") - )); - - // the updated property of the unchanged element - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), - NodeFactory.createLiteralString("B") - )); - - // the newly added element - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), - NodeFactory.createLiteralString("Name of new element to add") - )); - assertTrue(fullGraph.contains( - NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), - NodeFactory.createLiteralString("property of new element") - )); - - // the removed element must not be present - assertFalse(fullGraph.contains( - NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), - NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") - )); - assertFalse(fullGraph.contains( - NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), - NodeFactory.createLiteralString("Name of new element to remove entirely") - )); - assertFalse(fullGraph.contains( - NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), - NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), - NodeFactory.createLiteralString("property of new element to remove") - )); - } -} \ No newline at end of file diff --git a/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/TestCimDatasetGraph.java b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/TestCimDatasetGraph.java new file mode 100644 index 00000000..e4b1706e --- /dev/null +++ b/cimxml/src/test/java/de/soptim/opencgmes/cimxml/sparql/core/TestCimDatasetGraph.java @@ -0,0 +1,232 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.soptim.opencgmes.cimxml.sparql.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import de.soptim.opencgmes.cimxml.parser.CimXmlParser; +import java.io.StringReader; +import org.apache.jena.graph.NodeFactory; +import org.apache.jena.riot.system.ErrorHandlerFactory; +import org.junit.Test; + +public class TestCimDatasetGraph { + + @Test + public void fullModelToSingleGraph() { + final var rdfxml = """ + + + + http://soptim.de/CIM/MyProfile/1.1 + + + My Custom Equipment + + + """; + + var model = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) + .parseCimModel(new StringReader(rdfxml)); + + var fullGraph = model.fullModelToSingleGraph(); + assertNotNull(fullGraph); + assertEquals(4, fullGraph.size()); + + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:08984e27-811f-4042-9125-1531ae0de0f6"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/61970-552/ModelDescription/1#FullModel") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:08984e27-811f-4042-9125-1531ae0de0f6"), + NodeFactory.createURI("http://iec.ch/TC57/61970-552/ModelDescription/1#Model.profile"), + NodeFactory.createLiteralString("http://soptim.de/CIM/MyProfile/1.1") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:f67fc354-9e39-4191-a456-67537399bc48"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyEquipment") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:f67fc354-9e39-4191-a456-67537399bc48"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), + NodeFactory.createLiteralString("My Custom Equipment") + )); + } + + @Test + public void differenceModelToFullModel() { + final var rdfXmlFullModel = """ + + + + http://soptim.de/CIM/MyProfile/1.1 + + + Name of my element + A + + + Name of new element to remove entirely + property of new element to remove + + + Name of element to remain + property of new element to remain + + + """; + + final var rdfxmlDifferenceModel = """ + + + + http://soptim.de/CIM/MyProfile/1.1 + urn:uuid:d4336345-ad68-4566-afab-d9798ec5ca86 + + + + + Name of my element + + + + + + + + + B + + + + + Name of new element to add + property of new element + + + + + + + + + A + + + + + Name of new element to remove entirely + property of new element to remove + + + + + + + """; + + var predecessorFullModel = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) + .parseCimModel(new StringReader(rdfXmlFullModel)); + var differenceModel = new CimXmlParser(ErrorHandlerFactory.errorHandlerNoWarnings) + .parseCimModel(new StringReader(rdfxmlDifferenceModel)); + + var fullGraph = differenceModel.differenceModelToFullModel(predecessorFullModel); + assertNotNull(fullGraph); + assertEquals(9, fullGraph.size()); + // the element to remain unchanged + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), + NodeFactory.createLiteralString("Name of element to remain") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:5a70f6b8-8c77-41f9-9793-6fe5bd67b756"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), + NodeFactory.createLiteralString("property of new element to remain") + )); + + // the unchanged properties + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), + NodeFactory.createLiteralString("Name of my element") + )); + + // the updated property of the unchanged element + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:135c601e-bad4-4872-ba8f-b15baf91bd2f"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), + NodeFactory.createLiteralString("B") + )); + + // the newly added element + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), + NodeFactory.createLiteralString("Name of new element to add") + )); + assertTrue(fullGraph.contains( + NodeFactory.createURI("urn:uuid:2d1e4820-8858-49de-b441-5a03e7c40035"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), + NodeFactory.createLiteralString("property of new element") + )); + + // the removed element must not be present + assertFalse(fullGraph.contains( + NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), + NodeFactory.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement") + )); + assertFalse(fullGraph.contains( + NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#IdentifiedObject.name"), + NodeFactory.createLiteralString("Name of new element to remove entirely") + )); + assertFalse(fullGraph.contains( + NodeFactory.createURI("urn:uuid:c9fe6664-fcf0-44e6-9d20-656538b68d1c"), + NodeFactory.createURI("http://iec.ch/TC57/CIM100#MyElement.MyProperty"), + NodeFactory.createLiteralString("property of new element to remove") + )); + } +} \ No newline at end of file diff --git a/cimxml/src/test/java/de/soptim/opencgmes/parser/TestConvertCimXmlToJsonLd.java b/cimxml/src/test/java/de/soptim/opencgmes/parser/TestConvertCimXmlToJsonLd.java index 123ff33b..868f66ee 100644 --- a/cimxml/src/test/java/de/soptim/opencgmes/parser/TestConvertCimXmlToJsonLd.java +++ b/cimxml/src/test/java/de/soptim/opencgmes/parser/TestConvertCimXmlToJsonLd.java @@ -21,116 +21,125 @@ import de.soptim.opencgmes.cimxml.graph.CimProfile; import de.soptim.opencgmes.cimxml.parser.CimXmlParser; import de.soptim.opencgmes.cimxml.sparql.core.LinkedCimDatasetGraph; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import org.apache.jena.graph.NodeFactory; import org.apache.jena.riot.Lang; import org.apache.jena.riot.RDFDataMgr; import org.junit.Ignore; import org.junit.Test; -import java.io.BufferedOutputStream; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - public class TestConvertCimXmlToJsonLd { - private static final String CIM_SCHEMA_IRI = "cgmes:schema:"; - private static final String CIM_MODEL_IRI = "cgmes:model:"; - private static final String CIM_HEADER_IRI = "cgmes:header:"; - - public void convertCimXmlToJsonLd(String rdfsSchemaFolder, String cimXmlFolder, String jsonldOutputFile) { - convertCimXmlToJsonLd(Path.of(rdfsSchemaFolder), Path.of(cimXmlFolder), Path.of(jsonldOutputFile)); - } + private static final String CIM_SCHEMA_IRI = "cgmes:schema:"; + private static final String CIM_MODEL_IRI = "cgmes:model:"; + private static final String CIM_HEADER_IRI = "cgmes:header:"; - public void convertCimXmlToJsonLd(Path rdfsSchemaFolder, Path cimXmlFolder, Path jsonldOutputFile) { - final var dataset = new LinkedCimDatasetGraph(); - final var parser = new CimXmlParser(); - try (var rdfFiles = Files.list(rdfsSchemaFolder)) { - rdfFiles.parallel() - .filter(f -> f.toString().endsWith(".rdf")) - .forEach(rdfFile -> { - try { - final var schema = parser.parseAndRegisterCimProfile(rdfFile); - // add only header profiles to the dataset. - // the other profiles are only added when referenced in the dataset. - if(schema.isHeaderProfile()) { - dataset.addGraph(NodeFactory.createURI(CIM_SCHEMA_IRI + schema.getDcatKeyword()), schema); - dataset.prefixes().putAll(schema.getPrefixMapping()); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - } catch (IOException e) { - throw new RuntimeException(e); - } + public void convertCimXmlToJsonLd(String rdfsSchemaFolder, String cimXmlFolder, + String jsonldOutputFile) { + convertCimXmlToJsonLd(Path.of(rdfsSchemaFolder), Path.of(cimXmlFolder), + Path.of(jsonldOutputFile)); + } - try (var cimXmlFiles = Files.list(cimXmlFolder)) { - cimXmlFiles.parallel() - .filter(f -> f.toString().endsWith(".xml")) - .forEach(cimXmlFile -> { - try { - final var cimDataset = parser.parseCimModel(cimXmlFile); - if(!cimDataset.isFullModel()) { - System.out.println("File " + cimXmlFile.getFileName() + " is not a FullModel. Skipping."); - return; - } - final var keywords = parser.getCimProfileRegistry() - .getMatchingProfiles(cimDataset.getModelHeader().getProfiles()) - .stream() - .peek(schema -> { - final var schemaGraphName = NodeFactory.createURI( - CIM_SCHEMA_IRI + schema.getDcatKeyword()); - if(!dataset.containsGraph(schemaGraphName)) { - dataset.addGraph(schemaGraphName, schema); - dataset.prefixes().putAll(schema.getPrefixMapping()); - } - }) - .map(CimProfile::getDcatKeyword) - .distinct() - .sorted() - .toList(); - if(keywords.isEmpty()) { - System.out.println("File " + cimXmlFile.getFileName() + " does not match any profile. Skipping."); - return; - } - var keyword = keywords.getFirst(); - if(keywords.size() > 1) { - System.out.println("File " + cimXmlFile.getFileName() + " matches multiple profiles: " + keywords + ". Using first one: " + keyword); - } - dataset.addGraph(NodeFactory.createURI(CIM_HEADER_IRI + keyword), cimDataset.getModelHeader()); - dataset.addGraph(NodeFactory.createURI(CIM_MODEL_IRI + keyword), cimDataset.getBody()); - dataset.prefixes().putAll(cimDataset.prefixes()); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - } catch (IOException e) { - throw new RuntimeException(e); - } - // create java nio file and output stream - try (var rdfFiles = new BufferedOutputStream(Files.newOutputStream(jsonldOutputFile))) { - RDFDataMgr.write(rdfFiles, dataset, Lang.JSONLD11); - } catch (IOException e) { - throw new RuntimeException(e); - } + public void convertCimXmlToJsonLd(Path rdfsSchemaFolder, Path cimXmlFolder, + Path jsonldOutputFile) { + final var dataset = new LinkedCimDatasetGraph(); + final var parser = new CimXmlParser(); + try (var rdfFiles = Files.list(rdfsSchemaFolder)) { + rdfFiles.parallel() + .filter(f -> f.toString().endsWith(".rdf")) + .forEach(rdfFile -> { + try { + final var schema = parser.parseAndRegisterCimProfile(rdfFile); + // add only header profiles to the dataset. + // the other profiles are only added when referenced in the dataset. + if (schema.isHeaderProfile()) { + dataset.addGraph(NodeFactory.createURI(CIM_SCHEMA_IRI + schema.getDcatKeyword()), + schema); + dataset.prefixes().putAll(schema.getPrefixMapping()); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } catch (IOException e) { + throw new RuntimeException(e); } - @Ignore - @Test - public void convertFullGridMerged() { - convertCimXmlToJsonLd( - "../data/ApplicationProfilesLibrary-release1-1-0/CGMES/RDFS/", - "../data/CGMES_ConformityAssessmentScheme_r3-0-2/CGMES_ConformityAssessmentScheme_TestConfigurations_v3-0-3/v3.0/FullGrid/FullGrid-Merged/", - "../output/FullGrid-Merged.jsonld"); + try (var cimXmlFiles = Files.list(cimXmlFolder)) { + cimXmlFiles.parallel() + .filter(f -> f.toString().endsWith(".xml")) + .forEach(cimXmlFile -> { + try { + final var cimDataset = parser.parseCimModel(cimXmlFile); + if (!cimDataset.isFullModel()) { + System.out.println( + "File " + cimXmlFile.getFileName() + " is not a FullModel. Skipping."); + return; + } + final var keywords = parser.getCimProfileRegistry() + .getMatchingProfiles(cimDataset.getModelHeader().getProfiles()) + .stream() + .peek(schema -> { + final var schemaGraphName = NodeFactory.createURI( + CIM_SCHEMA_IRI + schema.getDcatKeyword()); + if (!dataset.containsGraph(schemaGraphName)) { + dataset.addGraph(schemaGraphName, schema); + dataset.prefixes().putAll(schema.getPrefixMapping()); + } + }) + .map(CimProfile::getDcatKeyword) + .distinct() + .sorted() + .toList(); + if (keywords.isEmpty()) { + System.out.println( + "File " + cimXmlFile.getFileName() + " does not match any profile. Skipping."); + return; + } + var keyword = keywords.getFirst(); + if (keywords.size() > 1) { + System.out.println( + "File " + cimXmlFile.getFileName() + " matches multiple profiles: " + keywords + + ". Using first one: " + keyword); + } + dataset.addGraph(NodeFactory.createURI(CIM_HEADER_IRI + keyword), + cimDataset.getModelHeader()); + dataset.addGraph(NodeFactory.createURI(CIM_MODEL_IRI + keyword), + cimDataset.getBody()); + dataset.prefixes().putAll(cimDataset.prefixes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + } catch (IOException e) { + throw new RuntimeException(e); } - - @Ignore - @Test - public void convertRealGridMerged() { - convertCimXmlToJsonLd( - "../data/ApplicationProfilesLibrary-release1-1-0/CGMES/RDFS/", - "../data/CGMES_ConformityAssessmentScheme_r3-0-2/CGMES_ConformityAssessmentScheme_TestConfigurations_v3-0-3/v3.0/RealGrid/RealGrid-Merged/", - "../output/RealGrid-Merged.jsonld"); + // create java nio file and output stream + try (var rdfFiles = new BufferedOutputStream(Files.newOutputStream(jsonldOutputFile))) { + RDFDataMgr.write(rdfFiles, dataset, Lang.JSONLD11); + } catch (IOException e) { + throw new RuntimeException(e); } + } + + @Ignore + @Test + public void convertFullGridMerged() { + convertCimXmlToJsonLd( + "../data/ApplicationProfilesLibrary-release1-1-0/CGMES/RDFS/", + "../data/CGMES_ConformityAssessmentScheme_r3-0-2/CGMES_ConformityAssessmentScheme_TestConfigurations_v3-0-3/v3.0/FullGrid/FullGrid-Merged/", + "../output/FullGrid-Merged.jsonld"); + } + + @Ignore + @Test + public void convertRealGridMerged() { + convertCimXmlToJsonLd( + "../data/ApplicationProfilesLibrary-release1-1-0/CGMES/RDFS/", + "../data/CGMES_ConformityAssessmentScheme_r3-0-2/CGMES_ConformityAssessmentScheme_TestConfigurations_v3-0-3/v3.0/RealGrid/RealGrid-Merged/", + "../output/RealGrid-Merged.jsonld"); + } } \ No newline at end of file