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>");
}
}
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);
}
}
}
}
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 + "&" + 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.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);
}
}
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;
}
Aggregations