Search in sources :

Example 21 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class AnnotationExistenceValidator method transform.

@Override
public QueryData transform(QueryData data) {
    List<Long> corpusList = data.getCorpusList();
    if (queryDao != null && (corpusList != null) && !corpusList.isEmpty()) {
        // get first corpus name
        // List<AnnisCorpus> mycorpora = queryDao.listCorpora();
        // String firstcorpusname =  mycorpora.get(0).getName();
        Set<String> result = new TreeSet<>();
        /*get a list of all annotations in a similar way that TigerQueryBuilder gets it through 
       QueryServiceImpl in queryDao.listAnnotations()*/
        List<AnnisAttribute> atts = queryDao.listAnnotations(corpusList, false, true);
        // among them, get only node annotations
        for (AnnisAttribute a : atts) {
            if (a.getType() == AnnisAttribute.Type.node) {
                List<String> splitted = Splitter.on(":").limit(2).omitEmptyStrings().trimResults().splitToList(a.getName());
                result.add(splitted.get(splitted.size() - 1));
            // result is a set of strings of available annotations
            }
        }
        List<AqlParseError> errors = new LinkedList<>();
        for (List<QueryNode> alternative : data.getAlternatives()) {
            for (QueryNode n : alternative) {
                Set<QueryAnnotation> m = n.getNodeAnnotations();
                // always get the first one
                if (!m.isEmpty()) {
                    // name is the node name string, ready to check if name is in the list of
                    // available names
                    String name = m.iterator().next().getName();
                    if (!result.contains(name)) {
                        errors.add(new AqlParseError(n, "\"" + name + "\"" + " is not a valid annotation name in selected corpora "));
                    }
                }
            }
        }
        if (!errors.isEmpty()) {
            throw new AnnisQLSemanticsException("Invalid annotation names detected.", errors);
        }
    }
    return data;
}
Also used : QueryAnnotation(annis.model.QueryAnnotation) AnnisAttribute(annis.service.objects.AnnisAttribute) AqlParseError(annis.model.AqlParseError) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) QueryNode(annis.model.QueryNode)

Example 22 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class TransitivePrecedenceOptimizer method propagateNodePrecedence.

private void propagateNodePrecedence(QueryNode initialNode, QueryNode currentNode, Set<Long> visitedNodes, Map<Long, Set<Precedence>> outJoins, Range range, String segmentation) {
    visitedNodes.add(currentNode.getId());
    Map<QueryNode, Range> nextNodes = new HashMap<>();
    // iterator over all outgoing precedence joins
    List<Join> originalJoins = new LinkedList<>(currentNode.getOutgoingJoins());
    for (Join join : originalJoins) {
        if (join instanceof Precedence) {
            Precedence p = (Precedence) join;
            if ((segmentation == null && p.getSegmentationName() == null) || (segmentation != null && segmentation.equals(p.getSegmentationName()))) {
                Range newRange;
                if (range == null) {
                    // create a new range at initial node
                    newRange = new Range(p.getMinDistance(), p.getMaxDistance());
                } else {
                    // calculate the new range depending on old one
                    if (currentNode.isToken() == false || (range.getMin() == 0 && range.getMax() == 0) || (p.getMinDistance() == 0 && p.getMaxDistance() == 0)) {
                        // use unlimited range since
                        // a) the node could also be a
                        // span covering more than one token,
                        // b) the original constraint is an unlimited range
                        newRange = new Range(0, 0);
                    } else {
                        // add the new precendence values to the old one
                        newRange = new Range(range.getMin() + p.getMinDistance(), range.getMax() + p.getMaxDistance());
                    }
                }
                // put the target node in the list of nodes to check if not visited yet
                if (!visitedNodes.contains(p.getTarget().getId())) {
                    nextNodes.put(p.getTarget(), newRange);
                    Precedence newJoin = new Precedence(p.getTarget(), newRange.getMin(), newRange.getMax());
                    Set<Precedence> existingJoins = outJoins.get(initialNode.getId());
                    // only add if this join is not already included
                    // (which is always true for the initial node)
                    // and the join is more restrictive than any previous one
                    boolean moreRestrictive = true;
                    for (Precedence oldJoin : existingJoins) {
                        if (oldJoin.getTarget() == newJoin.getTarget()) {
                            if (!joinMoreRestrictive(oldJoin, newJoin)) {
                                moreRestrictive = false;
                                break;
                            }
                        }
                    }
                    if (moreRestrictive) {
                        // add newly created discovered transitive precedence
                        initialNode.addOutgoingJoin(newJoin);
                        existingJoins.add(newJoin);
                    }
                }
            // end if not visited yet
            }
        // end if segmentation matches
        }
    // end if is precedence join
    }
    for (Map.Entry<QueryNode, Range> e : nextNodes.entrySet()) {
        // call us recursivly but remember the range
        propagateNodePrecedence(initialNode, e.getKey(), visitedNodes, outJoins, e.getValue(), segmentation);
    }
}
Also used : HashMap(java.util.HashMap) QueryNode(annis.model.QueryNode) Join(annis.model.Join) Range(annis.model.QueryNode.Range) Precedence(annis.sqlgen.model.Precedence) Map(java.util.Map) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList)

Example 23 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class SampleWhereClause method whereConditions.

@Override
public Set<String> whereConditions(QueryData queryData, List<QueryNode> alternative, String indent) {
    Set<String> conditions = new HashSet<>();
    List<Long> corpusList = queryData.getCorpusList();
    List<Long> documents = queryData.getDocuments();
    for (QueryNode node : alternative) {
        conditions.addAll(whereConditions(node, corpusList, documents));
    }
    return conditions;
}
Also used : QueryNode(annis.model.QueryNode) HashSet(java.util.HashSet)

Example 24 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class SelectedFactsFromClauseGenerator method fromClause.

@Override
public String fromClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    List<String> clauses = new LinkedList<>();
    for (QueryNode node : alternative) {
        TableAccessStrategy tas = tables(node);
        String aliasName = TableAccessStrategy.aliasedTable(node, tas.getTableAliases(), TableAccessStrategy.FACTS_TABLE, 1);
        clauses.add(inheritedFactTables(queryData.getCorpusList(), indent) + " AS " + aliasName);
    }
    return Joiner.on(",\n" + indent + AbstractSqlGenerator.TABSTOP).join(clauses);
}
Also used : QueryNode(annis.model.QueryNode) LinkedList(java.util.LinkedList)

Example 25 with QueryNode

use of annis.model.QueryNode in project ANNIS by korpling.

the class FindSqlGenerator method selectClause.

@Override
public String selectClause(QueryData queryData, List<QueryNode> alternative, String indent) {
    StringBuilder sb = new StringBuilder();
    String indent2 = indent + TABSTOP;
    sb.append(indent2).append("solution.*,\n");
    // add node annotation namespace and name for each query node
    int i = 0;
    Iterator<QueryNode> itNodes = alternative.iterator();
    while (itNodes.hasNext()) {
        i++;
        QueryNode n = itNodes.next();
        TableAccessStrategy tas = tables(n);
        sb.append(indent2).append(annoCondition.getNodeAnnoNamespaceSQL(tas)).append(" AS node_annotation_ns").append(i).append(",\n");
        sb.append(indent2).append(annoCondition.getNodeAnnoNameSQL(tas)).append(" AS node_annotation_name").append(i).append(",\n");
        // corpus path is only needed once
        sb.append(indent2).append("c.path_name AS path_name");
        if (itNodes.hasNext()) {
            sb.append(",\n");
        }
    }
    return sb.toString();
}
Also used : QueryNode(annis.model.QueryNode)

Aggregations

QueryNode (annis.model.QueryNode)67 Join (annis.model.Join)12 LinkedList (java.util.LinkedList)11 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)10 ArrayList (java.util.ArrayList)8 QueryAnnotation (annis.model.QueryAnnotation)7 Precedence (annis.sqlgen.model.Precedence)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Dominance (annis.sqlgen.model.Dominance)5 PointingRelation (annis.sqlgen.model.PointingRelation)5 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 TreeSet (java.util.TreeSet)5 Test (org.junit.Test)5 Identical (annis.sqlgen.model.Identical)4 LeftDominance (annis.sqlgen.model.LeftDominance)4 Near (annis.sqlgen.model.Near)4 RightDominance (annis.sqlgen.model.RightDominance)4 TestUtils.uniqueLong (annis.test.TestUtils.uniqueLong)4 QueryData (annis.ql.parser.QueryData)3