Search in sources :

Example 26 with QueryResultRow

use of de.catma.queryengine.result.QueryResultRow in project catma by forTEXT.

the class QueryResultPanel method addPropertiesAsColumnsTagBasedRootItems.

void addPropertiesAsColumnsTagBasedRootItems(QueryResult result) {
    try {
        HashMap<String, QueryResultRowArray> rowsGroupedByTagInstance = new HashMap<String, QueryResultRowArray>();
        for (QueryResultRow row : result) {
            if (row instanceof TagQueryResultRow) {
                TagQueryResultRow tRow = (TagQueryResultRow) row;
                QueryResultRowArray rows = rowsGroupedByTagInstance.get(tRow.getTagInstanceId());
                if (rows == null) {
                    rows = new QueryResultRowArray();
                    rowsGroupedByTagInstance.put(tRow.getTagInstanceId(), rows);
                }
                rows.add(tRow);
                if (tRow.getPropertyName() != null) {
                    propertyNames.add(tRow.getPropertyName());
                }
            }
        }
        for (Map.Entry<String, QueryResultRowArray> entry : rowsGroupedByTagInstance.entrySet()) {
            QueryResultRowArray rows = entry.getValue();
            TagQueryResultRow masterRow = (TagQueryResultRow) rows.get(0);
            KwicProvider kwicProvider = kwicProviderCache.get(masterRow.getSourceDocumentId());
            TagDefinition tagDefinition = project.getTagManager().getTagLibrary().getTagDefinition(masterRow.getTagDefinitionId());
            KwicPropertiesAsColumnsQueryResultRowItem item = new KwicPropertiesAsColumnsQueryResultRowItem(rows, AnnotatedTextProvider.buildAnnotatedText(new ArrayList<>(masterRow.getRanges()), kwicProvider, tagDefinition), AnnotatedTextProvider.buildAnnotatedKeywordInContext(new ArrayList<>(masterRow.getRanges()), kwicProvider, tagDefinition, masterRow.getTagDefinitionPath()), kwicProvider.getSourceDocumentName(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(masterRow.getMarkupCollectionId()).getName());
            if (!propertiesAsColumnsTagBasedTreeData.contains(item)) {
                propertiesAsColumnsTagBasedTreeData.addItem(null, item);
            }
        }
        tokenCount = propertiesAsColumnsTagBasedTreeData.getRootItems().size();
    } catch (Exception e) {
        ((ErrorHandler) UI.getCurrent()).showAndLogError("error adding query result", e);
    }
}
Also used : TagDefinition(de.catma.tag.TagDefinition) HashMap(java.util.HashMap) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) ArrayList(java.util.ArrayList) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray) Map(java.util.Map) HashMap(java.util.HashMap)

Example 27 with QueryResultRow

use of de.catma.queryengine.result.QueryResultRow in project catma by forTEXT.

the class DoubleTreePanel method setSelectedQueryResultRows.

@Override
public void setSelectedQueryResultRows(Iterable<QueryResultRow> selectedRows) {
    kwics.clear();
    if ((selectedRows.iterator().next() instanceof TagQueryResultRow) && displaySettings.equals(DisplaySetting.GROUPED_BY_TAG)) {
        for (QueryResultRow row : selectedRows) {
            TagQueryResultRow tqrr = (TagQueryResultRow) row;
            String tagPath = tqrr.getTagDefinitionPath().replace("/", "");
            KwicProvider kwicProvider = null;
            try {
                kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
            } catch (ExecutionException e1) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing group by tag", e1);
            }
            KeywordInSpanContext kwic = null;
            try {
                kwic = kwicProvider.getKwic(row.getRange(), contextSize);
                KeywordInSpanContext newKwic = new KeywordInSpanContext(tagPath, kwic.getKwic(), kwic.getKwicSourceRange(), kwic.getRelativeKeywordStartPos(), kwic.isRightToLeft(), kwic.getSpanContext());
                kwics.add(newKwic);
            } catch (IOException e) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing group by tag", e);
            }
        }
        doubleTree.setupFromArrays(kwics, true);
    } else {
        for (QueryResultRow row : selectedRows) {
            KwicProvider kwicProvider = null;
            try {
                kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
            } catch (ExecutionException e1) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing selected data", e1);
            }
            KeywordInSpanContext kwic = null;
            try {
                kwic = kwicProvider.getKwic(row.getRange(), contextSize);
            } catch (IOException e) {
                ((CatmaApplication) UI.getCurrent()).showAndLogError("Error visualizing selected data", e);
            }
            kwics.add(kwic);
        }
        doubleTree.setupFromArrays(kwics, true);
    }
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) KwicProvider(de.catma.indexer.KwicProvider) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 28 with QueryResultRow

use of de.catma.queryengine.result.QueryResultRow in project catma by forTEXT.

the class CSVExportFlatStreamSource method getStream.

@Override
public InputStream getStream() {
    final QueryResult queryResult = queryResultSupplier.get();
    final PipedInputStream in = new PipedInputStream();
    final UI ui = UI.getCurrent();
    final Lock lock = new ReentrantLock();
    final Condition sending = lock.newCondition();
    lock.lock();
    backgroundServiceProvider.submit("csv-export", new DefaultProgressCallable<Void>() {

        @Override
        public Void call() throws Exception {
            PipedOutputStream out = new PipedOutputStream(in);
            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            LoadingCache<String, String> colorCache = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {

                @Override
                public String load(String tagDefinitionId) throws Exception {
                    return "#" + ColorConverter.toHex(project.getTagManager().getTagLibrary().getTagDefinition(tagDefinitionId).getColor());
                }
            });
            try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';'))) {
                for (QueryResultRow row : queryResult) {
                    KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
                    if (row instanceof TagQueryResultRow) {
                        TagQueryResultRow tRow = (TagQueryResultRow) row;
                        List<Range> mergedRanges = Range.mergeRanges(new TreeSet<>((tRow).getRanges()));
                        for (Range range : mergedRanges) {
                            KeywordInSpanContext kwic = kwicProvider.getKwic(range, 5);
                            csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), range.getStartPoint(), range.getEndPoint(), tRow.getMarkupCollectionId(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(tRow.getMarkupCollectionId()).toString(), tRow.getTagDefinitionPath(), tRow.getTagDefinitionVersion(), colorCache.get(tRow.getTagDefinitionId()), tRow.getTagInstanceId(), tRow.getPropertyDefinitionId(), tRow.getPropertyName(), tRow.getPropertyValue());
                        }
                    } else {
                        KeywordInSpanContext kwic = kwicProvider.getKwic(row.getRange(), 5);
                        csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), row.getRange().getStartPoint(), row.getRange().getEndPoint());
                    }
                    csvPrinter.flush();
                    lock.lock();
                    try {
                        sending.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            }
            // intended
            return null;
        }
    }, new ExecutionListener<Void>() {

        @Override
        public void done(Void result) {
        // noop
        }

        @Override
        public void error(Throwable t) {
            ((ErrorHandler) ui).showAndLogError("Error export data to CSV!", t);
        }
    });
    // waiting on the background thread to send data to the pipe
    try {
        try {
            sending.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error while waiting on CSV export!", e1);
        }
    } finally {
        lock.unlock();
    }
    return in;
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) PipedOutputStream(java.io.PipedOutputStream) CSVPrinter(org.apache.commons.csv.CSVPrinter) QueryResult(de.catma.queryengine.result.QueryResult) UI(com.vaadin.ui.UI) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TreeSet(java.util.TreeSet) KwicProvider(de.catma.indexer.KwicProvider) LoadingCache(com.google.common.cache.LoadingCache) List(java.util.List) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) PipedInputStream(java.io.PipedInputStream) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) Range(de.catma.document.Range) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) OutputStreamWriter(java.io.OutputStreamWriter) CacheLoader(com.google.common.cache.CacheLoader)

Example 29 with QueryResultRow

use of de.catma.queryengine.result.QueryResultRow in project catma by forTEXT.

the class TPGraphProjectIndexer method searchCollocation.

@Override
public QueryResult searchCollocation(QueryId queryId, QueryResult baseResult, QueryResult collocationConditionResult, int spanContextSize, SpanDirection direction) throws IOException {
    int baseResultSize = baseResult.size();
    int collocConditionResultSize = collocationConditionResult.size();
    boolean swapCollocationDirection = baseResultSize > collocConditionResultSize;
    // swap to reduce the amount of span context computation
    if (swapCollocationDirection) {
        QueryResult bufferResult = baseResult;
        baseResult = collocationConditionResult;
        collocationConditionResult = bufferResult;
    }
    Multimap<String, QueryResultRow> collocConditionResultBySourceDocumentId = ArrayListMultimap.create();
    collocationConditionResult.forEach(row -> collocConditionResultBySourceDocumentId.put(row.getSourceDocumentId(), row));
    QueryResultRowArray matchingBaseRows = new QueryResultRowArray();
    QueryResultRowArray matchingCollocConditionRows = new QueryResultRowArray();
    for (QueryResultRow row : baseResult) {
        if (collocConditionResultBySourceDocumentId.containsKey(row.getSourceDocumentId())) {
            SpanContext spanContext = getSpanContextFor(row.getSourceDocumentId(), row.getRange(), spanContextSize, direction);
            boolean baseMatch = matchingBaseRows.contains(row);
            for (QueryResultRow collocConditionRow : collocConditionResultBySourceDocumentId.get(row.getSourceDocumentId())) {
                boolean collocMatch = matchingCollocConditionRows.contains(collocConditionRow);
                if (!baseMatch || !collocMatch) {
                    if (spanContext.hasOverlappingRange(collocConditionRow.getRanges(), direction)) {
                        if (!baseMatch) {
                            matchingBaseRows.add(row);
                            baseMatch = true;
                        }
                        if (!collocMatch) {
                            matchingCollocConditionRows.add(collocConditionRow);
                            collocMatch = true;
                        }
                    }
                }
            }
        }
    }
    // swap back
    if (swapCollocationDirection) {
        QueryResultRowArray bufferResult = matchingBaseRows;
        matchingBaseRows = matchingCollocConditionRows;
        matchingCollocConditionRows = bufferResult;
    }
    return matchingBaseRows;
}
Also used : QueryResult(de.catma.queryengine.result.QueryResult) SpanContext(de.catma.indexer.SpanContext) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) CommentQueryResultRow(de.catma.queryengine.result.CommentQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray)

Aggregations

QueryResultRow (de.catma.queryengine.result.QueryResultRow)29 TagQueryResultRow (de.catma.queryengine.result.TagQueryResultRow)22 QueryResultRowArray (de.catma.queryengine.result.QueryResultRowArray)18 SourceDocument (de.catma.document.source.SourceDocument)13 Range (de.catma.document.Range)12 KwicProvider (de.catma.indexer.KwicProvider)10 Project (de.catma.project.Project)10 QueryResult (de.catma.queryengine.result.QueryResult)10 ArrayList (java.util.ArrayList)10 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)8 TagDefinition (de.catma.tag.TagDefinition)8 UI (com.vaadin.ui.UI)6 HashSet (java.util.HashSet)6 CacheLoader (com.google.common.cache.CacheLoader)5 LoadingCache (com.google.common.cache.LoadingCache)5 Query (com.vaadin.data.provider.Query)5 AnnotationCollectionManager (de.catma.document.annotation.AnnotationCollectionManager)5 List (java.util.List)5 Map (java.util.Map)5 CacheBuilder (com.google.common.cache.CacheBuilder)4