Search in sources :

Example 1 with OntologyCompiler

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 + "&amp;" + OntologyDownloadAction.PARAM_SYNTAX + "=" + Strings.encodeURL(syntax.getDefaultMIMEType()) + "&amp;" + 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);
}
Also used : Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) OntologyCompiler(de.knowwe.ontology.compile.OntologyCompiler) DefaultTool(de.knowwe.tools.DefaultTool) Section(de.knowwe.core.kdom.parsing.Section)

Example 2 with OntologyCompiler

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;
}
Also used : Identifier(com.denkbares.strings.Identifier) OntologyCompiler(de.knowwe.ontology.compile.OntologyCompiler) DefaultTool(de.knowwe.tools.DefaultTool) Tool(de.knowwe.tools.Tool)

Example 3 with OntologyCompiler

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;
}
Also used : TerminologyManager(de.knowwe.core.compile.terminology.TerminologyManager) Identifier(com.denkbares.strings.Identifier) OntologyCompiler(de.knowwe.ontology.compile.OntologyCompiler)

Example 4 with OntologyCompiler

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());
    }
}
Also used : JSONObject(org.json.JSONObject) ArticleManager(de.knowwe.core.ArticleManager) OntologyCompiler(de.knowwe.ontology.compile.OntologyCompiler) Statement(org.eclipse.rdf4j.model.Statement) Article(de.knowwe.core.kdom.Article) JSONException(org.json.JSONException)

Example 5 with OntologyCompiler

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);
}
Also used : Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) OntologyCompiler(de.knowwe.ontology.compile.OntologyCompiler) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Stopwatch(com.denkbares.utils.Stopwatch) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) RDFFormat(org.eclipse.rdf4j.rio.RDFFormat)

Aggregations

OntologyCompiler (de.knowwe.ontology.compile.OntologyCompiler)18 Section (de.knowwe.core.kdom.parsing.Section)8 Rdf2GoCore (de.knowwe.rdf2go.Rdf2GoCore)8 Identifier (com.denkbares.strings.Identifier)6 IRI (org.eclipse.rdf4j.model.IRI)5 Sections (de.knowwe.core.kdom.parsing.Sections)3 DefaultTool (de.knowwe.tools.DefaultTool)3 File (java.io.File)3 IOException (java.io.IOException)3 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)3 Stopwatch (com.denkbares.utils.Stopwatch)2 TerminologyManager (de.knowwe.core.compile.terminology.TerminologyManager)2 Article (de.knowwe.core.kdom.Article)2 Sections.$ (de.knowwe.core.kdom.parsing.Sections.$)2 Message (de.knowwe.core.report.Message)2 Messages (de.knowwe.core.report.Messages)2 KnowWEUtils (de.knowwe.core.utils.KnowWEUtils)2 AnnotationContentType (de.knowwe.kdom.defaultMarkup.AnnotationContentType)2 DefaultMarkupType (de.knowwe.kdom.defaultMarkup.DefaultMarkupType)2 OntologyType (de.knowwe.ontology.compile.OntologyType)2