Skip to content

Commit

Permalink
Make PropertyNameMapping more flexible.
Browse files Browse the repository at this point in the history
  • Loading branch information
BentolhodaH committed Nov 17, 2023
1 parent 05a1806 commit 0c1e89f
Show file tree
Hide file tree
Showing 13 changed files with 805 additions and 33 deletions.
33 changes: 32 additions & 1 deletion ExampleLPG2RDF.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ PREFIX ex: <http://example.org/>
#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
Expand Down Expand Up @@ -86,6 +95,14 @@ PREFIX ex: <http://example.org/>
# 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 ;
Expand All @@ -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 .
Expand All @@ -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) .
Original file line number Diff line number Diff line change
@@ -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<PropertyNameMapping> propertyNameMappings;

public CombinedPropertyNameMappingToURIsImpl(final List<PropertyNameMapping> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PropertyNameMapping> 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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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, "");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.");
}
}
}
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;
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());
}

}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit 0c1e89f

Please sign in to comment.