Search in sources :

Example 21 with QueryException

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

the class TestFieldDocument method jsonQuery.

public static SpanQueryWrapper jsonQuery(String jsonFile) {
    SpanQueryWrapper sqwi;
    try {
        String json = getString(jsonFile);
        sqwi = new KrillQuery("tokens").fromKoral(json);
    } catch (QueryException e) {
        fail(e.getMessage());
        sqwi = new QueryBuilder("tokens").seg("???");
    }
    ;
    return sqwi;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) KrillQuery(de.ids_mannheim.korap.KrillQuery) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder)

Example 22 with QueryException

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

the class Krill method fromKoral.

/**
 * Parse KoralQuery as a {@link JsonNode} object.
 *
 * @param query
 *            The KoralQuery {@link JsonNode} object.
 * @return The {@link Krill} object for chaining.
 * @throws QueryException
 */
public Krill fromKoral(JsonNode json) {
    // Parse "query" attribute
    if (json.has("query")) {
        try {
            final KrillQuery kq = new KrillQuery("tokens");
            this.setQuery(kq);
            final SpanQueryWrapper qw = kq.fromKoral(json.get("query"));
            // Koral messages are moved to the Krill object
            this.moveNotificationsFrom(kq);
            // Throw an error, in case the query matches everywhere
            if (qw.isEmpty()) {
                this.addError(780, "This query matches everywhere");
            } else if (qw.isNull()) {
                this.addError(783, "This query can't match anywhere");
            } else {
                // Serialize a Lucene SpanQuery based on the SpanQueryWrapper
                this.spanQuery = qw.toQuery();
                // Throw a warning in case the root object is optional
                if (qw.isOptional())
                    this.addWarning(781, "Optionality of query is ignored");
                // Throw a warning in case the root object is negative
                if (qw.isNegative())
                    this.addWarning(782, "Exclusivity of query is ignored");
            }
            ;
        } catch (QueryException q) {
            this.addError(q.getErrorCode(), q.getMessage());
        }
        ;
    } else
        this.addError(700, "No query given");
    // <legacycode>
    if (json.has("warning") && json.get("warning").asText().length() > 0) {
        this.addWarning(799, json.get("warning").asText());
    }
    ;
    // </legacycode>
    // Copy notifications from request
    this.copyNotificationsFrom(json);
    // Parse "collection" or "collections" attribute
    try {
        if (json.has("collection")) {
            final JsonNode collNode = json.get("collection");
            // TODO: Temporary
            if (collNode.fieldNames().hasNext()) {
                this.setCollection(new KrillCollection().fromKoral(collNode));
            }
            ;
        } else if (json.has("collections")) {
            this.addError(899, "Collections are not supported anymore in favour of a single collection");
        }
        ;
    } catch (QueryException q) {
        this.addError(q.getErrorCode(), q.getMessage());
    }
    ;
    // !this.hasErrors() &&
    if (json.has("meta"))
        this.setMeta(new KrillMeta(json.get("meta")));
    return this;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)

Example 23 with QueryException

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

the class KrillQuery method _fromKoral.

private SpanQueryWrapper _fromKoral(JsonNode json, boolean isOperationRelation) throws QueryException {
    int number = 0;
    // TODO: Support @context for cosmas:...
    if (!json.has("@type"))
        throw new QueryException(701, "JSON-LD group has no @type attribute");
    // Get @type for branching
    String type = json.get("@type").asText();
    switch(type) {
        case "koral:group":
            return this._groupFromJson(json);
        case "koral:reference":
            if (json.has("operation") && !json.get("operation").asText().equals("operation:focus"))
                throw new QueryException(712, "Unknown reference operation");
            if (!json.has("operands")) {
                throw new QueryException(766, "Peripheral references are currently not supported");
            }
            JsonNode operands = json.get("operands");
            if (!operands.isArray())
                throw new QueryException(704, "Operation needs operand list");
            if (operands.size() == 0)
                throw new QueryException(704, "Operation needs operand list");
            if (operands.size() != 1)
                throw new QueryException(705, "Number of operands is not acceptable");
            // Reference based on classes
            if (json.has("classRef")) {
                if (json.has("classRefOp")) {
                    throw new QueryException(761, "Class reference operators are currently not supported");
                }
                ;
                number = json.get("classRef").get(0).asInt();
                if (number > MAX_CLASS_NUM)
                    throw new QueryException(709, "Valid class numbers exceeded");
            } else // Reference based on spans
            if (json.has("spanRef")) {
                JsonNode spanRef = json.get("spanRef");
                int length = 0;
                int startOffset = 0;
                if (!spanRef.isArray() || spanRef.size() == 0) {
                    throw new QueryException(714, "Span references expect a start position" + " and a length parameter");
                }
                ;
                if (spanRef.size() > 1)
                    length = spanRef.get(1).asInt(0);
                startOffset = spanRef.get(0).asInt(0);
                if (DEBUG)
                    log.trace("Wrap span reference {},{}", startOffset, length);
                SpanQueryWrapper sqw = this._fromKoral(operands.get(0));
                SpanSubspanQueryWrapper ssqw = new SpanSubspanQueryWrapper(sqw, startOffset, length);
                return ssqw;
            }
            ;
            if (DEBUG)
                log.trace("Wrap class reference {}", number);
            return new SpanFocusQueryWrapper(this._fromKoral(operands.get(0)), number);
        case "koral:token":
            // The token is empty and should be treated like []
            if (!json.has("wrap"))
                return new SpanRepetitionQueryWrapper();
            // Get wrapped token
            return this._segFromJson(json.get("wrap"));
        case "koral:span":
            // it is allowed only in relation queries
            if (isOperationRelation && !json.has("key") && !json.has("wrap") && !json.has("attr")) {
                return new SpanRepetitionQueryWrapper();
            }
            if (!json.has("wrap"))
                return this._termFromJson(json);
            // This is an ugly hack
            return this._termFromJson(json.get("wrap"), true);
    }
    ;
    // Unknown query type
    throw new QueryException(713, "Query type is not supported");
}
Also used : SpanRepetitionQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper) QueryException(de.ids_mannheim.korap.util.QueryException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) SpanSubspanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSubspanQueryWrapper) SpanFocusQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanFocusQueryWrapper)

Example 24 with QueryException

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

the class KrillQuery method _operationSubmatchFromJson.

// Deserialize operation:submatch
@Deprecated
private SpanQueryWrapper _operationSubmatchFromJson(JsonNode json, JsonNode operands) throws QueryException {
    int number = 1;
    this.addMessage(0, "operation:submatch is deprecated");
    if (operands.size() != 1)
        throw new QueryException(705, "Number of operands is not acceptable");
    // Use class reference
    if (json.has("classRef")) {
        if (json.has("classRefOp")) {
            throw new QueryException(761, "Class reference operators are currently not supported");
        }
        ;
        number = json.get("classRef").get(0).asInt();
    } else // Use span reference
    if (json.has("spanRef")) {
        throw new QueryException(762, "Span references are currently not supported");
    }
    ;
    return new SpanFocusQueryWrapper(this._fromKoral(operands.get(0)), number);
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanFocusQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanFocusQueryWrapper)

Example 25 with QueryException

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

the class KrillQuery method _operationSequenceFromJson.

// Deserialize operation:sequence
private SpanQueryWrapper _operationSequenceFromJson(JsonNode json, JsonNode operands) throws QueryException {
    // Sequence with only one operand
    if (operands.size() == 1)
        return this._fromKoral(operands.get(0));
    SpanSequenceQueryWrapper sseqqw = this.builder().seq();
    // Say if the operand order is important
    if (json.has("inOrder"))
        sseqqw.setInOrder(json.get("inOrder").asBoolean());
    // ATTENTION: Distances have to be set before segments are added
    if (json.has("distances")) {
        // THIS IS NO LONGER NECESSARY, AS IT IS COVERED BY FRAMES
        if (json.has("exclude") && json.get("exclude").asBoolean()) {
            throw new QueryException(763, "Excluding distance constraints are currently not supported");
        }
        ;
        if (!json.get("distances").isArray()) {
            throw new QueryException(707, "Distance Constraints have to be defined as arrays");
        }
        ;
        // TEMPORARY: Workaround for group distances
        JsonNode firstDistance = json.get("distances").get(0);
        if (!firstDistance.has("@type")) {
            throw new QueryException(701, "JSON-LD group has no @type attribute");
        }
        ;
        JsonNode distances;
        if (firstDistance.get("@type").asText().equals("koral:group")) {
            if (!firstDistance.has("operands") || !firstDistance.get("operands").isArray())
                throw new QueryException(704, "Operation needs operand list");
            distances = firstDistance.get("operands");
        } else // TODO: Support cosmas distances
        if (firstDistance.get("@type").asText().equals("koral:distance") || firstDistance.get("@type").asText().equals("cosmas:distance")) {
            distances = json.get("distances");
        } else
            throw new QueryException(708, "No valid distances defined");
        // Add all distance constraint to query
        for (JsonNode constraint : distances) {
            String unit = "w";
            if (constraint.has("key"))
                unit = constraint.get("key").asText();
            // There is a maximum of 100 fix
            int min = 0, max = 100;
            if (constraint.has("boundary")) {
                Boundary b = new Boundary(constraint.get("boundary"), 0, 100);
                min = b.min;
                max = b.max;
            } else // <legacy>
            {
                if (constraint.has("min"))
                    min = constraint.get("min").asInt(0);
                if (constraint.has("max"))
                    max = constraint.get("max").asInt(100);
            }
            ;
            // Add foundry and layer to the unit for new indices
            if (constraint.has("foundry") && constraint.has("layer") && constraint.get("foundry").asText().length() > 0 && constraint.get("layer").asText().length() > 0) {
                StringBuilder value = new StringBuilder();
                value.append(constraint.get("foundry").asText());
                value.append('/');
                value.append(constraint.get("layer").asText());
                value.append(':').append(unit);
                unit = value.toString();
            } else // Use default foundry and layer - currently only base is supported!
            if (unit.equals("s") || unit.equals("p") || unit.equals("t")) {
                StringBuilder value = new StringBuilder();
                unit = value.append("base/s:").append(unit).toString();
            }
            ;
            // Workaround for koral:distance vs cosmas:distance
            if (constraint.get("@type").asText().equals("koral:distance")) {
                min++;
                max++;
            }
            ;
            // Set distance exclusion
            Boolean exclusion = false;
            if (constraint.has("exclude"))
                exclusion = constraint.get("exclude").asBoolean();
            // Sanitize boundary
            if (max < min)
                max = min;
            if (DEBUG)
                log.trace("Add distance constraint of '{}': {}-{}", unit, min, max);
            sseqqw.withConstraint(min, max, unit, exclusion);
        }
        ;
    }
    ;
    // Add segments to sequence
    for (JsonNode operand : operands) {
        sseqqw.append(this._fromKoral(operand));
    }
    ;
    // inOrder was set to false without a distance constraint
    if (!sseqqw.isInOrder() && !sseqqw.hasConstraints()) {
        if (DEBUG)
            log.trace("Add distance constraint - for the normal inorder case");
        sseqqw.withConstraint(1, 1, "w");
    }
    ;
    return sseqqw;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SpanSequenceQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper)

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