Search in sources :

Example 1 with DefaultMarkupType

use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType 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 DefaultMarkupType

use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.

the class Plugins method addAnnotations.

public static void addAnnotations(Type type, Type[] path) {
    if (type instanceof DefaultMarkupType) {
        DefaultMarkupType markupType = (DefaultMarkupType) type;
        List<Type> childrenTypes = markupType.getChildrenTypes();
        DefaultMarkup markup = markupType.getMarkup();
        Extension[] extensions = PluginManager.getInstance().getExtensions(EXTENDED_PLUGIN_ID, EXTENDED_POINT_Annotation);
        for (Extension extension : ScopeUtils.getMatchingExtensions(extensions, path)) {
            // add the annotation itself to the markup
            String regex = extension.getParameter("regex");
            String name = extension.getName();
            if (Strings.isBlank(regex)) {
                markup.addAnnotation(name, false);
            } else {
                markup.addAnnotation(name, false, regex);
            }
            // add the documentation from the plugin definition
            markup.getAnnotation(name).setDocumentation(extension.getParameter("description"));
            // add children type(s) defined as class attribute(s)
            for (String subTypeClass : extension.getParameters("class")) {
                Type subType = (Type) getNewInstance(extension, subTypeClass);
                markup.addAnnotationContentType(name, subType);
            }
            // set renderer for the annotation
            String renderer = extension.getParameter("renderer");
            if (!Strings.isBlank(renderer)) {
                Renderer pluggedRenderer = (Renderer) getNewInstance(extension, renderer);
                markup.addAnnotationRenderer(name, pluggedRenderer);
            }
            for (Type childTyp : childrenTypes) {
                if (childTyp.getClass().equals(UnknownAnnotationType.class)) {
                    Annotation annotation = markup.getAnnotation(name);
                    type.addChildType(new AnnotationType(annotation));
                    break;
                }
            }
        }
    }
}
Also used : TerminologyExtension(de.knowwe.core.compile.terminology.TerminologyExtension) JPFExtension(com.denkbares.plugin.JPFExtension) Extension(com.denkbares.plugin.Extension) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) AnnotationType(de.knowwe.kdom.defaultMarkup.AnnotationType) Type(de.knowwe.core.kdom.Type) UnknownAnnotationType(de.knowwe.kdom.defaultMarkup.UnknownAnnotationType) AbstractType(de.knowwe.core.kdom.AbstractType) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) DefaultMarkup(de.knowwe.kdom.defaultMarkup.DefaultMarkup) Renderer(de.knowwe.core.kdom.rendering.Renderer) Annotation(de.knowwe.kdom.defaultMarkup.DefaultMarkup.Annotation) AnnotationType(de.knowwe.kdom.defaultMarkup.AnnotationType) UnknownAnnotationType(de.knowwe.kdom.defaultMarkup.UnknownAnnotationType)

Example 3 with DefaultMarkupType

use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.

the class TestCaseUtils method getTestCaseProviders.

public static List<ProviderTriple> getTestCaseProviders(String web, String... kbPackages) {
    PackageManager packageManager = KnowWEUtils.getPackageManager(web);
    List<ProviderTriple> providers = new LinkedList<>();
    for (String kbPackage : kbPackages) {
        Collection<Section<?>> sectionsInPackage = packageManager.getSectionsOfPackage(kbPackage);
        for (Section<?> section : sectionsInPackage) {
            if (!(section.get() instanceof DefaultMarkupType))
                continue;
            D3webCompiler compiler = D3webUtils.getCompiler(section);
            TestCaseProviderStorage testCaseProviderStorage = getTestCaseProviderStorage(compiler, section);
            if (testCaseProviderStorage == null)
                continue;
            for (TestCaseProvider testCaseProvider : testCaseProviderStorage.getTestCaseProviders()) {
                providers.add(new ProviderTriple(testCaseProvider, section, compiler.getCompileSection()));
            }
        }
    }
    return providers;
}
Also used : PackageManager(de.knowwe.core.compile.packaging.PackageManager) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) Section(de.knowwe.core.kdom.parsing.Section) LinkedList(java.util.LinkedList) D3webCompiler(de.d3web.we.knowledgebase.D3webCompiler)

Example 4 with DefaultMarkupType

use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.

the class MatchingAttachmentsRenderer method render.

@Override
public void render(Section<?> section, UserContext user, RenderResult string) {
    Section<DefaultMarkupType> defaultMarkupSection;
    if (section.get() instanceof DefaultMarkupType) {
        defaultMarkupSection = Sections.cast(section, DefaultMarkupType.class);
    } else {
        defaultMarkupSection = Sections.ancestor(section, DefaultMarkupType.class);
    }
    String[] annotations = DefaultMarkupType.getAnnotations(defaultMarkupSection, ANNOTATION_FILE);
    Set<String> attachments = new TreeSet<>();
    for (String s : annotations) {
        try {
            for (WikiAttachment attachment : KnowWEUtils.getAttachments(section.getTitle(), s)) {
                attachments.add(attachment.getPath());
            }
        } catch (IOException e) {
        }
    }
    if (!attachments.isEmpty()) {
        string.append("The following attachments match the specified regular expression(s): \n");
        for (String s : attachments) {
            string.append("# " + s + "\n");
        }
        string.append("\n");
    }
}
Also used : WikiAttachment(de.knowwe.core.wikiConnector.WikiAttachment) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) TreeSet(java.util.TreeSet) IOException(java.io.IOException)

Example 5 with DefaultMarkupType

use of de.knowwe.kdom.defaultMarkup.DefaultMarkupType in project d3web-KnowWE by denkbares.

the class ArticleHasMarkupErrorsTest method execute.

@Override
public Message execute(Article article, String[] args2, String[]... ignores) throws InterruptedException {
    // list all ignored articles
    List<String> ignoredArticles = Stream.of(ignores).flatMap(Stream::of).map(Strings::unquote).collect(Collectors.toList());
    // when this one is ignored -> go on
    if (ignoredArticles.contains(article.getTitle()))
        return new Message(Message.Type.SUCCESS);
    // collect messages for erroneous markups
    Set<String> messages = new TreeSet<>(NumberAwareComparator.CASE_INSENSITIVE);
    List<MessageObject> messageObjects = new ArrayList<>();
    List<Section<?>> sections = article.getRootSection().getChildren();
    for (Section<?> sec : sections) {
        // Check for nested markups
        messageObjects.add(new MessageObject(sec.getTitle(), sec.get().getClass()));
        String text = sec.getText().trim();
        text = text.replaceAll("%%\\(color:.+%%", "");
        if (text.matches("%%[\\s\\S]+%%[\\s\\S]+")) {
            messages.add("In article [" + article.getTitle() + "] the section '" + sec.get().getName() + "' seems to have a nested markup.");
            continue;
        }
        // section is actually perceived as a markup -> it is correct and we can go to the next
        if (sec.get() instanceof DefaultMarkupType)
            continue;
        // text is only a % -> this might be a mistake ?
        if ("%".equals(text)) {
            messages.add("In article [" + article.getTitle() + "]  a lonely % is floating around. This may be a mistake");
            continue;
        }
        // text is not a markup -> we can go to the next
        if (!text.startsWith("%%") && !text.endsWith("%"))
            continue;
        if (text.startsWith(("@progress")))
            continue;
        // Markup is in one line, then no closing % is needed -> we can go to the next
        if (text.startsWith("%%") && !text.contains(System.getProperty("line.separator")))
            continue;
        // Check for missing % at the beginning or end of the markup
        if (!text.matches("%%[\\s\\S]+%")) {
            messages.add("In article [" + article.getTitle() + "] the section '" + sec.get().getName() + "' seems to have a wrong %-Markup. Did you start with '%%' and close the Markup with '%'?");
        }
    }
    return getMessage(messageObjects, messages, Message.Type.ERROR);
}
Also used : Message(de.d3web.testing.Message) ArrayList(java.util.ArrayList) Section(de.knowwe.core.kdom.parsing.Section) TreeSet(java.util.TreeSet) DefaultMarkupType(de.knowwe.kdom.defaultMarkup.DefaultMarkupType) Stream(java.util.stream.Stream) MessageObject(de.d3web.testing.MessageObject)

Aggregations

DefaultMarkupType (de.knowwe.kdom.defaultMarkup.DefaultMarkupType)12 Section (de.knowwe.core.kdom.parsing.Section)4 LinkedList (java.util.LinkedList)3 Extension (com.denkbares.plugin.Extension)2 JPFExtension (com.denkbares.plugin.JPFExtension)2 TerminologyExtension (de.knowwe.core.compile.terminology.TerminologyExtension)2 AbstractType (de.knowwe.core.kdom.AbstractType)2 Type (de.knowwe.core.kdom.Type)2 Message (de.knowwe.core.report.Message)2 AnnotationType (de.knowwe.kdom.defaultMarkup.AnnotationType)2 DefaultMarkup (de.knowwe.kdom.defaultMarkup.DefaultMarkup)2 UnknownAnnotationType (de.knowwe.kdom.defaultMarkup.UnknownAnnotationType)2 TestCaseProvider (de.knowwe.testcases.TestCaseProvider)2 Solution (de.d3web.core.knowledge.terminology.Solution)1 DescribedTestCase (de.d3web.testcase.model.DescribedTestCase)1 Message (de.d3web.testing.Message)1 MessageObject (de.d3web.testing.MessageObject)1 D3webCompiler (de.d3web.we.knowledgebase.D3webCompiler)1 XCLModel (de.d3web.xcl.XCLModel)1 ArticleManager (de.knowwe.core.ArticleManager)1