Search in sources :

Example 1 with BindingSet

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

the class PropertyGraphSailTest method testSPARQL.

@Test
public void testSPARQL() throws Exception {
    int count;
    String queryStr = "PREFIX pgm: <" + PropertyGraphSail.ONTOLOGY_NS + ">\n" + "PREFIX prop: <" + PropertyGraphSail.PROPERTY_NS + ">\n" + "SELECT ?project ?name WHERE {\n" + "   ?marko prop:name \"marko\".\n" + "   ?e1 pgm:label \"knows\".\n" + "   ?e1 pgm:tail ?marko.\n" + "   ?e1 pgm:head ?friend.\n" + "   ?e2 pgm:label \"created\".\n" + "   ?e2 pgm:tail ?friend.\n" + "   ?e2 pgm:head ?project.\n" + "   ?project prop:name ?name.\n" + "}";
    System.out.println(queryStr);
    ParsedQuery query = new SPARQLParser().parseQuery(queryStr, "http://example.org/bogus/");
    CloseableIteration<? extends BindingSet, QueryEvaluationException> results = sc.evaluate(query.getTupleExpr(), query.getDataset(), new EmptyBindingSet(), false);
    try {
        count = 0;
        while (results.hasNext()) {
            count++;
            BindingSet set = results.next();
            URI project = (URI) set.getValue("project");
            Literal name = (Literal) set.getValue("name");
            assertNotNull(project);
            assertNotNull(name);
            System.out.println("project = " + project + ", name = " + name);
        }
    } finally {
        results.close();
    }
    assertEquals(2, count);
}
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) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) ParsedQuery(org.openrdf.query.parser.ParsedQuery) Literal(org.openrdf.model.Literal) URI(org.openrdf.model.URI) Test(org.junit.Test)

Example 2 with BindingSet

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

the class SailGraph method executeSparql.

/**
     * Evaluate a SPARQL query against the SailGraph (http://www.w3.org/TR/rdf-sparql-query/). The result is a mapping between the ?-bindings and the bound URI, blank node, or literal represented as a Vertex.
     *
     * @param sparqlQuery the SPARQL query to evaluate
     * @return the mapping between a ?-binding and the URI, blank node, or literal as a Vertex
     * @throws RuntimeException if an error occurs in the SPARQL query engine
     */
public List<Map<String, Vertex>> executeSparql(String sparqlQuery) throws RuntimeException {
    try {
        sparqlQuery = getPrefixes() + sparqlQuery;
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery query = parser.parseQuery(sparqlQuery, null);
        boolean includeInferred = false;
        final CloseableIteration<? extends BindingSet, QueryEvaluationException> results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred);
        final List<Map<String, Vertex>> returnList = new ArrayList<Map<String, Vertex>>();
        try {
            while (results.hasNext()) {
                BindingSet bs = results.next();
                Map<String, Vertex> returnMap = new HashMap<String, Vertex>();
                for (Binding b : bs) {
                    returnMap.put(b.getName(), this.getVertex(b.getValue().toString()));
                }
                returnList.add(returnMap);
            }
        } finally {
            results.close();
        }
        return returnList;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : Binding(org.openrdf.query.Binding) Vertex(com.tinkerpop.blueprints.Vertex) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with BindingSet

use of org.openrdf.query.BindingSet in project backstage by zepheira.

the class DomLensNode method generateSubcontentAttributes.

protected void generateSubcontentAttributes(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    for (SubcontentAttribute a : _subcontentAttributes) {
        DefaultScriptableObject o = new DefaultScriptableObject();
        o.put("name", o, a.name);
        StringBuffer sb = new StringBuffer();
        for (Fragment f : a.fragments) {
            if (f instanceof StringFragment) {
                sb.append(((StringFragment) f).text);
            } else {
                try {
                    boolean first = true;
                    ExpressionQueryResult eqr = ((ExpressionFragment) f).expression.computeOutputOnValue(value, database, connection);
                    if (eqr != null) {
                        TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
                        try {
                            while (queryResult.hasNext()) {
                                BindingSet bindingSet = queryResult.next();
                                Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                                if (first) {
                                    first = false;
                                } else {
                                    sb.append(";");
                                }
                                sb.append(renderInnerValueToText(value2, database, connection));
                            }
                        } finally {
                            queryResult.close();
                        }
                    } else {
                        sb.append(renderInnerValueToText(value, database, connection));
                    }
                } catch (Exception e) {
                    _logger.error("", e);
                }
            }
        }
        o.put("value", o, sb.toString());
        arrayBuilder.add(o);
    }
    result.put("subcontentAttributes", result, arrayBuilder.toArray());
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) DefaultScriptableObject(edu.mit.simile.backstage.util.DefaultScriptableObject) StringFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.StringFragment) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult) Fragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.Fragment) StringFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.StringFragment) ExpressionFragment(edu.mit.simile.backstage.model.ui.lens.SubcontentAttribute.ExpressionFragment)

Example 4 with BindingSet

use of org.openrdf.query.BindingSet in project backstage by zepheira.

the class DomLensNode method generateContentWithInnerTemplates.

protected void generateContentWithInnerTemplates(Value value, Scriptable result, Database database, SailRepositoryConnection connection) {
    ScriptableArrayBuilder arrayBuilder = new ScriptableArrayBuilder();
    try {
        ExpressionQueryResult eqr = _contentExpression.computeOutputOnValue(value, database, connection);
        if (eqr != null) {
            TupleQueryResult queryResult = eqr.tupleQuery.evaluate();
            try {
                while (queryResult.hasNext()) {
                    BindingSet bindingSet = queryResult.next();
                    Value value2 = bindingSet.getValue(eqr.resultVar.getName());
                    arrayBuilder.add(generateInnerContentWithInnerTemplates(value2, database, connection));
                }
            } finally {
                queryResult.close();
            }
        } else {
            arrayBuilder.add(generateInnerContentWithInnerTemplates(value, database, connection));
        }
    } catch (Exception e) {
        _logger.error("", e);
    }
    result.put("content", result, arrayBuilder.toArray());
}
Also used : ScriptableArrayBuilder(edu.mit.simile.backstage.util.ScriptableArrayBuilder) BindingSet(org.openrdf.query.BindingSet) ExpressionQueryResult(edu.mit.simile.backstage.model.data.ExpressionQueryResult) Value(org.openrdf.model.Value) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 5 with BindingSet

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

Aggregations

BindingSet (org.openrdf.query.BindingSet)13 Value (org.openrdf.model.Value)10 TupleQueryResult (org.openrdf.query.TupleQueryResult)8 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)7 ExpressionQueryResult (edu.mit.simile.backstage.model.data.ExpressionQueryResult)5 URI (org.openrdf.model.URI)5 ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)4 ParsedQuery (org.openrdf.query.parser.ParsedQuery)4 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)4 DefaultScriptableObject (edu.mit.simile.backstage.util.DefaultScriptableObject)3 SparqlFieldQuery (org.apache.stanbol.entityhub.query.sparql.SparqlFieldQuery)3 YardException (org.apache.stanbol.entityhub.servicesapi.yard.YardException)3 Test (org.junit.Test)3 Literal (org.openrdf.model.Literal)3 EmptyBindingSet (org.openrdf.query.impl.EmptyBindingSet)3 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RepositoryException (org.openrdf.repository.RepositoryException)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 RdfRepresentation (org.apache.stanbol.entityhub.model.sesame.RdfRepresentation)2