Search in sources :

Example 6 with AqlParseError

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

the class AnnisParserAntlr method dumpTree.

public String dumpTree(String aql) {
    AqlLexer lexer = new AqlLexer(new ANTLRInputStream(aql));
    AqlParser parser = new AqlParser(new CommonTokenStream(lexer));
    final List<AqlParseError> errors = new LinkedList<>();
    parser.removeErrorListeners();
    parser.addErrorListener(new AqlParseErrorListener(errors));
    ParseTree tree = parser.start();
    if (errors.isEmpty()) {
        return tree.toStringTree();
    } else {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AqlLexer(annis.ql.AqlLexer) AqlParseError(annis.model.AqlParseError) AqlParser(annis.ql.AqlParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) LinkedList(java.util.LinkedList) ParseTree(org.antlr.v4.runtime.tree.ParseTree) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException)

Example 7 with AqlParseError

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

the class SemanticValidator method checkAlternative.

public void checkAlternative(QueryData data, List<QueryNode> alternative, int alternativeIndex, boolean queryWasNormalized) {
    // check if there is at least one search expression
    if (alternative.isEmpty()) {
        throw new AnnisQLSemanticsException("Missing search expression.");
    }
    // there are not linguistic binary relations allowed if there is only one node
    if (alternative.size() == 1) {
        QueryNode n = alternative.get(0);
        for (Join j : n.getOutgoingJoins()) {
            if (j.getTarget() != null) {
                throw new AnnisQLSemanticsException(j.getParseLocation(), "No binary linguistic relations allowed if there is only one node in query.");
            }
        }
    }
    // get all nodes connected to the first one
    Multimap<Long, QueryNode> connected = calculateConnected(alternative);
    Set<Long> transitiveHull = new HashSet<>();
    transitiveHull.add(alternative.get(0).getId());
    createTransitiveHull(alternative.get(0), connected, transitiveHull);
    Multiset<String> variableNames = TreeMultiset.create();
    Set<Long> unconnectedNodes = new HashSet<>();
    for (QueryNode n : alternative) {
        unconnectedNodes.add(n.getId());
        variableNames.add(n.getVariable());
    }
    unconnectedNodes.removeAll(transitiveHull);
    // check if each node is contained in the connected nodes
    if (!unconnectedNodes.isEmpty()) {
        List<AqlParseError> errors = new LinkedList<>();
        for (QueryNode n : alternative) {
            if (unconnectedNodes.contains(n.getId())) {
                errors.add(new AqlParseError(n, "variable \"" + n.getVariable() + "\" not bound (use linguistic operators)"));
            }
        }
        if (!errors.isEmpty()) {
            if (queryWasNormalized) {
                // add the normalized query as "error" so the user is able to see it
                errors.add(new AqlParseError("Normalized query is: \n" + data.toAQL()));
            }
            throw new AnnisQLSemanticsException("Not all variables bound", errors);
        }
    }
    // check if any variable name was given more than once
    List<String> invalidNames = new LinkedList<>();
    for (Multiset.Entry<String> e : variableNames.entrySet()) {
        if (e.getCount() > 1) {
            invalidNames.add(e.getElement());
        }
    }
    if (!invalidNames.isEmpty()) {
        throw new AnnisQLSemanticsException("The following variable names are " + "used for more than one node: " + Joiner.on(", ").join(invalidNames) + "\nNormalized Query is: \n" + data.toAQL());
    }
    // check no non-reflexive operator is used with the same operands
    for (QueryNode source : alternative) {
        for (Join join : source.getOutgoingJoins()) {
            if (join instanceof Inclusion || join instanceof SameSpan || join instanceof Overlap || join instanceof RightOverlap || join instanceof LeftOverlap || join instanceof RightAlignment || join instanceof LeftAlignment) {
                if (source.equals(join.getTarget())) {
                    throw new AnnisQLSemanticsException(join, "Not-reflexive operator used with the same node as argument.");
                }
            }
        }
    }
}
Also used : Inclusion(annis.sqlgen.model.Inclusion) SameSpan(annis.sqlgen.model.SameSpan) RightAlignment(annis.sqlgen.model.RightAlignment) AqlParseError(annis.model.AqlParseError) RightOverlap(annis.sqlgen.model.RightOverlap) LeftAlignment(annis.sqlgen.model.LeftAlignment) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) NonBindingJoin(annis.sqlgen.model.NonBindingJoin) Join(annis.model.Join) RightOverlap(annis.sqlgen.model.RightOverlap) Overlap(annis.sqlgen.model.Overlap) LeftOverlap(annis.sqlgen.model.LeftOverlap) LinkedList(java.util.LinkedList) QueryNode(annis.model.QueryNode) LeftOverlap(annis.sqlgen.model.LeftOverlap) Multiset(com.google.common.collect.Multiset) TreeMultiset(com.google.common.collect.TreeMultiset) HashSet(java.util.HashSet)

Aggregations

AqlParseError (annis.model.AqlParseError)7 LinkedList (java.util.LinkedList)5 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)3 AnnisQLSyntaxException (annis.exceptions.AnnisQLSyntaxException)2 QueryNode (annis.model.QueryNode)2 AqlLexer (annis.ql.AqlLexer)2 AqlParser (annis.ql.AqlParser)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 QueryPanel (annis.gui.controlpanel.QueryPanel)1 FrequencyQueryPanel (annis.gui.frequency.FrequencyQueryPanel)1 PagingComponent (annis.gui.paging.PagingComponent)1 Join (annis.model.Join)1 QueryAnnotation (annis.model.QueryAnnotation)1 RawAqlPreParser (annis.ql.RawAqlPreParser)1 AnnisAttribute (annis.service.objects.AnnisAttribute)1 Match (annis.service.objects.Match)1 MatchGroup (annis.service.objects.MatchGroup)1