Search in sources :

Example 1 with FrequencyTableEntry

use of annis.service.objects.FrequencyTableEntry in project ANNIS by korpling.

the class FrequencySqlGenerator method conditionsForEntries.

private Multimap<FrequencyTableEntry, String> conditionsForEntries(List<FrequencyTableEntry> frequencyEntries, QueryData queryData, List<QueryNode> alternative) {
    Multimap<FrequencyTableEntry, String> conditions = LinkedHashMultimap.create();
    int i = 1;
    ImmutableMap<String, QueryNode> idxNodeVariables = Maps.uniqueIndex(alternative.iterator(), new Function<QueryNode, String>() {

        @Override
        public String apply(QueryNode input) {
            return input.getVariable();
        }
    });
    for (FrequencyTableEntry e : frequencyEntries) {
        if (e.getType() == FrequencyTableEntryType.meta) {
            List<String> qName = Splitter.on(':').limit(2).omitEmptyStrings().splitToList(e.getKey());
            if (qName.size() == 2) {
                conditions.put(e, "v" + i + ".namespace = '" + escaper.escape(qName.get(0)) + "'");
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(1)) + "'");
            } else {
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(0)) + "'");
            }
            conditions.put(e, "v" + i + ".corpus_ref = solutions.corpus_ref");
        } else {
            // general partition restriction
            conditions.put(e, "v" + i + ".toplevel_corpus IN (" + StringUtils.join(queryData.getCorpusList(), ",") + ")");
            // specificly join on top level corpus
            conditions.put(e, "v" + i + ".toplevel_corpus = solutions.toplevel_corpus");
            // join on node ID
            QueryNode referencedNode = idxNodeVariables.get(e.getReferencedNode());
            if (referencedNode == null) {
                throw new AnnisQLSemanticsException("No such node \"" + e.getReferencedNode() + "\". " + "Your query contains " + alternative.size() + " node(s), make sure no node definition numbers are greater than this number");
            }
            conditions.put(e, "v" + i + ".id = solutions.id" + referencedNode.getId());
            if (e.getType() == FrequencyTableEntryType.span) {
                conditions.put(e, "v" + i + ".n_sample IS TRUE");
            } else if (e.getType() == FrequencyTableEntryType.annotation) {
                // TODO: support namespaces
                // filter by selected key
                conditions.put(e, "v" + i + ".node_annotext LIKE '" + AnnotationConditionProvider.likeEscaper.escape(e.getKey()) + ":%'");
                conditions.put(e, "v" + i + ".n_na_sample IS TRUE");
            }
        }
        i++;
    }
    return conditions;
}
Also used : QueryNode(annis.model.QueryNode) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Example 2 with FrequencyTableEntry

use of annis.service.objects.FrequencyTableEntry in project ANNIS by korpling.

the class QueryController method executeFrequency.

public void executeFrequency(FrequencyQueryPanel panel) {
    // kill old request
    Future freqFuture = state.getExecutedTasks().get(QueryUIState.QueryType.FREQUENCY);
    if (freqFuture != null && !freqFuture.isDone()) {
        freqFuture.cancel(true);
    }
    if ("".equals(state.getAql().getValue())) {
        Notification.show("Empty query", Notification.Type.WARNING_MESSAGE);
        panel.showQueryDefinitionPanel();
        return;
    } else if (state.getSelectedCorpora().getValue().isEmpty()) {
        Notification.show("Please select a corpus", Notification.Type.WARNING_MESSAGE);
        panel.showQueryDefinitionPanel();
        return;
    }
    BeanContainer<Integer, UserGeneratedFrequencyEntry> container = state.getFrequencyTableDefinition();
    FrequencyTableQuery freqDefinition = new FrequencyTableQuery();
    for (Integer id : container.getItemIds()) {
        UserGeneratedFrequencyEntry userGen = container.getItem(id).getBean();
        freqDefinition.add(userGen.toFrequencyTableEntry());
    }
    // additionally add meta data columns
    for (String m : state.getFrequencyMetaData().getValue()) {
        FrequencyTableEntry entry = new FrequencyTableEntry();
        entry.setType(FrequencyTableEntryType.meta);
        entry.setKey(m);
        freqDefinition.add(entry);
    }
    FrequencyQuery query = QueryGenerator.frequency().query(state.getAql().getValue()).corpora(state.getSelectedCorpora().getValue()).def(freqDefinition).build();
    addHistoryEntry(query);
    FrequencyBackgroundJob job = new FrequencyBackgroundJob(ui, query, panel);
    freqFuture = Background.call(job);
    state.getExecutedTasks().put(QueryUIState.QueryType.FREQUENCY, freqFuture);
}
Also used : FrequencyTableQuery(annis.service.objects.FrequencyTableQuery) FrequencyQuery(annis.gui.objects.FrequencyQuery) UserGeneratedFrequencyEntry(annis.gui.frequency.UserGeneratedFrequencyEntry) Future(java.util.concurrent.Future) FrequencyBackgroundJob(annis.gui.controller.FrequencyBackgroundJob) FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Example 3 with FrequencyTableEntry

use of annis.service.objects.FrequencyTableEntry in project ANNIS by korpling.

the class UserGeneratedFrequencyEntry method toFrequencyTableEntry.

/**
 * Converts this object to a proper definition.
 * @return
 */
public FrequencyTableEntry toFrequencyTableEntry() {
    FrequencyTableEntry result = new FrequencyTableEntry();
    result.setReferencedNode(nr);
    if (annotation != null && "tok".equals(annotation)) {
        result.setType(FrequencyTableEntryType.span);
    } else {
        result.setType(FrequencyTableEntryType.annotation);
        result.setKey(annotation);
    }
    return result;
}
Also used : FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Example 4 with FrequencyTableEntry

use of annis.service.objects.FrequencyTableEntry in project ANNIS by korpling.

the class FrequencySqlGenerator method fromClause.

@Override
public String fromClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    FrequencyTableQuery ext;
    List<FrequencyTableQuery> freqQueryData = queryData.getExtensions(FrequencyTableQuery.class);
    Validate.notNull(freqQueryData);
    Validate.notEmpty(freqQueryData);
    ext = freqQueryData.get(0);
    Multimap<FrequencyTableEntry, String> conditions = conditionsForEntries(ext, queryData, alternative);
    StringBuilder sb = new StringBuilder();
    sb.append(indent).append("(\n");
    sb.append(indent);
    sb.append(getSolutionSqlGenerator().toSql(queryData, indent + TABSTOP));
    sb.append(indent).append(") AS solutions\n");
    int i = 1;
    Iterator<FrequencyTableEntry> itEntry = ext.iterator();
    while (itEntry.hasNext()) {
        FrequencyTableEntry e = itEntry.next();
        sb.append(indent).append(TABSTOP);
        sb.append("LEFT JOIN ");
        String tableSql;
        if (e.getType() == FrequencyTableEntryType.meta) {
            tableSql = "corpus_annotation";
        } else {
            tableSql = SelectedFactsFromClauseGenerator.inheritedFactTables(queryData.getCorpusList(), indent);
        }
        sb.append(tableSql);
        sb.append(" AS v").append(i);
        sb.append(" ON (");
        sb.append(Joiner.on(" AND ").join(conditions.get(e)));
        sb.append(")\n");
        i++;
    }
    return sb.toString();
}
Also used : FrequencyTableQuery(annis.service.objects.FrequencyTableQuery) FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Example 5 with FrequencyTableEntry

use of annis.service.objects.FrequencyTableEntry in project ANNIS by korpling.

the class FrequencySqlGenerator method selectClause.

@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    TableAccessStrategy tas = tables(null);
    FrequencyTableQuery ext;
    List<FrequencyTableQuery> freqQueryData = queryData.getExtensions(FrequencyTableQuery.class);
    Validate.notNull(freqQueryData);
    Validate.notEmpty(freqQueryData);
    ext = freqQueryData.get(0);
    StringBuilder sb = new StringBuilder();
    int i = 1;
    for (FrequencyTableEntry e : ext) {
        if (e.getType() == FrequencyTableEntryType.annotation) {
            sb.append("v").append(i).append(".").append(tas.columnName(NODE_ANNOTATION_TABLE, "qannotext"));
        } else if (e.getType() == FrequencyTableEntryType.span) {
            sb.append("('annis:tok:' || ").append("v").append(i).append(".").append(tas.columnName(NODE_TABLE, "span)"));
        } else if (e.getType() == FrequencyTableEntryType.meta) {
            sb.append("('annis_meta:'").append(" || COALESCE(v").append(i).append(".").append("namespace").append(", '')").append(" || v").append(i).append(".").append("\"name\"").append(" || ':'").append(" || v").append(i).append(".").append("\"value\")");
        }
        sb.append(" AS value").append(i).append(", ");
        i++;
    }
    sb.append("count(*) AS \"count\"");
    return sb.toString();
}
Also used : FrequencyTableQuery(annis.service.objects.FrequencyTableQuery) FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Aggregations

FrequencyTableEntry (annis.service.objects.FrequencyTableEntry)6 FrequencyTableQuery (annis.service.objects.FrequencyTableQuery)3 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)1 FrequencyBackgroundJob (annis.gui.controller.FrequencyBackgroundJob)1 UserGeneratedFrequencyEntry (annis.gui.frequency.UserGeneratedFrequencyEntry)1 FrequencyQuery (annis.gui.objects.FrequencyQuery)1 QueryNode (annis.model.QueryNode)1 FrequencyTable (annis.service.objects.FrequencyTable)1 Table (com.vaadin.ui.Table)1 Future (java.util.concurrent.Future)1