Search in sources :

Example 1 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery in project stanbol by apache.

the class SparqlSearcher method find.

@Override
public final QueryResultList<Representation> find(FieldQuery parsedQuery) throws IOException {
    long start = System.currentTimeMillis();
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    String sparqlQuery = query.toSparqlConstruct();
    long initEnd = System.currentTimeMillis();
    log.debug("  > InitTime: " + (initEnd - start));
    log.debug("  > SPARQL query:\n" + sparqlQuery);
    InputStream in = SparqlEndpointUtils.sendSparqlRequest(getQueryUri(), sparqlQuery, DEFAULT_RDF_CONTENT_TYPE);
    long queryEnd = System.currentTimeMillis();
    log.debug("  > QueryTime: " + (queryEnd - initEnd));
    if (in != null) {
        Graph graph;
        Graph rdfData = parser.parse(in, DEFAULT_RDF_CONTENT_TYPE, new IRI(getBaseUri()));
        if (rdfData instanceof Graph) {
            graph = (Graph) rdfData;
        } else {
            graph = new IndexedGraph(rdfData);
        }
        long parseEnd = System.currentTimeMillis();
        log.debug("  > ParseTime: " + (parseEnd - queryEnd));
        return new RdfQueryResultList(query, graph);
    } else {
        return null;
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfQueryResultList(org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList) InputStream(java.io.InputStream) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph)

Example 2 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery in project stanbol by apache.

the class VirtuosoSearcher method findEntities.

@Override
public final QueryResultList<String> findEntities(FieldQuery parsedQuery) throws IOException {
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    query.setSparqlEndpointType(SparqlEndpointTypeEnum.Virtuoso);
    String sparqlQuery = query.toSparqlSelect(false);
    log.trace("Sending Sparql request [{}].", sparqlQuery);
    InputStream in = sendSparqlRequest(getQueryUri(), sparqlQuery, SparqlSearcher.DEFAULT_SPARQL_RESULT_CONTENT_TYPE);
    // Move to util class!
    final List<String> entities = extractEntitiesFromJsonResult(in, query.getRootVariableName());
    return new QueryResultListImpl<String>(query, entities.iterator(), String.class);
}
Also used : InputStream(java.io.InputStream) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl)

Example 3 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery in project stanbol by apache.

the class SparqlQueryUtilsTest method testMultiWordTextConstraints.

/**
     * Tests word level matching for {@link TextConstraint}s (STANBOL-1277)
     */
@Test
public void testMultiWordTextConstraints() {
    //queries for a TextConstraint with {text1} or {text2} in the languages
    // {lang1} or {lang2} are expected to look like:
    //
    //    select ?entity, ?label where {
    //        ?entity rdfs:label ?label
    //        FILTER((regex(str(?label),"\\b{text1}\\b","i") || regex(str(?label),"\\b{text2}\\b","i")) 
    //            && ((lang(?label) = "{lang1}") || (lang(?label) = "{lang2}"))) . 
    //    }
    //first test a pattern type NONE
    SparqlFieldQuery query = SparqlFieldQueryFactory.getInstance().createFieldQuery();
    query.setConstraint("urn:field4", new TextConstraint(Arrays.asList("Global", "Toy"), PatternType.none, false, "en", null));
    String queryString = SparqlQueryUtils.createSparqlSelectQuery(query, true, 0, SparqlEndpointTypeEnum.Standard);
    Assert.assertTrue(queryString.contains("regex(str(?tmp1),\"\\\\bGlobal\\\\b\",\"i\") " + "|| regex(str(?tmp1),\"\\\\bToy\\\\b\",\"i\")"));
    //also test for pattern type WILDCARD
    query = SparqlFieldQueryFactory.getInstance().createFieldQuery();
    query.setConstraint("urn:field4", new TextConstraint(Arrays.asList("Glo?al", "Toy"), PatternType.wildcard, false, "en", null));
    queryString = SparqlQueryUtils.createSparqlSelectQuery(query, true, 0, SparqlEndpointTypeEnum.Standard);
    Assert.assertTrue(queryString.contains("regex(str(?tmp1),\"\\\\bGlo.al\\\\b\",\"i\") " + "|| regex(str(?tmp1),\"\\\\bToy\\\\b\",\"i\")"));
}
Also used : SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint) Test(org.junit.Test)

Example 4 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery in project stanbol by apache.

the class SesameYard method findReferences.

@Override
public QueryResultList<String> findReferences(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results
        List<String> ids = limit > 0 ? new ArrayList<String>(limit) : new ArrayList<String>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof Resource) {
                ids.add(value.stringValue());
            }
        }
        con.commit();
        return new QueryResultListImpl<String>(query, ids, String.class);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) Resource(org.openrdf.model.Resource) RepositoryException(org.openrdf.repository.RepositoryException) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Value(org.openrdf.model.Value) QueryResultListImpl(org.apache.stanbol.entityhub.core.query.QueryResultListImpl) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 5 with SparqlFieldQuery

use of org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery in project stanbol by apache.

the class SesameYard method findRepresentation.

@Override
public QueryResultList<Representation> findRepresentation(FieldQuery parsedQuery) throws YardException, IllegalArgumentException {
    if (parsedQuery == null) {
        throw new IllegalArgumentException("The parsed query MUST NOT be NULL!");
    }
    final SparqlFieldQuery query = SparqlFieldQueryFactory.getSparqlFieldQuery(parsedQuery);
    RepositoryConnection con = null;
    TupleQueryResult results = null;
    try {
        con = repository.getConnection();
        con.begin();
        //execute the query
        int limit = QueryUtils.getLimit(query, getConfig().getDefaultQueryResultNumber(), getConfig().getMaxQueryResultNumber());
        results = executeSparqlFieldQuery(con, query, limit, false);
        //parse the results and generate the Representations
        //create an own valueFactors so that all the data of the query results
        //are added to the same Sesame Model
        Model model = new TreeModel();
        RdfValueFactory valueFactory = new RdfValueFactory(model, sesameFactory);
        List<Representation> representations = limit > 0 ? new ArrayList<Representation>(limit) : new ArrayList<Representation>();
        while (results.hasNext()) {
            BindingSet result = results.next();
            Value value = result.getValue(query.getRootVariableName());
            if (value instanceof URI) {
                //copy all data to the model and create the representation
                RdfRepresentation rep = createRepresentationGraph(con, valueFactory, (URI) value);
                //link the result with the query result
                model.add(queryRoot, queryResult, value);
                representations.add(rep);
            }
        //ignore non URI results
        }
        con.commit();
        return new SesameQueryResultList(model, query, representations);
    } catch (RepositoryException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } catch (QueryEvaluationException e) {
        throw new YardException("Unable to execute findReferences query", e);
    } finally {
        if (results != null) {
            //close the result if present
            try {
                results.close();
            } catch (QueryEvaluationException ignore) {
            /* ignore */
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (RepositoryException ignore) {
            /* ignore */
            }
        }
    }
}
Also used : RepositoryConnection(org.openrdf.repository.RepositoryConnection) BindingSet(org.openrdf.query.BindingSet) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) Representation(org.apache.stanbol.entityhub.servicesapi.model.Representation) RepositoryException(org.openrdf.repository.RepositoryException) URI(org.openrdf.model.URI) TreeModel(org.openrdf.model.impl.TreeModel) YardException(org.apache.stanbol.entityhub.servicesapi.yard.YardException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RdfRepresentation(org.apache.stanbol.entityhub.model.sesame.RdfRepresentation) SparqlFieldQuery(org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery) Model(org.openrdf.model.Model) TreeModel(org.openrdf.model.impl.TreeModel) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) RdfValueFactory(org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)

Aggregations

SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)11 InputStream (java.io.InputStream)6 QueryResultListImpl (org.apache.stanbol.entityhub.core.query.QueryResultListImpl)4 Graph (org.apache.clerezza.commons.rdf.Graph)3 IRI (org.apache.clerezza.commons.rdf.IRI)3 IndexedGraph (org.apache.stanbol.commons.indexedgraph.IndexedGraph)3 RdfQueryResultList (org.apache.stanbol.entityhub.query.clerezza.RdfQueryResultList)3 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)3 Value (org.openrdf.model.Value)3 BindingSet (org.openrdf.query.BindingSet)3 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)3 TupleQueryResult (org.openrdf.query.TupleQueryResult)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RepositoryException (org.openrdf.repository.RepositoryException)3 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)2 RdfValueFactory (org.apache.stanbol.entityhub.model.sesame.RdfValueFactory)2 Representation (org.apache.stanbol.entityhub.servicesapi.model.Representation)2 TextConstraint (org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)2 Model (org.openrdf.model.Model)2 URI (org.openrdf.model.URI)2