From 9e73760418cb1f38c578b0ee148ad67b1b2501f8 Mon Sep 17 00:00:00 2001 From: Hoda Date: Mon, 6 Nov 2023 22:03:53 +0100 Subject: [PATCH 1/3] Add SingleEdgeLabelMapping and its tests --- ExampleLPG2RDF.ttl | 18 ++++- .../impl/LPG2RDFConfigurationReader.java | 37 +++++++++++ .../SingleEdgeLabelMappingToURIsImpl.java | 37 +++++++++++ .../liu/ida/hefquin/vocabulary/LPG2RDF.java | 3 + .../impl/LPG2RDFConfigurationReaderTest.java | 65 +++++++++++++++++++ .../SingleEdgeLabelMappingToURIsImplTest.java | 58 +++++++++++++++++ 6 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java create mode 100644 src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImplTest.java diff --git a/ExampleLPG2RDF.ttl b/ExampleLPG2RDF.ttl index f0ba6e5ef..130ac85bb 100644 --- a/ExampleLPG2RDF.ttl +++ b/ExampleLPG2RDF.ttl @@ -33,6 +33,9 @@ PREFIX ex: #ex:RegexBasedEdgeLabelMapping a rdfs:Class ; # rdfs:subClassOf ex:EdgeLabelMapping . +#ex:SingleEdgeLabelMapping a rdfs:Class ; +# rdfs:subClassOf ex:EdgeLabelMapping . + #ex:PropertyNameMapping a rdfs:Class ; # #ex:IRIBasedPropertyNameMapping a rdfs:Class ; @@ -64,6 +67,14 @@ PREFIX ex: # rdfs:domain ex:RegexBasedEdgeLabelMapping ; # rdfs:range xsd:string . +#ex:label a rdf:Property ; +# rdfs:domain ex:SingleEdgeLabelMapping ; +# rdfs:range xsd:string . + +#ex:iri a rdf:Property ; +# rdfs:domain ex:SingleEdgeLabelMapping ; +# rdfs:range xsd:anyURI . + #ex:propertyNameMapping a rdf:Property ; # rdfs:domain ex:LPGtoRDFConfiguration ; # rdfs:range ex:PropertyNameMapping . @@ -74,7 +85,8 @@ ex:LPGtoRDFConfig a lr:LPGtoRDFConfiguration ; lr:nodeMapping ex:IRINodeMapping ; lr:nodeLabelMapping ex:IRINodeLabelMapping ; # lr:edgeLabelMapping ex:IRIEdgeLabelMapping ; - lr:edgeLabelMapping ex:RegexEdgeLabelMapping ; +# lr:edgeLabelMapping ex:RegexEdgeLabelMapping ; + lr:edgeLabelMapping ex:SingleIRIEdgeLabelMapping ; lr:propertyNameMapping ex:IRIPropertyNameMapping . ex:IRINodeMapping a lr:IRIBasedNodeMapping ; @@ -90,5 +102,9 @@ ex:RegexEdgeLabelMapping a lr:RegexBasedEdgeLabelMapping ; lr:regex "^\\w+" ; lr:prefixOfIRIs "http://purl.org/dc/terms/"^^xsd:anyURI . +ex:SingleIRIEdgeLabelMapping a lr:SingleEdgeLabelMapping ; + lr:label "DIRECTED" ; + lr:iri "http://dsgfrjlk.org/kdfj/directorOf"^^xsd:anyURI . + ex:IRIPropertyNameMapping a lr:IRIBasedPropertyNameMapping ; lr:prefixOfIRIs "https://example.org/property/"^^xsd:anyURI . diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReader.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReader.java index 33cd93a37..a046c05b7 100644 --- a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReader.java +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReader.java @@ -240,6 +240,43 @@ else if ( edgeLabelMappingResourceType.equals(LPG2RDF.RegexBasedEdgeLabelMapping throw new IllegalArgumentException("prefixOfIRIs is an invalid URI!"); } } + + else if ( edgeLabelMappingResourceType.equals(LPG2RDF.SingleEdgeLabelMapping) + || (edgeLabelMappingResourceType.equals(LPG2RDF.EdgeLabelMapping) && edgeLabelMappingResource.hasProperty(LPG2RDF.label) + && edgeLabelMappingResource.hasProperty(LPG2RDF.iri))) { + final StmtIterator labelIterator = edgeLabelMappingResource.listProperties(LPG2RDF.label); + if (!labelIterator.hasNext()) { + throw new IllegalArgumentException("label is required!"); + } + final RDFNode labelObj = labelIterator.next().getObject(); + if (labelIterator.hasNext()) { + throw new IllegalArgumentException("An instance of SingleEdgeLabelMapping has more than one label property!"); + } + + if (!labelObj.isLiteral() || !labelObj.asLiteral().getDatatypeURI().equals(XSD.xstring.getURI())) { + throw new IllegalArgumentException("Label is invalid, it should be a xsd:string!"); + } + final StmtIterator iriIterator = edgeLabelMappingResource.listProperties(LPG2RDF.iri); + if(!iriIterator.hasNext()){ + throw new IllegalArgumentException("iri is required!"); + } + final RDFNode iriObj = iriIterator.next().getObject(); + if(iriIterator.hasNext()){ + throw new IllegalArgumentException("An instance of SingleEdgeLabelMapping has more than one iri property!"); + } + + if (!iriObj.isLiteral() || !iriObj.asLiteral().getDatatypeURI().equals(XSD.anyURI.getURI())){ + throw new IllegalArgumentException("iri is invalid, it should be a xsd:anyURI!"); + } + final String label = labelObj.asLiteral().getString(); + final String iri = iriObj.asLiteral().getString(); + try { + return new SingleEdgeLabelMappingToURIsImpl(label,iri); + } catch (IllegalArgumentException exception) { + throw new IllegalArgumentException("iri is an invalid URI!"); + } + } + else { throw new IllegalArgumentException("EdgeLabelMapping type (" + edgeLabelMappingResourceType + ") is unexpected!"); } diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java new file mode 100644 index 000000000..676fcacf6 --- /dev/null +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java @@ -0,0 +1,37 @@ +package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl; + +import org.apache.jena.graph.Node; +import org.apache.jena.graph.NodeFactory; + +public class SingleEdgeLabelMappingToURIsImpl implements EdgeLabelMapping { + + protected final String label; + protected final String iri; + + public SingleEdgeLabelMappingToURIsImpl(final String label, final String iri){ + this.label=label; + this.iri=iri; + } + + @Override + public Node map(final String label) { + if (label.equals(this.label)) { + return NodeFactory.createURI(iri); + } + else { + throw new IllegalArgumentException("The given edge label (" + label + ") is not a supported label in the image of this edge label mapping."); + } + } + + public String unmap(final Node node) { + if (!isPossibleResult(node)) + throw new IllegalArgumentException("The given RDF term (" + node.toString() + ") is not an URI node or not in the image of this edge label mapping."); + return this.label; + } + + @Override + public boolean isPossibleResult(final Node node) { + return node.isURI() && node.getURI().equals(this.iri); + } + +} diff --git a/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java b/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java index 77b66a1b8..1f88fe54b 100644 --- a/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java +++ b/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java @@ -29,12 +29,15 @@ protected static final Property property(final String local ) { public static final Resource PropertyNameMapping = resource("PropertyNameMapping"); public static final Resource IRIBasedPropertyNameMapping = resource("IRIBasedPropertyNameMapping"); public static final Resource RegexBasedEdgeLabelMapping = resource("RegexBasedEdgeLabelMapping"); + public static final Resource SingleEdgeLabelMapping = resource("SingleEdgeLabelMapping"); public static final Property labelPredicate = property("labelPredicate"); public static final Property nodeMapping = property("nodeMapping"); public static final Property nodeLabelMapping = property("nodeLabelMapping"); public static final Property edgeLabelMapping = property("edgeLabelMapping"); public static final Property regex = property("regex"); + public static final Property label = property("label"); + public static final Property iri = property("iri"); public static final Property propertyNameMapping = property("propertyNameMapping"); public static final Property prefixOfIRIs = property("prefixOfIRIs"); diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReaderTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReaderTest.java index 59724693c..5c5402240 100644 --- a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReaderTest.java +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/LPG2RDFConfigurationReaderTest.java @@ -143,6 +143,71 @@ public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndRe assertTrue(resultPropertyName.isURI()); assertEquals(resultPropertyName.getURI(), "https://example.org/property/0"); } + @Test + public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndSingleEdgeLabelMapping() { + final String turtle = + "PREFIX lr: \n" + + "PREFIX ex: \n" + + "PREFIX xsd: \n" + + "\n" + + "ex:LPGtoRDFConfig\n" + + " a lr:LPGtoRDFConfiguration ;\n" + + " lr:labelPredicate \"http://www.w3.org/2000/01/rdf-schema#label\"^^xsd:anyURI ;\n" + + " lr:nodeMapping ex:IRINodeMapping ;\n" + + " lr:nodeLabelMapping ex:IRINodeLabelMapping ;\n" + + " lr:edgeLabelMapping ex:SingleIRIEdgeLabelMapping ;\n" + + " lr:propertyNameMapping ex:IRIPropertyNameMapping ." + + "\n" + + "ex:IRINodeLabelMapping\n" + + " a lr:IRIBasedNodeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ." + + "\n" + + "ex:SingleIRIEdgeLabelMapping\n" + + " a lr:SingleEdgeLabelMapping ;\n" + + " lr:label \"DIRECTED\" ;\n" + + " lr:iri \"http://singleExample.org/directorOf\"^^xsd:anyURI ." + + "\n" + + "ex:IRIPropertyNameMapping\n" + + " a lr:IRIBasedPropertyNameMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/property/\"^^xsd:anyURI ." + + "\n" + + "ex:IRINodeMapping\n" + + " a lr:IRIBasedNodeMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/node/\"^^xsd:anyURI ."; + + final Model lpg2rdf = ModelFactory.createDefaultModel(); + + final RDFParserBuilder b = RDFParser.fromString(turtle); + b.lang( Lang.TURTLE ); + b.parse(lpg2rdf); + + final LPG2RDFConfiguration lpg2RDFConfiguration = LPG2RDFConfigurationReader.readFromModel(lpg2rdf); + assert(lpg2RDFConfiguration.getLabel().isURI()); + assert(lpg2RDFConfiguration.getLabel().getURI().equals("http://www.w3.org/2000/01/rdf-schema#label")); + final LPGNode node = new LPGNode("0", null, null); + final Node resultNode = lpg2RDFConfiguration.mapNode(node); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(), "https://example.org/node/0"); + + final String label = "0"; + final Node resultNodeLabel = lpg2RDFConfiguration.mapNodeLabel(label); + assertNotNull(resultNodeLabel); + assertTrue(resultNodeLabel.isURI()); + assertEquals(resultNodeLabel.getURI(), "https://example.org/label/0"); + + final String edgeLabel = "DIRECTED"; + final Node resultEdgeLabel = lpg2RDFConfiguration.mapEdgeLabel(edgeLabel); + assertNotNull(resultEdgeLabel); + assertTrue(resultEdgeLabel.isURI()); + assertEquals(resultEdgeLabel.getURI(), "http://singleExample.org/directorOf"); + + final String propertyName = "0"; + final Node resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "https://example.org/property/0"); + } @Test public void LPG2RDFConfigWithIRIBasedNodeMappingAndLiteralBasedNodeLabelMapping() { diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImplTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImplTest.java new file mode 100644 index 000000000..3d059f643 --- /dev/null +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImplTest.java @@ -0,0 +1,58 @@ +package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl; + +import org.apache.jena.graph.Node; +import org.apache.jena.graph.NodeFactory; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SingleEdgeLabelMappingToURIsImplTest { + + protected final String label="DIRECTED"; + protected final String iri="http://singleExample.org/directorOf"; + protected final EdgeLabelMapping singleEdgeLabelMapping = new SingleEdgeLabelMappingToURIsImpl(label,iri); + + @Test + public void mapSingleEdgeLabel() { + final Node resultNode = singleEdgeLabelMapping.map("DIRECTED"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"http://singleExample.org/directorOf"); + } + + @Test + public void unmapSingleURIEdgeLabel(){ + final Node node = NodeFactory.createURI("http://singleExample.org/directorOf"); + final String resultString = singleEdgeLabelMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "DIRECTED"); + } + + @Test + public void edgeLabelIsPossibleResult(){ + final Node IRINode = NodeFactory.createURI("http://singleExample.org/directorOf"); + final boolean IRIIsPossible = singleEdgeLabelMapping.isPossibleResult(IRINode); + assertTrue(IRIIsPossible); + } + + + + /* + * In this test case, a node with an invalid URI is provided as an argument to the SingleEdgeLabelMappingToURIsImpl. + */ + @Test(expected = IllegalArgumentException.class) + public void unmapEdgeLabelWithInvalidURI(){ + final Node node = NodeFactory.createURI("https://example.org/3"); + singleEdgeLabelMapping.unmap(node); + } + + /* + * In this test case, a label which is not equal with the given label in SingleEdgeLabelMappingToURIsImpl. + */ + @Test(expected = IllegalArgumentException.class) + public void mapEdgeLabelWithUnmatchedLabel(){ + final String label = "test"; + singleEdgeLabelMapping.map(label); + } + +} From 397f4987385a433814495a4811686dd0ec304a17 Mon Sep 17 00:00:00 2001 From: Hoda Date: Tue, 7 Nov 2023 19:46:29 +0100 Subject: [PATCH 2/3] Improve mapping of SingleEdgeLabelMappingToURIsImpl --- .../lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java index 676fcacf6..35a6aa6bd 100644 --- a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java @@ -7,16 +7,18 @@ public class SingleEdgeLabelMappingToURIsImpl implements EdgeLabelMapping { protected final String label; protected final String iri; + protected final Node node; public SingleEdgeLabelMappingToURIsImpl(final String label, final String iri){ this.label=label; this.iri=iri; + this.node = NodeFactory.createURI(iri); } @Override public Node map(final String label) { if (label.equals(this.label)) { - return NodeFactory.createURI(iri); + return this.node; } else { throw new IllegalArgumentException("The given edge label (" + label + ") is not a supported label in the image of this edge label mapping."); From 97e723e9ef704409fa8697d5f343ac9960239bf6 Mon Sep 17 00:00:00 2001 From: Hoda Date: Tue, 7 Nov 2023 19:58:04 +0100 Subject: [PATCH 3/3] Remove redundant variable --- .../lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java index 35a6aa6bd..a08a6dbe0 100644 --- a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SingleEdgeLabelMappingToURIsImpl.java @@ -6,12 +6,10 @@ public class SingleEdgeLabelMappingToURIsImpl implements EdgeLabelMapping { protected final String label; - protected final String iri; protected final Node node; public SingleEdgeLabelMappingToURIsImpl(final String label, final String iri){ this.label=label; - this.iri=iri; this.node = NodeFactory.createURI(iri); } @@ -33,7 +31,7 @@ public String unmap(final Node node) { @Override public boolean isPossibleResult(final Node node) { - return node.isURI() && node.getURI().equals(this.iri); + return node.isURI() && node.getURI().equals(this.node.getURI()); } }