Search in sources :

Example 1 with QueryException

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

the class TestBenchmarkSpans 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 2 with QueryException

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

the class Resource method match.

@GET
@Path("/match/{matchID}")
@Produces(MediaType.APPLICATION_JSON)
public String match(@PathParam("matchID") String id, @Context UriInfo uri) {
    Response kresp = _initResponse();
    if (kresp.hasErrors())
        return kresp.toJsonString();
    // Get index
    KrillIndex index = Node.getIndex();
    // Get query parameters
    MultivaluedMap<String, String> qp = uri.getQueryParameters();
    boolean includeSpans = false, includeHighlights = true, extendToSentence = false, info = false;
    // Optional query parameter "info" for more information on the match
    if (!_isNull(qp.getFirst("info")))
        info = true;
    // Optional query parameter "spans" for span information inclusion
    if (!_isNull(qp.getFirst("spans"))) {
        includeSpans = true;
        info = true;
    }
    ;
    // Optional query parameter "highlights" for highlight information inclusion
    String highlights = qp.getFirst("highlights");
    if (highlights != null && _isNull(highlights))
        includeHighlights = false;
    // Optional query parameter "extended" for sentence expansion
    if (!_isNull(qp.getFirst("extended")))
        extendToSentence = true;
    List<String> foundries = qp.get("foundry");
    List<String> layers = qp.get("layer");
    try {
        // Get match info
        return index.getMatchInfo(id, "tokens", info, foundries, layers, includeSpans, includeHighlights, extendToSentence).toJsonString();
    }// Nothing found
     catch (QueryException qe) {
        // Todo: Make Match rely on Response!
        kresp.addError(qe.getErrorCode(), qe.getMessage());
    }
    ;
    return kresp.toJsonString();
}
Also used : Response(de.ids_mannheim.korap.response.Response) QueryException(de.ids_mannheim.korap.util.QueryException) KrillIndex(de.ids_mannheim.korap.KrillIndex) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with QueryException

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

the class SpanSequenceQueryWrapper method _solveProblematicSequence.

/*
      Check if there are problematic segments in the sequence
      (either negative, optional or empty) and deal with them
      (make optional segments to or-queries and negative and empty
      segments to extensions).
      This has to be done as long as there are problematic segments
      In the queries.
    
      While there is a segment isNegative() or isOptional() or isEmpty() do
      - look for an anchor next to it
      - merge the problematic segment with the anchor
      - go on
    
      - This does not work for distance constraints!
    */
private boolean _solveProblematicSequence() {
    int size = this.segments.size();
    // Check if there is a problematic segment
    SpanQueryWrapper underScrutiny;
    boolean noRemainingProblem = true;
    int i = 0;
    if (DEBUG)
        log.trace("Try to solve a query of {} segments", size);
    // Iterate over all segments
    for (; i < size; ) {
        underScrutiny = this.segments.get(i);
        // Check if there is a problem with the current segment
        if (!underScrutiny.maybeAnchor()) {
            if (DEBUG)
                log.trace("segment {} is problematic", i);
            // elemets
            if (this.hasConstraints()) {
                if (DEBUG) {
                    log.trace("Sequence has constraints, " + "that do not allow empty or optional elements");
                }
                ;
                this.isSolved = true;
                this.isProblematic = true;
                return false;
            }
            ;
            // [problem][anchor]
            if (i < (size - 1) && this.segments.get(i + 1).maybeAnchor()) {
                if (DEBUG)
                    log.trace("Situation is [problem][anchor]");
                // Insert the solution
                try {
                    this.segments.set(i + 1, _merge(this.segments.get(i + 1), underScrutiny, false));
                }// An error occurred while solving the problem
                 catch (QueryException e) {
                    this.isSolved = true;
                    this.isProblematic = true;
                    return false;
                }
                ;
                // Remove the problem
                this.segments.remove(i);
                size--;
                if (DEBUG)
                    log.trace("Remove segment {} - now size {}", i, size);
                // Restart checking
                i = 0;
            } else // [anchor][problem]
            if (i >= 1 && this.segments.get(i - 1).maybeAnchor()) {
                if (DEBUG)
                    log.trace("Situation is [anchor][problem]");
                // Insert the solution
                try {
                    this.segments.set(i - 1, _merge(this.segments.get(i - 1), underScrutiny, true));
                } catch (QueryException e) {
                    this.isSolved = true;
                    this.isProblematic = true;
                    return false;
                }
                ;
                // Remove the problem
                this.segments.remove(i);
                size--;
                if (DEBUG)
                    log.trace("Remove segment {} - now size {}", i, size);
                // Restart checking
                i = 0;
            } else // [problem][problem]
            {
                if (DEBUG)
                    log.trace("Situation is [problem][problem]");
                noRemainingProblem = false;
                i++;
            }
            ;
        } else {
            if (DEBUG)
                log.trace("segment {} can be an anchor", i);
            i++;
        }
        ;
    }
    ;
    // There is still a remaining problem
    if (!noRemainingProblem) {
        // The size has changed - retry!
        if (size != this.segments.size())
            return _solveProblematicSequence();
        this.isSolved = true;
        this.isProblematic = true;
        // true;
        return false;
    }
    ;
    this.isSolved = true;
    this.isProblematic = false;
    return false;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint)

Example 4 with QueryException

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

the class SpanSequenceQueryWrapper method toFragmentQuery.

/**
 * Serialize the wrapped sequence to a {@link SpanQuery} object.
 *
 * @return A {@link SpanQuery} object.
 * @throws QueryException
 */
public SpanQuery toFragmentQuery() throws QueryException {
    // There was a serialization failure not yet reported
    if (this.constraintException != null)
        throw constraintException;
    int size = this.segments.size();
    // Nothing to do
    if (size == 0 || this.isNull())
        return (SpanQuery) null;
    // No real sequence - only one element
    if (size == 1) {
        // But the element may be expanded
        if (this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
            throw new QueryException(613, limitationError);
        }
        ;
        // Unproblematic single query
        if (this.segments.get(0).maybeAnchor())
            return (SpanQuery) this.segments.get(0).retrieveNode(this.retrieveNode).toFragmentQuery();
        if (this.segments.get(0).isEmpty())
            throw new QueryException(613, "Sequence is not allowed to be empty");
        if (this.segments.get(0).isOptional())
            throw new QueryException(613, "Sequence is not allowed to be optional");
        if (this.segments.get(0).isNegative())
            throw new QueryException(613, "Sequence is not allowed to be negative");
    }
    ;
    if (!this.isSolved) {
        if (!_solveProblematicSequence()) {
            if (this.segments.get(0).maybeExtension()) {
                throw new QueryException(613, "Sequence contains unresolvable " + "empty, optional, or negative segments");
            }
            ;
        }
        ;
    }
    ;
    // The element may be expanded
    if (this.segments.size() == 1 && this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
        throw new QueryException(613, limitationError);
    }
    ;
    // Create the initial query
    SpanQuery query = null;
    int i = 0;
    // Get the first valid segment
    while (query == null && i < this.segments.size()) {
        query = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
        i++;
    }
    ;
    // No valid segment found
    if (query == null)
        return (SpanQuery) null;
    // NextQueries
    if (!this.hasConstraints() && this.isInOrder()) {
        for (; i < this.segments.size(); i++) {
            // Get the first query for next sequence
            SpanQuery second = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
            if (second == null)
                continue;
            query = new SpanNextQuery(query, second);
        }
        ;
        return (SpanQuery) query;
    }
    ;
    // DistanceQueries with problems
    if (this.hasConstraints() && this.isProblematic) {
        throw new QueryException(613, "Distance constraints not supported with empty, optional or negative operands");
    }
    ;
    // DistanceQueries
    if (this.constraints.size() == 1) {
        DistanceConstraint constraint = this.constraints.get(0);
        // Create spanElementDistance query
        if (!constraint.getUnit().equals("w")) {
            for (i = 1; i < this.segments.size(); i++) {
                // No support for extended spans in constraints
                if (this.segments.get(i).isExtended())
                    throw new QueryException(613, limitationError);
                /* Maybe important
                    if (this.segments.get(i).isOptional())
                        throw new QueryException(613, limitationError);
                    */
                SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
                if (sq == null)
                    continue;
                SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
                query = (SpanQuery) sdquery;
            }
            ;
        } else // Create spanDistance query
        {
            for (i = 1; i < this.segments.size(); i++) {
                // No support for extended spans in constraints
                if (this.segments.get(i).isExtended())
                    throw new QueryException(613, limitationError);
                /* May be necessary
                    if (this.segments.get(i).isOptional())
                        throw new QueryException(613, limitationError);
                    */
                SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
                if (sq == null)
                    continue;
                SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
                query = (SpanQuery) sdquery;
            }
            ;
        }
        ;
        return (SpanQuery) query;
    }
    ;
    // MultipleDistanceQueries
    for (i = 1; i < this.segments.size(); i++) {
        // No support for extended spans in constraints
        if (this.segments.get(i).isExtended())
            throw new QueryException(613, limitationError);
        SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
        if (sq == null)
            continue;
        query = new SpanMultipleDistanceQuery(query, sq, this.constraints, isInOrder, true);
    }
    ;
    return (SpanQuery) query;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) SpanMultipleDistanceQuery(de.ids_mannheim.korap.query.SpanMultipleDistanceQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery)

Example 5 with QueryException

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

the class Messages method add.

/**
 * Append an existing message comming from a JsonNode.
 *
 * @param node
 *            <code>JsonNode</code> representing a message
 * @return New Message object
 * @throws QueryException
 *             if notification is not well formed (Error 750)
 */
public Message add(JsonNode msg) throws QueryException {
    if (!msg.isArray() || !msg.has(0))
        throw new QueryException(750, "Passed notifications are not well formed");
    // Valid message
    Message newMsg = new Message();
    short i = 1;
    if (msg.get(0).isNumber()) {
        newMsg.setCode(msg.get(0).asInt());
        if (!msg.has(1))
            throw new QueryException(750, "Passed notifications are not well formed");
        newMsg.setMessage(msg.get(1).asText());
        i++;
    } else {
        newMsg.setMessage(msg.get(0).asText());
    }
    ;
    // Add parameters
    while (msg.has(i)) newMsg.addParameter(msg.get(i++).asText());
    // Add messages to list
    this.add(newMsg);
    return newMsg;
}
Also used : QueryException(de.ids_mannheim.korap.util.QueryException) Message(de.ids_mannheim.korap.response.Message)

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