use of de.knowwe.ontology.compile.OntologyCompiler in project d3web-KnowWE by denkbares.
the class OntologyDownloadProvider method getDownloadTool.
protected Tool getDownloadTool(Section<?> section, RDFFormat syntax) {
OntologyCompiler compiler = OntologyUtils.getOntologyCompiler(section);
if (compiler == null) {
return null;
}
Rdf2GoCore ontology = Rdf2GoCore.getInstance(compiler);
if (ontology == null || ontology.isEmpty()) {
return null;
}
// get name of ontology
String ontologyName = DefaultMarkupType.getContent(section).trim();
if (ontologyName.isEmpty()) {
ontologyName = "ontology";
}
String extension = syntax.getDefaultFileExtension();
List<Section<OntologyType>> ontologySections = Sections.successors(section.getArticle(), OntologyType.class);
String jsAction;
// if there is only one ontology section on this article provide static URL access per article name
String identifierForThisOntology;
if (ontologySections.size() == 1) {
identifierForThisOntology = TITLE + "=" + section.getTitle();
} else {
identifierForThisOntology = Attributes.SECTION_ID + "=" + section.getID();
}
jsAction = "action/OntologyDownloadAction" + "?" + identifierForThisOntology + "&" + OntologyDownloadAction.PARAM_SYNTAX + "=" + Strings.encodeURL(syntax.getDefaultMIMEType()) + "&" + OntologyDownloadAction.PARAM_FILENAME + "=" + ontologyName + "." + extension + "";
// assemble download tool
return new DefaultTool(Icon.DOWNLOAD, "Download " + syntax.getName().toUpperCase(), "Download the entire ontology in " + syntax.getName() + " format for deployment.", jsAction, Tool.ActionType.HREF, Tool.CATEGORY_DOWNLOAD);
}
use of de.knowwe.ontology.compile.OntologyCompiler in project d3web-KnowWE by denkbares.
the class LazyURIReferenceCompleteToolProvider method getTools.
@Override
public Tool[] getTools(Section<?> section, UserContext userContext) {
OntologyCompiler compiler = Compilers.getCompiler(userContext, section, OntologyCompiler.class);
Collection<Identifier> potentiallyMatchingIdentifiers = LazyURIReference.getPotentiallyMatchingIdentifiers(compiler, section);
Tool[] tools = new Tool[potentiallyMatchingIdentifiers.size()];
Iterator<Identifier> iterator = potentiallyMatchingIdentifiers.iterator();
for (int i = 0; i < potentiallyMatchingIdentifiers.size(); i++) {
Identifier identifier = iterator.next();
tools[i] = createModificationTool(section, identifier);
}
return tools;
}
use of de.knowwe.ontology.compile.OntologyCompiler in project d3web-KnowWE by denkbares.
the class Utils method createConceptURL.
public static String createConceptURL(String to, Config config, Section<?> section, LinkToTermDefinitionProvider uriProvider, String uri) {
if (section != null) {
final OntologyCompiler compiler = Compilers.getCompiler(section, OntologyCompiler.class);
final String shortIRI = Rdf2GoUtils.reduceNamespace(compiler.getRdf2GoCore(), uri);
Identifier identifier = new Identifier(shortIRI);
String[] identifierParts = shortIRI.split(":");
if (identifierParts.length == 2) {
identifier = new Identifier(identifierParts[0], Strings.decodeURL(identifierParts[1]));
}
final TerminologyManager terminologyManager = compiler.getTerminologyManager();
final Section<?> termDefiningSection = terminologyManager.getTermDefiningSection(identifier);
if (termDefiningSection == null) {
// we have no definition found
return null;
}
// get the closes ancestor that will have an anchor to jump to
Section<?> anchorAncestor = PreviewManager.getInstance().getPreviewAncestor(termDefiningSection);
String url = KnowWEUtils.getURLLink(anchorAncestor);
if (!url.startsWith("http:")) {
url = Environment.getInstance().getWikiConnector().getBaseUrl() + url;
}
return url;
}
return OntoGraphDataBuilder.createBaseURL() + "?concept=" + to;
}
use of de.knowwe.ontology.compile.OntologyCompiler in project d3web-KnowWE by denkbares.
the class AddStatementsAction method execute.
@Override
public void execute(UserActionContext context) throws IOException {
String jsonText = context.getParameter(PARAM_DATA);
if (Strings.isBlank(jsonText)) {
context.sendError(HttpServletResponse.SC_BAD_REQUEST, "no data to be inserted");
return;
}
try {
JSONObject json = new JSONObject(jsonText);
String articleName = (String) json.get("article");
boolean compactMode = json.optBoolean("compact");
ArticleManager manager = context.getArticleManager();
Article article = manager.getArticle(articleName);
if (article == null) {
context.sendError(HttpServletResponse.SC_NOT_FOUND, "article '" + articleName + "' not available");
return;
}
OntologyCompiler compiler = findCompiler(context, article);
if (compiler == null) {
context.sendError(HttpServletResponse.SC_NOT_FOUND, "Article '" + articleName + "' is not compiled in an ontology");
return;
}
Statement[] statementsToAdd = toStatements(compiler.getRdf2GoCore(), json.getJSONArray("add"));
Statement[] statementsToRemove = toStatements(compiler.getRdf2GoCore(), json.getJSONArray("remove"));
String newText = OntologyUtils.modifyTurtle(compiler, article, compactMode, statementsToAdd, statementsToRemove);
context.setContentType(PLAIN_TEXT);
context.getWriter().append(newText);
} catch (JSONException e) {
context.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
}
}
use of de.knowwe.ontology.compile.OntologyCompiler in project d3web-KnowWE by denkbares.
the class OntologyDownloadAction method execute.
@Override
public void execute(UserActionContext context) throws IOException {
OntologyCompiler compiler = getCompiler(context);
if (compiler == null) {
context.sendError(HttpServletResponse.SC_NOT_FOUND, "No compiler found");
return;
}
if (!KnowWEUtils.canView(compiler.getCompileSection().getArticle(), context)) {
context.sendError(HttpServletResponse.SC_FORBIDDEN, "You are not allowed to download this knowledge base");
return;
}
Compilers.awaitTermination(context.getArticleManager().getCompilerManager());
if (Boolean.parseBoolean(context.getParameter(PARAM_FULL_COMPILE, "false"))) {
if (compiler.isIncrementalBuild()) {
RecompileAction.recompile(compiler.getCompileSection().getArticle(), true);
Compilers.awaitTermination(context.getArticleManager().getCompilerManager());
compiler = getCompiler(context);
if (compiler == null)
failUnexpected(context, "Compile no longer available after recompile");
}
}
Rdf2GoCore rdf2GoCore = compiler.getRdf2GoCore();
RDFFormat syntax = Rio.getParserFormatForMIMEType(context.getParameter(PARAM_SYNTAX)).orElse(RDFFormat.RDFXML);
String mimeType = syntax.getDefaultMIMEType() + "; charset=UTF-8";
String filename = context.getParameter(PARAM_FILENAME);
if (filename == null) {
filename = Compilers.getCompilerName(compiler) + "." + syntax.getFileExtensions().stream().findFirst().orElse("ontology");
}
context.setContentType(mimeType);
context.getResponse().addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
context.getResponse().addHeader("Last-Modified", org.apache.http.client.utils.DateUtils.formatDate(compiler.getLastModified()));
Stopwatch stopwatch = new Stopwatch();
try (OutputStream outputStream = context.getOutputStream()) {
if (syntax == RDFFormat.TURTLE) {
// pretty formatted turtle doesn't always work, we try first and do fallback in case it does not work
// since we can't fallback if we write to the response directly, we have to write to a temp file first
final File tempFile = Files.createTempFile(rdf2GoCore.getName(), filename).toFile();
tempFile.deleteOnExit();
try {
try (FileOutputStream out = new FileOutputStream(tempFile)) {
rdf2GoCore.writeModel(out, syntax);
}
try (FileInputStream inputStream = new FileInputStream(tempFile)) {
Streams.stream(inputStream, outputStream);
}
} catch (Exception e) {
LOGGER.warn("Formatted turtle export failed, very likely due to setting inline_blank_nodes, trying again without...");
// formatted writing didn't work, just write to response directly, we don't expect failure
rdf2GoCore.writeModel(Rio.createWriter(syntax, outputStream));
} finally {
tempFile.delete();
}
} else {
rdf2GoCore.writeModel(outputStream, syntax);
}
}
stopwatch.log("Exported " + filename);
}
Aggregations