Search in sources :

Example 26 with QueryException

use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.

the class KrillQuery method _handleAttrGroup.

// Deserialize attribute groups
private SpanQueryWrapper _handleAttrGroup(SpanQueryWrapper elementWithIdWrapper, JsonNode attrNode) throws QueryException {
    if (!attrNode.has("relation")) {
        throw new QueryException(743, "Term group expects a relation");
    }
    if (!attrNode.has("operands")) {
        throw new QueryException(742, "Term group needs operand list");
    }
    String relation = attrNode.get("relation").asText();
    JsonNode operands = attrNode.get("operands");
    SpanQueryWrapper attrWrapper;
    if ("relation:and".equals(relation)) {
        List<SpanQueryWrapper> wrapperList = new ArrayList<SpanQueryWrapper>();
        for (JsonNode operand : operands) {
            attrWrapper = _termFromJson(operand);
            if (attrWrapper == null) {
                throw new QueryException(747, "Attribute is null");
            }
            wrapperList.add(attrWrapper);
        }
        if (elementWithIdWrapper != null) {
            return new SpanWithAttributeQueryWrapper(elementWithIdWrapper, wrapperList);
        } else {
            return new SpanWithAttributeQueryWrapper(wrapperList);
        }
    } else if ("relation:or".equals(relation)) {
        SpanAlterQueryWrapper saq = new SpanAlterQueryWrapper(field);
        SpanWithAttributeQueryWrapper saqw;
        for (JsonNode operand : operands) {
            attrWrapper = _termFromJson(operand);
            if (attrWrapper == null) {
                throw new QueryException(747, "Attribute is null");
            }
            if (elementWithIdWrapper != null) {
                saqw = new SpanWithAttributeQueryWrapper(elementWithIdWrapper, attrWrapper);
            } else {
                saqw = new SpanWithAttributeQueryWrapper(attrWrapper);
            }
            saq.or(saqw);
        }
        return saq;
    } else {
        throw new QueryException(716, "Unknown relation");
    }
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanAlterQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper) ArrayList(java.util.ArrayList) SpanWithAttributeQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanWithAttributeQueryWrapper) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 27 with QueryException

use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.

the class KrillQuery method _operationRelationFromJson.

private SpanQueryWrapper _operationRelationFromJson(JsonNode operands, JsonNode relation) throws QueryException {
    if (operands.size() < 2) {
        throw new QueryException(705, "Number of operands is not acceptable");
    }
    SpanQueryWrapper operand1 = this._fromKoral(operands.get(0), true);
    SpanQueryWrapper operand2 = this._fromKoral(operands.get(1), true);
    RelationDirection direction;
    if (operand1.isEmpty() && !operand2.isEmpty()) {
        // "<:";
        direction = RelationDirection.LEFT;
    } else {
        // ">:"
        direction = RelationDirection.RIGHT;
    }
    if (!relation.has("@type")) {
        throw new QueryException(701, "JSON-LD group has no @type attribute");
    }
    if (relation.get("@type").asText().equals("koral:relation")) {
        SpanRelationWrapper spanRelationWrapper;
        SpanQueryWrapper relationTermWrapper;
        if (!relation.has("wrap")) {
            throw new QueryException(718, "Missing relation term.");
        } else {
            relationTermWrapper = _termFromJson(relation.get("wrap"), false, direction);
            spanRelationWrapper = new SpanRelationWrapper(relationTermWrapper, operand1, operand2);
        }
        spanRelationWrapper.setDirection(direction);
        return spanRelationWrapper;
    } else {
        throw new QueryException(713, "Query type is not supported");
    }
// if (relation.has("boundary")){
// _operationRepetitionFromJson(relation, operands);
// }
// else{
// 
// }
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanRelationWrapper(de.ids_mannheim.korap.query.wrap.SpanRelationWrapper) RelationDirection(de.ids_mannheim.korap.constants.RelationDirection) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 28 with QueryException

use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.

the class KrillQuery method _segFromJson.

// Deserialize koral:token
private SpanQueryWrapper _segFromJson(JsonNode json) throws QueryException {
    if (!json.has("@type"))
        throw new QueryException(701, "JSON-LD group has no @type attribute");
    String type = json.get("@type").asText();
    if (DEBUG)
        log.trace("Wrap new token definition by {}", type);
    // Branch on type
    switch(type) {
        case "koral:term":
            // case "match:eq":
            return this._termFromJson(json);
        case "koral:termGroup":
            if (!json.has("operands"))
                throw new QueryException(742, "Term group needs operand list");
            // Get operands
            JsonNode operands = json.get("operands");
            SpanSegmentQueryWrapper ssegqw = this.builder().seg();
            if (!json.has("relation"))
                throw new QueryException(743, "Term group expects a relation");
            switch(json.get("relation").asText()) {
                case "relation:and":
                    for (JsonNode operand : operands) {
                        SpanQueryWrapper part = this._segFromJson(operand);
                        if (part instanceof SpanAlterQueryWrapper) {
                            ssegqw.with((SpanAlterQueryWrapper) part);
                        } else if (part instanceof SpanRegexQueryWrapper) {
                            ssegqw.with((SpanRegexQueryWrapper) part);
                        } else if (part instanceof SpanSegmentQueryWrapper) {
                            ssegqw.with((SpanSegmentQueryWrapper) part);
                        } else {
                            throw new QueryException(744, "Operand not supported in term group");
                        }
                        ;
                    }
                    ;
                    return ssegqw;
                case "relation:or":
                    SpanAlterQueryWrapper ssaq = new SpanAlterQueryWrapper(this.field);
                    for (JsonNode operand : operands) {
                        ssaq.or(this._segFromJson(operand));
                    }
                    ;
                    return ssaq;
            }
            ;
    }
    ;
    throw new QueryException(745, "Token type is not supported");
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanRegexQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper) SpanSegmentQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper) SpanAlterQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 29 with QueryException

use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.

the class TestKrillQueryJSON method queryJSONinfiniteExpansion.

@Test
public void queryJSONinfiniteExpansion() throws QueryException {
    // der []*
    try {
        String json = getString(getClass().getResource("/queries/bugs/expansion_bug_3.jsonld").getFile());
        KrillQuery kq = new KrillQuery("tokens");
        assertEquals(kq.fromKoral(json).toQuery().toString(), "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:c, []{0, 4}, right)}))");
    } catch (QueryException e) {
        fail(e.getMessage());
    }
    ;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) KrillQuery(de.ids_mannheim.korap.KrillQuery) Test(org.junit.Test)

Example 30 with QueryException

use of de.ids_mannheim.korap.util.QueryException in project Krill by KorAP.

the class TestKrillQueryJSON method queryJSONdistancesWithRegexes.

@Test
public void queryJSONdistancesWithRegexes() throws QueryException {
    // "der" []{2,3} [opennlp/p="NN"]
    try {
        String json = getString(getClass().getResource("/queries/bugs/distances_with_regex_bug.jsonld").getFile());
        KrillQuery kq = new KrillQuery("tokens");
        assertEquals(kq.fromKoral(json).toQuery().toString(), "spanDistance(SpanMultiTermQueryWrapper(tokens:/s:der/), SpanMultiTermQueryWrapper(tokens:/opennlp/p:NN/), [(w[3:4], ordered, notExcluded)])");
    } catch (QueryException e) {
        fail(e.getMessage());
    }
    ;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) KrillQuery(de.ids_mannheim.korap.KrillQuery) Test(org.junit.Test)

Aggregations

QueryException (de.ids_mannheim.korap.util.QueryException)32 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)14 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 KrillQuery (de.ids_mannheim.korap.KrillQuery)10 Test (org.junit.Test)9 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)5 SpanRepetitionQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper)4 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)2 SpanAlterQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper)2 SpanFocusQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanFocusQueryWrapper)2 SpanRegexQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper)2 SpanSegmentQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper)2 SpanSequenceQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 KrillIndex (de.ids_mannheim.korap.KrillIndex)1 RelationDirection (de.ids_mannheim.korap.constants.RelationDirection)1 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)1 SpanMultipleDistanceQuery (de.ids_mannheim.korap.query.SpanMultipleDistanceQuery)1