Search in sources :

Example 1 with RenderType

use of de.tudarmstadt.ukp.clarin.webanno.brat.metrics.BratMetrics.RenderType in project webanno by webanno.

the class BratAnnotationEditor method bratRenderCommand.

private String bratRenderCommand(JCas aJCas) {
    StopWatch timer = new StopWatch();
    timer.start();
    GetDocumentResponse response = new GetDocumentResponse();
    render(response, aJCas);
    String json = toJson(response);
    // By default, we do a full rendering...
    RenderType renderType = RenderType.FULL;
    String cmd = "renderData";
    String data = json;
    // ... try to render diff
    String diff = null;
    JsonNode current = null;
    JsonNode previous = null;
    try {
        ObjectMapper mapper = JSONUtil.getJsonConverter().getObjectMapper();
        current = mapper.readTree(json);
        previous = lastRenderedJson != null ? mapper.readTree(lastRenderedJson) : null;
    } catch (IOException e) {
        LOG.error("Unable to generate diff, falling back to full render.", e);
    // Fall-through
    }
    if (previous != null && current != null) {
        diff = JsonDiff.asJson(previous, current).toString();
        // pages, the patch usually ends up being twice as large as the full data.
        if (diff.length() < json.length()) {
            cmd = "renderDataPatch";
            data = diff;
            renderType = RenderType.DIFFERENTIAL;
        }
    // LOG.info("Diff:  " + diff);
    // LOG.info("Full: {}   Patch: {}   Diff time: {}", json.length(), diff.length(), timer);
    }
    // Storing the last rendered JSON as string because JsonNodes are not serializable.
    lastRenderedJson = json;
    timer.stop();
    metrics.renderComplete(renderType, timer.getTime(), json, diff);
    return "Wicket.$('" + vis.getMarkupId() + "').dispatcher.post('" + cmd + "', [" + data + "]);";
}
Also used : GetDocumentResponse(de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) RenderType(de.tudarmstadt.ukp.clarin.webanno.brat.metrics.BratMetrics.RenderType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) StopWatch(org.apache.commons.lang3.time.StopWatch)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GetDocumentResponse (de.tudarmstadt.ukp.clarin.webanno.brat.message.GetDocumentResponse)1 RenderType (de.tudarmstadt.ukp.clarin.webanno.brat.metrics.BratMetrics.RenderType)1 IOException (java.io.IOException)1 StopWatch (org.apache.commons.lang3.time.StopWatch)1