Search in sources :

Example 1 with AqlParseError

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

the class AnnotationExistenceValidator method transform.

@Override
public QueryData transform(QueryData data) {
    List<Long> corpusList = data.getCorpusList();
    if (queryDao != null && (corpusList != null) && !corpusList.isEmpty()) {
        // get first corpus name
        // List<AnnisCorpus> mycorpora = queryDao.listCorpora();
        // String firstcorpusname =  mycorpora.get(0).getName();
        Set<String> result = new TreeSet<>();
        /*get a list of all annotations in a similar way that TigerQueryBuilder gets it through 
       QueryServiceImpl in queryDao.listAnnotations()*/
        List<AnnisAttribute> atts = queryDao.listAnnotations(corpusList, false, true);
        // among them, get only node annotations
        for (AnnisAttribute a : atts) {
            if (a.getType() == AnnisAttribute.Type.node) {
                List<String> splitted = Splitter.on(":").limit(2).omitEmptyStrings().trimResults().splitToList(a.getName());
                result.add(splitted.get(splitted.size() - 1));
            // result is a set of strings of available annotations
            }
        }
        List<AqlParseError> errors = new LinkedList<>();
        for (List<QueryNode> alternative : data.getAlternatives()) {
            for (QueryNode n : alternative) {
                Set<QueryAnnotation> m = n.getNodeAnnotations();
                // always get the first one
                if (!m.isEmpty()) {
                    // name is the node name string, ready to check if name is in the list of
                    // available names
                    String name = m.iterator().next().getName();
                    if (!result.contains(name)) {
                        errors.add(new AqlParseError(n, "\"" + name + "\"" + " is not a valid annotation name in selected corpora "));
                    }
                }
            }
        }
        if (!errors.isEmpty()) {
            throw new AnnisQLSemanticsException("Invalid annotation names detected.", errors);
        }
    }
    return data;
}
Also used : QueryAnnotation(annis.model.QueryAnnotation) AnnisAttribute(annis.service.objects.AnnisAttribute) AqlParseError(annis.model.AqlParseError) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) LinkedList(java.util.LinkedList) TreeSet(java.util.TreeSet) QueryNode(annis.model.QueryNode)

Example 2 with AqlParseError

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

the class QueryController method reportServiceException.

/**
 * Show errors that occured during the execution of a query to the user.
 *
 * @param ex The exception to report in the user interface
 * @param showNotification If true a notification is shown instead of only
 * displaying the error in the status label.
 */
public void reportServiceException(UniformInterfaceException ex, boolean showNotification) {
    QueryPanel qp = searchView.getControlPanel().getQueryPanel();
    String caption = null;
    String description = null;
    if (!AnnisBaseUI.handleCommonError(ex, "execute query")) {
        switch(ex.getResponse().getStatus()) {
            case 400:
                List<AqlParseError> errors = ex.getResponse().getEntity(new GenericType<List<AqlParseError>>() {
                });
                caption = "Parsing error";
                description = Joiner.on("\n").join(errors);
                qp.setStatus(description);
                qp.setErrors(errors);
                break;
            case 504:
                caption = "Timeout";
                description = "Query execution took too long.";
                qp.setStatus(caption + ": " + description);
                break;
            case 403:
                if (Helper.getUser() == null) {
                    // not logged in
                    qp.setStatus("You don't have the access rights to query this corpus. " + "You might want to login to access more corpora.");
                    searchView.getMainToolbar().showLoginWindow(true);
                } else {
                    // logged in but wrong user
                    caption = "You don't have the access rights to query this corpus. " + "You might want to login as another user to access more corpora.";
                    qp.setStatus(caption);
                }
                break;
            default:
                log.error("Exception when communicating with service", ex);
                qp.setStatus("Unexpected exception:  " + ex.getMessage());
                ExceptionDialog.show(ex, "Exception when communicating with service.");
                break;
        }
        if (showNotification && caption != null) {
            Notification.show(caption, description, Notification.Type.WARNING_MESSAGE);
        }
    }
}
Also used : AqlParseError(annis.model.AqlParseError) List(java.util.List) ArrayList(java.util.ArrayList) FrequencyQueryPanel(annis.gui.frequency.FrequencyQueryPanel) QueryPanel(annis.gui.controlpanel.QueryPanel)

Example 3 with AqlParseError

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

the class ResultFetchJob method run.

@Override
public void run() {
    WebResource subgraphRes = Helper.getAnnisWebResource().path("query/search/subgraph");
    // holds the ids of the matches.
    MatchGroup result;
    try {
        if (Thread.interrupted()) {
            return;
        }
        // set the the progress bar, for given the user some information about the loading process
        ui.accessSynchronously(new Runnable() {

            @Override
            public void run() {
                resultPanel.showMatchSearchInProgress(query);
            }
        });
        // get the matches
        result = futureMatches.get();
        // get the subgraph for each match, when the result is not empty
        if (result.getMatches().isEmpty()) {
            // check if thread was interrupted
            if (Thread.interrupted()) {
                return;
            }
            // nothing found, so inform the user about this.
            ui.access(new Runnable() {

                @Override
                public void run() {
                    resultPanel.showNoResult();
                }
            });
        } else {
            if (Thread.interrupted()) {
                return;
            }
            // since annis found something, inform the user that subgraphs are created
            ui.access(new Runnable() {

                @Override
                public void run() {
                    resultPanel.showSubgraphSearchInProgress(query, 0.0f);
                }
            });
            // prepare fetching subgraphs
            final BlockingQueue<SaltProject> queue = new ArrayBlockingQueue<>(result.getMatches().size());
            int current = 0;
            final ArrayList<Match> matchList = new ArrayList<>(result.getMatches());
            for (Match m : matchList) {
                if (Thread.interrupted()) {
                    return;
                }
                List<Match> subList = new LinkedList<>();
                subList.add(m);
                final SaltProject p = executeQuery(subgraphRes, new MatchGroup(subList), query.getLeftContext(), query.getRightContext(), query.getSegmentation(), SubgraphFilter.all);
                queue.put(p);
                log.debug("added match {} to queue", current + 1);
                if (current == 0) {
                    ui.access(new Runnable() {

                        @Override
                        public void run() {
                            resultPanel.setQueryResultQueue(queue, query, matchList);
                        }
                    });
                }
                if (Thread.interrupted()) {
                    return;
                }
                current++;
            }
        }
    // end if no results
    } catch (InterruptedException ex) {
    // just return
    } catch (final ExecutionException root) {
        ui.accessSynchronously(new Runnable() {

            @Override
            public void run() {
                if (resultPanel != null && resultPanel.getPaging() != null) {
                    PagingComponent paging = resultPanel.getPaging();
                    Throwable cause = root.getCause();
                    if (cause instanceof UniformInterfaceException) {
                        UniformInterfaceException ex = (UniformInterfaceException) cause;
                        if (ex.getResponse().getStatus() == 400) {
                            List<AqlParseError> errors = ex.getResponse().getEntity(new GenericType<List<AqlParseError>>() {
                            });
                            String errMsg = Joiner.on(" | ").join(errors);
                            paging.setInfo("parsing error: " + errMsg);
                        } else if (ex.getResponse().getStatus() == 504) {
                            paging.setInfo("Timeout: query execution took too long");
                        } else if (ex.getResponse().getStatus() == 403) {
                            paging.setInfo("Not authorized to query this corpus.");
                        } else {
                            paging.setInfo("unknown error: " + ex);
                        }
                    } else {
                        log.error("Unexcepted ExecutionException cause", root);
                    }
                    resultPanel.showFinishedSubgraphSearch();
                }
            }
        });
    }
// end catch
}
Also used : ArrayList(java.util.ArrayList) AqlParseError(annis.model.AqlParseError) AsyncWebResource(com.sun.jersey.api.client.AsyncWebResource) WebResource(com.sun.jersey.api.client.WebResource) SaltProject(org.corpus_tools.salt.common.SaltProject) LinkedList(java.util.LinkedList) Match(annis.service.objects.Match) PagingComponent(annis.gui.paging.PagingComponent) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) MatchGroup(annis.service.objects.MatchGroup) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with AqlParseError

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

the class AqlCodeEditor method setErrors.

public void setErrors(List<AqlParseError> errors) {
    getState().errors.clear();
    if (errors != null) {
        for (AqlParseError e : errors) {
            getState().errors.add(new AqlCodeEditorState.ParseError(e));
        }
    }
    markAsDirty();
}
Also used : AqlParseError(annis.model.AqlParseError)

Example 5 with AqlParseError

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

the class AnnisParserAntlr method parse.

public QueryData parse(String aql, List<Long> corpusList) {
    final List<AqlParseError> errors = new LinkedList<>();
    AqlLexer lexerNonDNF = new AqlLexer(new ANTLRInputStream(aql));
    lexerNonDNF.removeErrorListeners();
    lexerNonDNF.addErrorListener(new AqlLexerErrorListener(errors));
    // bring first into DNF
    RawAqlPreParser rawParser = new RawAqlPreParser(new CommonTokenStream(lexerNonDNF));
    rawParser.removeErrorListeners();
    rawParser.addErrorListener(new AqlParseErrorListener(errors));
    RawAqlPreParser.StartContext treeRaw = rawParser.start();
    if (!errors.isEmpty()) {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
    // treeRaw.inspect(rawParser);
    ParseTreeWalker walkerRaw = new ParseTreeWalker();
    RawAqlListener listenerRaw = new RawAqlListener();
    walkerRaw.walk(listenerRaw, treeRaw);
    LogicClause topNode = listenerRaw.getRoot();
    DNFTransformer.toDNF(topNode);
    // use the DNF form and parse it again
    TokenSource source = new ListTokenSource(topNode.getCoveredToken());
    AqlParser parserDNF = new AqlParser(new CommonTokenStream(source));
    parserDNF.removeErrorListeners();
    parserDNF.addErrorListener(new AqlParseErrorListener(errors));
    AqlParser.StartContext treeDNF = parserDNF.start();
    if (!errors.isEmpty()) {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    NodeIDListener idListener = new NodeIDListener();
    walker.walk(idListener, treeDNF);
    QueryNodeListener nodeListener = new QueryNodeListener(idListener.getNodeIntervalToID());
    try {
        walker.walk(nodeListener, treeDNF);
        QueryData data = nodeListener.getQueryData();
        data.setCorpusList(corpusList);
        data.addMetaAnnotations(nodeListener.getMetaData());
        JoinListener joinListener = new JoinListener(data, precedenceBound, nodeListener.getTokenPositions());
        walker.walk(joinListener, treeDNF);
        if (postProcessors != null) {
            for (QueryDataTransformer transformer : postProcessors) {
                data = transformer.transform(data);
            }
        }
        return data;
    } catch (NullPointerException ex) {
        log.warn("Null pointer exception occured during parsing", ex);
        throw new AnnisQLSemanticsException(ex.getMessage());
    } catch (IllegalArgumentException ex) {
        throw new AnnisQLSemanticsException(ex.getMessage());
    }
}
Also used : AqlParseError(annis.model.AqlParseError) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TokenSource(org.antlr.v4.runtime.TokenSource) AqlLexer(annis.ql.AqlLexer) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) AqlParser(annis.ql.AqlParser) LinkedList(java.util.LinkedList) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) RawAqlPreParser(annis.ql.RawAqlPreParser)

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