Search in sources :

Example 6 with QueryEvaluationException

use of org.openrdf.query.QueryEvaluationException 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)

Example 7 with QueryEvaluationException

use of org.openrdf.query.QueryEvaluationException in project gocd by gocd.

the class SesameGraph method select.

public List<BoundVariables> select(String sparqlSelect) {
    List<BoundVariables> results = new LinkedList<>();
    TupleQueryResult tupleQueryResult = getTupleQueryResult(sparqlSelect);
    try {
        while (tupleQueryResult.hasNext()) {
            results.add(new SesameBoundVariables(tupleQueryResult.getBindingNames(), tupleQueryResult.next()));
        }
    } catch (QueryEvaluationException e) {
        throw new ShineRuntimeException(e);
    }
    return results;
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) TupleQueryResult(org.openrdf.query.TupleQueryResult) LinkedList(java.util.LinkedList)

Example 8 with QueryEvaluationException

use of org.openrdf.query.QueryEvaluationException in project blueprints by tinkerpop.

the class PropertyGraphSailConnection method evaluateInternal.

protected CloseableIteration<? extends BindingSet, QueryEvaluationException> evaluateInternal(final TupleExpr query, final Dataset dataset, final BindingSet bindings, final boolean includeInferred) throws SailException {
    try {
        TripleSource tripleSource = new SailConnectionTripleSource(this, context.valueFactory, includeInferred);
        EvaluationStrategyImpl strategy = new EvaluationStrategyImpl(tripleSource, dataset);
        return strategy.evaluate(query, bindings);
    } catch (QueryEvaluationException e) {
        throw new SailException(e);
    }
}
Also used : SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) TripleSource(org.openrdf.query.algebra.evaluation.TripleSource) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) SailConnectionTripleSource(net.fortytwo.sesametools.SailConnectionTripleSource) SailException(org.openrdf.sail.SailException) EvaluationStrategyImpl(org.openrdf.query.algebra.evaluation.impl.EvaluationStrategyImpl)

Example 9 with QueryEvaluationException

use of org.openrdf.query.QueryEvaluationException in project blueprints by tinkerpop.

the class SailTest method testJoins.

@Test
public void testJoins() throws Exception {
    SPARQLParser parser = new SPARQLParser();
    BindingSet bindings = new EmptyBindingSet();
    String baseURI = "http://example.org/bogus/";
    SailConnection sc = sail.getConnection();
    try {
        CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
        int count;
        String queryStr = "PREFIX : <urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/>\n" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?foaf WHERE {\n" + "    :ford foaf:knows ?friend .\n" + "    ?friend foaf:knows ?foaf .\n" + "}";
        ParsedQuery query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI foaf = (URI) set.getValue("foaf");
            assertTrue(foaf.stringValue().startsWith("urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/"));
        }
        results.close();
        assertEquals(4, count);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ParsedQuery(org.openrdf.query.parser.ParsedQuery) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 10 with QueryEvaluationException

use of org.openrdf.query.QueryEvaluationException in project blueprints by tinkerpop.

the class SailTest method testEvaluate.

// tuple queries ///////////////////////////////////////////////////////////
@Test
public void testEvaluate() throws Exception {
    Set<String> languages;
    String prefix = "urn:com.tinkerpop.blueprints.pgm.oupls.sail.test/";
    URI thorUri = sail.getValueFactory().createURI(prefix + "thor");
    SailConnection sc = sail.getConnection();
    try {
        sc.begin();
        URI uriA = sail.getValueFactory().createURI("http://example.org/uriA");
        URI uriB = sail.getValueFactory().createURI("http://example.org/uriB");
        URI uriC = sail.getValueFactory().createURI("http://example.org/uriC");
        sc.addStatement(uriA, uriB, uriC);
        sc.commit();
        sc.begin();
        SPARQLParser parser = new SPARQLParser();
        BindingSet bindings = new EmptyBindingSet();
        String baseURI = "http://example.org/bogus/";
        String queryStr;
        ParsedQuery query;
        CloseableIteration<? extends BindingSet, QueryEvaluationException> results;
        int count;
        // s ?p ?o SELECT
        queryStr = "SELECT ?y ?z WHERE { <http://example.org/uriA> ?y ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI y = (URI) set.getValue("y");
            Value z = (Value) set.getValue("z");
            assertNotNull(y);
            assertNotNull(z);
        // System.out.println("y = " + y + ", z = " + z);
        }
        results.close();
        assertTrue(count > 0);
        // s p ?o SELECT using a namespace prefix
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        languages = new HashSet<String>();
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            Literal z = (Literal) set.getValue("z");
            assertNotNull(z);
            languages.add(z.getLanguage());
        }
        results.close();
        assertTrue(count > 0);
        assertEquals(2, languages.size());
        assertTrue(languages.contains("en"));
        assertTrue(languages.contains("is"));
        // ?s p o SELECT using a plain literal value with no language tag
        queryStr = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" + "SELECT ?s WHERE { ?s rdfs:comment \"he really knows where his towel is\" }";
        URI fordUri = sail.getValueFactory().createURI(prefix + "ford");
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, fordUri);
        }
        results.close();
        assertTrue(count > 0);
        // ?s p o SELECT using a language-specific literal value
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\"@en }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, thorUri);
        }
        results.close();
        assertTrue(count > 0);
        // The language tag is necessary
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?s WHERE { ?s foaf:name \"Thor\" }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
        // ?s p o SELECT using a typed literal value
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\"^^xsd:string }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI s = (URI) set.getValue("s");
            assertNotNull(s);
            assertEquals(s, thorUri);
        }
        results.close();
        assertTrue(count > 0);
        // The data type is necessary
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?s WHERE { ?s foaf:msnChatID \"Thorster123\" }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
        // s ?p o SELECT
        // TODO: commented out languages for now
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" + "SELECT ?p WHERE { <" + prefix + "thor> ?p \"Thor\"@en }";
        query = parser.parseQuery(queryStr, baseURI);
        URI foafNameUri = sail.getValueFactory().createURI("http://xmlns.com/foaf/0.1/name");
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI p = (URI) set.getValue("p");
            assertNotNull(p);
            assertEquals(p, foafNameUri);
        }
        results.close();
        assertTrue(count > 0);
        // context-specific SELECT
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <" + prefix + "ctx1>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        languages = new HashSet<String>();
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            Literal z = (Literal) set.getValue("z");
            assertNotNull(z);
            languages.add(z.getLanguage());
        }
        results.close();
        assertTrue(count > 0);
        assertEquals(2, languages.size());
        assertTrue(languages.contains("en"));
        assertTrue(languages.contains("is"));
        queryStr = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n" + "SELECT ?z\n" + "FROM <http://example.org/emptycontext>\n" + "WHERE { <" + prefix + "thor> foaf:name ?z }";
        query = parser.parseQuery(queryStr, baseURI);
        results = sc.evaluate(query.getTupleExpr(), query.getDataset(), bindings, false);
        count = 0;
        while (results.hasNext()) {
            count++;
            results.next();
        }
        results.close();
        assertEquals(0, count);
    // s p o? select without and with inferencing
    // TODO commented out waiting for inferencing
    // queryStr =
    // "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
    // + "SELECT ?o\n"
    // + "WHERE { <" + prefix + "instance1> rdf:type ?o }";
    // query = parser.parseQuery(queryStr, baseURI);
    // results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
    // bindings, false);
    // count = 0;
    // while (results.hasNext()) {
    // count++;
    // BindingSet set = results.next();
    // URI o = (URI) set.getValue("o");
    // assertEquals(prefix + "classB", o.toString());
    // }
    // results.close();
    // assertEquals(1, count);
    // results = sc.evaluate(query.getTupleExpr(), query.getDataset(),
    // bindings, true);
    // count = 0;
    // boolean foundA = false, foundB = false;
    // while (results.hasNext()) {
    // count++;
    // BindingSet set = results.next();
    // URI o = (URI) set.getValue("o");
    // String s = o.toString();
    // if (s.equals(prefix + "classA")) {
    // foundA = true;
    // } else if (s.equals(prefix + "classB")) {
    // foundB = true;
    // }
    // }
    // results.close();
    // assertEquals(2, count);
    // assertTrue(foundA);
    // assertTrue(foundB);
    } finally {
        sc.rollback();
        sc.close();
    }
}
Also used : BindingSet(org.openrdf.query.BindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) URI(org.openrdf.model.URI) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) NotifyingSailConnection(org.openrdf.sail.NotifyingSailConnection) SailConnection(org.openrdf.sail.SailConnection) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) Test(org.junit.Test)

Aggregations

QueryEvaluationException (org.openrdf.query.QueryEvaluationException)12 BindingSet (org.openrdf.query.BindingSet)7 URI (org.openrdf.model.URI)5 TupleQueryResult (org.openrdf.query.TupleQueryResult)5 ParsedQuery (org.openrdf.query.parser.ParsedQuery)5 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)5 Test (org.junit.Test)4 Value (org.openrdf.model.Value)4 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)4 SailException (org.openrdf.sail.SailException)4 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RepositoryException (org.openrdf.repository.RepositoryException)3 SailConnection (org.openrdf.sail.SailConnection)3 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)2 BoundVariables (com.thoughtworks.studios.shine.semweb.BoundVariables)2 Vertex (com.tinkerpop.blueprints.Vertex)2 HashMap (java.util.HashMap)2 SailConnectionTripleSource (net.fortytwo.sesametools.SailConnectionTripleSource)2