Skip to content

Commit

Permalink
Merge pull request #316 from LiUSemWeb/singleEdgeLabelMapping
Browse files Browse the repository at this point in the history
Add SingleEdgeLabelMapping and its tests
  • Loading branch information
hartig authored Nov 7, 2023
2 parents e3a0cdf + 97e723e commit 9750c4c
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 1 deletion.
18 changes: 17 additions & 1 deletion ExampleLPG2RDF.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ PREFIX ex: <http://example.org/>
#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 ;
Expand Down Expand Up @@ -64,6 +67,14 @@ PREFIX ex: <http://example.org/>
# 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 .
Expand All @@ -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 ;
Expand All @@ -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 .
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 Node node;

public SingleEdgeLabelMappingToURIsImpl(final String label, final String iri){
this.label=label;
this.node = NodeFactory.createURI(iri);
}

@Override
public Node map(final String label) {
if (label.equals(this.label)) {
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.");
}
}

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.node.getURI());
}

}
3 changes: 3 additions & 0 deletions src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: <http://www.example.org/se/liu/ida/hefquin/lpg2rdf#>\n"
+ "PREFIX ex: <http://example.org/>\n"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 9750c4c

Please sign in to comment.