Skip to content

Commit

Permalink
Merge pull request #314 from LiUSemWeb/propertyMapping
Browse files Browse the repository at this point in the history
Add PropertyMapping and corresponding tests
  • Loading branch information
hartig authored Oct 25, 2023
2 parents 0b08b1c + 5fb3ade commit 042a845
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 29 deletions.
16 changes: 15 additions & 1 deletion ExampleLPG2RDF.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ PREFIX ex: <http://example.org/>
#ex:IRIBasedEdgeLabelMapping a rdfs:Class ;
# rdfs:subClassOf ex:EdgeLabelMapping .

#ex:PropertyNameMapping a rdfs:Class ;
#
#ex:IRIBasedPropertyNameMapping a rdfs:Class ;
# rdfs:subClassOf ex:PropertyMapping .



## Properties
#ex:labelPredicate a rdf:Property ;
Expand All @@ -51,12 +57,17 @@ PREFIX ex: <http://example.org/>
# rdfs:domain ex:LPGtoRDFConfiguration ;
# rdfs:range ex:EdgeLabelMapping .

#ex:propertyNameMapping a rdf:Property ;
# rdfs:domain ex:LPGtoRDFConfiguration ;
# rdfs:range ex:PropertyNameMapping .

# Instances
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:propertyNameMapping ex:IRIPropertyNameMapping .

ex:IRINodeMapping a lr:IRIBasedNodeMapping ;
lr:prefixOfIRIs "https://example.org/node/"^^xsd:anyURI .
Expand All @@ -66,3 +77,6 @@ ex:IRINodeLabelMapping a lr:IRIBasedNodeLabelMapping ;

ex:IRIEdgeLabelMapping a lr:IRIBasedEdgeLabelMapping ;
lr:prefixOfIRIs "https://example.org/relationship/"^^xsd:anyURI .

ex:IRIPropertyNameMapping a lr:IRIBasedPropertyNameMapping ;
lr:prefixOfIRIs "https://example.org/property/"^^xsd:anyURI .
6 changes: 5 additions & 1 deletion src/main/java/se/liu/ida/hefquin/cli/RunBGPOverNeo4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.NodeLabelMappingToURIsImpl;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.EdgeLabelMapping;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.EdgeLabelMappingToURIsImpl;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.PropertyNameMapping;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl.PropertyNameMappingToURIsImpl;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.query.CypherMatchQuery;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.query.CypherQuery;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.query.CypherUnionQuery;
Expand All @@ -68,6 +70,7 @@ public class RunBGPOverNeo4j extends CmdARQ
protected static final String NSNODE = "https://example.org/node/";
protected static final String NSNODELABEL = "https://example.org/label/";
protected static final String NSRELATIONSHIP = "https://example.org/relationship/";
protected static final String NSPROPERTY = "https://example.org/property/";

public static void main( final String[] args ) {
new RunBGPOverNeo4j(args).mainRun();
Expand Down Expand Up @@ -102,7 +105,8 @@ protected void exec() {
final NodeMapping nodeMapping = new NodeMappingToURIsImpl(NSNODE);
final NodeLabelMapping nodeLabelMapping = new NodeLabelMappingToURIsImpl(NSNODELABEL);
final EdgeLabelMapping edgeLabelMapping = new EdgeLabelMappingToURIsImpl(NSRELATIONSHIP);
final LPG2RDFConfiguration conf = new LPG2RDFConfigurationImpl(NodeFactory.createURI(LABEL), nodeMapping, nodeLabelMapping,edgeLabelMapping);
final PropertyNameMapping propertyNameMapping = new PropertyNameMappingToURIsImpl(NSPROPERTY);
final LPG2RDFConfiguration conf = new LPG2RDFConfigurationImpl(NodeFactory.createURI(LABEL), nodeMapping, nodeLabelMapping,edgeLabelMapping,propertyNameMapping);

final Pair<CypherQuery, Map<CypherVar,Var>> tRes = performQueryTranslation(bgp, conf);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
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.LPG2RDFConfiguration;
import se.liu.ida.hefquin.engine.wrappers.lpgwrapper.data.impl.LPGNode;

public class LPG2RDFConfigurationImpl implements LPG2RDFConfiguration {

protected final String NS = "https://example.org/";
protected final String PROPERTY = "property/";

protected final Node label;
protected final NodeMapping nodeMapping;

protected final NodeLabelMapping nodeLabelMapping;
protected final EdgeLabelMapping edgeLabelMapping;
protected final PropertyNameMapping propertyNameMapping;

public LPG2RDFConfigurationImpl(final Node label, final NodeMapping nodeMapping, final NodeLabelMapping nodeLabelMapping,
final EdgeLabelMapping edgeLabelMapping){
final EdgeLabelMapping edgeLabelMapping, final PropertyNameMapping propertyNameMapping){
this.label = label;
this.nodeMapping = nodeMapping;
this.nodeLabelMapping = nodeLabelMapping;
this.edgeLabelMapping = edgeLabelMapping;
this.propertyNameMapping=propertyNameMapping;
}

@Override
Expand Down Expand Up @@ -55,17 +52,13 @@ public String unmapEdgeLabel(final Node node) {
}

@Override
public Node mapProperty(final String property) {
return NodeFactory.createURI(NS + PROPERTY + property);
public Node mapProperty(final String propertyName) {
return propertyNameMapping.map(propertyName);
}

@Override
public String unmapProperty(final Node node) {
if (!node.isURI())
throw new IllegalArgumentException("Default configuration only accepts URI Property mappings");
if (!node.getURI().startsWith(NS + PROPERTY))
throw new IllegalArgumentException("The provided URI is not mapping a Property");
return node.getURI().replaceAll(NS + PROPERTY, "");
return propertyNameMapping.unmap(node);
}

@Override
Expand All @@ -75,7 +68,7 @@ public Node getLabel() {

@Override
public boolean mapsToProperty(final Node n) {
return n.isURI() && n.getURI().startsWith(NS + PROPERTY);
return propertyNameMapping.isPossibleResult(n);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public LPG2RDFConfiguration parseLPG2RDFConf(final Model lpg2Rdf ) {
final NodeMapping nodeMapping = getNodeMapping(lpg2Rdf, lpg2rdfConfig);
final NodeLabelMapping nodeLabelMapping = getNodeLabelMapping(lpg2Rdf, lpg2rdfConfig);
final EdgeLabelMapping edgeLabelMapping = getEdgeLabelMapping(lpg2Rdf, lpg2rdfConfig);
final PropertyNameMapping propertyNameMapping = getPropertyNameMapping(lpg2Rdf,lpg2rdfConfig);


return new LPG2RDFConfigurationImpl(label, nodeMapping, nodeLabelMapping, edgeLabelMapping);
return new LPG2RDFConfigurationImpl(label, nodeMapping, nodeLabelMapping, edgeLabelMapping,propertyNameMapping);
}

public Node getLabelPredicate(final Resource lpg2rdfConfig){
Expand Down Expand Up @@ -209,4 +210,45 @@ public EdgeLabelMapping getEdgeLabelMapping(final Model lpg2Rdf, final Resource
throw new IllegalArgumentException("EdgeLabelMapping type (" + edgeLabelMappingResourceType + ") is unexpected!");
}
}

public PropertyNameMapping getPropertyNameMapping(final Model lpg2Rdf, final Resource lpg2rdfConfig){

final StmtIterator propertyNameMappingIterator = lpg2rdfConfig.listProperties(LPG2RDF.propertyNameMapping);

if(!propertyNameMappingIterator.hasNext()){
throw new IllegalArgumentException("propertyNameMapping is required!");
}
final Resource propertyNameMappingResource = propertyNameMappingIterator.next().getObject().asResource();
if(propertyNameMappingIterator.hasNext()){
throw new IllegalArgumentException("More than one instance of propertyNameMapping!");
}

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!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl;

import org.apache.jena.graph.Node;


/**
* This interface captures the notion of a property name mapping that is part of the notion of an LPG-to-RDF configuration,
* where such a property name mapping is an injective function from names of properties in LPGs to IRIs nodes as
* can occur in RDF graphs.
* This interface contains the functions:
* -map: map String of properties to IRIs properties
* -unmap: map IRIs properties to String
* -isPossibleResult: Check if the given RDF term is in the image of this property mapping
*/
public interface PropertyNameMapping {
/**
* Returns the IRI (in the form of a Jena {@link Node} object) that is the result of
* applying this property name mapping to the given property name.
*/
Node map(String property);

/**
* Returns the String that corresponds to the given RDF term.
* It applies the inverse of this property mapping to the given RDF term (which is assumed to be
* an IRI) in order to obtain the corresponding String.
*/
String unmap(Node node);

/**
* Check if the given RDF term is in the image of this property mapping and,
* thus, may be one of the RDF terms returned by the {@link #map(String)}
* function for some String.
*/
boolean isPossibleResult(Node node);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package se.liu.ida.hefquin.engine.wrappers.lpgwrapper.impl;

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

public class PropertyNameMappingToURIsImpl implements PropertyNameMapping {

protected final String NSPROPERTY;


public PropertyNameMappingToURIsImpl(final String NSPROPERTY){
this.NSPROPERTY=NSPROPERTY ;
}

@Override
public Node map(final String propertyName) {
return NodeFactory.createURI(NSPROPERTY + 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.");
return node.getURI().replaceAll(NSPROPERTY, "");
}

@Override
public boolean isPossibleResult(final Node node) {
return node.isURI() && node.getURI().startsWith(NSPROPERTY);
}
}
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 @@ -26,11 +26,14 @@ 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 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 propertyNameMapping = property("propertyNameMapping");
public static final Property prefixOfIRIs = property("prefixOfIRIs");

}
Loading

0 comments on commit 042a845

Please sign in to comment.