Search in sources :

Example 1 with TupleQueryResult

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

the class SesameGraph method selectFirst.

public BoundVariables selectFirst(String sparqlSelect) {
    BoundVariables boundVariables;
    TupleQueryResult tupleQueryResult = getTupleQueryResult(sparqlSelect);
    try {
        if (!tupleQueryResult.hasNext()) {
            return null;
        }
        boundVariables = new SesameBoundVariables(tupleQueryResult.getBindingNames(), tupleQueryResult.next());
        if (tupleQueryResult.hasNext()) {
            tupleQueryResult.close();
            throw new MoreThanOneResultFoundException(sparqlSelect);
        }
    } catch (QueryEvaluationException e) {
        throw new ShineRuntimeException("Could not parse query: <<" + sparqlSelect + ">>", e);
    }
    return boundVariables;
}
Also used : BoundVariables(com.thoughtworks.studios.shine.semweb.BoundVariables) ShineRuntimeException(com.thoughtworks.studios.shine.ShineRuntimeException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MoreThanOneResultFoundException(com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 2 with TupleQueryResult

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

the class SesameGraph method renderTupleQuery.

private void renderTupleQuery(Query query, TupleQueryResultWriter writer) throws QueryEvaluationException, TupleQueryResultHandlerException {
    TupleQueryResult tupleQueryResult = ((TupleQuery) query).evaluate();
    writer.startQueryResult(tupleQueryResult.getBindingNames());
    while (tupleQueryResult.hasNext()) {
        writer.handleSolution(tupleQueryResult.next());
    }
    writer.endQueryResult();
}
Also used : TupleQuery(org.openrdf.query.TupleQuery) TupleQueryResult(org.openrdf.query.TupleQueryResult)

Example 3 with TupleQueryResult

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

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

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

TupleQueryResult (org.openrdf.query.TupleQueryResult)11 Value (org.openrdf.model.Value)8 BindingSet (org.openrdf.query.BindingSet)8 ExpressionQueryResult (edu.mit.simile.backstage.model.data.ExpressionQueryResult)5 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)5 ScriptableArrayBuilder (edu.mit.simile.backstage.util.ScriptableArrayBuilder)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 RepositoryConnection (org.openrdf.repository.RepositoryConnection)3 RepositoryException (org.openrdf.repository.RepositoryException)3 ShineRuntimeException (com.thoughtworks.studios.shine.ShineRuntimeException)2 BoundVariables (com.thoughtworks.studios.shine.semweb.BoundVariables)2 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 Model (org.openrdf.model.Model)2 URI (org.openrdf.model.URI)2 TreeModel (org.openrdf.model.impl.TreeModel)2 MoreThanOneResultFoundException (com.thoughtworks.studios.shine.semweb.MoreThanOneResultFoundException)1