Browse Source

Committing gatherAsNonEmpty

Forgot to commit gatherAsNonEmpty
Christophe Debruyne 8 years ago
parent
commit
97e3522b00

+ 2 - 0
src/r2rml/engine/RRF.java

@@ -26,5 +26,7 @@ public final class RRF {
 	// Properties for Containers and Collections
 	public static final Property gather = ResourceFactory.createProperty(NS + "gather");
 	public static final Property collectAs = ResourceFactory.createProperty(NS + "collectAs");
+	public static final Property gatherAsNonEmpty = ResourceFactory.createProperty(NS + "gatherAsNonEmpty");
+	public static final Property collectAsAsNonEmpty = ResourceFactory.createProperty(NS + "collectAsAsNonEmpty");
 	
 }

+ 5 - 0
src/r2rml/model/GatherAsNonEmpty.java

@@ -0,0 +1,5 @@
+package r2rml.model;
+
+public class GatherAsNonEmpty extends Gather {
+
+}

+ 3 - 1
src/r2rml/model/PredicateObjectMap.java

@@ -120,7 +120,9 @@ public class PredicateObjectMap extends R2RMLResource {
 					|| r.hasProperty(R2RML.constant) 
 					|| r.hasProperty(R2RML.template)
 					|| r.hasProperty(RRF.functionCall)
-					|| r.hasProperty(RRF.gather);
+					|| r.hasProperty(RRF.gather)
+					|| r.hasProperty(RRF.gatherAsNonEmpty);
+			
 			boolean isROM = r.hasProperty(R2RML.joinCondition);
 
 			// If it plays the roles of a OM, create OM

+ 23 - 6
src/r2rml/model/TermMap.java

@@ -84,6 +84,7 @@ public abstract class TermMap extends R2RMLResource {
 		List<Statement> columns = description.listProperties(R2RML.column).toList();
 		List<Statement> functions = description.listProperties(RRF.functionCall).toList();
 		List<Statement> gathers = description.listProperties(RRF.gather).toList();
+		gathers.addAll(description.listProperties(RRF.gatherAsNonEmpty).toList());
 
 		// Having exactly one of rr:constant, rr:column, rr:template
 		if(templates.size() + constants.size() + columns.size() + functions.size() + gathers.size() != 1) {
@@ -137,7 +138,9 @@ public abstract class TermMap extends R2RMLResource {
 				}
 			}
 		} else if(gathers.size() == 1) {
-			gather = distillGather(gathers.get(0).getObject());
+			RDFNode object = gathers.get(0).getObject();
+			boolean isGatherAsNonEmpty = gathers.get(0).getPredicate().getURI().equals(RRF.gatherAsNonEmpty.getURI());
+			gather = distillGather(object, isGatherAsNonEmpty);
 			if(gather == null)
 				return false;
 
@@ -178,21 +181,27 @@ public abstract class TermMap extends R2RMLResource {
 		return true;
 	}
 
-	private Gather distillGather(RDFNode node) {
+	/**
+	 * 
+	 * @param node the resource that contains information about the gather map
+	 * @param isGatherAsNonEmpty true if gather-as-non-empty
+	 * @return
+	 */
+	private Gather distillGather(RDFNode node, boolean isGatherAsNonEmpty) {
 		if(!node.canAs(RDFList.class)){
-			logger.error("rrf:gather should be used on a RDF collection.");
+			logger.error("rrf:gather(asNonEmpty) should be used on a RDF collection.");
 			return null;
 		}
 
 		RDFList list = node.as(RDFList.class);
 		
-		Gather gather = new Gather();
+		Gather gather = isGatherAsNonEmpty ? new GatherAsNonEmpty() : new Gather();
 
 		ExtendedIterator<RDFNode> iter = list.iterator();
 		while(iter.hasNext()) {
 			RDFNode param = iter.next();
 			if(!param.isResource()) {
-				logger.error("Nodes in rrf:gather have to be resources.");
+				logger.error("Nodes in rrf:gather(asNonEmpty) have to be resources.");
 				logger.error(description);
 				return null;
 			}
@@ -201,7 +210,7 @@ public abstract class TermMap extends R2RMLResource {
 			if(om.preProcessAndValidate()) {
 				gather.addTermMap(om);
 			} else {
-				logger.error("Something went wrong processing parameter.");
+				logger.error("Something went wrong processing element of rrf:gather(asNonEmpty).");
 				logger.error(description);
 				return null;
 			}
@@ -530,6 +539,10 @@ public abstract class TermMap extends R2RMLResource {
 					items.add(item);
 			}
 			
+			// return null if list is empty and gatherAsNonEmpty
+			if(isGatherAsNonEmpty() && items.size() == 0)
+				return null;
+			
 			if(isTermTypeContainer()) {
 				Container c = null;
 				if(getTermType().getURI().equals(RDF.Bag.getURI()))
@@ -559,4 +572,8 @@ public abstract class TermMap extends R2RMLResource {
 		}
 		return null;
 	}
+
+	private boolean isGatherAsNonEmpty() {
+		return gather instanceof GatherAsNonEmpty ;
+	}
 }

+ 18 - 99
test/resources/COL03.mapping.ttl

@@ -2,103 +2,22 @@
 @prefix ex: <http://example.com/ns#> .
 @prefix rrf: <http://kdeg.scss.tcd.ie/ns/rrf#>
 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+    
+<#BooksTriplesMap>
+    rr:logicalTable [ rr:tableName "COLAUTHOR" ];
 
-@prefix modsrdf: <http://www.loc.gov/mods/modsrdf/v1/> .
-@prefix madsrdf: <http://www.loc.gov/mads/rdf/v1#> .
-
-@prefix owl: <http://www.w3.org/2002/07/owl#>
-@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
-@prefix skos: <http://www.w3.org/2004/02/skos/core#>
-@prefix foaf: <http://xmlns.com/foaf/0.1/>
-
-<#TitleInfoPrincipal>
-	rr:logicalTable [
-		rr:sqlQuery """SELECT *,
-		IF(nonSort IS NULL, NULL, id) AS nonSortId,
-		IF(subtitle IS NULL, NULL, id) AS subtitleId,
-		IF(partNumber IS NULL, NULL, id) AS partNumberId,
-		IF(partName IS NULL, NULL, id) AS partNameId
-		FROM TitleInfo WHERE tiType IS NULL""" ;
-	] ;
-	
-	rr:subjectMap [
-		rr:template "http://data.library.tcd.ie/resource/titleinfo/{id}" ;
-		rr:class madsrdf:Title ;
-	] ;
-	
-	rr:predicateObjectMap [
-		rr:predicate rdfs:label ;
-		rr:objectMap [
-			rr:column "title" ;
-			rr:termType rr:Literal ;
-		] ;
-	] ;
-	
-	rr:predicateObjectMap [
-		rr:predicate madsrdf:elementList ;
-		rr:objectMap [
-			rrf:gather (
-				[ rr:template "urn:tcd:title-nonsort-{nonSortId}" ] 
-				[ rr:template "urn:tcd:title-main-{id}" ] 
-				[ rr:template "urn:tcd:title-subtitle-{subtitleId}" ] 
-				[ rr:template "urn:tcd:title-partname-{partNameId}" ] 
-				[ rr:template "urn:tcd:title-partnumber-{partNumberId}" ] 
-			) ;
-		] ;
-		rr:termType rdf:List ;
-	] ;
-	.
-	
-<#TitleInfoPrincipal-NonSort>
-	rr:logicalTable [
-		rr:sqlQuery "SELECT * FROM TitleInfo WHERE nonSort IS NOT NULL" ;
-	] ;
-	
-	rr:subjectMap [
-		rr:template "urn:tcd:title-nonsort-{id}" ;
-		rr:class madsrdf:NonSortElement ;
-	] ;
-	
-	rr:predicateObjectMap [
-		rr:predicate madsrdf:elementValue ;
-		rr:objectMap [
-			rr:column "nonSort" ;
-		] ;
-	] ;
-	.
-	
-<#TitleInfoPrincipal-Title>
-	rr:logicalTable [
-		rr:sqlQuery "SELECT * FROM TitleInfo" ;
-	] ;
-	
-	rr:subjectMap [
-		rr:template "urn:tcd:title-main-{id}" ;
-		rr:class madsrdf:MainTitleElement ;
-	] ;
-	
-	rr:predicateObjectMap [
-		rr:predicate madsrdf:elementValue ;
-		rr:objectMap [
-			rr:column "title" ;
-		] ;
-	] ;
-	.
-	
-<#TitleInfoPrincipal-PartName>
-	rr:logicalTable [
-		rr:sqlQuery "SELECT * FROM TitleInfo WHERE partNumber IS NOT NULL" ;
-	] ;
-	
-	rr:subjectMap [
-		rr:template "urn:tcd:title-partnumber-{id}" ;
-		rr:class madsrdf:PartNumberElement ;
-	] ;
-	
-	rr:predicateObjectMap [
-		rr:predicate madsrdf:elementValue ;
-		rr:objectMap [
-			rr:column "partNumber" ;
-		] ;
-	] ;
-	.
+    rr:subjectMap [
+        rr:template "http://example.com/book/{BOOKID}";
+    ];
+    
+    rr:predicateObjectMap [
+        rr:predicate ex:writtenby;
+        rr:objectMap [ 
+		  rrf:gatherAsNonEmpty (
+        		[ rr:column "TITLE" ]
+        	) ;
+          rrf:collectAs rdf:List ;
+        ];
+    ];
+    
+    .

+ 7 - 9
test/test/TestR2RMLCOL.java

@@ -1,7 +1,6 @@
 package test;
 
 import org.apache.jena.rdf.model.Model;
-import org.apache.jena.vocabulary.RDF;
 import org.apache.log4j.BasicConfigurator;
 import org.junit.BeforeClass;
 
@@ -28,7 +27,7 @@ public class TestR2RMLCOL extends TestCase {
 		BasicConfigurator.configure();
 	}
 
-//	public void testExampleCSV01() {
+//	public void test01() {
 //		Configuration configuration = new Configuration();
 //		configuration.setMappingFile("./test/resources/COL01.mapping.ttl");
 //		configuration.getCSVFiles().add("./test/resources/PERSON.CSV");
@@ -39,7 +38,7 @@ public class TestR2RMLCOL extends TestCase {
 //		model.write(System.out, "Turtle");
 //	}
 //	
-//	public void testExampleCSV02() {
+//	public void test02() {
 //		Configuration configuration = new Configuration();
 //		configuration.setMappingFile("./test/resources/COL02.mapping.ttl");
 //		configuration.getCSVFiles().add("./test/resources/PERSON.CSV");
@@ -50,7 +49,7 @@ public class TestR2RMLCOL extends TestCase {
 //		model.write(System.out, "Turtle");
 //	}
 	
-//	public void testExampleCSV04() {
+//	public void test04() {
 //		Configuration configuration = new Configuration();
 //		configuration.setMappingFile("./test/resources/COL04.mapping.ttl");
 //		configuration.getCSVFiles().add("./test/resources/COLAUTHOR.CSV");
@@ -65,7 +64,7 @@ public class TestR2RMLCOL extends TestCase {
 //		model.write(System.out, "Turtle");
 //	}
 //	
-//	public void testExampleCSV05() {
+//	public void test05() {
 //		Configuration configuration = new Configuration();
 //		configuration.setMappingFile("./test/resources/COL05.mapping.ttl");
 //		configuration.getCSVFiles().add("./test/resources/COLAUTHOR.CSV");
@@ -80,12 +79,11 @@ public class TestR2RMLCOL extends TestCase {
 //		model.write(System.out, "Turtle");
 //	}
 	
-	public void testExampleCSV03() {
+	public void test03() {
 		Configuration configuration = new Configuration();
 		configuration.setMappingFile("./test/resources/COL03.mapping.ttl");
-		configuration.setUser("root");
-		configuration.setPassword("root");
-		configuration.setConnectionURL("jdbc:mysql://127.0.0.1/modseditor2");
+		configuration.getCSVFiles().add("./test/resources/COLAUTHOR.CSV");
+		configuration.getCSVFiles().add("./test/resources/COLBOOK.CSV");
 		R2RMLProcessor engine = new R2RMLProcessor(configuration);
 		engine.execute();
 		Model model = engine.getDataset().getDefaultModel();