Search in sources :

Example 1 with AnnisQLSyntaxException

use of annis.exceptions.AnnisQLSyntaxException in project ANNIS by korpling.

the class GeneralTextExporter method convertText.

@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
    try {
        if (keys == null || keys.isEmpty()) {
            // auto set
            keys = new LinkedList<>();
            keys.add("tok");
            List<AnnisAttribute> attributes = new LinkedList<>();
            for (String corpus : corpora) {
                attributes.addAll(annisResource.path("corpora").path(urlPathEscape.escape(corpus)).path("annotations").queryParam("fetchvalues", "false").queryParam("onlymostfrequentvalues", "false").get(new AnnisAttributeListType()));
            }
            for (AnnisAttribute a : attributes) {
                if (a.getName() != null) {
                    String[] namespaceAndName = a.getName().split(":", 2);
                    if (namespaceAndName.length > 1) {
                        keys.add(namespaceAndName[1]);
                    } else {
                        keys.add(namespaceAndName[0]);
                    }
                }
            }
        }
        Map<String, String> args = new HashMap<>();
        for (String s : argsAsString.split("&|;")) {
            String[] splitted = s.split("=", 2);
            String key = splitted[0];
            String val = "";
            if (splitted.length > 1) {
                val = splitted[1];
            }
            args.put(key, val);
        }
        int stepSize = 10;
        // 1. Get all the matches as Salt ID
        InputStream matchStream = annisResource.path("search/find/").queryParam("q", Helper.encodeJersey(queryAnnisQL)).queryParam("corpora", StringUtils.join(corpora, ",")).accept(MediaType.TEXT_PLAIN_TYPE).get(InputStream.class);
        try (BufferedReader inReader = new BufferedReader(new InputStreamReader(matchStream, "UTF-8"))) {
            WebResource subgraphRes = annisResource.path("search/subgraph");
            MatchGroup currentMatches = new MatchGroup();
            String currentLine;
            int offset = 0;
            // 2. iterate over all matches and get the sub-graph for a group of matches
            while (!Thread.currentThread().isInterrupted() && (currentLine = inReader.readLine()) != null) {
                Match match = Match.parseFromString(currentLine);
                currentMatches.getMatches().add(match);
                if (currentMatches.getMatches().size() >= stepSize) {
                    WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                    if (args.containsKey("segmentation")) {
                        res = res.queryParam("segmentation", args.get("segmentation"));
                    }
                    SubgraphFilter filter = getSubgraphFilter();
                    if (filter != null) {
                        res = res.queryParam("filter", filter.name());
                    }
                    Stopwatch stopwatch = Stopwatch.createUnstarted();
                    stopwatch.start();
                    SaltProject p = res.post(SaltProject.class, currentMatches);
                    stopwatch.stop();
                    // export was fast enough
                    if (stopwatch.elapsed(TimeUnit.MILLISECONDS) < 500 && stepSize < 50) {
                        stepSize += 10;
                    }
                    convertText(LegacyGraphConverter.convertToResultSet(p), keys, args, out, offset - currentMatches.getMatches().size());
                    currentMatches.getMatches().clear();
                    if (eventBus != null) {
                        eventBus.post(offset + 1);
                    }
                }
                offset++;
            }
            if (Thread.interrupted()) {
                return new InterruptedException("Exporter job was interrupted");
            }
            // query the left over matches
            if (!currentMatches.getMatches().isEmpty()) {
                WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                if (args.containsKey("segmentation")) {
                    res = res.queryParam("segmentation", args.get("segmentation"));
                }
                SubgraphFilter filter = getSubgraphFilter();
                if (filter != null) {
                    res = res.queryParam("filter", filter.name());
                }
                SaltProject p = res.post(SaltProject.class, currentMatches);
                convertText(LegacyGraphConverter.convertToResultSet(p), keys, args, out, offset - currentMatches.getMatches().size() - 1);
            }
            offset = 0;
        }
        out.append("\n");
        out.append("\n");
        out.append("finished");
        return null;
    } catch (AnnisQLSemanticsException | AnnisQLSyntaxException | AnnisCorpusAccessException | UniformInterfaceException | IOException ex) {
        return ex;
    }
}
Also used : HashMap(java.util.HashMap) AnnisAttribute(annis.service.objects.AnnisAttribute) Stopwatch(com.google.common.base.Stopwatch) WebResource(com.sun.jersey.api.client.WebResource) Match(annis.service.objects.Match) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) SubgraphFilter(annis.service.objects.SubgraphFilter) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisCorpusAccessException(annis.exceptions.AnnisCorpusAccessException) MatchGroup(annis.service.objects.MatchGroup) BufferedReader(java.io.BufferedReader)

Example 2 with AnnisQLSyntaxException

use of annis.exceptions.AnnisQLSyntaxException in project ANNIS by korpling.

the class SaltBasedExporter method convertText.

@Override
public Exception convertText(String queryAnnisQL, int contextLeft, int contextRight, Set<String> corpora, List<String> keys, String argsAsString, boolean alignmc, WebResource annisResource, Writer out, EventBus eventBus, Map<String, CorpusConfig> corpusConfigs) {
    CacheManager cacheManager = CacheManager.create();
    try {
        Cache cache = cacheManager.getCache("saltProjectsCache");
        if (keys == null || keys.isEmpty()) {
            // auto set
            keys = new LinkedList<>();
            keys.add("tok");
            List<AnnisAttribute> attributes = new LinkedList<>();
            for (String corpus : corpora) {
                attributes.addAll(annisResource.path("corpora").path(urlPathEscape.escape(corpus)).path("annotations").queryParam("fetchvalues", "false").queryParam("onlymostfrequentvalues", "false").get(new AnnisAttributeListType()));
            }
            for (AnnisAttribute a : attributes) {
                if (a.getName() != null) {
                    String[] namespaceAndName = a.getName().split(":", 2);
                    if (namespaceAndName.length > 1) {
                        keys.add(namespaceAndName[1]);
                    } else {
                        keys.add(namespaceAndName[0]);
                    }
                }
            }
        }
        Map<String, String> args = new HashMap<>();
        for (String s : argsAsString.split("&|;")) {
            String[] splitted = s.split("=", 2);
            String key = splitted[0];
            String val = "";
            if (splitted.length > 1) {
                val = splitted[1];
            }
            args.put(key, val);
        }
        int stepSize = 10;
        int pCounter = 1;
        Map<Integer, Integer> offsets = new HashMap<Integer, Integer>();
        // 1. Get all the matches as Salt ID
        InputStream matchStream = annisResource.path("search/find/").queryParam("q", Helper.encodeJersey(queryAnnisQL)).queryParam("corpora", StringUtils.join(corpora, ",")).accept(MediaType.TEXT_PLAIN_TYPE).get(InputStream.class);
        // get node count for the query
        WebResource resource = Helper.getAnnisWebResource();
        List<QueryNode> nodes = resource.path("query/parse/nodes").queryParam("q", Helper.encodeJersey(queryAnnisQL)).get(new GenericType<List<QueryNode>>() {
        });
        Integer nodeCount = nodes.size();
        try (BufferedReader inReader = new BufferedReader(new InputStreamReader(matchStream, "UTF-8"))) {
            WebResource subgraphRes = annisResource.path("search/subgraph");
            MatchGroup currentMatches = new MatchGroup();
            String currentLine;
            int offset = 1;
            // 2. iterate over all matches and get the sub-graph for a group of matches
            while (!Thread.currentThread().isInterrupted() && (currentLine = inReader.readLine()) != null) {
                Match match = Match.parseFromString(currentLine);
                currentMatches.getMatches().add(match);
                if (currentMatches.getMatches().size() >= stepSize) {
                    WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                    if (args.containsKey("segmentation")) {
                        res = res.queryParam("segmentation", args.get("segmentation"));
                    }
                    SubgraphFilter filter = getSubgraphFilter();
                    if (filter != null) {
                        res = res.queryParam("filter", filter.name());
                    }
                    Stopwatch stopwatch = Stopwatch.createStarted();
                    SaltProject p = res.post(SaltProject.class, currentMatches);
                    stopwatch.stop();
                    // export was fast enough
                    if (stopwatch.elapsed(TimeUnit.MILLISECONDS) < 500 && stepSize < 50) {
                        stepSize += 10;
                    }
                    convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size(), corpusConfigs, out, nodeCount);
                    offsets.put(pCounter, offset - currentMatches.getMatches().size());
                    cache.put(new Element(pCounter++, p));
                    currentMatches.getMatches().clear();
                    if (eventBus != null) {
                        eventBus.post(offset + 1);
                    }
                }
                offset++;
            }
            if (Thread.interrupted()) {
                return new InterruptedException("Exporter job was interrupted");
            }
            // query the left over matches
            if (!currentMatches.getMatches().isEmpty()) {
                WebResource res = subgraphRes.queryParam("left", "" + contextLeft).queryParam("right", "" + contextRight);
                if (args.containsKey("segmentation")) {
                    res = res.queryParam("segmentation", args.get("segmentation"));
                }
                SubgraphFilter filter = getSubgraphFilter();
                if (filter != null) {
                    res = res.queryParam("filter", filter.name());
                }
                SaltProject p = res.post(SaltProject.class, currentMatches);
                convertSaltProject(p, keys, args, alignmc, offset - currentMatches.getMatches().size() - 1, corpusConfigs, out, nodeCount);
                offsets.put(pCounter, offset - currentMatches.getMatches().size() - 1);
                cache.put(new Element(pCounter++, p));
            }
            offset = 1;
        }
        // build the list of ordered match numbers (ordering by occurrence in text)
        getOrderedMatchNumbers();
        @SuppressWarnings("unchecked") List<Integer> cacheKeys = cache.getKeys();
        List<Integer> listOfKeys = new ArrayList<Integer>();
        for (Integer key : cacheKeys) {
            listOfKeys.add(key);
        }
        Collections.sort(listOfKeys);
        for (Integer key : listOfKeys) {
            SaltProject p = (SaltProject) cache.get(key).getObjectValue();
            convertSaltProject(p, keys, args, alignmc, offsets.get(key), corpusConfigs, out, null);
        }
        out.append(System.lineSeparator());
        return null;
    } catch (AnnisQLSemanticsException | AnnisQLSyntaxException | AnnisCorpusAccessException | UniformInterfaceException | IOException | CacheException | IllegalStateException | ClassCastException ex) {
        return ex;
    } finally {
        cacheManager.removalAll();
        cacheManager.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) CacheException(net.sf.ehcache.CacheException) AnnisAttribute(annis.service.objects.AnnisAttribute) Element(net.sf.ehcache.Element) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) WebResource(com.sun.jersey.api.client.WebResource) Match(annis.service.objects.Match) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException) CacheManager(net.sf.ehcache.CacheManager) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) SaltProject(org.corpus_tools.salt.common.SaltProject) IOException(java.io.IOException) SubgraphFilter(annis.service.objects.SubgraphFilter) LinkedList(java.util.LinkedList) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) AnnisCorpusAccessException(annis.exceptions.AnnisCorpusAccessException) QueryNode(annis.model.QueryNode) MatchGroup(annis.service.objects.MatchGroup) BufferedReader(java.io.BufferedReader) Cache(net.sf.ehcache.Cache)

Example 3 with AnnisQLSyntaxException

use of annis.exceptions.AnnisQLSyntaxException in project ANNIS by korpling.

the class AnnisBaseRunner method runCommand.

protected void runCommand(String command, String args) {
    String methodName = "do" + command.substring(0, 1).toUpperCase() + command.substring(1);
    log.debug("looking for: " + methodName);
    try {
        long start = new Date().getTime();
        Method commandMethod = getClass().getMethod(methodName, String.class);
        commandMethod.invoke(this, args);
        System.out.println("Time: " + (new Date().getTime() - start) + " ms");
        if (history != null) {
            history.flush();
        }
    } catch (InvocationTargetException e) {
        // FIXME: Exception-Handling is all over the place (refactor into a handleException method)
        Throwable cause = e.getCause();
        try {
            throw cause;
        } catch (AnnisQLSyntaxException ee) {
            error(ee.getMessage());
        } catch (Throwable ee) {
            log.error("Uncaught exception: ", ee);
            error("Uncaught Exception: " + ee.getMessage());
        }
    } catch (IllegalAccessException e) {
        log.error("BUG: IllegalAccessException should never be thrown", e);
        throw new AnnisRunnerException("BUG: can't access method: " + methodName, e);
    } catch (SecurityException e) {
        log.error("BUG: SecurityException should never be thrown", e);
        error(e);
    } catch (NoSuchMethodException e) {
        throw new UsageException("don't know how to do: " + command);
    } catch (IOException e) {
        log.error("IOException was thrown", e);
    }
}
Also used : Method(java.lang.reflect.Method) IOException(java.io.IOException) Date(java.util.Date) InvocationTargetException(java.lang.reflect.InvocationTargetException) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException)

Example 4 with AnnisQLSyntaxException

use of annis.exceptions.AnnisQLSyntaxException 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)

Example 5 with AnnisQLSyntaxException

use of annis.exceptions.AnnisQLSyntaxException in project ANNIS by korpling.

the class AnnisParserAntlr method dumpTree.

public String dumpTree(String aql) {
    AqlLexer lexer = new AqlLexer(new ANTLRInputStream(aql));
    AqlParser parser = new AqlParser(new CommonTokenStream(lexer));
    final List<AqlParseError> errors = new LinkedList<>();
    parser.removeErrorListeners();
    parser.addErrorListener(new AqlParseErrorListener(errors));
    ParseTree tree = parser.start();
    if (errors.isEmpty()) {
        return tree.toStringTree();
    } else {
        throw new AnnisQLSyntaxException(Joiner.on("\n").join(errors), errors);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) AqlLexer(annis.ql.AqlLexer) AqlParseError(annis.model.AqlParseError) AqlParser(annis.ql.AqlParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) LinkedList(java.util.LinkedList) ParseTree(org.antlr.v4.runtime.tree.ParseTree) AnnisQLSyntaxException(annis.exceptions.AnnisQLSyntaxException)

Aggregations

AnnisQLSyntaxException (annis.exceptions.AnnisQLSyntaxException)5 LinkedList (java.util.LinkedList)4 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)3 IOException (java.io.IOException)3 AnnisCorpusAccessException (annis.exceptions.AnnisCorpusAccessException)2 AqlParseError (annis.model.AqlParseError)2 AqlLexer (annis.ql.AqlLexer)2 AqlParser (annis.ql.AqlParser)2 AnnisAttribute (annis.service.objects.AnnisAttribute)2 Match (annis.service.objects.Match)2 MatchGroup (annis.service.objects.MatchGroup)2 SubgraphFilter (annis.service.objects.SubgraphFilter)2 Stopwatch (com.google.common.base.Stopwatch)2 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)2 WebResource (com.sun.jersey.api.client.WebResource)2 BufferedReader (java.io.BufferedReader)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 HashMap (java.util.HashMap)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2