Search in sources :

Example 1 with Rdf2GoCore

use of de.knowwe.rdf2go.Rdf2GoCore in project d3web-KnowWE by denkbares.

the class SparqlContentRenderer method render.

private void render(Section<?> section, UserContext user, RenderResult result, boolean renderPreview) {
    Section<SparqlType> sparqlTypeSection = Sections.cast(section, SparqlType.class);
    Section<DefaultMarkupType> markupSection = Sections.ancestor(section, DefaultMarkupType.class);
    Rdf2GoCore core = Rdf2GoUtils.getRdf2GoCore(user, markupSection);
    if (core == null) {
        // we render an empty div, otherwise the ajax rerendering does not
        // work properly
        result.appendHtmlElement("div", "");
        return;
    }
    /*
		 * Show query text above of query result
		 */
    String showQueryFlag = DefaultMarkupType.getAnnotation(markupSection, SparqlMarkupType.RENDER_QUERY);
    if ("true".equalsIgnoreCase(showQueryFlag)) {
        // we need an opening html element around all the content as for
        // some reason the ajax insert only inserts one (the first) html
        // element into the page
        result.appendHtml("<div>");
        // render query text
        result.appendHtml("<span>");
        DelegateRenderer.getInstance().render(section, user, result);
        result.appendHtml("</span>");
    }
    String sparqlString = Rdf2GoUtils.createSparqlString(core, section.getText());
    if (sparqlString.toLowerCase().startsWith("construct")) {
        final Set<Statement> statementsFromCache = core.getStatementsFromCache(section);
        if (!statementsFromCache.isEmpty()) {
            final SparqlResultRenderer sparqlResultRenderer = SparqlResultRenderer.getInstance();
            int limit = 20;
            int count = 0;
            result.append("(" + statementsFromCache.size() + " statements constructed)");
            result.appendHtml("<table>");
            for (Statement statement : statementsFromCache) {
                count++;
                if (count > limit) {
                    // TODO: implement pagination and remove limit
                    result.appendHtml("<tr>");
                    int moreStatements = statementsFromCache.size() - limit;
                    result.append("\n(" + moreStatements + " statements hidden)");
                    result.appendHtml("</tr>");
                    break;
                }
                result.appendHtml("<tr>");
                result.appendHtml("<td>");
                final Resource subject = statement.getSubject();
                result.appendHtml(sparqlResultRenderer.renderNode(subject, "", false, user, core, HTML));
                result.appendHtml("</td>");
                result.appendHtml("<td>");
                final IRI predicate = statement.getPredicate();
                result.appendHtml(sparqlResultRenderer.renderNode(predicate, "", false, user, core, HTML));
                result.appendHtml("</td>");
                result.appendHtml("<td>");
                final Value object = statement.getObject();
                result.appendHtml(sparqlResultRenderer.renderNode(object, "", false, user, core, HTML));
                result.appendHtml("</td>");
                result.appendHtml("</tr>");
            }
            result.appendHtml("</table>");
        } else {
            result.append("(No statements constructed)");
        }
    } else if (sparqlString.toLowerCase().startsWith("ask")) {
        // process sparql ask query
        RenderOptions opts = sparqlTypeSection.get().getRenderOptions(sparqlTypeSection, user);
        try {
            String query = sparqlTypeSection.get().getSparqlQuery(sparqlTypeSection, user);
            boolean askResult = opts.getRdf2GoCore().sparqlAsk(query, new Rdf2GoCore.Options().timeout(opts.getTimeout()));
            result.appendHtml("<div class='sparqlAsk' sparqlSectionId='" + opts.getId() + "'>");
            if (opts.isBorder())
                result.appendHtml("<div class='border'>");
            result.append(Boolean.valueOf(askResult).toString());
            if (opts.isBorder())
                result.appendHtml("</div>");
            result.appendHtml("</div>");
        } catch (RuntimeException e) {
            SparqlResultRenderer.handleRuntimeException(sparqlTypeSection, user, result, e);
        }
    } else {
        SparqlResultRenderer.getInstance().renderSparqlResult(sparqlTypeSection, user, result, renderPreview);
    }
    if ("true".equalsIgnoreCase(showQueryFlag)) {
        // we need an opening html element around all the content as
        // for some reason the ajax insert only inserts one (the
        // first) html element into the page
        result.appendHtml("</div>");
    }
}
Also used : RenderOptions(de.knowwe.rdf2go.sparql.utils.RenderOptions) IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(org.eclipse.rdf4j.model.Resource) Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) Value(org.eclipse.rdf4j.model.Value)

Example 2 with Rdf2GoCore

use of de.knowwe.rdf2go.Rdf2GoCore in project d3web-KnowWE by denkbares.

the class SparqlDownloadAction method execute.

@Override
public void execute(UserActionContext context) throws IOException {
    String filename = context.getParameter(PARAM_FILENAME);
    context.setContentType("application/vnd.ms-excel");
    context.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    // find query
    Section<?> rootSection = getSection(context);
    Section<SparqlContentType> querySection = Sections.successor(rootSection, SparqlContentType.class);
    if (querySection == null) {
        context.sendError(410, "Query not found, probably the page has been edited while you visiting it. Please reload the page and try again, or contact the administrator if the error persists.");
        return;
    }
    Section<SparqlMarkupType> markupSection = Sections.ancestor(querySection, SparqlMarkupType.class);
    RenderOptions opts = $(markupSection).successor(SparqlType.class).mapFirst(s -> s.get().getRenderOptions(s, context));
    Collection<Rdf2GoCompiler> compilers = Compilers.getCompilers(markupSection, Rdf2GoCompiler.class);
    if (!compilers.isEmpty()) {
        Rdf2GoCore core = compilers.iterator().next().getRdf2GoCore();
        String sparql = Rdf2GoUtils.createSparqlString(core, querySection.getText());
        CachedTupleQueryResult resultSet = core.sparqlSelect(sparql);
        try (OutputStream outputStream = context.getOutputStream()) {
            try (XSSFWorkbook workbook = new XSSFWorkbook()) {
                addSparqlResultAsSheet(workbook, resultSet, context, core, opts);
                workbook.write(outputStream);
            }
        }
    }
}
Also used : SparqlContentType(de.knowwe.ontology.sparql.SparqlContentType) RenderOptions(de.knowwe.rdf2go.sparql.utils.RenderOptions) SparqlType(de.knowwe.ontology.sparql.SparqlType) OutputStream(java.io.OutputStream) Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) Rdf2GoCompiler(de.knowwe.rdf2go.Rdf2GoCompiler) CachedTupleQueryResult(com.denkbares.semanticcore.CachedTupleQueryResult) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) SparqlMarkupType(de.knowwe.ontology.sparql.SparqlMarkupType)

Example 3 with Rdf2GoCore

use of de.knowwe.rdf2go.Rdf2GoCore 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 4 with Rdf2GoCore

use of de.knowwe.rdf2go.Rdf2GoCore in project d3web-KnowWE by denkbares.

the class TurtleURI method getNode.

@Override
public Value getNode(OntologyCompiler compiler, Section<? extends TurtleURI> section) {
    Rdf2GoCore core = compiler.getRdf2GoCore();
    String turtleURIText = section.getText();
    Section<ResourceReference> ref = Sections.successor(section, ResourceReference.class);
    if (ref != null) {
        Identifier identifier = ref.get().getTermIdentifier(compiler, ref);
        return getNodeForIdentifier(core, identifier);
    } else {
        String uri = Rdf2GoUtils.expandNamespace(core, turtleURIText);
        return core.createIRI(uri);
    }
}
Also used : Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) Identifier(com.denkbares.strings.Identifier) ResourceReference(de.knowwe.ontology.kdom.resource.ResourceReference) AbbreviatedResourceReference(de.knowwe.ontology.kdom.resource.AbbreviatedResourceReference)

Example 5 with Rdf2GoCore

use of de.knowwe.rdf2go.Rdf2GoCore in project d3web-KnowWE by denkbares.

the class LazyURIReference method resolveToEqualNS.

private boolean resolveToEqualNS(Rdf2GoCompiler compiler, Collection<Identifier> potentiallyMatchingIdentifiers) {
    Rdf2GoCore rdf2GoCore = compiler.getRdf2GoCore();
    if (potentiallyMatchingIdentifiers.size() == 2) {
        String localNS = rdf2GoCore.getLocalNamespace();
        String defaultNS = rdf2GoCore.getNamespacesMap().get("");
        Iterator<Identifier> iterator = potentiallyMatchingIdentifiers.iterator();
        Identifier firstIdentifier = iterator.next();
        Identifier secondIdentifier = iterator.next();
        String firstNS = firstIdentifier.getPathElementAt(0);
        String secondNS = secondIdentifier.getPathElementAt(0);
        String full = rdf2GoCore.getNamespacesMap().get(firstNS);
        return full != null && full.equals(rdf2GoCore.getNamespacesMap().get(secondNS));
    }
    return false;
}
Also used : Rdf2GoCore(de.knowwe.rdf2go.Rdf2GoCore) Identifier(com.denkbares.strings.Identifier)

Aggregations

Rdf2GoCore (de.knowwe.rdf2go.Rdf2GoCore)29 OntologyCompiler (de.knowwe.ontology.compile.OntologyCompiler)7 Section (de.knowwe.core.kdom.parsing.Section)6 IOException (java.io.IOException)6 Identifier (com.denkbares.strings.Identifier)5 Value (org.eclipse.rdf4j.model.Value)5 Message (de.d3web.testing.Message)4 Message (de.knowwe.core.report.Message)4 DefaultMarkupType (de.knowwe.kdom.defaultMarkup.DefaultMarkupType)4 Rdf2GoCompiler (de.knowwe.rdf2go.Rdf2GoCompiler)4 CachedTupleQueryResult (com.denkbares.semanticcore.CachedTupleQueryResult)3 TupleQueryResult (com.denkbares.semanticcore.TupleQueryResult)3 SparqlContentType (de.knowwe.ontology.sparql.SparqlContentType)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 BindingSet (org.eclipse.rdf4j.query.BindingSet)3 RDFFormat (org.eclipse.rdf4j.rio.RDFFormat)3 RdfConfig (com.denkbares.semanticcore.config.RdfConfig)2 ResultTableModel (com.denkbares.semanticcore.utils.ResultTableModel)2 Strings (com.denkbares.strings.Strings)2