From 0c1e89f53da9f0afb32904a5ca23447bf32b3356 Mon Sep 17 00:00:00 2001 From: Hoda Date: Fri, 17 Nov 2023 11:04:03 +0100 Subject: [PATCH] Make PropertyNameMapping more flexible. --- ExampleLPG2RDF.ttl | 33 +- ...CombinedPropertyNameMappingToURIsImpl.java | 46 +++ .../impl/LPG2RDFConfigurationReader.java | 164 ++++++++-- .../impl/PropertyNameMappingToURIsImpl.java | 3 +- ...gexBasedPropertyNameMappingToURIsImpl.java | 23 ++ .../SinglePropertyNameMappingToURIsImpl.java | 37 +++ .../UnSupportedPropertyNameException.java | 8 + .../liu/ida/hefquin/vocabulary/LPG2RDF.java | 15 +- ...inedPropertyNameMappingToURIsImplTest.java | 107 +++++++ .../impl/LPG2RDFConfigurationReaderTest.java | 282 ++++++++++++++++++ .../PropertyNameMappingToURIsImplTest.java | 3 +- ...asedPropertyNameMappingToURIsImplTest.java | 58 ++++ ...nglePropertyNameMappingToURIsImplTest.java | 59 ++++ 13 files changed, 805 insertions(+), 33 deletions(-) create mode 100644 src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImpl.java create mode 100644 src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImpl.java create mode 100644 src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImpl.java create mode 100644 src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/exceptions/UnSupportedPropertyNameException.java create mode 100644 src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImplTest.java create mode 100644 src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImplTest.java create mode 100644 src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImplTest.java diff --git a/ExampleLPG2RDF.ttl b/ExampleLPG2RDF.ttl index 4fda2a9eb..5bd921414 100644 --- a/ExampleLPG2RDF.ttl +++ b/ExampleLPG2RDF.ttl @@ -44,6 +44,15 @@ PREFIX ex: #ex:IRIBasedPropertyNameMapping a rdfs:Class ; # rdfs:subClassOf ex:PropertyMapping . +#ex:RegexBasedPropertyNameMapping a rdfs:Class ; +# rdfs:subClassOf ex:PropertyNameMapping . + +#ex:SinglePropertyNameMapping a rdfs:Class ; +# rdfs:subClassOf ex:PropertyNameMapping . + +#ex:CombinedPropertyNameMapping a rdfs:Class ; +# rdfs:subClassOf ex:PropertyNameMapping . + ## Properties @@ -86,6 +95,14 @@ PREFIX ex: # rdfs:domain ex:LPGtoRDFConfiguration ; # rdfs:range ex:PropertyNameMapping . +#ex:propertyName a rdf:Property ; +# rdfs:domain ex:SinglePropertyNamelMapping ; +# rdfs:range xsd:string . + +#ex:propertyNameMappings a rdf:Property ; +# rdfs:domain ex:CombinedpropertyNameMapping ; +# rdfs:range rdfs:List . + # Instances ex:LPGtoRDFConfig a lr:LPGtoRDFConfiguration ; lr:labelPredicate "http://www.w3.org/2000/01/rdf-schema#label"^^xsd:anyURI ; @@ -95,7 +112,10 @@ ex:LPGtoRDFConfig a lr:LPGtoRDFConfiguration ; # lr:edgeLabelMapping ex:RegexEdgeLabelMapping ; # lr:edgeLabelMapping ex:SingleIRIEdgeLabelMapping ; lr:edgeLabelMapping ex:CombinedIRIEdgeLabelMapping ; - lr:propertyNameMapping ex:IRIPropertyNameMapping . +# lr:propertyNameMapping ex:IRIPropertyNameMapping ; +# lr:propertyNameMapping ex:RegexPropertyNameMapping; +# lr:propertyNameMapping ex:SingleIRIPropertyNameMapping; + lr:propertyNameMapping ex:CombinedIRIPropertyNameMapping . ex:IRINodeMapping a lr:IRIBasedNodeMapping ; lr:prefixOfIRIs "https://example.org/node/"^^xsd:anyURI . @@ -119,3 +139,14 @@ ex:CombinedIRIEdgeLabelMapping a lr:CombinedEdgeLabelMapping ; ex:IRIPropertyNameMapping a lr:IRIBasedPropertyNameMapping ; lr:prefixOfIRIs "https://example.org/property/"^^xsd:anyURI . + +ex:RegexPropertyNameMapping a lr:RegexBasedPropertyNameMapping ; + lr:regex "^\\w+" ; + lr:prefixOfIRIs "http://purl.org/dc/terms/"^^xsd:anyURI . + +ex:SingleIRIPropertyNameMapping a lr:SinglePropertyNameMapping ; + lr:propertyName "DIRECTED" ; + lr:iri "http://dsgfrjlk.org/kdfj/directorOf"^^xsd:anyURI . + +ex:CombinedIRIPropertyNameMapping a lr:CombinedPropertyNameMapping ; + lr:propertyNameMappings (ex:SingleIRIPropertyNameMapping ex:RegexPropertyNameMapping ex:IRIPropertyNameMapping) . diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImpl.java new file mode 100644 index 000000000..d21702011 --- /dev/null +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImpl.java @@ -0,0 +1,46 @@ +package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl; + +import org.apache.jena.graph.Node; +import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +import java.util.List; + +public class CombinedPropertyNameMappingToURIsImpl implements PropertyNameMapping { + + protected final List propertyNameMappings; + + public CombinedPropertyNameMappingToURIsImpl(final List propertyNameMappings){ + this.propertyNameMappings = propertyNameMappings; + } + + @Override + public Node map(final String propertyName) { + for (final PropertyNameMapping propertyNameMapping : propertyNameMappings) { + try { + return propertyNameMapping.map(propertyName); + } + catch (UnSupportedPropertyNameException exception) {} + } + throw new UnSupportedPropertyNameException("The given property name (" + propertyName + ") is not a supported property name in the image of this property name mapping."); + } + + public String unmap(final Node node) { + for (final PropertyNameMapping propertyNameMapping : propertyNameMappings) { + try { + return propertyNameMapping.unmap(node); + } + catch (UnSupportedPropertyNameException exception) {} + } + throw new UnSupportedPropertyNameException("The given RDF term (" + node.toString() + ") is not an URI node or not in the image of this property name mapping."); + } + + @Override + public boolean isPossibleResult(final Node node) { + for (final PropertyNameMapping propertyNameMapping : propertyNameMappings) { + if(propertyNameMapping.isPossibleResult(node)){ + return true; + } + } + return false; + } +} 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 25440780a..451542481 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 @@ -330,7 +330,145 @@ public EdgeLabelMapping getEdgeLabelMapping(final Model lpg2Rdf, final Resource return createEdgeLabelMapping(edgeLabelMappingResource, edgeLabelMappingResourceType); } + public PropertyNameMapping createIRIBasedPropertyNameMapping(final Resource propertyNameMappingResource){ + final StmtIterator prefixOfIRIsIterator = propertyNameMappingResource.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 IRIBasedPropertyNameMapping 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 prefixOfIRIUri = prefixOfIRIObj.asLiteral().getString(); + try{ + return new PropertyNameMappingToURIsImpl(URI.create(prefixOfIRIUri).toString()); + } + catch (IllegalArgumentException exception){ + throw new IllegalArgumentException("prefixOfIRIs is an invalid URI!"); + } + } + + public PropertyNameMapping createRegexBasedPropertyNameMapping(final Resource propertyNameMappingResource){ + final StmtIterator regexIterator = propertyNameMappingResource.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 RegexPropertyNameMapping 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 = propertyNameMappingResource.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 RegexBasedPropertyNameMapping 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 RegexBasedPropertyNameMappingToURIsImpl(regex, URI.create(prefixOfIRIUri).toString()); + } catch (IllegalArgumentException exception) { + throw new IllegalArgumentException("prefixOfIRIs is an invalid URI!"); + } + } + public PropertyNameMapping createSinglePropertyNameMapping(final Resource propertyNameMappingResource){ + final StmtIterator propertyNameIterator = propertyNameMappingResource.listProperties(LPG2RDF.propertyName); + if (!propertyNameIterator.hasNext()) { + throw new IllegalArgumentException("propertyName is required!"); + } + final RDFNode propertyNameObj = propertyNameIterator.next().getObject(); + if (propertyNameIterator.hasNext()) { + throw new IllegalArgumentException("An instance of SinglePropertyNameMapping has more than one propertyName property!"); + } + + if (!propertyNameObj.isLiteral() || !propertyNameObj.asLiteral().getDatatypeURI().equals(XSD.xstring.getURI())) { + throw new IllegalArgumentException("Property Name is invalid, it should be a xsd:string!"); + } + final StmtIterator iriIterator = propertyNameMappingResource.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 SinglePropertyNameMapping 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 propertyName = propertyNameObj.asLiteral().getString(); + final String iri = iriObj.asLiteral().getString(); + try { + return new SinglePropertyNameMappingToURIsImpl(propertyName,iri); + } catch (IllegalArgumentException exception) { + throw new IllegalArgumentException("iri is an invalid URI!"); + } + } + public PropertyNameMapping createCombinedPropertyNameMapping(final Resource propertyNameMappingResource){ + final List propertyNameMappings = new ArrayList<>(); + final StmtIterator propertyNameMappingsPropertyIterator = propertyNameMappingResource.listProperties(LPG2RDF.propertyNameMappings); + if (!propertyNameMappingsPropertyIterator.hasNext()) { + throw new IllegalArgumentException("propertyNameMappings is required!"); + } + final RDFNode propertyNameMappingsList = propertyNameMappingsPropertyIterator.next().getObject(); + if(!propertyNameMappingsList.canAs(RDFList.class)){ + throw new IllegalArgumentException("PropertyNameMappings property of CombinedPropertyNameMapping should be a list!"); + } + final Iterator propertyNameMappingsIterator = propertyNameMappingsList.as(RDFList.class).iterator(); + if (!propertyNameMappingsIterator.hasNext()) { + throw new IllegalArgumentException("PropertyNameMappings list of CombinedPropertyNameMapping should not be empty!"); + } + + do{ + final Resource propertyNameMapping = (Resource)propertyNameMappingsIterator.next(); + final RDFNode propertyNameMappingType = propertyNameMapping.getProperty(RDF.type).getObject(); + if(propertyNameMappingType.equals(LPG2RDF.CombinedPropertyNameMapping) + || (propertyNameMappingType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMapping.hasProperty(LPG2RDF.propertyNameMappings))){ + throw new IllegalArgumentException("CombinedPropertyNameMapping Should not have an object of CombinedPropertyNameMapping as propertyNameMapping!"); + } + propertyNameMappings.add(createPropertyNameMapping(propertyNameMapping, propertyNameMappingType)); + }while(propertyNameMappingsIterator.hasNext()); + return new CombinedPropertyNameMappingToURIsImpl(propertyNameMappings); + } + + public PropertyNameMapping createPropertyNameMapping(final Resource propertyNameMappingResource, final RDFNode propertyNameMappingResourceType){ + if ( propertyNameMappingResourceType.equals(LPG2RDF.IRIBasedPropertyNameMapping) + || (propertyNameMappingResourceType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMappingResource.hasProperty(LPG2RDF.prefixOfIRIs)) ) { + return createIRIBasedPropertyNameMapping(propertyNameMappingResource); + } + else if ( propertyNameMappingResourceType.equals(LPG2RDF.RegexBasedPropertyNameMapping) + || (propertyNameMappingResourceType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMappingResource.hasProperty(LPG2RDF.regex)) ) { + return createRegexBasedPropertyNameMapping(propertyNameMappingResource); + } + + else if ( propertyNameMappingResourceType.equals(LPG2RDF.SinglePropertyNameMapping) + || (propertyNameMappingResourceType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMappingResource.hasProperty(LPG2RDF.propertyName) + && propertyNameMappingResource.hasProperty(LPG2RDF.iri))) { + return createSinglePropertyNameMapping(propertyNameMappingResource); + } + else if ( propertyNameMappingResourceType.equals(LPG2RDF.CombinedPropertyNameMapping) + || (propertyNameMappingResourceType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMappingResource.hasProperty(LPG2RDF.propertyNameMappings))) { + return createCombinedPropertyNameMapping(propertyNameMappingResource); + } + else { + throw new IllegalArgumentException("PropertyNameMapping type (" + propertyNameMappingResourceType + ") is unexpected!"); + } + } public PropertyNameMapping getPropertyNameMapping(final Model lpg2Rdf, final Resource lpg2rdfConfig){ final StmtIterator propertyNameMappingIterator = lpg2rdfConfig.listProperties(LPG2RDF.propertyNameMapping); @@ -345,30 +483,6 @@ public PropertyNameMapping getPropertyNameMapping(final Model lpg2Rdf, final Res final RDFNode propertyNameMappingResourceType = lpg2Rdf.getRequiredProperty(propertyNameMappingResource, RDF.type).getObject(); - if ( propertyNameMappingResourceType.equals(LPG2RDF.IRIBasedPropertyNameMapping) - || (propertyNameMappingResourceType.equals(LPG2RDF.PropertyNameMapping) && propertyNameMappingResource.hasProperty(LPG2RDF.prefixOfIRIs)) ) { - final StmtIterator prefixOfIRIsIterator = propertyNameMappingResource.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 IRIBasedPropertyNameMapping 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 prefixOfIRIUri = prefixOfIRIObj.asLiteral().getString(); - try{ - return new PropertyNameMappingToURIsImpl(URI.create(prefixOfIRIUri).toString()); - } - catch (IllegalArgumentException exception){ - throw new IllegalArgumentException("prefixOfIRIs is an invalid URI!"); - } - } - else { - throw new IllegalArgumentException("PropertyNameMapping type (" + propertyNameMappingResourceType + ") is unexpected!"); - } + return createPropertyNameMapping(propertyNameMappingResource, propertyNameMappingResourceType); } } diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImpl.java index 5300df8c2..49dfc2b05 100644 --- a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImpl.java +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImpl.java @@ -2,6 +2,7 @@ import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; +import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; public class PropertyNameMappingToURIsImpl implements PropertyNameMapping { @@ -20,7 +21,7 @@ public Node map(final String propertyName) { @Override 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 property name mapping."); + throw new UnSupportedPropertyNameException("The given RDF term (" + node.toString() + ") is not an URI node or not in the image of this property name mapping."); return node.getURI().replaceAll(NSPROPERTY, ""); } diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImpl.java new file mode 100644 index 000000000..bbd33aa1a --- /dev/null +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImpl.java @@ -0,0 +1,23 @@ +package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl; + +import org.apache.jena.graph.Node; +import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +public class RegexBasedPropertyNameMappingToURIsImpl extends PropertyNameMappingToURIsImpl { + + protected final String regex; + + public RegexBasedPropertyNameMappingToURIsImpl(final String regex, final String NSPROPERTY){ + super(NSPROPERTY); + this.regex = regex; + } + @Override + public Node map(final String propertyName) { + if (propertyName.matches(regex)) { + return super.map(propertyName); + } + else { + throw new UnSupportedPropertyNameException("The given property name (" + propertyName + ") is not a supported property name in the image of this property name mapping."); + } + } +} diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImpl.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImpl.java new file mode 100644 index 000000000..70f38060c --- /dev/null +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImpl.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; +import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +public class SinglePropertyNameMappingToURIsImpl implements PropertyNameMapping { + + protected final String propertyName; + protected final Node node; + + public SinglePropertyNameMappingToURIsImpl(final String propertyName, final String iri){ + this.propertyName=propertyName; + this.node = NodeFactory.createURI(iri); + } + @Override + public Node map(final String propertyName) { + if (propertyName.equals(this.propertyName)) { + return this.node; + } + else { + throw new UnSupportedPropertyNameException("The given property name (" + propertyName + ") is not a supported property name in the image of this property name mapping."); + } + } + + public String unmap(final Node node) { + if (!isPossibleResult(node)) + throw new UnSupportedPropertyNameException("The given RDF term (" + node.toString() + ") is not an URI node or not in the image of this property name mapping."); + return this.propertyName; + } + + @Override + public boolean isPossibleResult(final Node node) { + return node.isURI() && node.getURI().equals(this.node.getURI()); + } + +} diff --git a/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/exceptions/UnSupportedPropertyNameException.java b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/exceptions/UnSupportedPropertyNameException.java new file mode 100644 index 000000000..9d53e0d61 --- /dev/null +++ b/src/main/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/exceptions/UnSupportedPropertyNameException.java @@ -0,0 +1,8 @@ +package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions; + +public class UnSupportedPropertyNameException extends IllegalArgumentException{ + + public UnSupportedPropertyNameException(final String message){ + super(message); + } +} 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 90716b216..24d2e3ce9 100644 --- a/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java +++ b/src/main/java/se/liu/ida/hefquin/vocabulary/LPG2RDF.java @@ -26,21 +26,26 @@ protected static final Property property(final String local ) { public static final Resource LiteralBasedNodeLabelMapping = resource("LiteralBasedNodeLabelMapping"); public static final Resource EdgeLabelMapping = resource("EdgeLabelMapping"); 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 Resource SingleEdgeLabelMapping = resource("SingleEdgeLabelMapping"); public static final Resource CombinedEdgeLabelMapping = resource("CombinedEdgeLabelMapping"); + public static final Resource PropertyNameMapping = resource("PropertyNameMapping"); + public static final Resource IRIBasedPropertyNameMapping = resource("IRIBasedPropertyNameMapping"); + public static final Resource RegexBasedPropertyNameMapping = resource("RegexBasedPropertyNameMapping"); + public static final Resource SinglePropertyNameMapping = resource("SinglePropertyNameMapping"); + public static final Resource CombinedPropertyNameMapping = resource("CombinedPropertyNameMapping"); 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 edgeLabelMappings = property("edgeLabelMappings"); public static final Property propertyNameMapping = property("propertyNameMapping"); + public static final Property propertyName = property("propertyName"); + public static final Property propertyNameMappings = property("propertyNameMappings"); public static final Property prefixOfIRIs = property("prefixOfIRIs"); + public static final Property regex = property("regex"); + public static final Property label = property("label"); + public static final Property iri = property("iri"); } diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImplTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImplTest.java new file mode 100644 index 000000000..abebe6bd1 --- /dev/null +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/CombinedPropertyNameMappingToURIsImplTest.java @@ -0,0 +1,107 @@ +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 se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.*; + +public class CombinedPropertyNameMappingToURIsImplTest { + + protected final List propertyNameMappings = new ArrayList(Arrays.asList( + new SinglePropertyNameMappingToURIsImpl("0","http://singleExample.org/zero"), + new SinglePropertyNameMappingToURIsImpl("3","http://singleExample.org/three"), + new RegexBasedPropertyNameMappingToURIsImpl("^[0-9]+", "https://example2.org/test/"), + new PropertyNameMappingToURIsImpl("https://example.org/property/") + )); + + protected final PropertyNameMapping propertyNameMapping = new CombinedPropertyNameMappingToURIsImpl(propertyNameMappings); + + @Test + public void mapSinglePropertyName() { + Node resultNode = propertyNameMapping.map("0"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"http://singleExample.org/zero"); + + resultNode = propertyNameMapping.map("3"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"http://singleExample.org/three"); + + resultNode = propertyNameMapping.map("100"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"https://example2.org/test/100"); + + resultNode = propertyNameMapping.map("DIRECTED"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"https://example.org/property/DIRECTED"); + } + + @Test + public void unmapSingleURIPropertyName(){ + Node node = NodeFactory.createURI("http://singleExample.org/zero"); + String resultString = propertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "0"); + + node = NodeFactory.createURI("http://singleExample.org/three"); + resultString = propertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "3"); + + node = NodeFactory.createURI("https://example2.org/test/100"); + resultString = propertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "100"); + + node = NodeFactory.createURI("https://example.org/property/DIRECTED"); + resultString = propertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "DIRECTED"); + } + + @Test + public void propertyNameIsPossibleResult(){ + + Node node = NodeFactory.createURI("http://singleExample.org/zero"); + boolean IRIIsPossible = propertyNameMapping.isPossibleResult(node); + assertTrue(IRIIsPossible); + + node = NodeFactory.createURI("http://singleExample.org/three"); + IRIIsPossible = propertyNameMapping.isPossibleResult(node); + assertTrue(IRIIsPossible); + + node = NodeFactory.createURI("https://example2.org/test/100"); + IRIIsPossible = propertyNameMapping.isPossibleResult(node); + assertTrue(IRIIsPossible); + + node = NodeFactory.createURI("https://example.org/property/DIRECTED"); + IRIIsPossible = propertyNameMapping.isPossibleResult(node); + assertTrue(IRIIsPossible); + + node = NodeFactory.createURI("https://invalid.url.org/property/DIRECTED"); + IRIIsPossible = propertyNameMapping.isPossibleResult(node); + assertFalse(IRIIsPossible); + } + + + + /* + * In this test case, a node with an invalid URI is provided as an argument to the CombinedPropertyNameMappingToURIsImpl. + */ + @Test(expected = UnSupportedPropertyNameException.class) + public void unmapPropertyNameWithInvalidURI(){ + final Node node = NodeFactory.createURI("https://invalid.url.org/property/DIRECTED"); + propertyNameMapping.unmap(node); + } + + +} 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 2cabc7337..8cd5ef32a 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 @@ -550,6 +550,226 @@ public void LPG2RDFConfigWithNodeMappingAsIRIBasedNodeMappingAndNodeLabelMapping assertTrue(resultPropertyName.isURI()); assertEquals(resultPropertyName.getURI(), "https://example.org/property/0"); } + @Test + public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndRegexBasedPropertyNameMapping() { + 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:IRIEdgeLabelMapping ;\n" + + " lr:propertyNameMapping ex:RegexPropertyNameMapping ." + + "\n" + + "ex:IRINodeLabelMapping\n" + + " a lr:IRIBasedNodeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIEdgeLabelMapping\n" + + " a lr:IRIBasedEdgeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/relationship/\"^^xsd:anyURI ." + + "\n" + + "ex:RegexPropertyNameMapping\n" + + " a lr:RegexBasedPropertyNameMapping ;\n" + + " lr:regex \"[0-9]+\" ;\n" + + " lr:prefixOfIRIs \"https://test.org/test/\"^^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://example.org/relationship/0"); + + final String propertyName = "0"; + final Node resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "https://test.org/test/0"); + } + @Test + public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndSinglePropertyNameMapping() { + 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:IRIEdgeLabelMapping ;\n" + + " lr:propertyNameMapping ex:SingleIRIPropertyNameMapping ." + + "\n" + + "ex:IRINodeLabelMapping\n" + + " a lr:IRIBasedNodeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIEdgeLabelMapping\n" + + " a lr:IRIBasedEdgeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/relationship/\"^^xsd:anyURI ." + + "\n" + + "ex:SingleIRIPropertyNameMapping\n" + + " a lr:SinglePropertyNameMapping ;\n" + + " lr:propertyName \"DIRECTED\" ;\n" + + " lr:iri \"http://singleExample.org/directorOf\"^^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://example.org/relationship/0"); + + final String propertyName = "DIRECTED"; + final Node resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "http://singleExample.org/directorOf"); + } + + @Test + public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndCombinedPropertyNameMapping() { + 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:IRIEdgeLabelMapping ;\n" + + " lr:propertyNameMapping ex:CombinedIRIPropertyNameMapping ." + + "\n" + + "ex:IRINodeLabelMapping\n" + + " a lr:IRIBasedNodeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIEdgeLabelMapping\n" + + " a lr:IRIBasedEdgeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/relationship/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIPropertyNameMapping\n" + + " a lr:IRIBasedPropertyNameMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/property/\"^^xsd:anyURI ." + + "\n" + + "ex:RegexPropertyNameMapping\n" + + " a lr:RegexBasedPropertyNameMapping ;\n" + + " lr:regex \"^[0-9]+\" ;\n" + + " lr:prefixOfIRIs \"https://test.org/test/\"^^xsd:anyURI ." + + "\n" + + "ex:SingleIRIPropertyNameMapping\n" + + " a lr:SinglePropertyNameMapping ;\n" + + " lr:propertyName \"DIRECTED\" ;\n" + + " lr:iri \"http://singleExample.org/directorOf\"^^xsd:anyURI ." + + "\n" + + "ex:CombinedIRIPropertyNameMapping\n" + + " a lr:CombinedPropertyNameMapping ;\n" + + " lr:propertyNameMappings (ex:SingleIRIPropertyNameMapping ex:RegexPropertyNameMapping ex:IRIPropertyNameMapping) ." + + "\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://example.org/relationship/0"); + + String propertyName = "DIRECTED"; + Node resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "http://singleExample.org/directorOf"); + + propertyName = "0"; + resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "https://test.org/test/0"); + + propertyName = "ACTED_IN"; + resultPropertyName = lpg2RDFConfiguration.mapProperty(propertyName); + assertNotNull(resultPropertyName); + assertTrue(resultPropertyName.isURI()); + assertEquals(resultPropertyName.getURI(), "https://example.org/property/ACTED_IN"); + } + /* * In this test case, LabelPredicate is not a URI. @@ -1102,4 +1322,66 @@ public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndNe LPG2RDFConfigurationReader.readFromModel(lpg2rdf); } + + /* + * In this test case, there is an instance of CombinedPropertyNameMapping inside the propertyNameMappings list of another CombinedPropertyNameMapping. + */ + @Test(expected = IllegalArgumentException.class) + public void LPG2RDFConfigWithIRIBasedNodeMappingAndIRIBasedNodeLabelMappingAndNestedCombinedPropertyNameMapping() { + 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:IRIEdgeLabelMapping ;\n" + + " lr:propertyNameMapping ex:CombinedIRIPropertyNameMapping ." + + "\n" + + "ex:IRINodeLabelMapping\n" + + " a lr:IRIBasedNodeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/label/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIEdgeLabelMapping\n" + + " a lr:IRIBasedEdgeLabelMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/relationship/\"^^xsd:anyURI ." + + "\n" + + "ex:IRIPropertyNameMapping\n" + + " a lr:IRIBasedPropertyNameMapping ;\n" + + " lr:prefixOfIRIs \"https://example.org/property/\"^^xsd:anyURI ." + + "\n" + + "ex:RegexPropertyNameMapping\n" + + " a lr:RegexBasedPropertyNameMapping ;\n" + + " lr:regex \"^[0-9]+\" ;\n" + + " lr:prefixOfIRIs \"https://test.org/test/\"^^xsd:anyURI ." + + "\n" + + "ex:SingleIRIPropertyNameMapping\n" + + " a lr:SinglePropertyNameMapping ;\n" + + " lr:propertyName \"DIRECTED\" ;\n" + + " lr:iri \"http://singleExample.org/directorOf\"^^xsd:anyURI ." + + "\n" + + "ex:CombinedIRIPropertyNameMapping\n" + + " a lr:CombinedPropertyNameMapping ;\n" + + " lr:propertyNameMappings (ex:InnerCombinedIRIPropertyNameMapping) ." + + "\n" + + "ex:InnerCombinedIRIPropertyNameMapping\n" + + " a lr:PropertyNameMapping ;\n" + + " lr:propertyNameMappings (ex:SingleIRIPropertyNameMapping ex:RegexPropertyNameMapping ex:IRIPropertyNameMapping) ." + + + "\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); + + LPG2RDFConfigurationReader.readFromModel(lpg2rdf); + } } diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImplTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImplTest.java index b9955cf57..0080dc2ad 100644 --- a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImplTest.java +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/PropertyNameMappingToURIsImplTest.java @@ -3,6 +3,7 @@ import org.apache.jena.graph.Node; import org.apache.jena.graph.NodeFactory; import org.junit.Test; +import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; import static org.junit.Assert.*; @@ -39,7 +40,7 @@ public void propertyIsPossibleResult(){ /* * In this test case, a node with an invalid URI is provided as an argument to the PropertyNameMappingToURIsImpl. */ - @Test(expected = IllegalArgumentException.class) + @Test(expected = UnSupportedPropertyNameException.class) public void unmapPropertyNameWithInvalidURI(){ final Node node = NodeFactory.createURI("https://example.com/property/3"); propertyNameMapping.unmap(node); diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImplTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImplTest.java new file mode 100644 index 000000000..2812d939d --- /dev/null +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/RegexBasedPropertyNameMappingToURIsImplTest.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 se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +import static org.junit.Assert.*; + +public class RegexBasedPropertyNameMappingToURIsImplTest { + protected final String NSPROPERTY = "https://example2.org/test/"; + protected final String regex = "^[0-9]+"; + + protected final PropertyNameMapping propertyNameMapping = new RegexBasedPropertyNameMappingToURIsImpl(regex, NSPROPERTY); + + @Test + public void mapPropertyName() { + final String propertyName = "0"; + final Node resultNode = propertyNameMapping.map(propertyName); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(), NSPROPERTY + "0"); + } + + @Test + public void unmapURIPropertyName(){ + final Node node = NodeFactory.createURI(NSPROPERTY + "0"); + final String resultString = propertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "0"); + } + + @Test + public void propertyNameIsPossibleResult(){ + final Node IRINode = NodeFactory.createURI(NSPROPERTY + "0"); + final boolean IRIIsPossible = propertyNameMapping.isPossibleResult(IRINode); + assertTrue(IRIIsPossible); + } + + + /* + * In this test case, a node with an invalid URI is provided as an argument to the RegexBasedPropertyNameMappingToURIsImpl. + */ + @Test(expected = UnSupportedPropertyNameException.class) + public void unmapPropertyNameWithInvalidURI(){ + final Node node = NodeFactory.createURI("https://example.org/3"); + propertyNameMapping.unmap(node); + } + + /* + * In this test case, a propertyName which is not match with provided regex in the RegexBasedPropertyNameMappingToURIsImpl. + */ + @Test(expected = UnSupportedPropertyNameException.class) + public void mapPropertyNameWithUnmatchedPropertyName(){ + final String propertyName = "test"; + propertyNameMapping.map(propertyName); + } +} diff --git a/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImplTest.java b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImplTest.java new file mode 100644 index 000000000..bcbec8014 --- /dev/null +++ b/src/test/java/se/liu/ida/hefquin/engine/wrappers/lpgwrapper/impl/SinglePropertyNameMappingToURIsImplTest.java @@ -0,0 +1,59 @@ +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 se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.exceptions.UnSupportedPropertyNameException; + +import static org.junit.Assert.*; + +public class SinglePropertyNameMappingToURIsImplTest { + + protected final String propertyName="DIRECTED"; + protected final String iri="http://singleExample.org/directorOf"; + protected final PropertyNameMapping singlePropertyNameMapping = new SinglePropertyNameMappingToURIsImpl(propertyName,iri); + + @Test + public void mapSinglePropertyName() { + final Node resultNode = singlePropertyNameMapping.map("DIRECTED"); + assertNotNull(resultNode); + assertTrue(resultNode.isURI()); + assertEquals(resultNode.getURI(),"http://singleExample.org/directorOf"); + } + + @Test + public void unmapSingleURIPropertyName(){ + final Node node = NodeFactory.createURI("http://singleExample.org/directorOf"); + final String resultString = singlePropertyNameMapping.unmap(node); + assertNotNull(resultString); + assertEquals(resultString, "DIRECTED"); + } + + @Test + public void propertyNameIsPossibleResult(){ + final Node IRINode = NodeFactory.createURI("http://singleExample.org/directorOf"); + final boolean IRIIsPossible = singlePropertyNameMapping.isPossibleResult(IRINode); + assertTrue(IRIIsPossible); + } + + + + /* + * In this test case, a node with an invalid URI is provided as an argument to the SinglePropertyNameMappingToURIsImpl. + */ + @Test(expected = UnSupportedPropertyNameException.class) + public void unmapPropertyNameWithInvalidURI(){ + final Node node = NodeFactory.createURI("https://example.org/3"); + singlePropertyNameMapping.unmap(node); + } + + /* + * In this test case, a property name which is not equal with the given property name in SinglePropertyNameMappingToURIsImpl. + */ + @Test(expected = UnSupportedPropertyNameException.class) + public void mapPropertyNameWithUnmatchedPropertyName(){ + final String propertyName = "test"; + singlePropertyNameMapping.map(propertyName); + } + +}