} 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();
// TriplesMap triplesMap = mapping.getTriplesMapIndex().get(alignmentNodeId);
if (contextFromModel) {
KR2RMLMappingWriter writer;
try {
StringWriter string = new StringWriter();
PrintWriter pw = new PrintWriter(string);
writer = new KR2RMLMappingWriter();
writer.addR2RMLMapping(mapping, worksheet, workspace);
writer.writeR2RMLMapping(pw);
writer.close();
pw.flush();
pw.close();
Model model = ModelFactory.createDefaultModel();
InputStream s = new ReaderInputStream(new StringReader(string.toString()));
model.read(s, null, "TURTLE");
contextJSON = new ContextGenerator(model, true).generateContext().toString();
} catch (Exception e) {
}
}
logger.debug(mapping.toString());
//****************************************************************************************************/
//*** Extract list of TripleMaps *************************************************************************************************/
List<TriplesMap> triplesMapList = mapping.getTriplesMapList();
String rootTriplesMapId = null;
for(TriplesMap map: triplesMapList)
{
if(map.getSubject().getId().compareTo(alignmentNodeId) == 0)
{
rootTriplesMapId = map.getId();
break;
}
}
if(null == rootTriplesMapId)
{
String errmsg ="Invalid alignment id " + alignmentNodeId;
logger.error(errmsg);
return new UpdateContainer(new ErrorUpdate("Error occured while searching for root for JSON: " +errmsg));
}
// create JSONKR2RMLRDFWriter
final String jsonFileName = workspace.getCommandPreferencesId() + worksheetId + "-" +
worksheet.getTitle().replaceAll("\\.", "_") + "-export"+".json";
final String jsonFileLocalPath = ServletContextParameterMap.getParameterValue(ContextParameter.JSON_PUBLISH_DIR) +
jsonFileName;
final String contextName = workspace.getCommandPreferencesId() + worksheetId + "-" + worksheet.getTitle().replaceAll("\\.", "_") + "-context.json";
final String jsonContextFileLocalPath = ServletContextParameterMap.getParameterValue(ContextParameter.JSON_PUBLISH_DIR) + contextName;
PrintWriter printWriter;
try {
printWriter = new PrintWriter(jsonFileLocalPath);
String baseURI = worksheet.getMetadataContainer().getWorksheetProperties().getPropertyValue(Property.baseURI);
JSONKR2RMLRDFWriter writer = new JSONKR2RMLRDFWriter(printWriter, baseURI);
if (contextJSON != null && !contextJSON.isEmpty()) {
JSONObject context = new JSONObject();
try {
context = new JSONObject(this.contextJSON);
}catch(Exception e)
{
}
PrintWriter pw = new PrintWriter(jsonContextFileLocalPath);
pw.println(context.toString(4));
pw.close();
StringBuilder url = new StringBuilder();
url.append(ServletContextParameterMap.getParameterValue(ContextParameter.JETTY_HOST));
url.append(":");
url.append(ServletContextParameterMap.getParameterValue(ContextParameter.JETTY_PORT));
url.append("/");
url.append(ServletContextParameterMap.getParameterValue(ContextParameter.JSON_PUBLISH_RELATIVE_DIR));
url.append(contextName);
writer.setGlobalContext(context, new ContextIdentifier(context.toString(), new URL(url.toString())));
}
writer.addPrefixes(mapping.getPrefixes());
RootStrategy strategy = new UserSpecifiedRootStrategy(rootTriplesMapId, new SteinerTreeRootStrategy(new WorksheetDepthRootStrategy()));
KR2RMLWorksheetRDFGenerator generator = new KR2RMLWorksheetRDFGenerator(worksheet, f, ontMgr, writer, false, strategy, mapping, errorReport, selection);
try {
generator.generateRDF(true);
logger.info("RDF written to file.");