Skip to content

Commit

Permalink
Merge pull request #315 from LiUSemWeb/flexibleEdgeLabelMapping
Browse files Browse the repository at this point in the history
Add RegexBasedEdgeLabelMapping and its tests
  • Loading branch information
hartig authored Nov 3, 2023
2 parents 042a845 + d03c064 commit e3a0cdf
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ExampleLPG2RDF.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ PREFIX ex: <http://example.org/>
#ex:IRIBasedEdgeLabelMapping a rdfs:Class ;
# rdfs:subClassOf ex:EdgeLabelMapping .

#ex:RegexBasedEdgeLabelMapping a rdfs:Class ;
# rdfs:subClassOf ex:EdgeLabelMapping .

#ex:PropertyNameMapping a rdfs:Class ;
#
#ex:IRIBasedPropertyNameMapping a rdfs:Class ;
Expand Down Expand Up @@ -57,6 +60,10 @@ PREFIX ex: <http://example.org/>
# rdfs:domain ex:LPGtoRDFConfiguration ;
# rdfs:range ex:EdgeLabelMapping .

#ex:regex a rdf:Property ;
# rdfs:domain ex:RegexBasedEdgeLabelMapping ;
# rdfs:range xsd:string .

#ex:propertyNameMapping a rdf:Property ;
# rdfs:domain ex:LPGtoRDFConfiguration ;
# rdfs:range ex:PropertyNameMapping .
Expand All @@ -66,7 +73,8 @@ ex:LPGtoRDFConfig a lr:LPGtoRDFConfiguration ;
lr:labelPredicate "http://www.w3.org/2000/01/rdf-schema#label"^^xsd:anyURI ;
lr:nodeMapping ex:IRINodeMapping ;
lr:nodeLabelMapping ex:IRINodeLabelMapping ;
lr:edgeLabelMapping ex:IRIEdgeLabelMapping ;
# lr:edgeLabelMapping ex:IRIEdgeLabelMapping ;
lr:edgeLabelMapping ex:RegexEdgeLabelMapping ;
lr:propertyNameMapping ex:IRIPropertyNameMapping .

ex:IRINodeMapping a lr:IRIBasedNodeMapping ;
Expand All @@ -78,5 +86,9 @@ ex:IRINodeLabelMapping a lr:IRIBasedNodeLabelMapping ;
ex:IRIEdgeLabelMapping a lr:IRIBasedEdgeLabelMapping ;
lr:prefixOfIRIs "https://example.org/relationship/"^^xsd:anyURI .

ex:RegexEdgeLabelMapping a lr:RegexBasedEdgeLabelMapping ;
lr:regex "^\\w+" ;
lr:prefixOfIRIs "http://purl.org/dc/terms/"^^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 @@ -206,6 +206,40 @@ public EdgeLabelMapping getEdgeLabelMapping(final Model lpg2Rdf, final Resource
throw new IllegalArgumentException("prefixOfIRIs is an invalid URI!");
}
}
else if ( edgeLabelMappingResourceType.equals(LPG2RDF.RegexBasedEdgeLabelMapping)
|| (edgeLabelMappingResourceType.equals(LPG2RDF.EdgeLabelMapping) && edgeLabelMappingResource.hasProperty(LPG2RDF.regex)) ) {
final StmtIterator regexIterator = edgeLabelMappingResource.listProperties(LPG2RDF.regex);
if (!regexIterator.hasNext()) {
throw new IllegalArgumentException("regex is required!");
}
final RDFNode regexObj = regexIterator.next().getObject();
if (regexIterator.hasNext()) {
throw new IllegalArgumentException("An instance of RegexEdgeLabelMapping has more than one regex property!");
}

if (!regexObj.isLiteral() || !regexObj.asLiteral().getDatatypeURI().equals(XSD.xstring.getURI())) {
throw new IllegalArgumentException("Regex is invalid, it should be a xsd:string!");
}
final StmtIterator prefixOfIRIsIterator = edgeLabelMappingResource.listProperties(LPG2RDF.prefixOfIRIs);
if(!prefixOfIRIsIterator.hasNext()){
throw new IllegalArgumentException("prefixOfIRIs is required!");
}
final RDFNode prefixOfIRIObj = prefixOfIRIsIterator.next().getObject();
if(prefixOfIRIsIterator.hasNext()){
throw new IllegalArgumentException("An instance of IRIBasedEdgeLabelMapping has more than one prefixOfIRIs property!");
}

if (!prefixOfIRIObj.isLiteral() || !prefixOfIRIObj.asLiteral().getDatatypeURI().equals(XSD.anyURI.getURI())){
throw new IllegalArgumentException("prefixOfIRIs is invalid, it should be a xsd:anyURI!");
}
final String regex = regexObj.asLiteral().getString();
final String prefixOfIRIUri = prefixOfIRIObj.asLiteral().getString();
try {
return new RegexBasedEdgeLabelMappingToURIsImpl(regex, URI.create(prefixOfIRIUri).toString());
} catch (IllegalArgumentException exception) {
throw new IllegalArgumentException("prefixOfIRIs 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,24 @@
package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;

public class RegexBasedEdgeLabelMappingToURIsImpl extends EdgeLabelMappingToURIsImpl {

protected final String regex;

public RegexBasedEdgeLabelMappingToURIsImpl(final String regex, final String NSRELATIONSHIP){
super(NSRELATIONSHIP);
this.regex = regex;
}

@Override
public Node map(final String label) {
if (label.matches(regex)) {
return super.map(label);
}
else {
throw new IllegalArgumentException("The given edge label (" + label + ") is not a supported label in the image of this edge label mapping.");
}
}
}
2 changes: 2 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 @@ -28,11 +28,13 @@ protected static final Property property(final String local ) {
public static final Resource IRIBasedEdgeLabelMapping = resource("IRIBasedEdgeLabelMapping");
public static final Resource PropertyNameMapping = resource("PropertyNameMapping");
public static final Resource IRIBasedPropertyNameMapping = resource("IRIBasedPropertyNameMapping");
public static final Resource RegexBasedEdgeLabelMapping = resource("RegexBasedEdgeLabelMapping");

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 propertyNameMapping = property("propertyNameMapping");
public static final Property prefixOfIRIs = property("prefixOfIRIs");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,71 @@ public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMapping() {
assertEquals(resultPropertyName.getURI(), "https://example.org/property/0");
}

@Test
public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndRegexBasedEdgeLabelMapping() {
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:RegexEdgeLabelMapping ;\n"
+ " lr:propertyNameMapping ex:IRIPropertyNameMapping ."
+ "\n"
+ "ex:IRINodeLabelMapping\n"
+ " a lr:IRIBasedNodeLabelMapping ;\n"
+ " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ."
+ "\n"
+ "ex:RegexEdgeLabelMapping\n"
+ " a lr:RegexBasedEdgeLabelMapping ;\n"
+ " lr:regex \"[0-9]+\" ;\n"
+ " lr:prefixOfIRIs \"https://test.org/test/\"^^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 Node resultEdgeLabel = lpg2RDFConfiguration.mapEdgeLabel(label);
assertNotNull(resultEdgeLabel);
assertTrue(resultEdgeLabel.isURI());
assertEquals(resultEdgeLabel.getURI(), "https://test.org/test/0");

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() {
final String turtle =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 RegexBasedEdgeLabelMappingToURIsImplTest {
protected final String NSRELATIONSHIP = "https://example2.org/test/";
protected final String regex = "^[0-9]+";

protected final EdgeLabelMapping edgeLabelMapping = new RegexBasedEdgeLabelMappingToURIsImpl(regex, NSRELATIONSHIP);

@Test
public void mapEdgeLabel() {
final String label = "0";
final Node resultNode = edgeLabelMapping.map(label);
assertNotNull(resultNode);
assertTrue(resultNode.isURI());
assertEquals(resultNode.getURI(), NSRELATIONSHIP + "0");
}

@Test
public void unmapURIEdgeLabel(){
final Node node = NodeFactory.createURI(NSRELATIONSHIP + "0");
final String resultString = edgeLabelMapping.unmap(node);
assertNotNull(resultString);
assertEquals(resultString, "0");
}

@Test
public void edgeLabelIsPossibleResult(){
final Node IRINode = NodeFactory.createURI(NSRELATIONSHIP + "0");
final boolean IRIIsPossible = edgeLabelMapping.isPossibleResult(IRINode);
assertTrue(IRIIsPossible);
}


/*
* In this test case, a node with an invalid URI is provided as an argument to the RegexBasedEdgeLabelMappingToURIsImpl.
*/
@Test(expected = IllegalArgumentException.class)
public void unmapEdgeLabelWithInvalidURI(){
final Node node = NodeFactory.createURI("https://example.org/3");
edgeLabelMapping.unmap(node);
}

/*
* In this test case, a label which is not match with provided regex in the RegexBasedEdgeLabelMappingToURIsImpl.
*/
@Test(expected = IllegalArgumentException.class)
public void mapEdgeLabelWithUnmatchedLabel(){
final String label = "test";
edgeLabelMapping.map(label);
}
}

0 comments on commit e3a0cdf

Please sign in to comment.