|
@@ -33,7 +33,7 @@ public class TriplesMap extends R2RMLResource {
|
|
|
|
|
|
private LogicalTable logicalTable = null;
|
|
private LogicalTable logicalTable = null;
|
|
private SubjectMap subjectMap = null;
|
|
private SubjectMap subjectMap = null;
|
|
- private List<PredicateObjectMap> predicateObjectMaps = new ArrayList<PredicateObjectMap> ();
|
|
|
|
|
|
+ private List<PredicateObjectMap> predicateObjectMaps = new ArrayList<PredicateObjectMap>();
|
|
private String baseIRI = null;
|
|
private String baseIRI = null;
|
|
|
|
|
|
private int count = 0;
|
|
private int count = 0;
|
|
@@ -73,14 +73,14 @@ public class TriplesMap extends R2RMLResource {
|
|
|
|
|
|
// TermMap must have an rr:logicalTable property (exactly one?)
|
|
// TermMap must have an rr:logicalTable property (exactly one?)
|
|
List<Statement> list = description.listProperties(R2RML.logicalTable).toList();
|
|
List<Statement> list = description.listProperties(R2RML.logicalTable).toList();
|
|
- if(list.size() != 1) {
|
|
|
|
|
|
+ if (list.size() != 1) {
|
|
logger.error("TriplesMap must have exactly one rr:logicalTable property.");
|
|
logger.error("TriplesMap must have exactly one rr:logicalTable property.");
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
RDFNode node = list.get(0).getObject();
|
|
RDFNode node = list.get(0).getObject();
|
|
- if(!node.isResource()) {
|
|
|
|
|
|
+ if (!node.isResource()) {
|
|
logger.error("LogicalTable of TriplesMap is not a resource.");
|
|
logger.error("LogicalTable of TriplesMap is not a resource.");
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
@@ -88,20 +88,21 @@ public class TriplesMap extends R2RMLResource {
|
|
|
|
|
|
// Pre-process and validate LogicalTable
|
|
// Pre-process and validate LogicalTable
|
|
logicalTable = new LogicalTable(node.asResource());
|
|
logicalTable = new LogicalTable(node.asResource());
|
|
- if(!logicalTable.preProcessAndValidate())
|
|
|
|
|
|
+ if (!logicalTable.preProcessAndValidate())
|
|
return false;
|
|
return false;
|
|
|
|
|
|
// TermMap must have exactly one of rr:subject and rr:subjectMap
|
|
// TermMap must have exactly one of rr:subject and rr:subjectMap
|
|
- // But we constructed rr:subjectMap from rr:subject, thus only check one!
|
|
|
|
- list = description.listProperties(R2RML.subjectMap).toList();
|
|
|
|
- if(list.size() != 1) {
|
|
|
|
|
|
+ // But we constructed rr:subjectMap from rr:subject, thus only check
|
|
|
|
+ // one!
|
|
|
|
+ list = description.listProperties(R2RML.subjectMap).toList();
|
|
|
|
+ if (list.size() != 1) {
|
|
logger.error("TriplesMap must have exactly one one of rr:subject and rr:subjectMap.");
|
|
logger.error("TriplesMap must have exactly one one of rr:subject and rr:subjectMap.");
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
node = list.get(0).getObject();
|
|
node = list.get(0).getObject();
|
|
- if(!node.isResource()) {
|
|
|
|
|
|
+ if (!node.isResource()) {
|
|
logger.error("SubjectMap of TriplesMap is not a resource.");
|
|
logger.error("SubjectMap of TriplesMap is not a resource.");
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
@@ -109,23 +110,23 @@ public class TriplesMap extends R2RMLResource {
|
|
|
|
|
|
// Pre-process and validate SubjectMap
|
|
// Pre-process and validate SubjectMap
|
|
subjectMap = new SubjectMap(node.asResource(), baseIRI);
|
|
subjectMap = new SubjectMap(node.asResource(), baseIRI);
|
|
- if(!subjectMap.preProcessAndValidate())
|
|
|
|
|
|
+ if (!subjectMap.preProcessAndValidate())
|
|
return false;
|
|
return false;
|
|
|
|
|
|
// Pre-process and validate PredicateObjectMaps
|
|
// Pre-process and validate PredicateObjectMaps
|
|
// TriplesMaps may have zero or more PredicateObjectMaps
|
|
// TriplesMaps may have zero or more PredicateObjectMaps
|
|
// Just iterate over them.
|
|
// Just iterate over them.
|
|
list = description.listProperties(R2RML.predicateObjectMap).toList();
|
|
list = description.listProperties(R2RML.predicateObjectMap).toList();
|
|
- for(Statement s : list) {
|
|
|
|
|
|
+ for (Statement s : list) {
|
|
node = s.getObject();
|
|
node = s.getObject();
|
|
- if(!node.isResource()) {
|
|
|
|
|
|
+ if (!node.isResource()) {
|
|
logger.error("PredicateObjectMap is not a resource.");
|
|
logger.error("PredicateObjectMap is not a resource.");
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
PredicateObjectMap opm = new PredicateObjectMap(s.getObject().asResource(), baseIRI);
|
|
PredicateObjectMap opm = new PredicateObjectMap(s.getObject().asResource(), baseIRI);
|
|
- if(!opm.preProcessAndValidate())
|
|
|
|
|
|
+ if (!opm.preProcessAndValidate())
|
|
return false;
|
|
return false;
|
|
predicateObjectMaps.add(opm);
|
|
predicateObjectMaps.add(opm);
|
|
}
|
|
}
|
|
@@ -133,13 +134,28 @@ public class TriplesMap extends R2RMLResource {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public boolean generateTriples(
|
|
|
|
- DB database,
|
|
|
|
- Dataset dataset,
|
|
|
|
- Map<Resource, TriplesMap> triplesMaps) {
|
|
|
|
-
|
|
|
|
|
|
+ public boolean generateTriples(DB database, Dataset dataset, Map<Resource, TriplesMap> triplesMaps) {
|
|
|
|
+
|
|
try {
|
|
try {
|
|
String query = getLogicalTable().generateQuery();
|
|
String query = getLogicalTable().generateQuery();
|
|
|
|
+
|
|
|
|
+ // TODO maybe not necessary
|
|
|
|
+// if (database.getConfiguration().isPreview()) {
|
|
|
|
+// String previewSQL = "";
|
|
|
|
+// for (PredicateObjectMap opm : getPredicateObjectMaps()) {
|
|
|
|
+// for (RefObjectMap rof : opm.getRefObjectMaps()) {
|
|
|
|
+// for(Join join : rof.getJoins()) {
|
|
|
|
+// if(query.contains("WHERE") && previewSQL.contains("WHERE")) {
|
|
|
|
+// previewSQL += " AND "+ join.getChild() + " IS NOT NULL ";
|
|
|
|
+// }else {
|
|
|
|
+// previewSQL += " WHERE "+ join.getChild() + " IS NOT NULL ";
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// query = query + previewSQL + " UNION ALL " + query;
|
|
|
|
+// System.out.println(query);
|
|
|
|
+// }
|
|
Rows rows = database.getRows(query);
|
|
Rows rows = database.getRows(query);
|
|
|
|
|
|
List<Resource> classes = getSubjectMap().getClasses();
|
|
List<Resource> classes = getSubjectMap().getClasses();
|
|
@@ -149,137 +165,161 @@ public class TriplesMap extends R2RMLResource {
|
|
|
|
|
|
int child_column_count = rows.getColumnCount();
|
|
int child_column_count = rows.getColumnCount();
|
|
|
|
|
|
|
|
+ Map<String, Object> attributeValueMap = null;
|
|
|
|
+
|
|
Row row = null;
|
|
Row row = null;
|
|
- while((row = rows.nextRow()) != null) {
|
|
|
|
|
|
+ while ((row = rows.nextRow()) != null) {
|
|
|
|
+ if (database.getConfiguration().isPreview()) {
|
|
|
|
+ attributeValueMap = R2RMLUtil.getAttributeValueMap(row.getResultSet());
|
|
|
|
+ }
|
|
Resource subject = getSubjectMap().generateRDFTerm(row);
|
|
Resource subject = getSubjectMap().generateRDFTerm(row);
|
|
|
|
|
|
// According to term generation rules, if the value is null,
|
|
// According to term generation rules, if the value is null,
|
|
// then no RDFTerm is generated. If that is the case for the
|
|
// then no RDFTerm is generated. If that is the case for the
|
|
// subject, then we do not generate RDF for that resource.
|
|
// subject, then we do not generate RDF for that resource.
|
|
- if(subject != null) {
|
|
|
|
|
|
+ if (subject != null) {
|
|
|
|
|
|
Set<String> subjectGraphs = new HashSet<String>();
|
|
Set<String> subjectGraphs = new HashSet<String>();
|
|
- for(GraphMap gm : sgm) {
|
|
|
|
|
|
+ for (GraphMap gm : sgm) {
|
|
Resource gmiri = gm.generateRDFTerm(row);
|
|
Resource gmiri = gm.generateRDFTerm(row);
|
|
- if(gmiri != null)
|
|
|
|
|
|
+ if (gmiri != null)
|
|
subjectGraphs.add(gmiri.getURI());
|
|
subjectGraphs.add(gmiri.getURI());
|
|
}
|
|
}
|
|
|
|
|
|
- for(Resource c : classes) {
|
|
|
|
|
|
+ for (Resource c : classes) {
|
|
addTriplesToDataset(dataset, subject, RDF.type, c, subjectGraphs);
|
|
addTriplesToDataset(dataset, subject, RDF.type, c, subjectGraphs);
|
|
}
|
|
}
|
|
|
|
|
|
// Now process each predicate-object map of the triples map
|
|
// Now process each predicate-object map of the triples map
|
|
- for(PredicateObjectMap opm : getPredicateObjectMaps()) {
|
|
|
|
- // Let predicates be the set of generated RDF terms that
|
|
|
|
- // result from applying each of the predicate-object
|
|
|
|
|
|
+ for (PredicateObjectMap opm : getPredicateObjectMaps()) {
|
|
|
|
+ // Let predicates be the set of generated RDF terms that
|
|
|
|
+ // result from applying each of the predicate-object
|
|
// map's predicate maps to row
|
|
// map's predicate maps to row
|
|
List<Property> predicates = new ArrayList<Property>();
|
|
List<Property> predicates = new ArrayList<Property>();
|
|
- for(PredicateMap pm : opm.getPredicateMaps()) {
|
|
|
|
|
|
+ for (PredicateMap pm : opm.getPredicateMaps()) {
|
|
Property p = pm.generateRDFTerm(row);
|
|
Property p = pm.generateRDFTerm(row);
|
|
- if(p != null)
|
|
|
|
|
|
+ if (p != null)
|
|
predicates.add(p);
|
|
predicates.add(p);
|
|
}
|
|
}
|
|
|
|
|
|
- // Let objects be the set of generated RDF terms that result
|
|
|
|
- // from applying each of the predicate-object map's object
|
|
|
|
|
|
+ // Let objects be the set of generated RDF terms that
|
|
|
|
+ // result
|
|
|
|
+ // from applying each of the predicate-object map's
|
|
|
|
+ // object
|
|
// maps (but not referencing object maps) to row
|
|
// maps (but not referencing object maps) to row
|
|
List<RDFNode> objects = new ArrayList<RDFNode>();
|
|
List<RDFNode> objects = new ArrayList<RDFNode>();
|
|
- for(ObjectMap om : opm.getObjectMaps()) {
|
|
|
|
- RDFNode o = om.generateRDFTerm(row);
|
|
|
|
- if(o != null)
|
|
|
|
|
|
+ for (ObjectMap om : opm.getObjectMaps()) {
|
|
|
|
+ RDFNode o = om.generateRDFTerm(row);
|
|
|
|
+ if (o != null)
|
|
objects.add(o);
|
|
objects.add(o);
|
|
}
|
|
}
|
|
|
|
|
|
- // Let pogm be the set of graph maps of the predicate-object
|
|
|
|
- // map. Let predicate-object_graphs be the set of generated
|
|
|
|
- // RDF terms that result from applying each graph map in pogm
|
|
|
|
|
|
+ // Let pogm be the set of graph maps of the
|
|
|
|
+ // predicate-object
|
|
|
|
+ // map. Let predicate-object_graphs be the set of
|
|
|
|
+ // generated
|
|
|
|
+ // RDF terms that result from applying each graph map in
|
|
|
|
+ // pogm
|
|
// to row
|
|
// to row
|
|
Set<String> pogs = new HashSet<String>();
|
|
Set<String> pogs = new HashSet<String>();
|
|
- for(GraphMap gm : opm.getGraphMaps()) {
|
|
|
|
|
|
+ for (GraphMap gm : opm.getGraphMaps()) {
|
|
Resource gmiri = gm.generateRDFTerm(row);
|
|
Resource gmiri = gm.generateRDFTerm(row);
|
|
- if(gmiri != null)
|
|
|
|
|
|
+ if (gmiri != null)
|
|
pogs.add(gmiri.getURI());
|
|
pogs.add(gmiri.getURI());
|
|
}
|
|
}
|
|
|
|
|
|
- // Target graphs: If sgm and pogm are empty: rr:defaultGraph;
|
|
|
|
- // otherwise: union of subject_graphs and predicate-object_graphs
|
|
|
|
|
|
+ // Target graphs: If sgm and pogm are empty:
|
|
|
|
+ // rr:defaultGraph;
|
|
|
|
+ // otherwise: union of subject_graphs and
|
|
|
|
+ // predicate-object_graphs
|
|
pogs.addAll(subjectGraphs);
|
|
pogs.addAll(subjectGraphs);
|
|
- for(Property p : predicates) {
|
|
|
|
- for(RDFNode o : objects) {
|
|
|
|
|
|
+ for (Property p : predicates) {
|
|
|
|
+ for (RDFNode o : objects) {
|
|
addTriplesToDataset(dataset, subject, p, o, pogs);
|
|
addTriplesToDataset(dataset, subject, p, o, pogs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} // end while
|
|
} // end while
|
|
|
|
|
|
- // For each referencing object map of a predicate-object map of
|
|
|
|
|
|
+ // For each referencing object map of a predicate-object map of
|
|
// the triples map, apply the following steps:
|
|
// the triples map, apply the following steps:
|
|
- for(PredicateObjectMap opm : getPredicateObjectMaps()) {
|
|
|
|
- for(RefObjectMap rof : opm.getRefObjectMaps()) {
|
|
|
|
|
|
+ for (PredicateObjectMap opm : getPredicateObjectMaps()) {
|
|
|
|
+ for (RefObjectMap rof : opm.getRefObjectMaps()) {
|
|
TriplesMap ptm = triplesMaps.get(rof.getParentTriplesMap());
|
|
TriplesMap ptm = triplesMaps.get(rof.getParentTriplesMap());
|
|
SubjectMap psm = ptm.getSubjectMap();
|
|
SubjectMap psm = ptm.getSubjectMap();
|
|
List<GraphMap> pogm = opm.getGraphMaps();
|
|
List<GraphMap> pogm = opm.getGraphMaps();
|
|
-
|
|
|
|
- String jointQuery = R2RMLUtil.createJointQuery(this, ptm, rof.getJoins());
|
|
|
|
- if(jointQuery == null)
|
|
|
|
|
|
+
|
|
|
|
+ String jointQuery = null;
|
|
|
|
+ if (database.getConfiguration().isPreview()) {
|
|
|
|
+ jointQuery = R2RMLUtil.createJointQueryPreview(this, ptm, rof.getJoins(), attributeValueMap);
|
|
|
|
+ } else {
|
|
|
|
+ jointQuery = R2RMLUtil.createJointQuery(this, ptm, rof.getJoins());
|
|
|
|
+ }
|
|
|
|
+ if (jointQuery == null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
Rows rows2 = database.getRows(jointQuery);
|
|
Rows rows2 = database.getRows(jointQuery);
|
|
int column_count = rows2.getColumnCount();
|
|
int column_count = rows2.getColumnCount();
|
|
// For each row in rows2...
|
|
// For each row in rows2...
|
|
- while(rows2.nextRow() != null) {
|
|
|
|
|
|
+ while (rows2.nextRow() != null) {
|
|
Row child_row = rows2.projectCurrentRow(1, child_column_count);
|
|
Row child_row = rows2.projectCurrentRow(1, child_column_count);
|
|
Row parent_row = rows2.projectCurrentRow(child_column_count + 1, column_count);
|
|
Row parent_row = rows2.projectCurrentRow(child_column_count + 1, column_count);
|
|
|
|
|
|
- // Let subject be the generated RDF term that results
|
|
|
|
|
|
+ // Let subject be the generated RDF term that results
|
|
// from applying sm to "child_row"
|
|
// from applying sm to "child_row"
|
|
Resource subject = getSubjectMap().generateRDFTerm(child_row);
|
|
Resource subject = getSubjectMap().generateRDFTerm(child_row);
|
|
|
|
|
|
- // Let object be the generated RDF term that results
|
|
|
|
|
|
+ // Let object be the generated RDF term that results
|
|
// from applying psm to parent_row
|
|
// from applying psm to parent_row
|
|
Resource object = psm.generateRDFTerm(parent_row);
|
|
Resource object = psm.generateRDFTerm(parent_row);
|
|
-
|
|
|
|
|
|
+
|
|
// if subject or object is NULL, don't generate triples
|
|
// if subject or object is NULL, don't generate triples
|
|
- if(subject != null || object != null) {
|
|
|
|
|
|
+ if (subject != null || object != null) {
|
|
|
|
|
|
- // Let predicates be the set of generated RDF terms that result
|
|
|
|
- // from applying each of the predicate-object map's predicate
|
|
|
|
|
|
+ // Let predicates be the set of generated RDF terms
|
|
|
|
+ // that result
|
|
|
|
+ // from applying each of the predicate-object map's
|
|
|
|
+ // predicate
|
|
// maps to child_row
|
|
// maps to child_row
|
|
List<Property> predicates = new ArrayList<Property>();
|
|
List<Property> predicates = new ArrayList<Property>();
|
|
- for(PredicateMap pm : opm.getPredicateMaps()) {
|
|
|
|
|
|
+ for (PredicateMap pm : opm.getPredicateMaps()) {
|
|
Property p = pm.generateRDFTerm(child_row);
|
|
Property p = pm.generateRDFTerm(child_row);
|
|
- if(p != null)
|
|
|
|
|
|
+ if (p != null)
|
|
predicates.add(p);
|
|
predicates.add(p);
|
|
}
|
|
}
|
|
|
|
|
|
- // Let subject_graphs be the set of generated RDF terms
|
|
|
|
- // that result from applying each graph map of sgm to
|
|
|
|
|
|
+ // Let subject_graphs be the set of generated RDF
|
|
|
|
+ // terms
|
|
|
|
+ // that result from applying each graph map of sgm
|
|
|
|
+ // to
|
|
// child_row
|
|
// child_row
|
|
Set<String> subjectGraphs = new HashSet<String>();
|
|
Set<String> subjectGraphs = new HashSet<String>();
|
|
- for(GraphMap gm : sgm) {
|
|
|
|
|
|
+ for (GraphMap gm : sgm) {
|
|
Resource gmiri = gm.generateRDFTerm(child_row);
|
|
Resource gmiri = gm.generateRDFTerm(child_row);
|
|
- if(gmiri != null)
|
|
|
|
|
|
+ if (gmiri != null)
|
|
subjectGraphs.add(gmiri.getURI());
|
|
subjectGraphs.add(gmiri.getURI());
|
|
}
|
|
}
|
|
|
|
|
|
- // Let predicate-object_graphs be the set of generated
|
|
|
|
- // RDF terms that result from applying each graph map in
|
|
|
|
|
|
+ // Let predicate-object_graphs be the set of
|
|
|
|
+ // generated
|
|
|
|
+ // RDF terms that result from applying each graph
|
|
|
|
+ // map in
|
|
// pogm to child_row
|
|
// pogm to child_row
|
|
Set<String> pogs = new HashSet<String>();
|
|
Set<String> pogs = new HashSet<String>();
|
|
- for(GraphMap gm : pogm) {
|
|
|
|
|
|
+ for (GraphMap gm : pogm) {
|
|
Resource gmiri = gm.generateRDFTerm(child_row);
|
|
Resource gmiri = gm.generateRDFTerm(child_row);
|
|
- if(gmiri != null)
|
|
|
|
|
|
+ if (gmiri != null)
|
|
pogs.add(gmiri.getURI());
|
|
pogs.add(gmiri.getURI());
|
|
}
|
|
}
|
|
|
|
|
|
- // Target graphs: If sgm and pogm are empty: rr:defaultGraph;
|
|
|
|
- // otherwise: union of subject_graphs and predicate-object_graphs
|
|
|
|
|
|
+ // Target graphs: If sgm and pogm are empty:
|
|
|
|
+ // rr:defaultGraph;
|
|
|
|
+ // otherwise: union of subject_graphs and
|
|
|
|
+ // predicate-object_graphs
|
|
pogs.addAll(subjectGraphs);
|
|
pogs.addAll(subjectGraphs);
|
|
- for(Property p : predicates) {
|
|
|
|
|
|
+ for (Property p : predicates) {
|
|
addTriplesToDataset(dataset, subject, p, object, pogs);
|
|
addTriplesToDataset(dataset, subject, p, object, pogs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -294,7 +334,7 @@ public class TriplesMap extends R2RMLResource {
|
|
logger.error(description);
|
|
logger.error(description);
|
|
return false;
|
|
return false;
|
|
} finally {
|
|
} finally {
|
|
- if(database != null) {
|
|
|
|
|
|
+ if (database != null) {
|
|
try {
|
|
try {
|
|
database.close();
|
|
database.close();
|
|
} catch (R2RMLException e) {
|
|
} catch (R2RMLException e) {
|
|
@@ -311,21 +351,21 @@ public class TriplesMap extends R2RMLResource {
|
|
* Adding triples to the dataset. If the set of named graphs is empty, they
|
|
* Adding triples to the dataset. If the set of named graphs is empty, they
|
|
* added to the default model.
|
|
* added to the default model.
|
|
*
|
|
*
|
|
- * @param ds The dataset
|
|
|
|
- * @param s The subject
|
|
|
|
- * @param p The predicate
|
|
|
|
- * @param o The object
|
|
|
|
|
|
+ * @param ds The dataset
|
|
|
|
+ * @param s The subject
|
|
|
|
+ * @param p The predicate
|
|
|
|
+ * @param o The object
|
|
* @param ngs The set of named graphs
|
|
* @param ngs The set of named graphs
|
|
*/
|
|
*/
|
|
private void addTriplesToDataset(Dataset ds, Resource s, Property p, RDFNode o, Set<String> ngs) {
|
|
private void addTriplesToDataset(Dataset ds, Resource s, Property p, RDFNode o, Set<String> ngs) {
|
|
- if(ngs.isEmpty()) {
|
|
|
|
|
|
+ if (ngs.isEmpty()) {
|
|
ds.getDefaultModel().add(s, p, o);
|
|
ds.getDefaultModel().add(s, p, o);
|
|
count++;
|
|
count++;
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- for(String ng : ngs) {
|
|
|
|
- if(ng.equals(R2RML.defaultGraph.getURI())) {
|
|
|
|
|
|
+ for (String ng : ngs) {
|
|
|
|
+ if (ng.equals(R2RML.defaultGraph.getURI())) {
|
|
ds.getDefaultModel().add(s, p, o);
|
|
ds.getDefaultModel().add(s, p, o);
|
|
count++;
|
|
count++;
|
|
} else {
|
|
} else {
|