Search in sources :

Example 26 with QueryNode

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

the class FrequencySqlGenerator method conditionsForEntries.

private Multimap<FrequencyTableEntry, String> conditionsForEntries(List<FrequencyTableEntry> frequencyEntries, QueryData queryData, List<QueryNode> alternative) {
    Multimap<FrequencyTableEntry, String> conditions = LinkedHashMultimap.create();
    int i = 1;
    ImmutableMap<String, QueryNode> idxNodeVariables = Maps.uniqueIndex(alternative.iterator(), new Function<QueryNode, String>() {

        @Override
        public String apply(QueryNode input) {
            return input.getVariable();
        }
    });
    for (FrequencyTableEntry e : frequencyEntries) {
        if (e.getType() == FrequencyTableEntryType.meta) {
            List<String> qName = Splitter.on(':').limit(2).omitEmptyStrings().splitToList(e.getKey());
            if (qName.size() == 2) {
                conditions.put(e, "v" + i + ".namespace = '" + escaper.escape(qName.get(0)) + "'");
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(1)) + "'");
            } else {
                conditions.put(e, "v" + i + ".name = '" + escaper.escape(qName.get(0)) + "'");
            }
            conditions.put(e, "v" + i + ".corpus_ref = solutions.corpus_ref");
        } else {
            // general partition restriction
            conditions.put(e, "v" + i + ".toplevel_corpus IN (" + StringUtils.join(queryData.getCorpusList(), ",") + ")");
            // specificly join on top level corpus
            conditions.put(e, "v" + i + ".toplevel_corpus = solutions.toplevel_corpus");
            // join on node ID
            QueryNode referencedNode = idxNodeVariables.get(e.getReferencedNode());
            if (referencedNode == null) {
                throw new AnnisQLSemanticsException("No such node \"" + e.getReferencedNode() + "\". " + "Your query contains " + alternative.size() + " node(s), make sure no node definition numbers are greater than this number");
            }
            conditions.put(e, "v" + i + ".id = solutions.id" + referencedNode.getId());
            if (e.getType() == FrequencyTableEntryType.span) {
                conditions.put(e, "v" + i + ".n_sample IS TRUE");
            } else if (e.getType() == FrequencyTableEntryType.annotation) {
                // TODO: support namespaces
                // filter by selected key
                conditions.put(e, "v" + i + ".node_annotext LIKE '" + AnnotationConditionProvider.likeEscaper.escape(e.getKey()) + ":%'");
                conditions.put(e, "v" + i + ".n_na_sample IS TRUE");
            }
        }
        i++;
    }
    return conditions;
}
Also used : QueryNode(annis.model.QueryNode) AnnisQLSemanticsException(annis.exceptions.AnnisQLSemanticsException) FrequencyTableEntry(annis.service.objects.FrequencyTableEntry)

Example 27 with QueryNode

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

the class SubcorpusConstraintWhereClause method commonWhereConditions.

// VR: inline
@Deprecated
public List<String> commonWhereConditions(List<QueryNode> nodes, List<Long> corpusList, List<Long> documents) {
    LinkedList<String> conditions = new LinkedList<>();
    // annotations can always only be inside a subcorpus/document
    QueryNode[] copyNodes = nodes.toArray(new QueryNode[nodes.size()]);
    for (int left = 0; left < copyNodes.length; left++) {
        for (int right = 0; right < copyNodes.length; right++) {
            if (left != right) {
                // only add constraint if the two nodes are not already connected by their component or node id
                boolean needsCorpusRef = false;
                for (Join j : copyNodes[left].getOutgoingJoins()) {
                    if (j.getTarget() != null && j.getTarget().getId() == copyNodes[right].getId()) {
                        if ((j instanceof RankTableJoin || j instanceof Identical)) {
                            // we definitly don't have to apply this join
                            needsCorpusRef = false;
                            break;
                        } else {
                            // there is at least one actual join between this nodes, assume we
                            // need a corpus_ref join for now
                            needsCorpusRef = true;
                        }
                    }
                }
                if (needsCorpusRef) {
                    conditions.add(join("=", tables(copyNodes[left]).aliasedColumn(NODE_TABLE, "corpus_ref"), tables(copyNodes[right]).aliasedColumn(NODE_TABLE, "corpus_ref")));
                }
            }
        // end if left != right
        }
    // end right loop
    }
    return conditions;
}
Also used : Identical(annis.sqlgen.model.Identical) QueryNode(annis.model.QueryNode) Join(annis.model.Join) RankTableJoin(annis.sqlgen.model.RankTableJoin) RankTableJoin(annis.sqlgen.model.RankTableJoin) LinkedList(java.util.LinkedList)

Example 28 with QueryNode

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

the class QueryController method validateQuery.

public void validateQuery() {
    QueryPanel qp = searchView.getControlPanel().getQueryPanel();
    // reset status
    qp.setErrors(null);
    qp.setNodes(null);
    String query = state.getAql().getValue();
    if (query == null || query.isEmpty()) {
        qp.setStatus("Empty query");
    } else {
        // validate query
        try {
            AsyncWebResource annisResource = Helper.getAnnisAsyncWebResource();
            Future<List<QueryNode>> future = annisResource.path("query").path("parse/nodes").queryParam("q", Helper.encodeJersey(query)).get(new GenericType<List<QueryNode>>() {
            });
            // wait for maximal one seconds
            try {
                List<QueryNode> nodes = future.get(1, TimeUnit.SECONDS);
                qp.setNodes(nodes);
                if (state.getSelectedCorpora().getValue() == null || state.getSelectedCorpora().getValue().isEmpty()) {
                    qp.setStatus("Please select a corpus from the list below, then click on \"Search\".");
                } else {
                    qp.setStatus("Valid query, click on \"Search\" to start searching.");
                }
            } catch (InterruptedException ex) {
                log.warn(null, ex);
            } catch (ExecutionException ex) {
                if (AnnisBaseUI.handleCommonError(ex, "validate query")) {
                    log.error(null, ex);
                } else if (ex.getCause() instanceof UniformInterfaceException) {
                    reportServiceException((UniformInterfaceException) ex.getCause(), false);
                } else {
                    // something unknown, report
                    ExceptionDialog.show(ex);
                }
            } catch (TimeoutException ex) {
                qp.setStatus("Validation of query took too long.");
            }
        } catch (ClientHandlerException ex) {
            log.error("Could not connect to web service", ex);
            ExceptionDialog.show(ex, "Could not connect to web service");
        }
    }
}
Also used : ClientHandlerException(com.sun.jersey.api.client.ClientHandlerException) FrequencyQueryPanel(annis.gui.frequency.FrequencyQueryPanel) QueryPanel(annis.gui.controlpanel.QueryPanel) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) QueryNode(annis.model.QueryNode) List(java.util.List) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) AsyncWebResource(com.sun.jersey.api.client.AsyncWebResource) TimeoutException(java.util.concurrent.TimeoutException)

Example 29 with QueryNode

use of annis.model.QueryNode 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 30 with QueryNode

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

the class FrequencyQueryPanel method parseQuery.

private List<QueryNode> parseQuery(String query) {
    if (query == null || query.isEmpty()) {
        return new LinkedList<>();
    }
    // let the service parse the query
    WebResource res = Helper.getAnnisWebResource();
    List<QueryNode> nodes = res.path("query/parse/nodes").queryParam("q", Helper.encodeJersey(query)).get(new GenericType<List<QueryNode>>() {
    });
    return nodes;
}
Also used : QueryNode(annis.model.QueryNode) WebResource(com.sun.jersey.api.client.WebResource) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Aggregations

QueryNode (annis.model.QueryNode)67 Join (annis.model.Join)12 LinkedList (java.util.LinkedList)11 AnnisQLSemanticsException (annis.exceptions.AnnisQLSemanticsException)10 ArrayList (java.util.ArrayList)8 QueryAnnotation (annis.model.QueryAnnotation)7 Precedence (annis.sqlgen.model.Precedence)7 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 Dominance (annis.sqlgen.model.Dominance)5 PointingRelation (annis.sqlgen.model.PointingRelation)5 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 TreeSet (java.util.TreeSet)5 Test (org.junit.Test)5 Identical (annis.sqlgen.model.Identical)4 LeftDominance (annis.sqlgen.model.LeftDominance)4 Near (annis.sqlgen.model.Near)4 RightDominance (annis.sqlgen.model.RightDominance)4 TestUtils.uniqueLong (annis.test.TestUtils.uniqueLong)4 QueryData (annis.ql.parser.QueryData)3