Search in sources :

Example 1 with Inclusion

use of annis.sqlgen.model.Inclusion in project ANNIS by korpling.

the class TestDefaultWhereClauseGenerator method shouldOptimizeInclusion.

/**
 * Inclusion benefits from two-sided boundaries for both left and right.
 */
@Test
public void shouldOptimizeInclusion() {
    // given
    generator.setOptimizeInclusion(true);
    // when
    node23.addOutgoingJoin(new Inclusion(node42));
    checkWhereConditions(join("=", "_node23.text_ref", "_node42.text_ref"), join("<=", "_node23.left_token", "_node42.left_token"), join("<=", "_node42.left_token", "_node23.right_token"), join(">=", "_node23.right_token", "_node42.right_token"), join(">=", "_node42.right_token", "_node23.left_token"), join("<>", "_node23.id", "_node42.id"));
}
Also used : Inclusion(annis.sqlgen.model.Inclusion) Test(org.junit.Test)

Example 2 with Inclusion

use of annis.sqlgen.model.Inclusion in project ANNIS by korpling.

the class TestDefaultWhereClauseGenerator method shouldGenerateWhereConditionsForInclusion.

/**
 * WHERE conditions for inclusion (_i_).
 */
@Test
public void shouldGenerateWhereConditionsForInclusion() {
    node23.addOutgoingJoin(new Inclusion(node42));
    checkWhereConditions(join("=", "_node23.text_ref", "_node42.text_ref"), join("<=", "_node23.left_token", "_node42.left_token"), join(">=", "_node23.right_token", "_node42.right_token"), join("<>", "_node23.id", "_node42.id"));
}
Also used : Inclusion(annis.sqlgen.model.Inclusion) Test(org.junit.Test)

Example 3 with Inclusion

use of annis.sqlgen.model.Inclusion in project ANNIS by korpling.

the class AbstractWhereClauseGenerator method whereConditions.

@Override
public Set<String> whereConditions(QueryData queryData, List<QueryNode> alternative, String indent) {
    List<String> conditions = new ArrayList<>();
    for (QueryNode node : alternative) {
        // node constraints
        if (node.getSpannedText() != null) {
            addSpanConditions(conditions, queryData, node);
        }
        if (node.isToken()) {
            addIsTokenConditions(conditions, queryData, node);
        }
        if (node.isRoot()) {
            addIsRootConditions(conditions, queryData, node);
        }
        if (node.getArity() != null) {
            addNodeArityConditions(conditions, queryData, node);
        }
        if (node.getTokenArity() != null) {
            addTokenArityConditions(conditions, queryData, node);
        }
        // node joins
        for (Join join : node.getOutgoingJoins()) {
            QueryNode target = join.getTarget();
            if (join instanceof SameSpan) {
                addSameSpanConditions(conditions, node, target, (SameSpan) join, queryData);
            } else if (join instanceof Identical) {
                addIdenticalConditions(conditions, node, target, (Identical) join, queryData);
            } else if (join instanceof LeftAlignment) {
                addLeftAlignmentConditions(conditions, node, target, (LeftAlignment) join, queryData);
            } else if (join instanceof RightAlignment) {
                addRightAlignmentConditions(conditions, node, target, (RightAlignment) join, queryData);
            } else if (join instanceof Inclusion) {
                addInclusionConditions(conditions, node, target, (Inclusion) join, queryData);
            } else if (join instanceof Overlap) {
                addOverlapConditions(conditions, node, target, (Overlap) join, queryData);
            } else if (join instanceof LeftOverlap) {
                addLeftOverlapConditions(conditions, target, node, (LeftOverlap) join, queryData);
            } else if (join instanceof RightOverlap) {
                addRightOverlapConditions(conditions, target, node, (RightOverlap) join, queryData);
            } else if (join instanceof Precedence) {
                addPrecedenceConditions(conditions, node, target, (Precedence) join, queryData);
            } else if (join instanceof Near) {
                addNearConditions(conditions, node, target, (Near) join, queryData);
            } else if (join instanceof Sibling) {
                addSiblingConditions(conditions, node, target, (Sibling) join, queryData);
            } else if (join instanceof CommonAncestor) {
                addCommonAncestorConditions(conditions, node, target, (CommonAncestor) join, queryData);
            } else if (join instanceof LeftDominance) {
                addLeftDominanceConditions(conditions, node, target, (LeftDominance) join, queryData);
            } else if (join instanceof RightDominance) {
                addRightDominanceConditions(conditions, node, target, (RightDominance) join, queryData);
            } else if (join instanceof Dominance) {
                addDominanceConditions(conditions, node, target, (Dominance) join, queryData);
            } else if (join instanceof PointingRelation) {
                addPointingRelationConditions(conditions, node, target, (PointingRelation) join, queryData);
            } else if (join instanceof EqualValue) {
                addEqualValueConditions(conditions, node, target, (EqualValue) join, queryData);
            } else if (join instanceof NotEqualValue) {
                addNotEqualValueConditions(conditions, node, target, (NotEqualValue) join, queryData);
            }
        }
        // node annotations
        int i = 0;
        for (QueryAnnotation annotation : node.getNodeAnnotations()) {
            ++i;
            addAnnotationConditions(conditions, node, i, annotation, NODE_ANNOTATION_TABLE, queryData);
        }
        // relation annotations
        int j = 0;
        for (QueryAnnotation annotation : node.getEdgeAnnotations()) {
            ++j;
            addAnnotationConditions(conditions, node, j, annotation, EDGE_ANNOTATION_TABLE, queryData);
        }
    }
    return new HashSet<>(conditions);
}
Also used : Identical(annis.sqlgen.model.Identical) Inclusion(annis.sqlgen.model.Inclusion) SameSpan(annis.sqlgen.model.SameSpan) RightAlignment(annis.sqlgen.model.RightAlignment) ArrayList(java.util.ArrayList) LeftAlignment(annis.sqlgen.model.LeftAlignment) RightOverlap(annis.sqlgen.model.RightOverlap) Overlap(annis.sqlgen.model.Overlap) LeftOverlap(annis.sqlgen.model.LeftOverlap) Sibling(annis.sqlgen.model.Sibling) CommonAncestor(annis.sqlgen.model.CommonAncestor) NotEqualValue(annis.sqlgen.model.NotEqualValue) HashSet(java.util.HashSet) PointingRelation(annis.sqlgen.model.PointingRelation) EqualValue(annis.sqlgen.model.EqualValue) NotEqualValue(annis.sqlgen.model.NotEqualValue) QueryAnnotation(annis.model.QueryAnnotation) RightOverlap(annis.sqlgen.model.RightOverlap) Join(annis.model.Join) RightDominance(annis.sqlgen.model.RightDominance) Dominance(annis.sqlgen.model.Dominance) LeftDominance(annis.sqlgen.model.LeftDominance) RightDominance(annis.sqlgen.model.RightDominance) Near(annis.sqlgen.model.Near) LeftDominance(annis.sqlgen.model.LeftDominance) QueryNode(annis.model.QueryNode) LeftOverlap(annis.sqlgen.model.LeftOverlap) Precedence(annis.sqlgen.model.Precedence)

Example 4 with Inclusion

use of annis.sqlgen.model.Inclusion 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

Inclusion (annis.sqlgen.model.Inclusion)4 Join (annis.model.Join)2 QueryNode (annis.model.QueryNode)2 LeftAlignment (annis.sqlgen.model.LeftAlignment)2 LeftOverlap (annis.sqlgen.model.LeftOverlap)2 Overlap (annis.sqlgen.model.Overlap)2 RightAlignment (annis.sqlgen.model.RightAlignment)2 RightOverlap (annis.sqlgen.model.RightOverlap)2 SameSpan (annis.sqlgen.model.SameSpan)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)1 AqlParseError (annis.model.AqlParseError)1 QueryAnnotation (annis.model.QueryAnnotation)1 CommonAncestor (annis.sqlgen.model.CommonAncestor)1 Dominance (annis.sqlgen.model.Dominance)1 EqualValue (annis.sqlgen.model.EqualValue)1 Identical (annis.sqlgen.model.Identical)1 LeftDominance (annis.sqlgen.model.LeftDominance)1 Near (annis.sqlgen.model.Near)1