// Generate the KR2RML data structures for the RDF generation
final ErrorReport errorReport = new ErrorReport();
KR2RMLMappingGenerator mappingGen = null;
String url = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.modelUrl);
String modelContext = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.modelContext);
TripleStoreUtil utilObj = new TripleStoreUtil();
String modelRepoUrl = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.modelRepository);
modelRepoUrl = modelRepoUrl == null || modelRepoUrl.isEmpty()? TripleStoreUtil.defaultModelsRepoUrl : modelRepoUrl;
Map<String, String> bloomfilterMapping = new HashMap<String, String>();
boolean result = true;
try{
mappingGen = new KR2RMLMappingGenerator(workspace, worksheet,
alignment, worksheet.getSemanticTypes(), rdfSourcePrefix, rdfSourceNamespace,
Boolean.valueOf(addInverseProperties), errorReport);
}
catch (KarmaException e)
{
logger.error("Error occured while generating RDF!", e);
return new UpdateContainer(new ErrorUpdate("Error occured while generating RDF: " + e.getMessage()));
}
KR2RMLMapping mapping = mappingGen.getKR2RMLMapping();
if (url != null && !url.trim().isEmpty() && modelContext != null && !modelContext.trim().isEmpty() && generateBloomFilters && utilObj.testURIExists(modelRepoUrl, "", url)) {
try {
File tmp = new File("tmp");
PrintWriter pw = new PrintWriter(tmp);
pw.println(utilObj.getMappingFromTripleStore(modelRepoUrl, modelContext, url));
pw.close();
Model model = WorksheetR2RMLJenaModelParser.loadSourceModelIntoJenaModel(tmp.toURI().toURL());
tmp.delete();
R2RMLMappingIdentifier identifier = new R2RMLMappingIdentifier(mapping.getId().getName(), new URL(url));
WorksheetR2RMLJenaModelParser parser = new WorksheetR2RMLJenaModelParser(model, identifier);
mapping = parser.parse();
} catch (Exception e) {
e.printStackTrace();
}
}
logger.debug(mapping.toString());
StringWriter sw = new StringWriter();
// Generate the RDF using KR2RML data structures
long start = 0;
try {
List<KR2RMLRDFWriter> writers = new ArrayList<KR2RMLRDFWriter>();
File f = new File(rdfFileLocalPath);
File parentDir = f.getParentFile();
parentDir.mkdirs();
BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(f),"UTF-8"));
N3KR2RMLRDFWriter writer = new N3KR2RMLRDFWriter(new URIFormatter(workspace.getOntologyManager(), errorReport), new PrintWriter (bw));
writer.setBaseURI(rdfSourceNamespace);
writers.add(writer);
if (generateBloomFilters && utilObj.testURIExists(modelRepoUrl, "", url)) {
BloomFilterKR2RMLRDFWriter bfWriter = new BloomFilterKR2RMLRDFWriter(new PrintWriter(sw), false, this.rdfSourceNamespace);
writers.add(bfWriter);
bfWriter.setR2RMLMappingIdentifier(mapping.getId());
}
KR2RMLWorksheetRDFGenerator rdfGen = new KR2RMLWorksheetRDFGenerator(worksheet,
workspace.getFactory(), workspace.getOntologyManager(),
writers, false, mapping, errorReport, selection);
rdfGen.generateRDF(true);
logger.info("RDF written to file: " + rdfFileLocalPath);
if(saveToStore){
//take the contents of the RDF file and save them to the store
logger.info("Using Jena DB:" + hostName + "/"+dbName + " user="+userName);
saveToStore(rdfFileLocalPath);
}
start = System.currentTimeMillis();
if (generateBloomFilters && utilObj.testURIExists(modelRepoUrl, "", url)) {
JSONObject obj = new JSONObject(sw.toString());
result &= updateTripleStore(obj, bloomfilterMapping, modelRepoUrl, modelContext, utilObj);
Map<String, String> verification = new HashMap<String, String>();
Set<String> triplemaps = new HashSet<String>(Arrays.asList(obj.getString("ids").split(",")));
verification.putAll(utilObj.getBloomFiltersForMaps(modelRepoUrl, modelContext, triplemaps));
boolean verify = true;
for (Entry<String, String> entry : verification.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
KR2RMLBloomFilter bf2 = new KR2RMLBloomFilter(KR2RMLBloomFilter.defaultVectorSize, KR2RMLBloomFilter.defaultnbHash, Hash.JENKINS_HASH);
KR2RMLBloomFilter bf = new KR2RMLBloomFilter(KR2RMLBloomFilter.defaultVectorSize, KR2RMLBloomFilter.defaultnbHash, Hash.JENKINS_HASH);
bf2.populateFromCompressedAndBase64EncodedString(value);
bf.populateFromCompressedAndBase64EncodedString(obj.getString(key));
bf2.and(bf);
bf2.xor(bf);
try {
Field f1 = BloomFilter.class.getDeclaredField("bits");
f1.setAccessible(true);
BitSet bits = (BitSet) f1.get(bf2);
if (bits.cardinality() != 0) {
verify = false;
break;
}
} catch (Exception e) {
}
}
if (!verify) {
result &= updateTripleStore(obj, verification, modelRepoUrl, modelContext, utilObj);
}
long end = System.currentTimeMillis();
System.out.println("execution time: " + (end - start) + " node total: " + bloomfilterMapping.size());
}
} catch (Exception e1) {
logger.error("Error occured while generating RDF!", e1);
return new UpdateContainer(new ErrorUpdate("Error occured while generating RDF: " + e1.getMessage()));
}
try {
// Get the graph name from properties if empty graph uri
// String graphName = worksheet.getMetadataContainer().getWorksheetProperties()
// .getPropertyValue(Property.graphName);
// if (this.graphUri == null || this.graphUri.isEmpty()) {
// // Set to default
// worksheet.getMetadataContainer().getWorksheetProperties().setPropertyValue(
// Property.graphName, WorksheetProperties.createDefaultGraphName(worksheet.getTitle()));
// this.graphUri = WorksheetProperties.createDefaultGraphName(worksheet.getTitle());
// }
if (tripleStoreUrl == null || tripleStoreUrl.isEmpty()) {
tripleStoreUrl = TripleStoreUtil.defaultDataRepoUrl;
}
logger.info("tripleStoreURl : " + tripleStoreUrl);
result &= utilObj.saveToStoreFromFile(rdfFileLocalPath, tripleStoreUrl, this.graphUri, this.replaceContext, this.rdfSourceNamespace);
if (url != null && !url.isEmpty() && url.compareTo("") != 0 && utilObj.testURIExists(modelRepoUrl, "", url)) {
StringBuilder sb = new StringBuilder();
url = url.trim();
if(!url.startsWith("<"))
{
sb.append("<");
}
sb.append(url);
if(!url.endsWith(">"))
{
sb.append(">");
}
sb.append(" <");
sb.append( Uris.MODEL_HAS_DATA_URI);
sb.append("> \"true\" .\n");
String input = sb.toString();
result &= utilObj.saveToStoreFromString(input, modelRepoUrl, modelContext, new Boolean(false), this.rdfSourceNamespace);
}
if(result) {
logger.info("Saved rdf to store");
} else {
logger.error("Falied to store rdf to karma_data store");