Search in sources :

Example 16 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueryServiceImpl method parseNodes.

/**
 * Return the list of the query nodes if this is a valid query
 * or throw exception when invalid
 *
 * @param query Query to get the query nodes for
 * @param rawCorpusNames
 * @return
 */
@GET
@Path("parse/nodes")
@Produces("application/xml")
public Response parseNodes(@QueryParam("q") String query, @DefaultValue("") @QueryParam("corpora") String rawCorpusNames) {
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:parse:" + c);
    }
    Collections.sort(corpusNames);
    List<Long> corpusIDs = queryDao.mapCorpusNamesToIds(corpusNames);
    QueryData data = queryDao.parseAQL(query, corpusIDs);
    List<QueryNode> nodes = new LinkedList<>();
    int i = 0;
    for (List<QueryNode> alternative : data.getAlternatives()) {
        for (QueryNode n : alternative) {
            n.setAlternativeNumber(i);
            nodes.add(n);
        }
        i++;
    }
    return Response.ok(new GenericEntity<List<QueryNode>>(nodes) {
    }).build();
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) QueryNode(annis.model.QueryNode) GenericEntity(javax.ws.rs.core.GenericEntity) Subject(org.apache.shiro.subject.Subject) LinkedList(java.util.LinkedList) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueryServiceImpl method count.

@GET
@Path("search/count")
@Produces("application/xml")
@Override
public Response count(@QueryParam("q") String query, @QueryParam("corpora") String rawCorpusNames) {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:count:" + c);
    }
    QueryData data = queryDataFromParameters(query, rawCorpusNames);
    long start = new Date().getTime();
    MatchAndDocumentCount count = queryDao.countMatchesAndDocuments(data);
    long end = new Date().getTime();
    logQuery("COUNT", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
    return Response.ok(count).type(MediaType.APPLICATION_XML_TYPE).build();
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) Subject(org.apache.shiro.subject.Subject) Date(java.util.Date) MatchAndDocumentCount(annis.service.objects.MatchAndDocumentCount) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 18 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueryServiceImpl method matrix.

/**
 * Get result as matrix in WEKA (ARFF) format.
 * @param query
 * @param rawCorpusNames
 * @param rawMetaKeys
 * @param rawCsv
 * @return
 */
@GET
@Path("search/matrix")
@Produces("text/plain")
public StreamingOutput matrix(@QueryParam("q") final String query, @QueryParam("corpora") final String rawCorpusNames, @QueryParam("metakeys") String rawMetaKeys, @DefaultValue("false") @QueryParam("csv") String rawCsv) {
    requiredParameter(query, "q", "AnnisQL query");
    requiredParameter(rawCorpusNames, "corpora", "comma separated list of corpus names");
    final boolean outputCsv = Boolean.parseBoolean(rawCsv);
    Subject user = SecurityUtils.getSubject();
    List<String> corpusNames = splitCorpusNamesFromRaw(rawCorpusNames);
    for (String c : corpusNames) {
        user.checkPermission("query:matrix:" + c);
    }
    final QueryData data = queryDataFromParameters(query, rawCorpusNames);
    MatrixQueryData ext = new MatrixQueryData();
    if (rawMetaKeys != null) {
        ext.setMetaKeys(splitMatrixKeysFromRaw(rawMetaKeys));
    }
    if (ext.getMetaKeys() != null && ext.getMetaKeys().isEmpty()) {
        ext.setMetaKeys(null);
    }
    data.addExtension(ext);
    StreamingOutput result = new StreamingOutput() {

        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            long start = new Date().getTime();
            queryDao.matrix(data, outputCsv, output);
            long end = new Date().getTime();
            logQuery("MATRIX", query, splitCorpusNamesFromRaw(rawCorpusNames), end - start);
        }
    };
    return result;
}
Also used : MatrixQueryData(annis.sqlgen.MatrixQueryData) QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput) Subject(org.apache.shiro.subject.Subject) Date(java.util.Date) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueryServiceImpl method basicSubgraph.

protected SaltProject basicSubgraph(MatchGroup matches, @QueryParam("segmentation") String segmentation, @DefaultValue("0") @QueryParam("left") String leftRaw, @DefaultValue("0") @QueryParam("right") String rightRaw, @DefaultValue("all") @QueryParam("filter") String filterRaw) {
    Subject user = SecurityUtils.getSubject();
    int left = Integer.parseInt(leftRaw);
    int right = Integer.parseInt(rightRaw);
    SubgraphFilter filter = SubgraphFilter.valueOf(filterRaw);
    QueryData data = GraphHelper.createQueryData(matches, queryDao);
    data.addExtension(new AnnotateQueryData(left, right, segmentation, filter));
    Set<String> corpusNames = new TreeSet<>();
    for (Match singleMatch : matches.getMatches()) {
        // collect list of used corpora
        for (java.net.URI u : singleMatch.getSaltIDs()) {
            corpusNames.add(CommonHelper.getCorpusPath(u).get(0));
        }
    }
    for (String c : corpusNames) {
        user.checkPermission("query:subgraph:" + c);
    }
    List<String> corpusNamesList = new LinkedList<>(corpusNames);
    if (data.getCorpusList() == null || data.getCorpusList().isEmpty()) {
        throw new WebApplicationException(Response.Status.BAD_REQUEST.getStatusCode());
    }
    long start = new Date().getTime();
    SaltProject p = queryDao.graph(data);
    long end = new Date().getTime();
    String options = "matches: " + matches.toString() + ", seg: " + segmentation + ", left: " + left + ", right: " + right + ", filter: " + filter;
    logQuery("SUBGRAPH", "", corpusNamesList, end - start, options);
    return p;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatrixQueryData(annis.sqlgen.MatrixQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) SaltProject(org.corpus_tools.salt.common.SaltProject) SubgraphFilter(annis.service.objects.SubgraphFilter) Subject(org.apache.shiro.subject.Subject) LinkedList(java.util.LinkedList) Date(java.util.Date) Match(annis.service.objects.Match) TreeSet(java.util.TreeSet)

Example 20 with QueryData

use of annis.ql.parser.QueryData in project ANNIS by korpling.

the class QueriesGenerator method generateQuery.

private void generateQuery(QueryBuilder queryBuilder) {
    try {
        // retrieve the aql query for analyzing purposes
        String aql = queryBuilder.getAQL();
        // set some necessary extensions for generating complete sql
        QueryData queryData = getQueryDao().parseAQL(aql, this.corpusIds);
        queryData.addExtension(queryBuilder.getLimitOffsetQueryData());
        // retrieve the salt project to analyze
        List<Match> matches = getQueryDao().find(queryData);
        if (matches.isEmpty()) {
            return;
        }
        QueryData matchQueryData = GraphHelper.createQueryData(new MatchGroup(matches), queryDao);
        matchQueryData.addExtension(queryBuilder.getAnnotateQueryData());
        SaltProject saltProject = getQueryDao().graph(matchQueryData);
        queryBuilder.analyzingQuery(saltProject);
        // set the corpus name
        ExampleQuery exampleQuery = queryBuilder.getExampleQuery();
        exampleQuery.setCorpusName(corpusName);
        // copy the example query to the database
        if (exampleQuery.getExampleQuery() != null && !"".equals(exampleQuery.getExampleQuery())) {
            if (getTableInsertSelect().containsKey("example_queries")) {
                Object[] values = new Object[] { exampleQuery.getExampleQuery(), exampleQuery.getDescription(), exampleQuery.getType() == null ? "" : exampleQuery.getType(), exampleQuery.getNodes(), "{}", corpusIds.get(0) };
                int[] argTypes = new int[] { Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.INTEGER };
                getJdbcTemplate().update("INSERT INTO example_queries(" + getTableInsertSelect().get("example_queries") + ") VALUES(?, ?, ?, ?, ?::text[], ?)", values, argTypes);
                log.info("generated example query: {}", exampleQuery.getExampleQuery());
            }
        } else {
            log.warn("could not generating auto query with {}", queryBuilder.getClass().getName());
        }
    } catch (Exception ex) {
        log.warn("Cannot generate example query", ex);
    }
}
Also used : QueryData(annis.ql.parser.QueryData) LimitOffsetQueryData(annis.sqlgen.extensions.LimitOffsetQueryData) AnnotateQueryData(annis.sqlgen.extensions.AnnotateQueryData) MatchGroup(annis.service.objects.MatchGroup) ExampleQuery(annis.examplequeries.ExampleQuery) SaltProject(org.corpus_tools.salt.common.SaltProject) Match(annis.service.objects.Match)

Aggregations

QueryData (annis.ql.parser.QueryData)20 AnnotateQueryData (annis.sqlgen.extensions.AnnotateQueryData)13 LimitOffsetQueryData (annis.sqlgen.extensions.LimitOffsetQueryData)13 MatrixQueryData (annis.sqlgen.MatrixQueryData)6 Date (java.util.Date)6 Subject (org.apache.shiro.subject.Subject)6 TestUtils.uniqueString (annis.test.TestUtils.uniqueString)5 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 Test (org.junit.Test)5 Matchers.anyString (org.mockito.Matchers.anyString)5 QueryNode (annis.model.QueryNode)4 Match (annis.service.objects.Match)4 LinkedList (java.util.LinkedList)3 SaltProject (org.corpus_tools.salt.common.SaltProject)3 ExampleQuery (annis.examplequeries.ExampleQuery)2 MatchGroup (annis.service.objects.MatchGroup)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2